@name c43
@inputs 
@outputs 
@persist Col Row Board:array
@trigger none

Row = 6
Col = 7
Board = array()

for (I = 1, Row*Col) {
    Board[I, number] = 0
}


function array:print() {
    print("\n-----------------------------")
    for (R = 0, Row - 1) {
        local S = ""
        for (C = 0, Col - 1) {
            S = S + format("| %s ", This[Col*R + C + 1, number])
        }
        print(S + "|\n-----------------------------")
    }
    print("  1   2   3   4   5   6   7\n")
}

function array:setP(R, C, V) {
    This[Col*R + C + 1, number] = V
}


Board:setP(5, 0, 1)
Board:setP(4, 0, 1)
Board:setP(3, 0, 1)
Board:setP(2, 0, 1)

#Board:print()

function number array:check4(A, B, C, D) {
    return ((This[A, number] + This[B, number] + This[C, number] + This[D, number]) == This[A, number]*4) & This[A, number] != 0
}

function number array:horizontal() {
    for (R = 0, Row - 1) {
        for (C = 0, Col - 1 - 3) {
            local N = Col*R + C + 1
            if (This:check4(N, N + 1, N + 2, N+3)) {
                return 1
            }
        }
    }  
    return 0
}

function number array:diagonal() {
    local DR = 6
    local DL = 8
    local CN = 0

    for (R = 0, Row - 1 - 3) {
        for (C = 0, Col - 1) {
            local N = Col*R + C + 1
            if ((CN <= 3 & This:check4(N, N + DL, N + DL*2, N + DL*3)) | CN >= 3 & This:check4(N, N + DR, N + DR*2, N + DR*3)) {
                return 1
            }
            CN++
        }
        CN = 0
    }
    return 0
}


function number array:vertical() {
    for (R = 0, Row - 1 - 3) {
        for (C = 0, Col - 1) {
            local N = Col*R + C + 1
            if (This:check4(N, N + 7, N + 14, N + 21)) {
                return 1
            }
        }
    }  
    return 0
}

#print( Board:diagonal() )

Board:print()
print(Board:vertical())

#[int takeTurn(char *board, int player, const char *PIECES){
   int row, col = 0;
   printf("Player %d (%c):\nEnter number coordinate: ", player + 1, PIECES[player]);

   while(1){ 
      if(1 != scanf("%d", &col) || col < 1 || col > 7 ){
         while(getchar() != '\n');
         puts("Number out of bounds! Try again.");
      } else { 
         break;
      }
   }
   col--;

   for(row = BOARD_ROWS - 1; row >= 0; row--){
      if(board[BOARD_COLS * row + col] == ' '){
         board[BOARD_COLS * row + col] = PIECES[player];
         return 1;
      }
   }
   return 0;

}]#
