'''
Created on Jun 17, 2011

@author: chenowet
'''

import math, time
from zellegraphics import *

global turn, winner, squaresPlayed

# Initialize the window
win = GraphWin("Tic Tac Toe", 449, 449)
v1 = Line(Point(150,0), Point(150,449))
v1.draw(win)
v2 = Line(Point(300,0), Point(300,449))
v2.draw(win)
h1 = Line(Point(0,150), Point(449,150))
h1.draw(win)
h2 = Line(Point(0,300), Point(449,300))
h2.draw(win)


class Square:
    # This class represents all 9 squares.  
    # Uses the init routine below to define them,
    # and the draw (which also sets the value) and checkSquare methods.
    def __init__ (self, row, col):
        self.row = row
        self.col = col
        self.value = -1 #means not yet marked by a player
    
    # draw x or o - x is player 1, o is player 0.    
    def draw(self, player, win):
        if self.value == -1: #ok to play here
            okToPlay = True
            self.value = player
            if player == 0:
                mark = "O"
            else:
                mark = "X"
            markToDo = Text(Point(150*(self.row)+75, 150*(self.col)+75), mark)
            markToDo.setSize(35)
            markToDo.draw(win)
        else: # can't go here!
            okToPlay = False
        print("played x,y of ", self.row, self.col, okToPlay) #debug line    
        return okToPlay
    
    def checkSquare(self):
        return self.value
    
 
# Auxiliary functions

def boardCoord(pixelCoord):
    'Find out row or colum number that corresponds to this pixel coordinate'
    return pixelCoord // 150;

# Functions called from Main game loop

def nextPlay(turn, win):
    doneWithTurn = False
    while not doneWithTurn:
        p = win.getMouse()
        xSquare = boardCoord(p.getX())
        ySquare = boardCoord(p.getY())
        print ("playing x,y of ", xSquare, ySquare) # test line
        doneWithTurn = squareArray[xSquare][ySquare].draw(turn, win)
    
def checkWinner(turn):
    wonIt = -1
    for y in range(3): # check rows
        if squareArray[0][y].checkSquare()==turn and squareArray[1][y].checkSquare()==turn and squareArray[2][y].checkSquare()==turn:
            wonIt = turn
    for x in range(3): # check columns
        if squareArray[x][0].checkSquare()==turn and squareArray[x][1].checkSquare()==turn and squareArray[x][2].checkSquare()==turn:
            wonIt = turn
    # check diagonals
    if squareArray[0][0].checkSquare()==turn and squareArray[1][1].checkSquare()==turn and squareArray[2][2].checkSquare()==turn:
        wonIt = turn
    if squareArray[0][2].checkSquare()==turn and squareArray[1][1].checkSquare()==turn and squareArray[2][0].checkSquare()==turn:
        wonIt = turn
    return wonIt


# Initialize the square "objects" for the game
s00 = Square(0,0)
s01 = Square(0,1)
s02 = Square(0,2)
s10 = Square(1,0)
s11 = Square(1,1)
s12 = Square(1,2)
s20 = Square(2,0)
s21 = Square(2,1)
s22 = Square(2,2)
squareArray = [[s00, s01, s02], [s10, s11, s12], [s20, s21, s22]]

# Initialize other values for the game
turn = 1 #start with X
winner = -1 # 0 = "O" wins, 1 = "X" wins
#winner = 0 # for testing
squaresPlayed = 0

# Main game loop - play till a winner emerges or done
while winner == -1 and (squaresPlayed < 9):
    nextPlay(turn, win)
    winner = checkWinner(turn)
    squaresPlayed = squaresPlayed + 1
    print("squares played", squaresPlayed, " and winner is ", winner)
    if turn == 1:
        turn = 0
    else:
        turn = 1

# End game

if winner == 0:
    tWinner = "And O Wins!"
elif winner == 1:
    tWinner = "And x Wins!"
else:
    tWinner = "Cat's Game!"

tWinnerGr = Text(Point(225, 225), tWinner)
tWinnerGr.setSize(25)
tWinnerGr.setTextColor('red')
tWinnerGr.draw(win)

win.getMouse()
win.close()


if __name__ == '__main__':
    pass