잠토의 잠망경

[딥러닝과 바둑] 틱택토 본문

Book/0006.딥러닝과 바둑

[딥러닝과 바둑] 틱택토

잠수함토끼 2020. 6. 19. 20:41

bookgithub

github

main source


from dlgo.ttt.ttttypes import Point
from dlgo.ttt.ttttypes import Player
from dlgo.ttt.tttboard import GameState
from dlgo.ttt.tttboard import Move
from dlgo.minmax.minmax import MinMaxAgent

from six.moves import input

COL_NAMES = 'ABC'


def print_board(board)->None:
    print('   A   B   C')
    for row in (1, 2, 3):
        pieces = []
        for col in (1, 2, 3):
            piece = board.get(Point(row, col))
            if piece == Player.x:
                pieces.append('X')
            elif piece == Player.o:
                pieces.append('O')
            else:
                pieces.append(' ')
        print('%d  %s' % (row, ' | '.join(pieces)))


def point_from_coords(text):
    col_name = text[0]
    row = int(text[1])
    return Point(row, COL_NAMES.index(col_name) + 1)

def main():

    game:GameState = GameState.new_game()

    human_player = Player.x

    bot = MinMaxAgent()

    while(not game.is_over()):
        print_board(game.board)

        if(game.next_player == human_player):
            human_move = input('-- ')
            point:Point = point_from_coords(human_move.strip())
            move = Move(point)
        else:
            move:Move = bot.select_move(game)

        game:GameState = game.apply_move(move)

    print_board(game.board)

    winner = game.winner()

    if winner is None:
        print("비김")
    else:
        print('winner: {0}'.format(winner))


    pass


if __name__ == '__main__':
    main()

실행 결과

Comments