def human_vs_human(self):
        self.state = TicTacToeState()
        
        while True:
            print("---------------------------------------------------------------\n")
            print(self.state)
            print("It's the turn of ", self.state.curr_player)
            move_valid = False
            while not move_valid:
                try:
                    row = int(input("Tell me a row: "))
                    col = int(input("Tell me a column: "))
                    if {row, col}.issubset(range(0,3)):
                        self.state.make_move(row, col)
                        move_valid = True
                    else:
                        raise ValueError
                except ValueError:
                   print("The move isn't valid!") 

            if not self.state.is_game_over():
                self.state = TicTacToeState(self.state)
            else:
                print("GAME OVER!")
                break
Example #2
0
import Square
from MiniMax import MiniMax
from TicTacToeAction import TicTacToeAction
from TicTacToeState import TicTacToeState

if __name__ == '__main__':
    print("The squares are numbered as follows:")
    print("1|2|3\n-+-+-\n4|5|6\n-+-+-\n7|8|9\n")
    mark = False
    print("Do you want to use pruning? 1=no 2=yes ")
    use = (int)(input())
    if use == 2:
        mark = True
    print("Who should start? 1=you 2=computer ")
    temp = (int)(input())
    s = TicTacToeState()
    s.player = Square.X
    if (temp == 1):
        s.playerToMove = Square.O
    else:
        s.playerToMove = Square.X
    while (True):
        if (s.playerToMove == Square.X):
            minimax = MiniMax()
            s = s.getResult(minimax.MinimaxDecision(s, mark))
        else:
            print("Which square do you want to set? (1--9) ")
            while (True):
                temp = (int)(input())
                if temp >= 1 & temp <= 9:
                    break
class TicTacToeGame(object):
    """description of class"""

    def __init__(self):
        return
            
        
    def human_vs_human(self):
        self.state = TicTacToeState()
        
        while True:
            print("---------------------------------------------------------------\n")
            print(self.state)
            print("It's the turn of ", self.state.curr_player)
            move_valid = False
            while not move_valid:
                try:
                    row = int(input("Tell me a row: "))
                    col = int(input("Tell me a column: "))
                    if {row, col}.issubset(range(0,3)):
                        self.state.make_move(row, col)
                        move_valid = True
                    else:
                        raise ValueError
                except ValueError:
                   print("The move isn't valid!") 

            if not self.state.is_game_over():
                self.state = TicTacToeState(self.state)
            else:
                print("GAME OVER!")
                break
        
    def ai_vs_ai(self):
        initial_state = TicTacToeState()
        initial_node = TicTacToeNode(initial_state)
        problem = TicTacToeProblem()
        engine = Negascout(problem, search_depth = 4, order_moves = True)
       

        
        current_node = initial_node
 
        while not problem.is_end_node(current_node):
            engine.reset_engine()
            print("\n---------------------------------------------------------------")
            print("\nIt's the turn of ", current_node.state.curr_player, "\n")
            print(current_node.state)
                               
            engine.perform_search(current_node)           
            current_node = engine.obtained_successor
            print("Obtained value: ", engine.obtained_value)
            print("Visited states: ", engine.num_of_visited_states, "\n")
            time.sleep(0)
            clear = lambda: os.system('cls')
            #clear()
                

        print("\n------------------------------------------------------------")
        print("------------------------ GAME OVER! ------------------------")
        print("------------------------------------------------------------\n")
        print(current_node.state)
        print("Obtained value: ", engine.obtained_value, "\n")