• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

[SOLVED] Organization: naming temporary variables

Bentley

Member
I think I am on attempt 9 making a Chess game and I have realized a pattern: big switch statements = overwhelming project. That means I need a better approach. I thought that making a directions array for every piece would help. However, there are nestled arrays within nestled arrays. I do not have a good nomenclature (fancy word) for pulling these out.

With that said, I was wondering how others would organize this code:
Only the rook is done (the others are there so that I can get the correct array index)
Directions array
Code:
directions =
[
    // Pawn
    [[0, 0]],
 
    // Knight
    [[0, 0]],
 
    // Bishop
    [[0, 0]],
 
    // Rook
    [[ 1,  0 ],
     [-1,  0 ],
     [ 0,  1 ],
     [ 0, -1 ]],
];
Script
Code:
// Loop through the board
for (var i = 0; i < 8; i++)
{
    for (var j = 0; j < 8; j++)
    {
        // Get the array stored at this grid position
        array = board[# i, j];
        color = array[COLOR];
        piece = array[PIECE];
   
        // Get the array index for the directions array
        // "piece" matches up with array index of piece we are looking at
        // For ex: rook = 3 and the directions[3] is for the rook
        index = piece;
   
        // Get the inner array
        _directions = directions[index];
   
        // Loop through the inner array
        for (var k = 0; k < array_length_1d(_directions); k++)
        {
            // Get the inner-inner array
            var help = _directions[k];
       
            // Get the x direction
            dx = help[0];
       
            // Get the y direction
            dy = help[1];
       
            while (on_board(i + dx, j + dy))
            {
                // Get the array stored at this position to check for pieces in the way
                _array = board[# i + dx, j + dy];
                _color = _array[COLOR];
                _piece = _array[PIECE];
           
                // Check stuff...
           
                // Increment
                dx += sign(dx);
                dy += sign(dy);
            }
        }
    }
}
I rushed this post because I am just on the way out. Please let me know if did not explain things well enough.

Any advice is more than welcomed and thanks for reading.
 
Last edited:
Top