• 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] Chess game: garbarge collection regarding filling grid with arrays

Bentley

Member
Hello, I'm working on a Chess game (no AI, I'm sooo far from that). Atm, I'm just setting up the board. I highly recommend @matharoo 's tutorial here https://gdpalace.wordpress.com/

Every grid position holds an array. I was wondering about the order the board should be initialized:
1. Set every grid position to an array, and then set every grid position where a piece should be to an array.
2. Set every grid position where a piece should be to an array, and then set every other grid position to an array.

The difference being #1 creates (I think) 32 extra arrays. I know GM does garbage collection, so is this something I should be thinking about?

Here's the two different ways:
1.
Code:
///// Setup board /////

// Empty squares
var array;
array[COLOR] = NONE;
array[PIECE] = NONE;
ds_grid_clear(board, array_clone(array));

// Pieces
for (var i = 0; i < 8; i++)
{
    board[# i, 1] = [BLACK, PAWN];
    board[# i, 6] = [WHITE, PAWN];
}
2.
Code:
///// Setup board /////

// Pieces
for (var i = 0; i < 8; i++)
{
    board[# i, 1] = [BLACK, PAWN];
    board[# i, 6] = [WHITE, PAWN];
}

// Empty squares
var array;
array[COLOR] = NONE;
array[PIECE] = NONE;
for (var i = 0; i < 8; i++)
{
    for (var j = 0; j < 8; j++)
    {
        if (!is_array(board[# i, j]))
        {
            board[# i, j] = array_clone(array);
        }
    }
}
Thanks for reading.
 

FrostyCat

Redemption Seeker
When a piece of code constitutes a one-time setup, that's the #1 tell-tale sign that speed optimizations for it have little to no lasting significance. Your player would spend more time thinking over a single move than the time you save taking the faster alternative over the slower one.

If you have no reason to use a grid specifically, I would recommend going as far as implementing the board as a 2D array as well. Then even that can be thrown to the garbage collector when you're done.
 

Bentley

Member
When a piece of code constitutes a one-time setup, that's the #1 tell-tale sign that speed optimizations for it have little to no lasting significance. Your player would spend more time thinking over a single move than the time you save taking the faster alternative over the slower one.

If you have no reason to use a grid specifically, I would recommend going as far as implementing the board as a 2D array as well. Then even that can be thrown to the garbage collector when you're done.
Thanks for the reply. I won't worry about those arrays then. I'll fill the board with arrays for empty pieces and then fill in the board with arrays for pieces, as that order feels better.

What are some of the advantages, other than it being garbage collected, for using an array over a grid?
 

Bentley

Member
@FrostyCat Ah I see what you did there. A little mean lol, but that's ok. Just know I was coming from a place of "is this good form" over "is this faster". The latter didn't even cross my mind. That's probably my fault because I didn't articulate my question well. But, the way you treat people is your prerogative.
 

FrostyCat

Redemption Seeker
@FrostyCat Ah I see what you did there. A little mean lol, but that's ok. Just know I was coming from a place of "is this good form" over "is this faster". The latter didn't even cross my mind. That's probably my fault because I didn't articulate my question well. But, the way you treat people is your prerogative.
At no point did I intend any malice with my statement. I was simply stating the ability to dispose of the state without manual freeing calls, when you take on a fully array-based state model instead of a DS-based state model (until YoYo promotes data structures to true types).

Being able to throw the entire board state at the garbage collector has practical advantages, especially when implementing AI. You won't notice the difference with just one copy of the state, but with thousands of copies you'd regret choosing any option involving manual cleanup. It will happen in any worthwhile AI mechanism. I've lost the link to it, but someone did attempt a chess AI in GM8 by modeling the state and search tree with instances. The resulting performance from having to wipe stuff up at GML speed got him rather disappointed at the result.

If you think that's frosty being mean you're in for a shock at some point later.
If what you mean by "shock" is that I'm not always frosty, then yes, then yes, you'd be in for a surprise.
 

Yal

šŸ§ *penguin noises*
GMC Elder
You should optimize code to be conceptually easy, that makes it faster to write and debug... and protip, it's also easier to not make mistakes in the first place if you always know what you're doing. Clever hacks tend to cause just as many problems as they solve.

I agree with Frosty on that thing about "as long as a human input is necessary, you have infinite time". Only optimize things once the game starts FEELING slow (Knuth's famous rule). And I also agree that arrays are better than grids, there's nothing grids have that arrays doesn't. (Stacks, lists and queues all have some unique perks, though).
 

Bentley

Member
At no point did I intend any malice with my statement. I was simply stating the ability to dispose of the state without manual freeing calls, when you take on a fully array-based state model instead of a DS-based state model (until YoYo promotes data structures to true types).

Being able to throw the entire board state at the garbage collector has practical advantages, especially when implementing AI. You won't notice the difference with just one copy of the state, but with thousands of copies you'd regret choosing any option involving manual cleanup. It will happen in any worthwhile AI mechanism. I've lost the link to it, but someone did attempt a chess AI in GM8 by modeling the state and search tree with instances. The resulting performance from having to wipe stuff up at GML speed got him rather disappointed at the result.


If what you mean by "shock" is that I'm not always frosty, then yes, then yes, you'd be in for a surprise.
Oh, ok. I thought you were being sarcastic and making fun of me or something. I was being paranoid.

Thanks for the advice about array use over data structures. I will surely keep that in mind as, if I get to AI, I would think there will be a lot of temporary boards that the AI will use to come up with a move. And, as you said, deleting board after board might make the code hard to follow and eventually overwhelming.

I've never even programmed a working Chess game, so I'm getting way ahead of myself talking about the AI, but it's good to get good advice : )

@Yal You made some really good points, especially about keeping the code conceptually easy and how workarounds can cause more harm than good. I should write this on a post-it on my computer: readability > efficiency. Anyway, enough of my rambling. Thanks for the reply.
 
Last edited:
Top