def applymove(self, move): #{pos: 8, quarto: true, nextPiece: 2} stateBackup = copy.deepcopy(self._state) try: state = self._state['visible'] if state['pieceToPlay'] is not None: try: if state['board'][move['pos']] is not None: raise game.InvalidMoveException('The position is not free') state['board'][move['pos']] = state['remainingPieces'][state['pieceToPlay']] del(state['remainingPieces'][state['pieceToPlay']]) except game.InvalidMoveException as e: raise e except: raise game.InvalidMoveException("Your move should contain a \"pos\" key in range(16)") if len(state['remainingPieces']) > 0: try: state['pieceToPlay'] = move['nextPiece'] except: raise game.InvalidMoveException("You must specify the next piece to play") else: state['pieceToPlay'] = None if 'quarto' in move: state['quartoAnnounced'] = move['quarto'] winner = self.winner() if winner is None or winner == -1: raise game.InvalidMoveException("There is no Quarto !") else: state['quartoAnnounced'] = False except game.InvalidMoveException as e: self._state = stateBackup raise e
def update(self, coord, player): state = self._state['visible'] line, column = coord index = 4 * line + column if not (0 <= line <= 3 and 0 <= column <= 3): raise game.InvalidMoveException('The move is outside of the board') if state[index] is not None: raise game.InvalidMoveException('The specified cell is not empty') state[index] = player
def applymove(self, move): try: move = json.loads(move) except: raise game.InvalidMoveException('A valid move must be a valid JSON string') else: self._state.applymove(move)
def applymove(self, move): try: index = int(move) except: raise game.InvalidMoveException('A valid move must be a stringified integer') else: self._state.update((index // 4, index % 4), self.currentplayer)