Beispiel #1
0
    def iterate(self,
                game: TicTacToe,
                train: bool = False,
                print_q: bool = False):
        moves = game.get_legal_moves()
        move = self.random_move(moves)
        field_hash = game.get_hash()

        if self.ai:
            move = self.get_optimal_move(moves, field_hash, print_q, train)
            self.history = [{
                "hash": field_hash,
                "move": str(move)
            }] + self.history

        game.input(int(move))
                    print(game.get_winner(), "has won!")
                else:
                    print("It's a tie!")
                restart = input("Do you want to restart? (Y/n) ")
                if restart == "n":
                    break
                else:
                    game.reset()
                    continue

            if game.get_player() is Player.TWO:
                field = input(
                    str(game.get_player(human=True)) +
                    " it's your turn! Input a field between 1-9: ")
            else:
                ai1.iterate(game, False, True)
                continue

            try:
                field = int(field) - 1
                if field < 0 or field > 8:
                    raise ValueError
            except ValueError:
                print("ERROR! Please input a field number between 1-9!")
                continue

            try:
                game.input(field)
            except ValueError:
                print("ERROR! The field is taken")