Exemple #1
0
class Play:

    def __init__(self, AIModel, playerRole, inputter=DefaultInputter, displayer=DefaultDisplayer):
        self.aiModel = AIModel
        self.playerRole = playerRole
        self.inputter = inputter
        self.displayer = displayer

    def Start(self):
        self.game = Game()
        while not self.game.IsFull():
            self.displayer(self.game)
            if self.playerRole == self.game.GetCurrentPlayer():
                while True:
                    inputPos = self.inputter()
                    x, y = self.game.PosToXY(inputPos)
                    if self.game.IsEmpty(x, y):
                        break
                    else:
                        print("Place already taken up")
                        continue
            else:
                x, y = Train.GetNextBestMove(self.aiModel, self.game)

            self.game.Play(self.game.GetCurrentPlayer(), x, y)

            if self.game.GetWinner() != Game.EMPTY:
                self.displayer(self.game)
                print("Player %s wins ! " % Game.PLAYER_ATTR[self.game.GetWinner()]['name'])
                break
            elif self.game.IsFull():
                self.displayer(self.game)
                print("It's a tie !")
                break
Exemple #2
0
    def PlayGame(self, game : Game, cb = None):
        winner = game.GetWinner()
        steps = 1
        while not game.IsFull():
            winner = game.GetWinner()
            if winner != Game.EMPTY:
                break
            if game.GetCurrentPlayer() == Game.O:
                x, y = self.GetNextBestMove(self.modelO, game)
            else:
                x, y = self.GetNextBestMove(self.modelX, game)
            game.Play(game.GetCurrentPlayer(), x, y)
            steps += 1
            if cb:
                cb(game.clone())

        return winner, steps
Exemple #3
0
def TestGame():
    print(Game().GetBoard())

    assert (Game().GetWinner() == Game.EMPTY)

    game = Game()
    game.Play(Game.O, 0, 0)
    game.Play(Game.O, 0, 1)
    game.Play(Game.O, 0, 2)
    print(game)
    assert (game.GetWinner() == Game.O)

    game = Game()
    game.Play(Game.X, 1, 0)
    game.Play(Game.X, 0, 0)
    game.Play(Game.X, 2, 0)
    print(game)
    assert (game.GetWinner() == Game.X)

    game = Game()
    game.Play(Game.X, 0, 0)
    game.Play(Game.X, 1, 1)
    game.Play(Game.X, 2, 2)
    print(game)
    assert (game.GetWinner() == Game.X)

    game = Game()
    game.Play(Game.O, 0, 0)
    game.Play(Game.O, 1, 1)
    game.Play(Game.O, 2, 2)
    print(game)
    assert (game.GetWinner() == Game.O)

    game = Game()
    game.Play(Game.O, 2, 0)
    game.Play(Game.O, 1, 1)
    game.Play(Game.O, 0, 2)
    print(game)
    assert (game.GetWinner() == Game.O)

    game = Game()
    game.Play(Game.O, 1, 0)
    game.Play(Game.O, 1, 1)
    game.Play(Game.O, 1, 2)
    print(game)
    assert (game.GetWinner() == Game.O)

    game = Game()
    game.Play(Game.O, 0, 0)
    game.Play(Game.O, 0, 2)
    game.Play(Game.O, 1, 2)
    game.Play(Game.X, 1, 0)
    game.Play(Game.X, 1, 1)
    game.Play(Game.X, 2, 1)
    game.Play(Game.O, 2, 2)
    print(game)
    assert (game.GetWinner() == Game.O)

    def RandomGames():
        game = Game.GetRandomGame()
        assert (game.GetWinner() == Game.EMPTY)
        return game

    for i in range(1000):
        RandomGames()

    for i in range(4):
        print(RandomGames())

    print(Game().AsVector())

    game = Game()
    assert (not game.IsFull())
    for i in range(3):
        for j in range(3):
            game.Play(Game.O, i, j)
    assert (game.IsFull())

    game = Game()
    game.Play(Game.O, 0, 2)
    game.Play(Game.O, 1, 2)
    game.Play(Game.O, 2, 2)
    print(game)
    assert (game.GetWinner() == Game.O)

    game = Game()
    game.Play(Game.X, 0, 2)
    game.Play(Game.X, 1, 2)
    game.Play(Game.X, 2, 2)
    print(game)
    assert (game.GetWinner() == Game.X)