# tic-tac-toc.py -- for lab11
# 15110, Summer 2014

from PythonLabs.Canvas import *

def play():
    # draw the inital (empty) game grid
    Canvas.init(120, 120, "TicTacToe")

    grid = new_grid()
    display_grid(grid)
    player = 0

    for i in range(0, 9):
        # keep on asking for rows/columns until the user inputs a valid move
        # add a mark to the correct cell
        repeat = True
        while repeat:
            row = input_num("Player " + str(player) + ": Which row (0-2)? ")
            if row == "quit":
                print("Quitters never win.")
                return

            col = input_num("Player " + str(player) + ": Which column (0-2)? ")
            if col == "quit":
                print("Quitters never win.")
                return

            if add_mark(grid, row, col, player):
                repeat = False
            else:
                print("Grid position (row =", row, " column =", col,
                      ") is already occupied.")


        # redisplay grid with new mark
        display_grid(grid)
        Canvas.update() # push latest drawing to display

        if check_win(grid):
            print("Player ", player, "won!")
            return

        # alternate between player 0 and player 1
        player = (player + 1) % 2


    # If no-one wins in 9 moves, the game is a tie.
    print("The game ended in a tie.")


# input_num(prompt)
# This function gets user input and normally returns 0, 1, or 2 based on
# the input.  If, however, the user enters the word "quit", it returns
# "quit" which signals that play should end
def input_num(prompt):
    while True:
        s = input(prompt)
        if s == "quit": return 'quit'
        if s == "0": return 0
        if s == "1": return 1
        if s == "2": return 2
        print("input must be a number between 0 and 2 (inclusive) or quit")


def new_grid():
    return [[None] * 3, [None] * 3, [None] * 3]


def add_mark(grid, row, col, player):
    if grid[row][col] == None:
        grid[row][col] = player
        return True
    return False


# This is a 'stub' definition (i.e. placeholder) of display_grid.
# You should replace its body with the correct code.
#
def display_grid(grid):
    return None


def check_win_horiz(grid, row):
    if grid[row][0] == None:
        return False
    if grid[row][0] != grid[row][1]:
            return False
    if grid[row][0] != grid[row][2]:
            return False
    return True

def check_win_vert(grid, column):
    if grid[0][column] == None:
        return False
    if grid[0][column] != grid[1][column]:
        return False
    if grid[0][column] != grid[2][column]:
        return False
    return True


# This is a 'stub' definition (i.e. placeholder) of check_win_diagonal1
# You should replace its body with the correct code.
#
def check_win_diagonal1(grid):
    return False
        

# This is a 'stub' definition (i.e. placeholder) of check_win_diagonal2
# You should replace its body with the correct code.
#
def check_win_diagonal2(grid):
    return False
        
# After you get the game to run correctly, try to
# simplify this function.
#
def check_win(grid):
    if check_win_horiz(grid, 0):   # check row 0
        return True
    if check_win_horiz(grid, 1):   # check row 1
        return True
    if check_win_horiz(grid, 2):   # check row 2
        return True
    if check_win_vert(grid, 0):    # check column 0
        return True
    if check_win_vert(grid, 1):    # check column 1
        return True
    if check_win_vert(grid, 2):    # check column 2
        return True
    if check_win_diagonal1(grid):
        return True
    if check_win_diagonal2(grid):
        return True
    return False

