Welcome to the 9th week. It has been hard ride making it far but together we've made it. I am grateful that at last this series is finally rounding up.
The algorithm to create the entire game starts with what actions must be
Welcome to the 9th week. It has been hard ride making it far but together we've made it. I am grateful that at last this series is finally rounding up.
The algorithm to create the entire game starts with what actions must be carried out when a pot is clicked by the user and it is given below:
- Check if the pot is empty or has seeds inside it
- If the pot has seeds inside it, check the number of seeds it has
- Pick the seeds from the pot the user selected
- Distribute the seeds picked from the pot to its neighbouring pots in ascending order one pot at a time until all the seeds are finished. Cycle back from pots 12 to 1. Drop a seed into each pot.
- If the last seed is placed on a pot with no seed, stop the seed distribution.
- Else if the last seed is placed in a pot with 3 seeds in it, capture the seeds, assign it to the player's score and end the distribution.
- Else pick the seeds from the pot and repeat step 4.
- Assign seeds that reach up to 4 during distribution to the player who owns the pot the belong to and capture them.
In our last post, we covered steps 1 to 4. Now we will do the remaining steps and complete our game.
In order to complete our game, I needed to add 4 extra variables. They are:
- playerID which is used to identify whose turn it is to play
- distributionInProgress which prevents the user from clicking a pot while seeds are being distributed
- playerOneScore which is the score for the first player
- playerTwoScore which is a score for the second player
The variables are thus shown below:
It is important to remember that we are making a two player game so these variables must be included in our game.
When our game loads up, we want it to display instructions that the player 1 should start the game. This is also the reason why our playerID variable is initialized to 1. To this end we will create the code for that part of the program first. The code for when our game starts is shown below:
Next we need to ensure that our pots cannot be clicked while the seed distribution is ongoing. To do this, create a new variable called distributionInProgress and set its value to false. Next we check if this value is whenever a pot is touched by any of the users. Our code for all our pots now becomes as shown below:
So as long as the distribution of seeds is in progress, the players cannot select a seed. They have to wait for the seed distribution to be completed.
When a distribution of seeds is not ongoing, you can then assign the potID to the setPotID procedure. Your setPotID procedure needs to notify the user when they select a pot with no seeds. But that’s not all. Because we are making a two player game, we need to know at each point of the game who’s turn it is to play.
To do this, we create a new function called getPlayerID which will allow us know the ID for the player whose turn it is.
As a result of this, we have to change our setPotID procedure from what we had last week. Our new procedure is shown below:
The new additions are the if block to check who’s turn it is to play and the getPlayerID function.
We do this to ensure that the user can only select from their own pots. In our game pots 1 to 6 are assigned to the first player while pots 7 to 12 are assigned to the second player.
Our getPlayerID function is simple enough. It uses the potID assigned from the pot selected by the user to get whose turn it is. When our potID is less than 7, then return a playerID of 1. Otherwise, return a playerID of 2.
The code to do this is shown below:
Once we have the playerID, we can pick the seeds in the pot and start the seed distribution. In the last post, we had solved this problem but we didn’t continue the distribution once it ended.
We made a slight change to our distributeSeeds function. In this function we now set the distributionInProgress variable to true.
To make the distribution continue, we must modify our Clock1 code. The first part of the code will now test for when the distribution lands in a pot with no seeds. This is the stopping condition for the loop.
The code to do this is shown below:
The moment the seed distribution lands on a pot with no seeds, we have to stop the seed distribution. Stopping our distribution means that we disable Clock1, set the distributionInProgress variable to false and notify the user that the distribution has ended.
Our first condition after this is to check which player has just played. Next we also need to check if the number of seeds in the opponent’s pots is equal to zero. If that is the case, we simple skip the turn of the opponent and allow our user play again.
The functions sumPlayerTwoSeeds and sumPlayerOneSeeds checks if the number of see