
import os

# creates a matrix of * characters
# to represent the game
matrix = []
playedMoves = []
for rows in range(6):

    matrix.append([])

    for columns in range(6):
        matrix[rows].append("*")


def printMatrix(matrix):
    for rows in range(7):
        for columns in range(6):
            if(rows == 0):  # prints characters for Columns
                print columns, "",
            else:
                print matrix[rows - 1][columns] + " ",
        print " ",
        print" "

# places piece into game matrix


def playPiece(matrix, piece, column):

    for i in range(6):
        if(matrix[5 - i][column] == '*'):
            playedMoves.append([(5 - i), column])
            matrix[5 - i][column] = piece
            break

# return matrix


def checkWin(matrix, playedMoves):

    lastMove = len(playedMoves) - 1
    row = playedMoves[lastMove][0]
    column = playedMoves[lastMove][1]
    piece = matrix[row][column]
    count = 0
    win = False

    # checks down
    for i in range(4):
        if row + i < 6:  # checks in bounds
            if matrix[row + i][column] != piece:
                count = 0  # if empty or opp. piece, count is reset.
            else:
                count += 1
        else:
            count = 0  # out of bounds count reset
            break

    if count == 4:
        print "you have won vertically"
        win = True

    count = 0  # resets count for next check

    # checks horizantally
    for i in range(7):
        # checks that the column is in bounds
        if (column - 3 + i) >= 0 and (column - 3 + i) < 6:
                # if you past the middle and you hit a wrong piece
                # you can stop checking you cant win
            if i > 3:
                if matrix[row][column - 3 + i] != piece:
                    break
                else:
                    count += 1
            elif matrix[row][column - 3 + i] != piece:
                count = 0
            else:
                count += 1
        if count == 4:
            break

    if count == 4:
        print "you have won horizantally"
        win = True

    count = 0  # resets count for next check

    # check up and right
    for i in range(-3, 4):
        # counts from - 3 to 3 by 1
        if ((column + i >= 0 and row - i >= 0) and
                (column + i < 6 and row - i < 6)):

            if matrix[row - i][column + i] != piece:
                count = 0
            else:
                count += 1
        if count == 4:
            break
    if count == 4:
        print "you have won diagonally"
        win = True

    count = 0  # resets count for next check
    # check up and teft
    for i in range(3, -4, -1):  # counts from 3 to -3 by -1
        if ((column + i >= 0 and row + i >= 0) and
                (column + i < 6 and row + i < 6)):
            if matrix[row + i][column + i] != piece:
                count = 0
            else:
                count += 1
        if count == 4:
            break
    if count == 4:
        print "you have won diagonally"
        win = True

    return win


def runGame(matrix):

    moves = 1
    won = False

    # print("Welcome to connect four!\n")
    # print("please select an option:")
    # print("\t1. Start New Game")
    # print("\t2. Quit")

    while(not won):

        column = raw_input("player " + str(moves) + " make a move: ")

        column = int(column)
        if(moves % 2 == 1):
            playPiece(matrix, 'x', column)
            moves = moves + 1

        else:
            playPiece(matrix, 'O', column)
            moves = moves + 1

        os.system('cls' if os.name == 'nt' else 'clear')
        printMatrix(matrix)
        won = checkWin(matrix, playedMoves)



os.system('cls' if os.name == 'nt' else 'clear')
printMatrix(matrix)
runGame(matrix)
