Esempio n. 1
0
 def __init__(self, master):
     ''' Initializes the variables for the game and tkinter window.
     Pre: The Tkinter master is supplied.
     Post: Game and window initialized. '''
     self.turn = "O"
     ## Adding menu bar
     self.themenu = Menu(master)
     self.file_label_menu = Menu(self.themenu, tearoff=0)
     # Submenu
     self.file_new_submenu = Menu(self.file_label_menu)
     self.file_new_submenu.add_command(label="Human vs. Human",command = self.new_human)
     # Difficulty submenus
     self.new_submenu_ai_first_submenu = Menu(self.file_new_submenu)
     self.new_submenu_ai_first_submenu.add_command(label="Easy",command = self.new_ai_first_easy)
     self.new_submenu_ai_first_submenu.add_command(label="Medium",command = self.new_ai_first_medium)
     self.new_submenu_ai_first_submenu.add_command(label="Hard",command = self.new_ai_first_hard)
     self.file_new_submenu.add_cascade(label="Computer vs. Human",menu=self.new_submenu_ai_first_submenu)
     self.new_submenu_ai_submenu = Menu(self.file_new_submenu)
     self.new_submenu_ai_submenu.add_command(label="Easy",command=self.new_ai_easy)
     self.new_submenu_ai_submenu.add_command(label="Medium",command=self.new_ai_medium)
     self.new_submenu_ai_submenu.add_command(label="Hard",command=self.new_ai_hard)
     self.file_new_submenu.add_cascade(label="Human vs Computer",menu=self.new_submenu_ai_submenu)
     self.file_label_menu.add_cascade(label="New Game",menu=self.file_new_submenu)
     self.file_label_menu.add_separator()
     self.file_label_menu.add_command(label="Exit",command=master.quit)
     self.themenu.add_cascade(label="File",menu=self.file_label_menu)
     master.config(menu=self.themenu)
     # Frames used in window.
     buttonframe = Frame(master)
     buttonframe.pack()
     frame = Frame(master)
     frame.pack(side=TOP,fill=BOTH,expand=True)
     # Label in button frame
     self.label_message = StringVar()
     Label(buttonframe,textvariable=self.label_message,font=("serif",16)).pack()
     # Canvas for drawing
     self.width = 400
     self.height = 400
     self.x_offset = 0
     self.y_offset = 0
     self.canvas = Canvas(frame,width=self.width,height=self.height,relief=SUNKEN)
     self.canvas.grid()
     self.canvas.pack(side=TOP,fill=BOTH,expand=True)
     # Setting event responses
     self.canvas.bind("<Configure>",self.draw_board)
     self.canvas.bind("<Button-1>",self.WhichSquare)
     # Initializes Game.
     self.g = TTT_game.game()
     self.label_message.set(self.g.message)
     # AI is false
     self.use_ai = False
Esempio n. 2
0
def main():
    board_numbers()
    g = TTT_game.game()

    # Asking for AI game difficulity
    difficulty_message = "Enter game difficulity (0 for easy, 1 for medium, 2 for hard): "
    difficulty = int(raw_input(difficulty_message))
    a = TTT_AI.AI(difficulty)

    # Asking who goes first
    order_message = "AI first [y/n]: "
    order = raw_input(order_message)
    if order == "y":
        ai_move = a.next_move(g.board, g.turn)
        if g.TakeTurn(ai_move) == False:
            error_message = "AI gave bad coordinate: " + str(ai_move)
            print (error_message)
            exit()
    elif order != "n":
        error_message = "Input needs to be 'y' or 'n'"
        print (error_message)
        exit()

    # Game main loop
    while g.gameover == False:
        # Beginning Output
        print (g.message)
        print_board(g.board)

        # Finding the move
        coor = int(raw_input("Please Enter Move: "))

        # Running through game move
        if coor < 9 and coor >= 0:
            if g.TakeTurn(coor):
                g.CheckEnd()
                if g.gameover == False:  # Continue AI move if game is not over
                    ai_move = a.next_move(g.board, g.turn)
                    if g.TakeTurn(ai_move):  # Check move
                        g.CheckEnd()  # Checking if AI made winning move or tie.
                    else:
                        error_message = "AI gave bad coordinate: " + str(ai_move)
                        print (error_message)
                        exit()

    # End Message and goodbye
    print ("\n" + g.message)
    print_board(g.board)
    print ("Goodbye")