# tic-tac-toe.py -- for lab11
# 15110, Summer 2015

import tkinter
from tkinter import Canvas

def play():
    # draw the inital (empty) game grid
    my_window = tkinter.Tk()
    my_canvas = Canvas(my_window, width = 120, height = 120)
    my_canvas.pack()

    grid = new_grid()
    display_grid(my_canvas, 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("Quiters never win.")
                return

            col = input_num("Player " + str(player) + ": Which column (0-2)? ")
            if col == "quit":
                print("Quiters 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(my_canvas, grid)

        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

# x0, y0 are the coordinates of the top-left corner of the x.
# x1, y1 are the coordinates of the bottom-right corner of the x.
def draw_x(my_canvas, x0, y0, x1, y1):
    my_canvas.create_line(x0, y0, x1, y1, width = 3)
    my_canvas.create_line(x1, y0, x0, y1, width = 3)

def test_display():
    # draw the inital (empty) game grid
    my_window = tkinter.Tk()
    my_canvas = Canvas(my_window, width = 120, height = 120)
    my_canvas.pack()
    grid = [[1, None, 0], [None, 0, None], [None, None, None]]
    display_grid(my_canvas, grid)

# This is a stub definition of display_grid.
# You should replace its body with actual code.
def display_grid(my_canvas, grid):
    return None

def check_win_horiz(grid, row):
    if grid[row][0] == None:
        return False
    for i in range(1,3):
        if grid[row][0] != grid[row][i]:
            return False
    return True

#complete this function
def check_win_vert(grid, column):
    return False

def check_win_diagonal1(grid):
    if grid[0][0] == None:
        return False
    for i in range(1, 3):
        if grid[0][0] != grid[i][i]:
            return False
    return True


#complete this function
def check_win_diagonal2(grid):
    return False        

def check_win(grid):
    for i in range(3):
        if check_win_horiz(grid, i):
            return True
        if check_win_vert(grid, i):
            return True
    return check_win_diagonal1(grid) or check_win_diagonal2(grid)

