public class GameControl
extends java.lang.Object
In this game, the dealer deals two "cards" to each player, one hidden, so that only the player who gets the card knows what it is, and one face up, so that everyone can see it. (Actually, what the other players see is the total of each other player's visible cards, not the individual cards.)
The players then take turns requesting cards, trying to get as close to 21 as possible, but not going over 21. These cards will be visible to all players. A player may pass (ask for no more cards). Once a player has passed, he or she cannot later ask for another card. When all players have passed, the game ends.
The winner is the player who has come closest to 21 without exceeding it. In the case of a tie, or if everyone goes over 21, no one wins.
In this program, there are some number of computer players and one human player. The game is only played once.
| Modifier and Type | Field and Description |
|---|---|
(package private) boolean[] |
passed
passed[i] == true indicates that players[i] has passed.
|
(package private) Player[] |
players
All the Player objects, including the Human player.
|
(package private) java.util.Random |
random
A random number generator.
|
(package private) java.util.Scanner |
scanner
Used for getting input from the user.
|
| Constructor and Description |
|---|
GameControl() |
| Modifier and Type | Method and Description |
|---|---|
(package private) void |
controlPlay()
Gives each player in turn a chance to take a card, until all
players have passed.
|
(package private) void |
createPlayers()
Asks the human player for a name, and then creates a Human
object and all the other Player objects, saving them in the
players array. |
(package private) void |
deal()
Deals the initial two cards (one hidden, one not hidden)
to each player.
|
(package private) int |
findWinningPlayer()
Determines who won the game, and returns it as an index into
the players array.
|
static void |
main(java.lang.String[] args)
The main method just creates a GameControl object and calls
its
run method. |
(package private) int |
nextCard()
Returns a random "card", represented by a integer between
1 and 10, inclusive.
|
(package private) void |
printResults()
Prints a summary at the end of the game, saying how many
points each player had, and who won.
|
(package private) void |
run()
Prints a welcome method, then calls methods to perform each
of the following actions:
Create the Players (one of them a Human),
Deal the initial two cards to each player,
Control the play of the game, and
Print the final results.
|
Player[] players
players.lengt.boolean[] passed
java.util.Scanner scanner
java.util.Random random
public static void main(java.lang.String[] args)
run method.args - Not used.void run()
void createPlayers()
players array. (Names of the other players may
be hardwired; don't ask the user for them.)void deal()
int nextCard()
Random
class, which is perfect for generating random numbers.void controlPlay()
passed is used to
keep track of which players have passed.void printResults()
int findWinningPlayer()