@name chess_test_new
@inputs 
@outputs 
@persist Pieces:table
@trigger 

#- Board creation
Pieces = table(
    "p" = "Pawn",
    "r" = "Rook",
    "n" = "Knight",
    "b" = "Bishop",
    "q" = "Queen",
    "k" = "King",
    "x" = "Empty"
)

function table loadChessBoard( Setup:string ) {
    local Table = table()
    for (N = 1, 64) { 
        local X = floor((N - 1) / 8)
        local Y = (N - 1) % 8
        
        local S = Setup[N]
        local P = Pieces[S:lower(), string]

        Table[N, table] = table(
            "itemp"   = P,
            "itemc"   = S != "x" ? ( inrange(toByte(S), 96, 122) ? "Black" : "White" ) : "",
            "squarec" = (X + Y) % 2 == 0 ? "White" : "Black"
        )
    }
    return Table
}

function table newChessBoard() {
    return loadChessBoard("rnbqkbnrppppppppxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxPPPPPPPPRNBQKBNR")  
}

function string encodeBoard( Board:table ) {
    return vonEncode( Board )  
}

function table decodeBoard( String:string ) {
    return vonDecodeTable( String )
}


#- Square info
function table getSquare( Board:table, Index ) {
    return Board[Index, table]
}

function string table:getSquareColor( ) {
    return This["squarec", string]
}

function string table:getSquarePiece( ) {
    return This["itemp", string]
}

function string table:getSquarePieceColor( ) {
    return This["itemc", string]
}





local TestB = newChessBoard()
local Square = getSquare(TestB, 16)

print( Square:getSquareColor() )
print( Square:getSquarePiece() )
print( Square:getSquarePieceColor() )




