コード例 #1
0
ファイル: main.py プロジェクト: taaha-khan/chess-ai
def run(agents, show_display = True):

	board = chess.Board()
	display.start()

	player = 0

	while not display.checkForQuit():

		display.update(board.fen())

		if not board.is_game_over():

			obs = lambda: None
			obs.board = board.fen()
			obs.mark = board.turn

			config = lambda: None
			config.actTimeout = 1

			active = agents[player]
			agent_move = active(obs, config)

			board.push(agent_move)
			player = -(player - 1)

	display.terminate()
コード例 #2
0
    display.start('8/8/8/8/8/8/8/8')
    state = 0
    while True:
        new_line = in_file.readline()
        if not new_line:
            time.sleep(1)
            continue
        new_line = new_line[0:-1]

        if new_line == 'reset':
            state = 0
            display.start('8/8/8/8/8/8/8/8')
            continue

        if new_line == 'stop':
            display.terminate()
            print('Game stopped. Bye bye.')
            break

        if state == 0:  # first board is detected
            fen = new_line
            print(fen)
            board = chess.Board(fen=fen)

            illegal = int(not board.is_valid())
            with open(out_path, 'w') as out_file:
                out_file.write(str(illegal))

            if not illegal:
                state = 1
                display.start(fen)
コード例 #3
0
def _terminate_display() -> None:
    """Close the Minichess visualization pygame window."""
    display.terminate()
コード例 #4
0
def lets_play_chess(player_color='white', previous_moves=None, fancy_display=False):
    if player_color == 'white':
        player_color = True
    elif player_color == 'black':
        player_color = False
    else:
        player_color = None
    if fancy_display:
        display.start()
    board = chess.Board()
    game_so_far = '<#endofdoc#>'
    tries = 0
    turn_counter = 1
    while not board.is_game_over():
        if board.turn and game_so_far[-1] != '.':
            game_so_far += f' {board.fullmove_number}.'
        if board.turn == player_color:
            proposed_move = input(f'{"White" if player_color else "Black"}\'s turn: ')
            try:
                move_san = board.parse_san(proposed_move)
            except:
                print('that is not a legal move. Try again')
                continue
            if board.is_legal(move_san):
                board.push_san(proposed_move)
                game_so_far += ' ' + str(proposed_move)
                print(board.unicode(borders=True, empty_square=' '))
                if fancy_display:
                    display.update(board.fen())

            else:
                print('that is not a legal move. Try again')
                continue
        else:
            proposed_move, tries = predict_next_move(game_so_far, tries)
            try:
                move_san = board.parse_san(proposed_move)
                board.push_san(proposed_move)
                game_so_far += ' ' + str(proposed_move)
                print(f'The computer succeeded on attempt number {tries} to make a legal move.')
                print(board.unicode(borders=True, empty_square=' '))
                if fancy_display:
                    display.update(board.fen())

                tries = 0
            except:
                print(f'try #{tries}: {proposed_move}')
                if tries > 10:
                    print('Looks like I need a little help... lets move randomly and move on')
                    random_moves = random.sample(board.legal_moves.__str__().split(' ')[3:], 1)[0]
                    random_moves = random_moves.replace('(', '').replace(')', '').replace('>', '').replace(',', '')
                    board.push_san(random_moves)
                    game_so_far += ' ' + str(proposed_move)
                    print(board.unicode(borders=True, empty_square=' '))
                    if fancy_display:
                        display.update(board.fen())
                    tries = 0
                else:
                    continue
            # if board.is_legal(move_san):
            #
            # else:

        print(game_so_far)
        if board.fullmove_number > 40:
            lets_play_chess(None)
            break
        if fancy_display:
            display.checkForQuit()
    print(f'the game is over! congrats to {"white" if board.result() == "1-0" else "black" if board.result() == "0-1" else "both on the tie"}')
    if fancy_display:
        display.terminate()