def generate_moves(str_moves): for str_move in str_moves: source = StandardPosition.from_str(str_move[0:2]) destination = StandardPosition.from_str(str_move[2:4]) promotion_char = str_move[4:] if promotion_char: yield StandardMove(source=source, destination=destination, promotion=from_str(promotion_char, initialized=False)) else: yield StandardMove(source=source, destination=destination)
def test_init_move(): a_pos = StandardPosition((0, 0)) b_pos = StandardPosition((0, 1)) move = StandardMove(source=a_pos, destination=b_pos) assert move.source == a_pos assert move.destination == b_pos assert move.promotion is None move = StandardMove(source=a_pos, destination=b_pos, promotion=Queen) assert move.source == a_pos assert move.destination == b_pos assert move.promotion == Queen with pytest.raises(ValueError, message="Source and destination should be not the same field"): StandardMove(source=StandardPosition((0, 0)), destination=StandardPosition((0, 0)))
def can_i_make_a_move(self) -> bool: """ return True if some move is available, False if not (duh) """ # TODO: improvement to check if piece that causes check can be captured for pos, piece in self.board.pieces: if piece and piece.side == self.on_move: for moves in [self.standard_captures(pos), self.standard_moves(pos), self.special_moves(pos)]: for dest in moves: try: self.assert_move(StandardMove(pos, dest)) except NotAValidMove: continue return True return False
def all_available_moves(self, side: Type['Side'] = None) -> List[StandardMove]: """ return list of all available and validated moves by the current "on move" side, very inefficient. """ if not side: side = self.on_move moves = set() for pos, piece in self.board.pieces: if piece.side != side: continue for destination in self.standard_moves(pos) | self.standard_captures(pos) | self.special_moves(pos): move = StandardMove(pos, destination) try: self.assert_move(move) except NotAValidMove: continue else: moves.add(move) return list(moves)
def test_init_move_from_uci(): move = StandardMove.from_str('e2e4') assert move.source == StandardPosition((4, 1)) assert move.destination == StandardPosition((4, 3)) assert move.promotion is None move = StandardMove.from_str('a7a8q') assert move.source == StandardPosition((0, 6)) assert move.destination == StandardPosition((0, 7)) assert move.promotion == Queen # cases based on wrong length bad_moves = ['a', 'e2', 'e2e', 'e2e4qq', 'sadnhjfegesj'] # edge cases, depended on proper UCI syntax and position ranges bad_moves += ['eeee', 'e234', '4123f', 'qqqqq'] for move_str in bad_moves: with pytest.raises(ValueError, message='Not a proper UCI format should be declined'): StandardMove.from_str(move_str) with pytest.raises(ValueError, message="Source and destination should be not the same field"): StandardMove.from_str('e2e2')
def move_from_str(self, move_str: str): return StandardMove( StandardPosition.from_str(move_str[:2]), StandardPosition.from_str(move_str[2:]) )
def moves_from_str(self, moves_str): for move_str in moves_str: yield StandardMove( StandardPosition.from_str(move_str[:2]), StandardPosition.from_str(move_str[-2:]) )
continue print(board_rendererer.normal(game.board)) continue try: source = StandardPosition.from_str(move_str[0:2]) destination = StandardPosition.from_str(move_str[2:4]) promotion_char = move_str[4:] except ValueError as err: print("bad syntax (%s)" % err) continue if not game.board.validate_position(source) or not game.board.validate_position(destination): print("You give position above actual board range (%dx%d)" % game.board.size) continue if promotion_char: move = StandardMove(source=source, destination=destination, promotion=from_str(promotion_char, initialized=False)) else: move = StandardMove(source=source, destination=destination) try: game.move(move) except Exception as err: print(err) continue if not args.random_response: print_board(variant) if game.variant.game_state[0]: print('State: {state}, Winner(s): {winner}'.format( state=game.variant.game_state[1], winner=','.join(str(side) for side in game.variant.game_state[0])) )
engine1.start_engine() engine2.start_engine() game = Game(player1=player1, player2=player2, variant=mode) if not args.silent: print_state(game) try: while True: str_move = engine1.best_move(moves_seq=' '.join( [str(move) for move in game.variant.moves_history]), wtime=args.wtime, btime=args.btime) move = StandardMove.from_str(str_move) game.move(move) if not args.silent: print_state(game) if game.variant.game_state[0]: break str_move = engine2.best_move(moves_seq=' '.join( [str(move) for move in game.variant.moves_history]), wtime=args.wtime, btime=args.btime) move = StandardMove.from_str(str_move) game.move(move) if not args.silent: print_state(game) if game.variant.game_state[0]: