Monday 25 July 2016

Android Development for Everyday People - Game Development II

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:
  1. Check if the pot is empty or has seeds inside it
  2. If the pot has seeds inside it, check the number of seeds it has
  3. Pick the seeds from the pot the user selected
  4. 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.
  5. If the last seed is placed on a pot with no seed, stop the seed distribution.
  6. 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.
  7. Else pick the seeds from the pot and repeat step 4.
  8. 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:
  1. playerID which is used to identify whose turn it is to play
  2. distributionInProgress which prevents the user from clicking a pot while seeds are being distributed
  3. playerOneScore which is the score for the first player
  4. 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 seeds in the respective pots of the second player and the first player are 0.

So for sumPlayerOneSeeds we find the total of the contents of the pot list from index 1 to 6. For sumPlayerOneSeeds we find the total of the contents of the pot list from index 7 to 12. Although not shown at this point, we have another function called sumTotalSeeds which sums all the elements from index 1 to 12. I won’t show you the code for these 3 functions. I leave this as an exercise for you.

For the next case where the number of seeds held in the pot is not 0, our code is shown below:

When on the other hand the seedsHeld is not 0, we need to move on to the next pot and add 1 to the pots in the next pot. This is the same logic as we had in the previous post. For the distribution to progress however, we need to determine if the conditions for it to continue are met.

The moment when the seedsHeld variable is zero after the execution of the code above it is the moment we need to test if we are to continue the distribution or not.

For the first test where the potSeeds variable is greater than 1 and not equal to 4, it means that our loop can continue. All we need to do is set the seedsHeld variable to the seeds in our currentPot and continue the loop. To display this to the user, we Clock1 and start Clock2. This way, the seeds the seed distribution and the flipping of images.

When our potSeeds are equal to 1, it means that all the program needs to do is stop the seed distribution as the distribution got to an empty pot. To do this, we assign the value of 0 to the variable seedsHeld which will stop the Clock1 timer when the timer is active again.

When the potSeeds are equal to 4, it means that the distribution of the player landed on a pot with 3 seeds. In such a case, we have a seed capture and we assign the seeds captured to the score of the player who played the move.

For our last case when the potSeeds are equal to 4 but our seedsHeld variable is not equal to 0, it means that the seeds used in the distribution have not been exhausted. However, a pot has been filled with 4 seeds and so we must capture the pots in that seed. The seed from that pot are assigned to the owner of the pot.

Without a second clock Clock2 to perform seed capture, the whole process would execute quicker than the eye can see. With our second clock we slow down this process and make it such that the user can see how the seeds in a pot are picked.

The code to do this is shown below:

The first part of the code picks the seeds in the pot that has the ID of the currentPot. Next we activate Clock1 and deactivate Clock2. We then test for the condition we need to follow to do the scoring.

When the seedsHeld variable is 0, it means that the distribution ended and the player who played should have his score updated. However, when the potSeeds is 4, it means that in the course of distribution, the seeds in the pot became 4 and so we simply have to add to the score of the pot owner.

scorePlayer is a procedure to add to the score of the player who’s turn it was that lead to seed capture. The code is self explanatory and is shown below:

scorePotOwner is a procedure to add to the score of the owner of the pot whose seeds have been increased up to 4 during the seed distribution. In this case the distribution would continue but we have to stop seed distribution and score the owner of the pot. The code to do this is shown below:

sumTotalSeeds is a function that calculates the total of all the seeds in the game board. I asked you to create this earlier so now you get to use it. The easiest way to do this is to sum all the values of the individual elements of the list. So this will be your task.

gameOver is a procedure to end the game. It shows a notification screen and displays who the winner of the game is. It is simple enough and the code is shown below:

This week we finished the game but there is still a lot of room for improvement before this game is ready for prime time.

I have left the sumPlayerOneSeeds, sumPlayerOneSeeds and sumTotalSeeds functions as exercises for you to do in order to have a complete working program. You didn’t think it would be that easy did you?

Next week, I will round up on this series. All our projects are done. Thank you for sticking with me this far. See you next week.

1 comment:

  1. Thanks for sharing, nice post! Post really provice useful information!

    Công ty vận chuyển hàng nước ngoài FadoExpress hàng đầu chuyên vận chuyển, chuyển phát nhanh siêu tốc đi khắp thế giới, nổi bật là dịch vụ gửi hàng đi mỹ, gửi hàng đi nhậtgửi hàng đi đài loan và dịch vụ chuyển phát nhanh đi hàn quốc uy tín, giá rẻ