import system
import random
import time
import rgb
import buttons
import defines

def input_a(pressed):
    global player
    global board
    global winner
    global game_over
    global col
    global row
    global exit

    if pressed:
        if game_over or exit>0:
            exit = 2
        elif winner == 0:
            board[col][row] = player
            if player_wins():
                winner = player
                scoreboard[winner] += 1
                if scoreboard[winner] == 6:
                    game_over = True
            else:
                player = (player % 2) + 1
                if board[3][5] == 0:
                    col = 3
                    for row in range(6):
                        if board[col][row] == 0:
                            return
                else:
                    col = -1
                    for i in range(1,4):
                        if board[3-i][5] == 0:
                            col = 3 - i
                            break
                        if board[3+i][5] == 0:
                            col = 3 + i
                            break
                    if col == -1:
                        winner = 3
                        scoreboard[0] += 1
                        if scoreboard[0] == 6:
                            game_over = True
                        return

                    for row in range(6):
                        if board[col][row] == 0:
                            return

        else:
            if winner == 1 or winner == 2:
                pixels[18 + 2 * winner + 32*(7-scoreboard[winner])] \
                    = 0xff << ((winner+1)*8)
            else:
                pixels[21 + 32*(7-scoreboard[0])] = 0xffff0000
            init_game()
            

def input_b(pressed):
    global exit
    global game_over

    if pressed:
        if game_over:
            exit = 2
        elif exit>0:
            exit = 0
            for y in range(8):
                pixels[31 + 32*y] = 0x00000000
        else:
            exit = 1

def input_left(pressed):
    global col
    global row
    if pressed:
        x = col
        while x > 0:
            x = x - 1
            for y in range(6):
                if board[x][y] == 0:
                    col = x
                    row = y
                    return

def input_right(pressed):
    global col
    global row
    if pressed:
        x = col
        while x < 6:
            x = x + 1
            for y in range(6):
                if board[x][y] == 0:
                    col = x
                    row = y
                    return

buttons.register(defines.BTN_LEFT, input_left)
buttons.register(defines.BTN_RIGHT, input_right)
buttons.register(defines.BTN_A, input_a)
buttons.register(defines.BTN_B, input_b)

def init_game():
    global board
    global startplayer
    global player
    global winner
    global winning_series
    global col
    global row

    winner = 0
    winning_series = []
    startplayer = (startplayer % 2) + 1
    player = startplayer
    col = 3
    row = 0

    board = [[0 for y in range(6)] for x in range(7)]
    draw_board()

def init_pixels():
    global pixels

    pixels = [0x00000000 for i in range(8*32)]

    for y in range(8):
        pixels[ 8 + 32*y] = 0x80808000
        pixels[16 + 32*y] = 0x80808000
        pixels[19 + 32*y] = 0x80808000
        pixels[23 + 32*y] = 0x80808000

    for x in range(7):
        pixels[9+x       ] = 0x80808000
        pixels[9+x + 32*7] = 0x80808000

    for x in range(3):
        pixels[20+x       ] = 0x80808000
        pixels[20+x + 32*7] = 0x80808000

def draw_board():
    global pixels

    for x in range(7):
        for y in range(6):
            if board[x][y] != 0:
                pixels[9+x + 32*(6-y)] = 0xff << ((board[x][y]+1)*8)
            else:
                pixels[9+x + 32*(6-y)] = 0x00000000

def player_wins():
    global board
    global winning_series
    global col
    global row
    global player

    # check vertical
    streak = 1
    x = col
    y = row
    winning_series = [(x,y)]
    while y > 0:
        y -= 1
        if board[x][y] == player:
            streak += 1
            winning_series.append((x,y))
        else:
            break
    if streak >= 4:
        return True

    # check horizontal
    streak = 1
    x = col
    y = row
    winning_series = [(x,y)]
    while x > 0:
        x -= 1
        if board[x][y] == player:
            streak += 1
            winning_series.append((x,y))
        else:
            break
    x = col
    y = row
    while x < 6:
        x += 1
        if board[x][y] == player:
            streak += 1
            winning_series.append((x,y))
        else:
            break
    if streak >= 4:
        return True

    # check diagonal1
    streak = 1
    x = col
    y = row
    winning_series = [(x,y)]
    while x > 0 and y > 0:
        x -= 1
        y -= 1
        if board[x][y] == player:
            streak += 1
            winning_series.append((x,y))
        else:
            break
    x = col
    y = row
    while x < 6 and y < 5:
        x += 1
        y += 1
        if board[x][y] == player:
            streak += 1
            winning_series.append((x,y))
        else:
            break
    if streak >= 4:
        return True

    # check diagonal2
    streak = 1
    x = col
    y = row
    winning_series = [(x,y)]
    while x > 0 and y < 5:
        x -= 1
        y += 1
        if board[x][y] == player:
            streak += 1
            winning_series.append((x,y))
        else:
            break
    x = col
    y = row
    while x < 6 and y > 0:
        x += 1
        y -= 1
        if board[x][y] == player:
            streak += 1
            winning_series.append((x,y))
        else:
            break
    if streak >= 4:
        return True

    return False
# end player_wins()

rgb.framerate(30)
rgb.disablecomp()

flashing = [8*i for i in range(32)]
flashing.extend([8*(31-i) for i in range(32)])

random.seed(time.ticks_ms())
startplayer = random.randint(1,2)
player = 0
winner = 0
game_over = False
scoreboard = [0,0,0]
col = 3
row = 0
init_pixels()
init_game()

it = 0
exit = 0
while exit < 2:
    draw_board()
    color = flashing[(it % 64)]<<((player+1)*8)
    if exit>0:
        for y in range(4):
            pixels[31 + 32*y]     = flashing[(it % 64)]<<16
            pixels[31 + 32*(4+y)] = flashing[(it % 64)]<<24
    elif game_over:
        for i in range(6):
            if winner == 3:
                pixels[21 + 32*(6-i)] = flashing[(it%64)] * 0x01010000
            else:
                pixels[18 + 2*winner + 32*(6-i)] = color
    else:
        if winner == 0:
            pixels[9+col + 32*(6-row)] = color
        elif winner == 1 or winner == 2:
            for x,y in winning_series:
                pixels[9+x + 32*(6-y)] = color
            pixels[18 + 2 * winner + 32*(7-scoreboard[winner])] = color
        else:
            pixels[21 + 32*(7-scoreboard[0])] = flashing[(it%64)] * 0x01010000
    rgb.frame(pixels)
    it = it + 1
    time.sleep(0.003)
system.reboot()
