Wednesday, February 18, 2009

GUI Tic Tac Toe

Extra Credit:
Checkout chapter 10 in Python Programming for the Absolute Beginner.
Python Tic Tac Toe

# GUI Tic Tac Toe

from Tkinter import *
import sys

X = "X"
O = "O"
EMPTY = ""
TIE = "TIE"
NUM_SQUARES = 9

# YOUR TIC TAC TOE CODE GOES HERE


board = new_board()

class Application(Frame):
# GUI for the Main Window for Tic Tac Toe Game
def __init__(self,master): # Constructor Class Definition
Frame.__init__(self,master)
self.grid()
self.player1=X
self.player2=O
self.turn=X
self.CreateWidgets()

# Button Click Events
def BtnTopLeftClick(self):
legal = legal_moves(board)
move = 0

if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnTopLeft["text"] = "X"
else:
board[move] = self.player2
self.BtnTopLeft["text"] = "O"
self.turn = next_turn(self.turn)

if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)

def BtnTopMidClick(self):
legal = legal_moves(board)
move = 1

if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnTopMid["text"] = "X"
else:
board[move] = self.player2
self.BtnTopMid["text"] = "O"
self.turn = next_turn(self.turn)

if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)

def BtnTopRightClick(self):
legal = legal_moves(board)
move = 2

if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnTopRight["text"] = "X"
else:
board[move] = self.player2
self.BtnTopRight["text"] = "O"
self.turn = next_turn(self.turn)

if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)

def BtnMidLeftClick(self):
legal = legal_moves(board)
move = 3

if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnMidLeft["text"] = "X"
else:
board[move] = self.player2
self.BtnMidLeft["text"] = "O"
self.turn = next_turn(self.turn)

if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)

def BtnMidMidClick(self):
legal = legal_moves(board)
move = 4

if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnMidMid["text"] = "X"
else:
board[move] = self.player2
self.BtnMidMid["text"] = "O"
self.turn = next_turn(self.turn)

if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)

def BtnMidRightClick(self):
legal = legal_moves(board)
move = 5

if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnMidRight["text"] = "X"
else:
board[move] = self.player2
self.BtnMidRight["text"] = "O"
self.turn = next_turn(self.turn)

if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)

def BtnBotLeftClick(self):
legal = legal_moves(board)
move = 6

if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnBotLeft["text"] = "X"
else:
board[move] = self.player2
self.BtnBotLeft["text"] = "O"
self.turn = next_turn(self.turn)

if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)

def BtnBotMidClick(self):
legal = legal_moves(board)
move = 7

if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnBotMid["text"] = "X"
else:
board[move] = self.player2
self.BtnBotMid["text"] = "O"
self.turn = next_turn(self.turn)

if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)

def BtnBotRightClick(self):
legal = legal_moves(board)
move = 8

if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnBotRight["text"] = "X"
else:
board[move] = self.player2
self.BtnBotRight["text"] = "O"
self.turn = next_turn(self.turn)

if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)


def CreateWidgets(self):
# Tic Tac Toe Buttons
self.BtnTopLeft = Button(self)
self.BtnTopLeft["text"] = " "
self.BtnTopLeft.grid(row=0,column=0,columnspan=1,sticky=W)
self.BtnTopLeft["command"]=self.BtnTopLeftClick
self.BtnTopMid = Button(self)
self.BtnTopMid["text"] = " "
self.BtnTopMid.grid(row=0,column=1,columnspan=1,sticky=W)
self.BtnTopMid["command"]=self.BtnTopMidClick
self.BtnTopRight = Button(self)
self.BtnTopRight["text"] = " "
self.BtnTopRight.grid(row=0,column=2,columnspan=1,sticky=W)
self.BtnTopRight["command"]=self.BtnTopRightClick

self.BtnMidLeft = Button(self)
self.BtnMidLeft["text"] = " "
self.BtnMidLeft.grid(row=1,column=0,columnspan=1,sticky=W)
self.BtnMidLeft["command"]=self.BtnMidLeftClick
self.BtnMidMid = Button(self)
self.BtnMidMid["text"] = " "
self.BtnMidMid.grid(row=1,column=1,columnspan=1,sticky=W)
self.BtnMidMid["command"]=self.BtnMidMidClick
self.BtnMidRight = Button(self)
self.BtnMidRight["text"] = " "
self.BtnMidRight.grid(row=1,column=2,columnspan=1,sticky=W)
self.BtnMidRight["command"]=self.BtnMidRightClick

self.BtnBotLeft = Button(self)
self.BtnBotLeft["text"] = " "
self.BtnBotLeft.grid(row=2,column=0,columnspan=1,sticky=W)
self.BtnBotLeft["command"]=self.BtnBotLeftClick
self.BtnBotMid = Button(self)
self.BtnBotMid["text"] = " "
self.BtnBotMid.grid(row=2,column=1,columnspan=1,sticky=W)
self.BtnBotMid["command"]=self.BtnBotMidClick
self.BtnBotRight = Button(self)
self.BtnBotRight["text"] = " "
self.BtnBotRight.grid(row=2,column=2,columnspan=1,sticky=W)
self.BtnBotRight["command"]=self.BtnBotRightClick

# main
def main():
player1Name=""
player2Name=""
player1Score=0
player2Score=0
namesScores=[]

player1Name,player2Name,player1Score,player2Score,namesScores=readFileNamesScores(player1Name,player2Name,player1Score,player2Score)

root= Tk()
root.title("Tic Tac Toe")
root.geometry("200x100")
app = Application(root)
root.mainloop()

main()