Explaining the 1D array approach for the game board
01:23
Board Setup
Setting up the game board with integers
Transcript
00:03
This is a simple implementation of the classic game of Snakes and Ladders.
00:07
Let's write our boilerplate code and as we do, I'll explain the approach to the programme.
00:11
Players take it in turns to roll a dice and move that many squares.
00:16
If they land on a ladder, they follow the ladder to a higher square.
00:19
If they land on a snake, they follow the snake to a lower square.
00:23
The first player to reach the final square or beyond wins the game.
00:28
The game is usually played on a 10x10 grid of 100 squares.
00:32
We'll write the programme so that's easily possible without any more code, but limit it to, a 5x5 grid of 25 squares for testing purposes.
00:42
In this programme, we learn that just because something looks like a 2D array with rows and columns doesn't mean it has to be.
00:50
If you think about it, Snakes and Ladders is really just a single track from the number one to the end.
00:56
It's not a table at all.
00:58
That's just how we're choosing to visualise it.
01:01
To a computer, these human visualisations are irrelevant, so we can abstract them out and just use a 1D array instead.
01:10
You could code the programme with a 2D array and in actual fact, it doesn't change the memory requirements because there are still the same number of squares, so it doesn't really matter.
01:20
The 1D approach is just easier.
01:23
Having declared a global random number generator, we'll start in the main programme by calling a subprogram to set up the board, which will be integers and 25 squares.
01:34
Each square will hold a number, which is the square it leads to.
01:38
If you land on it, the board is an array of integers and it becomes a new array of squares plus one.
01:46
That's because in this case, we want the squares to be numbered 1 to 25, or in the real game, 100, not 0 to 99, just so it matches the physical game.
01:59
By using a for loop, we can assign each square to lead to itself.
02:03
Board square equals square.
02:07
Now we'll set the Snakes and Ladders Land on board square four will set you to square seven.
02:13
Because it's a small ladder, the board square six will set you to square 15.
02:18
A larger ladder land on square 18 would put you on 23.
02:23
Our final ladder, square 19 would set you back to square two, a big snake and 24 will put you on 17, a second snake.
02:35
The board is now ready, so we'll return the board to the main programme.
02:40
Next, we'll initialise the players in a subprogram identified as initialised players.
02:45
Players will be an array of two integers.
02:48
This stores which square each player is currently on.
02:51
So we can also assign both elements or players to square one with one comma one.
02:57
Using an array instead of two variables makes it easier to add more players later.
03:04
Now that we have a board and players, we can write the game.
03:08
The play subprogram will take both the board and the players as parameters.
03:13
There's an argument here to say that both the board and players could have been global arrays, since they're used by most subprograms.
03:19
And I'd agree initialise the current player to be zero, which is actually player one.
03:26
The game has not been won.
03:29
And while the game has not been won, we need to take it in turns.
03:33
The player's square is wherever we were last time it was our turn and we're storing that in the player's array.
03:41
Output a blank line and some hyphens just to make the console more readable.
03:46
Output which player's turn it is.
03:48
Remembering that we need to add 1 to this so that 0 becomes 1 and 1 becomes 2.
03:55
Tell the player what square they're on and invite them to roll the dice.
04:00
Dice is then the outcome of rolling the dice a number between one and six.
04:05
Remember, that means the last argument must be a seven.
04:08
And in C, tell the player what they rolled.
04:13
Now move the player that many spaces so they land on their current square plus the value of the dice.
04:20
They may have gone past square 25 and if so, we need to bring them back to the last square on the board minus one here.
04:28
Because our array goes from 0 to 25, which is 26 squares.
04:32
In reality, we could have said if player square is greater than 25, assign it to 25.
04:39
But using variables enables us to increase the number of squares in future more easily.
04:45
Now we can tell the player which square they've landed on.
04:49
However, what square does this take you to?
04:51
A variable.
04:52
Baldsquare will read this from our board array.
04:56
We don't need to store this.
04:57
We could have referenced the board array directly in the next step.
05:00
But this will make the code a little bit more readable in a moment.
05:04
If the value of the board square is less than the player's current square, we we must have landed on a snake.
05:10
We can tell the player that, move them to where the board square takes us, and then tell them the bad news of where they've ended up.
05:19
If the value of board square is more than the player's current square, we must have landed on a ladder.
05:25
We can tell the player that move them to where the board square takes us and then tell them the good news of where they've ended up.
05:35
Finally, if we're now on the last square or beyond, then the game has been won.
05:40
We set the game won flag to be true and we can also tell the player they've won the game.
05:45
If the game hasn't been won, then we need to store the new position of the player back to the players array and tell the players that it's time for the next player's turn.
05:56
We'll wait for the user to press enter before continuing and the current player then becomes the current player plus one modulus the length of the player's array, in other words the number of players.
06:10
So this means if current player one is two, then it's player two's turn next.
06:16
If current player one is three, then it's player one's turn next.
06:21
Using modulo enables us to change the player without using an if statement, which you could do instead if you were happier with that.
06:29
This is just less code, maybe a little bit less processing too.