@name c46
@inputs 
@outputs 
@persist Game:array
@trigger none

MAX_COL = 7
MAX_ROW = 6

for (I = 1, MAX_COL*MAX_ROW) { Game:pushNumber(0) }

function number array:get_move( Col ) {
    local ROW = MAX_ROW - 1
    for (I = 0, ROW) {
        if (This[MAX_COL*I + Col + 1, number] > 0) {
            ROW = I - 1
            break
        }    
    }
    
    return ROW    
}

function number array:make_move( Col, Row, P ) {
    This[MAX_COL*Row + Col + 1, number] = P
}


WIN   =  1
LOSE  = -1
UNC   =  0
DEPTH =  3

function number array:maketurn() {
    
}

#[function number minimax(GameState:array, GameTurn, Depth) {
    local Best  = 0
    local Move  = -1
    local Value = 0
    
    if (GameTurn % 2 == 0) {
        Value = 1
        Best  = -1000
        Move  = -1
    }
    else {
        Value = 2
        Best  = 1000
        Move  = -1    
    }
    
    if (Depth == MAX_DEPTH) {
        return 1
    }
    
    local Val = !GameTurn + 1
    
    for (I = 0, MAX_COL - 1) {
        local Row = GameState:get_move( I )
        if (Row > 0) {
            GameState:make_move(I, Row, Val)
        }  
    }
}]#


#[           row   = makeMove(b, col, piece)
            turn += 1  # now the other player's turn
            score = minMax(b, turn, depth+1)

            if player == 'max':
                if score > best:
                    best, move = score, col
            else:
                if score < best:
                    best, move = score, col

            reset(b, row, col)]#
            
            
#minimax(Game, 0, 0)




    function void array:c4_print() {
        local Gray = vec(125, 125, 125)
        local Piece = ".OX"
    
        printColor(Gray, "\n-----------------------------")
        for (Row = 0, 5) {
            local String = ""
            local Print  = array()
    
            for (Col = 0, 6) {
                Print[Col + 1, number] = This[7*Row + Col + 1, number]
            }
            
            printColor( 
                Gray, "| ",  hsv2rgb(Print[1, number]*120, 0.75, 0.85), Piece[Print[1, number] + 1],
                Gray, " | ", hsv2rgb(Print[2, number]*120, 0.75, 0.85), Piece[Print[2, number] + 1],
                Gray, " | ", hsv2rgb(Print[3, number]*120, 0.75, 0.85), Piece[Print[3, number] + 1],
                Gray, " | ", hsv2rgb(Print[4, number]*120, 0.75, 0.85), Piece[Print[4, number] + 1],
                Gray, " | ", hsv2rgb(Print[5, number]*120, 0.75, 0.85), Piece[Print[5, number] + 1],
                Gray, " | ", hsv2rgb(Print[6, number]*120, 0.75, 0.85), Piece[Print[6, number] + 1],
                Gray, " | ", hsv2rgb(Print[7, number]*120, 0.75, 0.85), Piece[Print[7, number] + 1], 
                Gray, " | \n-----------------------------"
            )
        }
        printColor(Gray, "  0   1   2   3   4   5   6\n")
    }

Game:c4_print()
