コード例 #1
0
def minmax(board: TTTBoard, player, maxDepth=8):
    if maxDepth == 0 or board.evaluate(player):
        return board.evaluate(player)
    legal = board.legal_moves
    if player == "O":
        x = []
        for i in legal:
            x.append((minmax(board.move(i), "X", maxDepth=len(legal) - 1), i))
            # print(board)
        x = sorted(x, key=lambda x: x[0], reverse=False)
    else:
        x = []
        for i in legal:
            x.append((minmax(board.move(i), "O", maxDepth=len(legal) - 1), i))
            # print(board)
        x = sorted(x, key=lambda x: x[0], reverse=True)
    # print(c4t)
    for i in x:
        if i[1] in legal:
            return i[0]
    if len(x) == 0:
        if player == "O":
            return 100
        else:
            return -100
コード例 #2
0
ファイル: Salman Test.py プロジェクト: FAdy-200/School-stuff
class TestTicTacToe(unittest.TestCase):
    def setUp(self):
        self.board = TTTBoard()

    def test_legal_moves(self):
        self.assertEqual(self.board.legal_moves, list(range(9)))
        n_moves = randint(0, 9)
        list_of_moves = list(range(9))
        # make random moves and test legal moves
        for i in range(n_moves):
            idx = randint(0, len(list_of_moves) - 1)
            position = list_of_moves[idx]
            self.board = self.board.move(position)
            del (list_of_moves[idx])
            self.assertEqual(self.board.legal_moves, list_of_moves)

    def test_win(self):
        # this is a win for X
        for move in [0, 1, 4, 2, 8]:
            self.assertFalse(self.board.is_win)
            self.board = self.board.move(move)
        self.assertTrue(self.board.is_win)

    def test_draw(self):
        def is_draw(board):
            return (not board.is_win) and (board.legal_moves == [])

        for move in [0, 1, 2, 3, 5, 8, 4, 6, 7]:
            self.assertFalse(is_draw(self.board))
            self.board = self.board.move(move)
        # The game is drawn now
        self.assertTrue(is_draw(self.board))
コード例 #3
0
class GuiBoard():
    def __init__(self, screen):
        self._screen = screen
        self._board = TTTBoard()
        self._grid = Grid(self._screen)
        self._msgBox = MsgBox(self._screen)
        self._msgStatus = STATUS.HIDE
        self._game_over = False

    def draw(self):
        self._grid.draw()
        self._msgBox.draw()

    def setMove(self, value):
        mx, my = value
        player_move = (my // CELL_HEIGHT * 3) + (mx // CELL_WIDTH)
        if player_move in self._board.legal_moves:
            self._board = self._board.move(Move(player_move))
            self._grid.pic(PLAYER, player_move)
            if self._board.is_win:
                self._game_over = True
                self._msgBox.active = STATUS.WIN
            elif self._board.is_draw:
                self._game_over = True
                self._msgBox.active = STATUS.DRAW
            if not self._game_over:
                computer_move = find_best_move(self._board)
                self._board = self._board.move(Move(computer_move))
                self._grid.pic(COMPUTER, computer_move)
                if self._board.is_win:
                    self._game_over = True
                    self._msgBox.active = STATUS.LOS
                elif self._board.is_draw:
                    self._game_over = True
                    self._msgBox.active = STATUS.DRAW

    def reset(self):
        self._board.reset()
        self._grid.reset()
        self._msgBox.active = STATUS.HIDE
        self._game_over = False

    @property
    def game_over(self):
        return self._game_over
コード例 #4
0
        for i in legal:
            x.append((minmax(board.move(i), "X", maxDepth=len(legal) - 1), i))
            # print(board)
        x = sorted(x, key=lambda x: x[0], reverse=False)
    else:
        x = []
        for i in legal:
            x.append((minmax(board.move(i), "O", maxDepth=len(legal) - 1), i))
            # print(board)
        x = sorted(x, key=lambda x: x[0], reverse=True)
    # print(c4t)
    for i in x:
        if i[1] in legal:
            return i[0]
    if len(x) == 0:
        if player == "O":
            return 100
        else:
            return -100


while not game.is_win:
    n = int(input())
    if n == 1:
        print()
        print()
    game = game.move(n)
    game = game.move(minmax(game, "O", maxDepth=len(game.legal_moves)-1))
    print(game)
    print(game.legal_moves)