示例#1
0
def main(do_print=True):
    net = DummyNetwork()
    agent = AiAgent(net)
    ui = ConsoleUi()

    while True:
        game = GameState()
        game.start_new()

        # self-play phase
        # history of the game including stats for training are stored in session
        session = Session(game, agent, agent)
        session.before_move(lambda: ui.draw_board(game.board))
        session.play()

        # training phase
        # TODO

        if do_print:
            if game.state == game.WHITE_WON:
                print(f"Game finished, white won")
            elif game.state == game.BLACK_WON:
                print(f"Game finished, black won")
            elif game.state == game.STALEMATE:
                print(f"Game finished, stalemate")
            elif game.state == game.DRAW:
                print(f"Game finished, draw")
示例#2
0
    def test_setup(self):
        rules = GameState()
        rules.setup_board()
        self.assertEqual(None, rules.last_move_double_file)
        self.assertEqual(True, rules.specific[PlayerEnum.white].can_castle_queen_side)
        self.assertEqual(True, rules.specific[PlayerEnum.white].can_castle_king_side)
        self.assertEqual(True, rules.specific[PlayerEnum.black].can_castle_queen_side)
        self.assertEqual(True, rules.specific[PlayerEnum.black].can_castle_king_side)
        self.assertEqual(PlayerEnum.white, rules.board.turn)

        for rank in range(8):
            for file in range(8):
                if rank < 2:
                    self.assertEqual(PlayerEnum.white, rules.board.board[file][rank].player)
                elif rank < 6:
                    self.assertEqual(None, rules.board.board[file][rank].player)
                else:
                    self.assertEqual(PlayerEnum.black, rules.board.board[file][rank].player)

        for rank in (1, 6):
            for file in range(8):
                self.assertEqual(PieceEnum.pawn, rules.board.board[file][rank].piece)

        for rank in (0, 7):
            self.assertEqual(PieceEnum.rook, rules.board.board[0][rank].piece)
            self.assertEqual(PieceEnum.knight, rules.board.board[1][rank].piece)
            self.assertEqual(PieceEnum.bishop, rules.board.board[2][rank].piece)
            self.assertEqual(PieceEnum.queen, rules.board.board[3][rank].piece)
            self.assertEqual(PieceEnum.king, rules.board.board[4][rank].piece)
            self.assertEqual(PieceEnum.bishop, rules.board.board[5][rank].piece)
            self.assertEqual(PieceEnum.knight, rules.board.board[6][rank].piece)
            self.assertEqual(PieceEnum.rook, rules.board.board[7][rank].piece)
示例#3
0
def prepare_board(config, turn):
    rules = GameState()
    for pos, (player, piece) in config.items():
        rules.board.board[pos.file][pos.rank].player = player
        rules.board.board[pos.file][pos.rank].piece = piece
    rules.board.turn = turn
    rules.update_legal_moves()
    return rules
示例#4
0
def main(do_print=True):
    game = GameState()
    game.start_new()

    # setup custom position
    # from chess_backend.common import PieceEnum, POS, PlayerEnum
    # game.board.clear_board()
    # custom_white = {POS("a6"): PieceEnum.pawn,
    #                 POS("h1"): PieceEnum.king}
    # custom_black = {POS("e1"): PieceEnum.knight,
    #                 POS("h2"): PieceEnum.pawn,
    #                 POS("h3"): PieceEnum.king}
    # for pos, piece in custom_white.items():
    #     game.board.board[pos.file][pos.rank].player = PlayerEnum.white
    #     game.board.board[pos.file][pos.rank].piece = piece
    # for pos, piece in custom_black.items():
    #     game.board.board[pos.file][pos.rank].player = PlayerEnum.black
    #     game.board.board[pos.file][pos.rank].piece = piece
    # game.specific[PlayerEnum.white].can_castle_queen_side = False
    # game.specific[PlayerEnum.white].can_castle_king_side = False
    # game.specific[PlayerEnum.black].can_castle_queen_side = False
    # game.specific[PlayerEnum.black].can_castle_king_side = False
    # game.board.turn = PlayerEnum.black
    # game.update_legal_moves()

    net = DummyNetwork()
    white = RandomAiAgent()
    black = AiAgent(net)
    ui = ConsoleUi()

    session = Session(game, white, black)
    session.before_move(lambda: ui.draw_board(game.board))
    session.play()

    if do_print:
        if game.state == game.WHITE_WON:
            print(f"Game finished, white won")
        elif game.state == game.BLACK_WON:
            print(f"Game finished, black won")
        elif game.state == game.STALEMATE:
            print(f"Game finished, stalemate")
        elif game.state == game.DRAW:
            print(f"Game finished, draw")
示例#5
0
def main(do_print=True):
    game = GameState()
    game.start_new()

    white = RandomAiAgent(prefer_takes=True)
    black = RandomAiAgent(prefer_takes=True)
    ui = ConsoleUi()

    session = Session(game, white, black)
    session.before_move(lambda: ui.draw_board(game.board))
    session.play()

    if do_print:
        if game.state == game.WHITE_WON:
            print(f"Game finished, white won")
        elif game.state == game.BLACK_WON:
            print(f"Game finished, black won")
        elif game.state == game.STALEMATE:
            print(f"Game finished, stalemate")
        elif game.state == game.DRAW:
            print(f"Game finished, draw")
示例#6
0
def main(do_print=True):
    net = DummyNetwork()
    white = RandomAiAgent()
    black = AiAgent(net)

    game_number = 1
    white_wins = 0
    black_wins = 0
    draws = 0
    all = 0
    while True:

        game = GameState()
        game.start_new()

        idx = 1
        while game.state == game.RUNNING:

            if game.board.turn == PlayerEnum.white:
                selected = white.select_action(game)
            else:
                selected = black.select_action(game)

            game.apply(selected)
            idx += 1

        if game.state == game.WHITE_WON:
            white_wins += 1
        elif game.state == game.BLACK_WON:
            black_wins += 1
        elif game.state == game.STALEMATE:
            draws += 1
        elif game.state == game.DRAW:
            draws += 1
        all += 1

        print(f"{game_number:4d}: "
              f"white_wins:{(100 * white_wins) / all:3.2f}%\t"
              f"black_wins:{(100 * black_wins) / all:3.2f}%\t"
              f"draws:{(100 * draws) / all:3.2f}%")
        game_number += 1
示例#7
0
from chess_backend.GameState import GameState
from chess_backend.Session import Session
from agents.HumanConsoleAgent import HumanConsoleAgent
from agents.RandomAiAgent import RandomAiAgent
from ui.console import ConsoleUi

if __name__ == '__main__':

    game = GameState()
    game.start_new()

    white = HumanConsoleAgent()
    black = RandomAiAgent()
    ui = ConsoleUi()

    session = Session(game, white, black)
    session.before_move(lambda: ui.draw_board(game.board))

    def report_move():
        last_move = session.history[-1][1]
        if last_move.claim_draw:
            print(f"move chosen: claim draw")
        else:
            print(f"move chosen: {last_move.move_from} -> {last_move.move_to}")
    session.after_move(report_move)

    session.play()

    if game.state == game.WHITE_WON:
        print(f"Game finished, white won")
    elif game.state == game.BLACK_WON: