-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCardGameFramework.java
More file actions
167 lines (135 loc) · 4.99 KB
/
Copy pathCardGameFramework.java
File metadata and controls
167 lines (135 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
class CardGameFramework
{
private static final int MAX_PLAYERS = 50;
private int numPlayers;
private int numPacks; // # standard 52-card packs per deck
// ignoring jokers or unused cards
private int numJokersPerPack; // if 2 per pack & 3 packs per deck, get 6
private int numUnusedCardsPerPack; // # cards removed from each pack
private int numCardsPerHand; // # cards to deal each player
private Deck deck; // holds the initial full deck and gets
// smaller (usually) during play
private Hand[] hand; // one Hand for each player
private Card[] unusedCardsPerPack; // an array holding the cards not used
// in the game. e.g. pinochle does not
// use cards 2-8 of any suit
public CardGameFramework( int numPacks, int numJokersPerPack,
int numUnusedCardsPerPack, Card[] unusedCardsPerPack,
int numPlayers, int numCardsPerHand)
{
int k;
// filter bad values
if (numPacks < 1 || numPacks > 6)
numPacks = 1;
if (numJokersPerPack < 0 || numJokersPerPack > 4)
numJokersPerPack = 0;
if (numUnusedCardsPerPack < 0 || numUnusedCardsPerPack > 50) // > 1 card
numUnusedCardsPerPack = 0;
if (numPlayers < 1 || numPlayers > MAX_PLAYERS)
numPlayers = 4;
// one of many ways to assure at least one full deal to all players
if (numCardsPerHand < 1 ||
numCardsPerHand > numPacks * (52 - numUnusedCardsPerPack)
/ numPlayers )
numCardsPerHand = numPacks * (52 - numUnusedCardsPerPack) / numPlayers;
// allocate
this.unusedCardsPerPack = new Card[numUnusedCardsPerPack];
this.hand = new Hand[numPlayers];
for (k = 0; k < numPlayers; k++)
this.hand[k] = new Hand();
deck = new Deck(numPacks);
// assign to members
this.numPacks = numPacks;
this.numJokersPerPack = numJokersPerPack;
this.numUnusedCardsPerPack = numUnusedCardsPerPack;
this.numPlayers = numPlayers;
this.numCardsPerHand = numCardsPerHand;
for (k = 0; k < numUnusedCardsPerPack; k++)
this.unusedCardsPerPack[k] = unusedCardsPerPack[k];
// prepare deck and shuffle
newGame();
}
// constructor overload/default for game like bridge
public CardGameFramework()
{
this(1, 0, 0, null, 4, 13);
}
public Hand getHand(int k)
{
// hands start from 0 like arrays
// on error return automatic empty hand
if (k < 0 || k >= numPlayers)
return new Hand();
return hand[k];
}
public Card getCardFromDeck() { return deck.dealCard(); }
public int getNumCardsRemainingInDeck() { return deck.getNumCards(); }
public void newGame()
{
int k, j;
// clear the hands
for (k = 0; k < numPlayers; k++)
hand[k].resetHand();
// restock the deck
deck.init(numPacks);
// remove unused cards
for (k = 0; k < numUnusedCardsPerPack; k++)
deck.removeCard( unusedCardsPerPack[k] );
// add jokers
for (k = 0; k < numPacks; k++)
for ( j = 0; j < numJokersPerPack; j++)
deck.addCard( new Card('X', Card.Suit.values()[j]) );
// shuffle the cards
deck.shuffle();
}
public boolean deal()
{
// returns false if not enough cards, but deals what it can
int k, j;
boolean enoughCards;
// clear all hands
for (j = 0; j < numPlayers; j++)
hand[j].resetHand();
enoughCards = true;
for (k = 0; k < numCardsPerHand && enoughCards ; k++)
{
for (j = 0; j < numPlayers; j++)
if (deck.getNumCards() > 0)
hand[j].takeCard( deck.dealCard() );
else
{
enoughCards = false;
break;
}
}
return enoughCards;
}
void sortHands()
{
int k;
for (k = 0; k < numPlayers; k++)
hand[k].sort();
}
Card playCard(int playerIndex, int cardIndex)
{
// returns bad card if either argument is bad
if (playerIndex < 0 || playerIndex > numPlayers - 1 ||
cardIndex < 0 || cardIndex > numCardsPerHand - 1)
{
//Creates a card that does not work
return new Card('M', Card.Suit.spades);
}
// return the card played
return hand[playerIndex].playCard(cardIndex);
}
boolean takeCard(int playerIndex)
{
// returns false if either argument is bad
if (playerIndex < 0 || playerIndex > numPlayers - 1)
return false;
// Are there enough Cards?
if (deck.getNumCards() <= 0)
return false;
return hand[playerIndex].takeCard(deck.dealCard());
}
}