Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from tkinter import ttk
- root = Tk()
- #root.geometry('495x480')
- root.configure(bg="black")
- style = ttk.Style()
- s = ttk.Style()
- s.configure('TButton',background = 'blue', foreground = "red"
- , font = ('Sans','20','bold'), relief = "blue")
- s.map('TButton',foreground = [('active','black')] )
- s.map('TButton', background = [('active',"red")])
- userXO=" _ "
- AI_XO=" _ "
- def board(event=None):
- global labelframe9
- global button9
- labelframe9 = LabelFrame(m1,bg=AI_XO_COLOR)
- labelframe9.pack(side=BOTTOM,fill=BOTH, expand=YES)
- button9 = Label(labelframe9, text=userXO, font=fontSize, bg="blue")
- button9.bind("<Button-1>",setMove9)
- button9.bind("<Enter>",setDinamicBg9)
- button9.bind("<Leave>",setBg9)
- button9.pack(fill=BOTH,expand=YES)
- #this is Key
- if(list[0]==" "):
- setMove9(event=None)
- setDinamicBg9(event=None)
- setBg9(event=None)
- #####################################
- global labelframe6
- global button6
- labelframe6 = LabelFrame(m1,bg="red")
- labelframe6.pack(fill=BOTH, expand=YES)
- button6 = Label(labelframe6, text=userXO, font=fontSize, bg="blue")
- ##
- button6.bind("<Button-1>",setMove6)
- button6.bind("<Enter>",setDinamicBg6)
- button6.bind("<Leave>",setBg6)
- button6.pack(fill=BOTH,expand=YES)
- ##################################
- global labelframe3
- global button3
- labelframe3 = LabelFrame(m1,bg="red")
- labelframe3.pack(fill=BOTH, expand=YES)
- button3 = Label(labelframe3, text=userXO, font=fontSize,bg="blue")
- button3.pack(fill=BOTH, expand=YES)
- button3.bind("<Button-1>",setMove3)
- button3.bind("<Enter>",setDinamicBg3)
- button3.bind("<Leave>",setBg3)
- ###################################
- global labelframe1
- global button1
- labelframe1 = LabelFrame(m2,bg="red")
- labelframe1.pack(fill=BOTH, expand=YES)
- button1 = Label(labelframe1, text=AI_XO,font=fontSize,bg="blue")
- button1.pack(side=BOTTOM,fill=BOTH,expand=YES)
- button1.bind("<Button-1>",setMove1)
- button1.bind("<Enter>",setDinamicBg1)
- button1.bind("<Leave>",setBg1)
- ####################################
- global labelframe4
- global button4
- labelframe4 = LabelFrame(m2,bg="red")
- labelframe4.pack(fill=BOTH, expand=YES)
- button4 = Label(labelframe4, text=AI_XO,font=fontSize,bg="blue")
- button4.pack(side=BOTTOM,fill=BOTH,expand=YES)
- button4.bind("<Button-1>",setMove4)
- button4.bind("<Enter>",setDinamicBg4)
- button4.bind("<Leave>",setBg4)
- ########################################
- global labelframe7
- global button7
- labelframe7 = LabelFrame(m2,bg="red")
- labelframe7.pack(fill=BOTH, expand=YES)
- button7 = Label(labelframe7, text=AI_XO,font=fontSize,bg="blue")
- button7.pack(side=BOTTOM,fill=BOTH,expand=YES)
- button7.bind("<Button-1>",setMove7)
- button7.bind("<Enter>",setDinamicBg7)
- button7.bind("<Leave>",setBg7)
- #########################################
- global labelframe2
- global button2
- labelframe2 = LabelFrame(m3,bg="red")
- labelframe2.pack(fill=BOTH, expand=YES)
- button2 = Label(labelframe2, text=AI_XO,font=fontSize,bg="blue")
- button2.pack(side=BOTTOM,fill=BOTH,expand=YES)
- button2.bind("<Button-1>",setMove2)
- button2.bind("<Enter>",setDinamicBg2)
- button2.bind("<Leave>",setBg2)
- #########################################
- global labelframe5
- global button5
- labelframe5 = LabelFrame(m3,bg="red")
- labelframe5.pack(fill=BOTH, expand=YES)
- button5 = Label(labelframe5, text=AI_XO,font=fontSize,bg="blue")
- button5.pack(side=BOTTOM,fill=BOTH,expand=YES)
- button5.bind("<Button-1>",setMove5)
- button5.bind("<Enter>",setDinamicBg5)
- button5.bind("<Leave>",setBg5)
- #########################################
- global labelframe8
- global button8
- labelframe8 = LabelFrame(m3,bg="red")
- labelframe8.pack(fill=BOTH, expand=YES)
- button8 = Label(labelframe8, text=AI_XO,font=fontSize,bg="blue")
- button8.pack(side=BOTTOM,fill=BOTH,expand=YES)
- button8.bind("<Button-1>",setMove8)
- button8.bind("<Enter>",setDinamicBg8)
- button8.bind("<Leave>",setBg8)
- def setUserOX(XO):
- global userXO
- userXO=XO
- def setAI_OX(XO):
- global AI_XO
- AI_XO=XO
- def remMainMenue():
- play.destroy()
- exitMenue.destroy()
- global selectXO
- selectXO()
- board()
- def mainMenue():
- container=PanedWindow()
- container.pack(side=TOP,fill=BOTH,expand=YES)
- global board
- global play
- global exitMenue
- play=ttk.Button(container,command=remMainMenue,text="play")
- play.pack(side=LEFT,fill=BOTH,expand=YES)
- exitMenue=ttk.Button(container,command=root.destroy,text="exit")
- exitMenue.pack(side=LEFT,fill=BOTH,expand=YES)
- def selectXO():
- global container
- global setO
- global setX
- global O
- global X
- container=PanedWindow()
- container.pack(side=TOP,fill=BOTH,expand=YES)
- O=ttk.Button(container,text="O")
- O.pack(side=LEFT,fill=BOTH,expand=YES)
- X=ttk.Button(container,text="X")
- X.pack(side=LEFT,fill=BOTH,expand=YES)
- O.bind("<Button-1>",setO)
- X.bind("<Button-1>",setX)
- def restart1():
- global reset
- global restart
- global exit
- restart=ttk.Button(container,command=reset,text="restart")
- restart.pack(side=LEFT,fill=BOTH,expand=YES)
- exit=ttk.Button(container,command=root.destroy,text="quit")
- exit.pack(side=LEFT,fill=BOTH,expand=YES)
- def exit():
- root.destroy()
- def setO(self):
- setAI_OX(" X ")
- setUserOX(" O ")
- O.destroy()
- X.destroy()
- global restart1
- restart1()
- def setX(slef):
- setAI_OX(" O ")
- setUserOX(" X ")
- O.destroy()
- X.destroy()
- global restart1
- restart1()
- def reset(event=None):
- global userXO
- userXO=" _ "
- global AI_XO
- AI_XO=" _ "
- button9.config(text=userXO)
- button8.config(text=userXO)
- button7.config(text=userXO)
- button6.config(text=userXO)
- button5.config(text=userXO)
- button4.config(text=userXO)
- button3.config(text=userXO)
- button2.config(text=userXO)
- button1.config(text=userXO)
- global restart1
- restart.destroy()
- exit.destroy()
- board()
- X=ttk.Button(container,command=restart1,text="X")
- X.pack(side=LEFT,fill=BOTH,expand=YES)
- O=ttk.Button(container,command=root.destroy,text="O")
- O.pack(side=LEFT,fill=BOTH,expand=YES)
- AI_XO_COLOR="black"
- fontSize="size 80"
- dinamicColorBg="red"
- list=[" "," "," "," "," "," "," "," "," "]
- m1 = PanedWindow()
- m1.pack(side=RIGHT,fill=BOTH, expand=YES)
- m2 = PanedWindow()
- m2.pack(side=LEFT,fill=BOTH, expand=YES)
- m3 = PanedWindow()
- m3.pack(side=LEFT,fill=BOTH, expand=YES)
- from random import randrange,uniform
- ai=randrange(0,9)
- while(list[ai]=="X" and list[ai]=="O"):
- ai=randrange(0,9)
- ##########################################
- def setMove9(event=None):
- list[8]=userXO
- button9.config(text=userXO)
- button9.config(bg=dinamicColorBg)
- def setDinamicBg9(event=None):
- button9.config(fg="black")
- button9.config(bg="red")
- def setBg9(event=None):
- button9.config(bg="blue")
- ###########################################
- def setMove7(self):
- list[6]=userXO
- button7.config(text=userXO)
- button7.config(bg="red")
- def setDinamicBg7(event=None):
- button7.config(fg="black")
- button7.config(bg="red")
- def setBg7(event=None):
- button7.config(bg="blue")
- ############################################
- def setMove6(self):
- list[5]=userXO
- button6.config(text=userXO)
- button6.config(bg="red")
- def setDinamicBg6(event=None):
- button6.config(fg="black")
- button6.config(bg="red")
- def setBg6(event=None):
- button6.config(bg="blue")
- ##################################
- def setMove3(self):
- list[2]=userXO
- button3.config(text=userXO)
- button3.config(bg="red")
- def setDinamicBg3(event=None):
- button3.config(fg="black")
- button3.config(bg="red")
- def setBg3(event=None):
- button3.config(bg="blue")
- ###################################
- def setMove1(self):
- list[0]=userXO
- button1.config(text=userXO)
- button1.config(bg="red")
- def setDinamicBg1(event=None):
- button1.config(fg="black")
- button1.config(bg="red")
- def setBg1(event=None):
- button1.config(bg="blue")
- ####################################
- def setMove4(self):
- list[3]=userXO
- button4.config(text=userXO)
- button4.config(bg="red")
- def setDinamicBg4(event=None):
- button4.config(fg="black")
- button4.config(bg="red")
- def setBg4(event=None):
- button4.config(bg="blue")
- ####################################
- def setMove2(self):
- list[1]=userXO
- button2.config(text=userXO)
- button2.config(bg="red")
- def setDinamicBg2(event=None):
- button2.config(fg="black")
- button2.config(bg="red")
- def setBg2(event=None):
- button2.config(bg="blue")
- ####################################
- def setMove5(self):
- list[4]=userXO
- button5.config(text=userXO)
- button5.config(bg="red")
- def setDinamicBg5(event=None):
- button5.config(fg="black")
- button5.config(bg="red")
- def setBg5(event=None):
- button5.config(bg="blue")
- #########################################
- def setMove8(self):
- list[7]=userXO
- button8.config(text=userXO)
- button8.config(bg="red")
- def setDinamicBg8(event=None):
- button8.config(fg="black")
- button8.config(bg="red")
- def setBg8(event=None):
- button8.config(bg="blue")
- mainMenue()
- """""
- #display instructions for the player
- print("The object of Tic Tac Toe is to get three in a row. \n"
- "You play on a three by three game board. \n"
- "The first player is known as X and the second is O. \n"
- "Players alternate placing Xs and Os on the game board until\n"
- "either opponent has three in a row or all nine squares are filled.\n")
- move=[0,1,2,3,4,5,6,7,8]
- #function that prints board
- def board():
- print("::======:=======:======::")
- print("|| ",move[0]," | ",move[1]," | ",move[2]," ||")
- print("::------|-------|------::")
- print("|| ",move[3]," | ",move[4]," | ",move[5]," ||")
- print("::------|-------|------::")
- print("|| ",move[6]," | ",move[7]," | ",move[8]," ||")
- print("::======:=======:======::")
- print("\n\n")
- #function that clears the board
- def resetBoard():
- for i in range(0,9):
- move[i]=" "
- userXO=" "
- AI_XO=" "
- #function that makes a random selection of X or O for the user
- from random import randrange,uniform
- print("Do you want to be X or O?:")
- playerXO=input()
- playerXO=str(playerXO)
- xrand=randrange(0,2)
- if(playerXO=="X" or playerXO=="x"):
- userXO="X"
- AI_XO="O"
- elif(playerXO=="O" or playerXO=="o"):
- userXO="O"
- AI_XO="X"
- else:
- while(playerXO!="X" or playerXO!="x" or playerXO!="O" or playerXO!="o"):
- print("Wrong input\nDo you want to be X or O?:")
- playerXO=input()
- playerXO=str(playerXO)
- if(playerXO=="X" or playerXO=="x"):
- userXO="X"
- AI_XO="O"
- break
- elif(playerXO=="O" or playerXO=="o"):
- userXO="O"
- AI_XO="X"
- break
- # function that sets moves
- def set(setMove,XO):
- move[setMove]=XO
- #function that sets user's move
- def userMove():
- print("Please select a move for "+userXO+":")
- user=input()
- user=int(user)
- if(move[user]=="X" or move[user]=="O"):
- while(move[user]=="X" or move[user]=="O"):
- print("Move not available please select another move for ",userXO,":")
- user=input()
- user=int(user)
- set(user,userXO)
- else:
- set(user,userXO)
- def checkForWinner():
- if(move[0]==AI_XO and move[1]==AI_XO and move[2]==AI_XO):
- resetBoard()
- set(0,AI_XO)
- set(1,AI_XO)
- set(2,AI_XO)
- return 1
- elif(move[3]==AI_XO and move[4]==AI_XO and move[5]==AI_XO ):
- resetBoard()
- set(3,AI_XO)
- set(4,AI_XO)
- set(5,AI_XO)
- return 1
- elif(move[6]==AI_XO and move[7]==AI_XO and move[8]==AI_XO ):
- resetBoard()
- set(6,AI_XO)
- set(7,AI_XO)
- set(8,AI_XO)
- return 1
- elif(move[0]==AI_XO and move[3]==AI_XO and move[6]==AI_XO ):
- resetBoard()
- set(0,AI_XO)
- set(3,AI_XO)
- set(6,AI_XO)
- return 1
- elif(move[1]==AI_XO and move[4]==AI_XO and move[7]==AI_XO ):
- resetBoard()
- set(1,AI_XO)
- set(4,AI_XO)
- set(7,AI_XO)
- return 1
- elif(move[2]==AI_XO and move[5]==AI_XO and move[8]==AI_XO ):
- resetBoard()
- set(2,AI_XO)
- set(5,AI_XO)
- set(8,AI_XO)
- return 1
- elif(move[0]==AI_XO and move[4]==AI_XO and move[8]==AI_XO ):
- resetBoard()
- set(0,AI_XO)
- set(4,AI_XO)
- set(8,AI_XO)
- return 1
- elif(move[6]==AI_XO and move[4]==AI_XO and move[2]==AI_XO ):
- resetBoard()
- set(6,AI_XO)
- set(4,AI_XO)
- set(2,AI_XO)
- return 1
- elif(move[0]==userXO and move[1]==userXO and move[2]==userXO):
- resetBoard()
- set(0,userXO)
- set(1,userXO)
- set(2,userXO)
- return -1
- elif(move[3]==userXO and move[4]==userXO and move[5]==userXO ):
- resetBoard()
- set(3,userXO)
- set(4,userXO)
- set(5,userXO)
- return -1
- elif(move[6]==userXO and move[7]==userXO and move[8]==userXO ):
- resetBoard()
- set(6,userXO)
- set(7,userXO)
- set(8,userXO)
- return -1
- elif(move[0]==userXO and move[3]==userXO and move[6]==userXO ):
- resetBoard()
- set(0,userXO)
- set(3,userXO)
- set(6,userXO)
- return -1
- elif(move[1]==userXO and move[4]==userXO and move[7]==userXO ):
- resetBoard()
- set(1,userXO)
- set(4,userXO)
- set(7,userXO)
- return -1
- elif(move[2]==userXO and move[5]==userXO and move[8]==userXO ):
- resetBoard()
- set(2,userXO)
- set(5,userXO)
- set(8,userXO)
- return -1
- elif(move[0]==userXO and move[4]==userXO and move[8]==userXO ):
- resetBoard()
- set(0,userXO)
- set(4,userXO)
- set(8,userXO)
- return -1
- elif(move[6]==userXO and move[4]==userXO and move[2]==userXO ):
- resetBoard()
- set(6,userXO)
- set(4,userXO)
- set(2,userXO)
- return -1
- else:
- return 0
- def chechIfBoardIsFull():
- checkBoard=1
- for i in range(0,9):
- if(move[i]==" "):
- checkBoard=0
- return checkBoard
- #function that sets AI's move
- def AI_Move():
- ai=randrange(0,9)
- winner=[[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[6,4,2]]
- ### best move across the top
- if(move[0]==AI_XO and move[1]==AI_XO and move[2]!=userXO):
- set(2,AI_XO)
- elif(move[2]==AI_XO and move[1]==AI_XO and move[0]!=userXO):
- set(0,AI_XO)
- elif(move[0]==AI_XO and move[2]==AI_XO and move[1]!=userXO):
- set(1,AI_XO)
- ## best move across the middle
- elif(move[3]==AI_XO and move[4]==AI_XO and move[5]!=userXO):
- set(5,AI_XO)
- elif(move[5]==AI_XO and move[4]==AI_XO and move[3]!=userXO):
- set(3,AI_XO)
- elif(move[3]==AI_XO and move[5]==AI_XO and move[4]!=userXO):
- set(4,AI_XO)
- ## best move across the bottom
- elif(move[6]==AI_XO and move[7]==AI_XO and move[8]!=userXO):
- set(7,AI_XO)
- elif(move[8]==AI_XO and move[7]==AI_XO and move[6]!=userXO):
- set(6,AI_XO)
- elif(move[6]==AI_XO and move[8]==AI_XO and move[7]!=userXO):
- set(7,AI_XO)
- ### best move down the right
- elif(move[0]==AI_XO and move[3]==AI_XO and move[6]!=userXO):
- set(6,AI_XO)
- elif(move[6]==AI_XO and move[3]==AI_XO and move[0]!=userXO):
- set(0,AI_XO)
- elif(move[0]==AI_XO and move[6]==AI_XO and move[3]!=userXO):
- set(3,AI_XO)
- ### best move down the middle
- elif(move[1]==AI_XO and move[4]==AI_XO and move[7]!=userXO):
- set(7,AI_XO)
- elif(move[7]==AI_XO and move[4]==AI_XO and move[1]!=userXO):
- set(1,AI_XO)
- elif(move[1]==AI_XO and move[7]==AI_XO and move[4]!=userXO):
- set(4,AI_XO)
- ### best move down the left
- elif(move[2]==AI_XO and move[5]==AI_XO and move[8]!=userXO):
- set(8,AI_XO)
- elif(move[8]==AI_XO and move[5]==AI_XO and move[2]!=userXO):
- set(2,AI_XO)
- elif(move[2]==AI_XO and move[8]==AI_XO and move[5]!=userXO):
- set(5,AI_XO)
- ## best move diagonal
- elif(move[0]==AI_XO and move[4]==AI_XO and move[8]!=userXO):
- set(8,AI_XO)
- elif(move[8]==AI_XO and move[4]==AI_XO and move[0]!=userXO):
- set(0,AI_XO)
- elif(move[0]==AI_XO and move[8]==AI_XO and move[4]!=userXO):
- set(4,AI_XO)
- ## best move diagonal
- elif(move[2]==AI_XO and move[4]==AI_XO and move[6]!=userXO):
- set(6,AI_XO)
- elif(move[6]==AI_XO and move[4]==AI_XO and move[2]!=userXO):
- set(2,AI_XO)
- elif(move[6]==AI_XO and move[2]==AI_XO and move[4]!=userXO):
- set(4,AI_XO)
- ### blocks across the top
- elif(move[0]==userXO and move[1]==userXO and move[2]!=userXO and move[2]!=AI_XO):
- set(2,AI_XO)
- elif(move[2]==userXO and move[1]==userXO and move[0]!=userXO and move[0]!=AI_XO):
- set(0,AI_XO)
- elif(move[0]==userXO and move[2]==userXO and move[1]!=userXO and move[1]!=AI_XO):
- set(1,AI_XO)
- ### blocks across the middle
- elif(move[3]==userXO and move[4]==userXO and move[5]!=userXO and move[5]!=AI_XO):
- set(5,AI_XO)
- ###
- elif(move[5]==userXO and move[4]==userXO and move[3]!=userXO and move[3]!=AI_XO):
- set(3,AI_XO)
- elif(move[3]==userXO and move[5]==userXO and move[4]!=userXO and move[4]!=AI_XO):
- set(4,AI_XO)
- ### blocks across the bottom
- elif(move[6]==userXO and move[7]==userXO and move[8]!=userXO and move[8]!=AI_XO):
- set(8,AI_XO)
- elif(move[8]==userXO and move[7]==userXO and move[6]!=userXO and move[6]!=AI_XO):
- set(6,AI_XO)
- elif(move[6]==userXO and move[8]==userXO and move[7]!=userXO and move[7]!=AI_XO):
- set(7,AI_XO)
- ### blocks down the right
- elif(move[0]==userXO and move[3]==userXO and move[6]!=userXO and move[6]!=AI_XO):
- set(6,AI_XO)
- elif(move[6]==userXO and move[3]==userXO and move[0]!=userXO and move[0]!=AI_XO):
- set(0,AI_XO)
- elif(move[0]==userXO and move[6]==userXO and move[3]!=userXO and move[3]!=AI_XO):
- set(3,AI_XO)
- ### blocks down the middle
- elif(move[1]==userXO and move[4]==userXO and move[7]!=userXO and move[7]!=AI_XO):
- set(7,AI_XO)
- elif(move[7]==userXO and move[4]==userXO and move[1]!=userXO and move[1]!=AI_XO):
- set(1,AI_XO)
- elif(move[1]==userXO and move[7]==userXO and move[4]!=userXO and move[4]!=AI_XO):
- set(4,AI_XO)
- ### blocks down the left
- elif(move[2]==userXO and move[5]==userXO and move[8]!=userXO and move[8]!=AI_XO):
- set(8,AI_XO)
- elif(move[8]==userXO and move[5]==userXO and move[2]!=userXO and move[2]!=AI_XO):
- set(2,AI_XO)
- elif(move[8]==userXO and move[2]==userXO and move[5]!=userXO and move[5]!=AI_XO):
- set(5,AI_XO)
- ### blocks diagonal
- elif(move[0]==userXO and move[4]==userXO and move[8]!=userXO and move[8]!=AI_XO):
- set(8,AI_XO)
- elif(move[8]==userXO and move[4]==userXO and move[0]!=userXO and move[0]!=AI_XO):
- set(0,AI_XO)
- elif(move[8]==userXO and move[0]==userXO and move[4]!=userXO and move[4]!=AI_XO):
- set(4,AI_XO)
- ### blocks diagonal
- elif(move[2]==userXO and move[4]==userXO and move[6]!=userXO and move[6]!=AI_XO):
- set(6,AI_XO)
- elif(move[6]==userXO and move[4]==userXO and move[2]!=userXO and move[2]!=AI_XO):
- set(2,AI_XO)
- elif(move[6]==userXO and move[2]==userXO and move[4]!=userXO and move[4]!=AI_XO):
- set(4,AI_XO)
- ### generates random move for AI
- elif(move[ai]=="X" or move[ai]=="O"):
- while(move[ai]=="X" or move[ai]=="O"):
- ai=randrange(0,9)
- if(chechIfBoardIsFull()==1):
- break
- set(ai,AI_XO)
- else:
- set(ai,AI_XO)
- ## function that class user and AI move and look for a winner or for a tie
- def user_AI_move():
- board()
- resetBoard()
- for i in range(0,8):
- if(userXO=="X"):
- userMove()
- AI_Move()
- board()
- if(checkForWinner()==1):
- print("\'"+AI_XO+"\'"+"is the winner")
- print("Computer won!")
- board()
- break
- elif(checkForWinner()==-1):
- print("\'"+userXO+"\'"+"is the winner!")
- print("You won!")
- board()
- break
- elif(chechIfBoardIsFull()==1):
- print("DRAW!")
- break
- else:
- AI_Move()
- board()
- if(checkForWinner()==1):
- print("\'"+AI_XO+"\'"+"is the winner")
- print("Computer won!")
- board()
- break
- elif(checkForWinner()==-1):
- print("\'"+userXO+"\'"+"is the winner!")
- print("You won!")
- board()
- break
- elif(chechIfBoardIsFull()==1):
- print("DRAW!")
- break
- userMove()
- while True:
- try:
- user_AI_move()
- break
- except ValueError:
- print("Oops! Wrong input. Try again...")
- print("Board will be reset. Start over..." )
- """
- root.wait_window()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement