Android Game Programming For Dummies by Derek James

Android Game Programming For Dummies by Derek James

Author:Derek James
Language: eng
Format: epub, pdf
Publisher: Wiley Publishing
Published: 2012-11-27T16:00:00+00:00


Figure 6-6: The game screen displays the discard pile.

Taking Your Turn

After you display the vital game elements displayed on the play screen (both scores, both hands, the draw pile, and the discard pile), you can move on to handling the game logic, such as determining who plays first and how to alternate turns.

Handling turns

To handle turns, you

Add a boolean variable to GameView that keeps track of the player’s turn.

Enable or disable certain logic based on the value of that boolean.

Add the following declaration to your other variable declarations:

private boolean myTurn;

Then, in the constructor, add this line:

myTurn = new Random().nextBoolean();

This value randomly decides who goes first — the computer opponent or the player. You toggle this value every time either player makes a valid play.

The next couple of sections show you how to

Enable players to draw and pick up cards to make valid plays.

Enable the computer opponent to make valid plays.

Crazy Eights has only two valid plays:

Play a card that matches either the suit or rank of the card on top of the discard pile (or an eight).

Draw a card.

Before you move on, make a simple computer opponent that returns legal moves. Later in this chapter, I show you how to make the opponent more sophisticated.

Because you need to get both the rank and suit from a card to evaluate valid plays, you need to modify the Card class. Modify its code to look like Listing 6-11.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.