forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatienceGame.java
More file actions
300 lines (266 loc) · 8.75 KB
/
Copy pathPatienceGame.java
File metadata and controls
300 lines (266 loc) · 8.75 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
public class PatienceGame {
private static final int NUM_LANES = 7;
private static final int NUM_SUITS = 4;
private static final int NUM_CARDS_PER_SUIT = 13;
private Stack<Card> drawPile = new Stack<>();
private List<Stack<Card>> lanes = new ArrayList<>(NUM_LANES);
private List<Stack<Card>> suitPiles = new ArrayList<>(NUM_SUITS);
private Stack<Card> uncoveredPile = new Stack<>();
private int score = 0;
private int moves = 0;
public PatienceGame() {
initializeGame();
}
// Add this method to the PatienceGame class
private void moveUncoveredCardToSuit(char suitLabel) {
int suitIndex = getLabelIndex(suitLabel);
if (suitIndex >= NUM_LANES && suitIndex < NUM_LANES + NUM_SUITS) {
if (uncoveredPile.isEmpty()) {
System.out.println("Uncovered pile is empty.");
} else {
Card card = uncoveredPile.peek();
if (suitPiles.get(suitIndex - NUM_LANES).isEmpty()) {
if (card.getValue() == 1) {
suitPiles.get(suitIndex - NUM_LANES).push(uncoveredPile.pop());
calculateScore(card, 'P', suitLabel);
moves++;
} else {
System.out.println("Invalid move. Only Aces can start a new pile.");
}
} else {
Card topCard = suitPiles.get(suitIndex - NUM_LANES).peek();
if (
topCard.isOneValueHigher(card) &&
topCard.getSuit() == card.getSuit()
) {
suitPiles.get(suitIndex - NUM_LANES).push(uncoveredPile.pop());
calculateScore(card, 'P', suitLabel);
moves++;
} else {
System.out.println("Invalid move. Check the destination pile.");
}
}
}
} else {
System.out.println("Invalid command. Cannot move to a lane.");
}
}
private void initializeGame() {
// Create and shuffle a deck of cards
List<Card> deck = new ArrayList<>();
for (int suit = 0; suit < NUM_SUITS; suit++) {
for (int value = 1; value <= NUM_CARDS_PER_SUIT; value++) {
deck.add(new Card(suit, value));
}
}
Collections.shuffle(deck);
// Initialize the draw pile with the shuffled deck
drawPile.addAll(deck);
// Initialize lanes and suit piles
for (int i = 0; i < NUM_LANES; i++) {
lanes.add(new Stack<>());
}
for (int i = 0; i < NUM_SUITS; i++) {
suitPiles.add(new Stack<>());
}
}
public void playGame() {
Scanner scanner = new Scanner(System.in);
while (true) {
displayGameState();
System.out.print("Enter a command (Q, D, or move): ");
String command = scanner.nextLine().toUpperCase();
if (command.equals("Q")) {
System.out.println("Game Over. Final Score: " + score);
break;
} else if (command.equals("D")) {
drawCard();
} else if (command.length() == 2) {
moveCard(command);
} else if (command.length() == 3) {
moveMultipleCards(command);
} else {
System.out.println("Invalid command. Please try again.");
}
if (isGameOver()) {
System.out.println("Congratulations! You won the game!");
break;
}
}
scanner.close();
}
private void displayGameState() {
String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
String[] suits = {"H", "D", "S", "C"};
for (String suit : suits) {
for (String rank : ranks) {
String card = rank + suit;
System.out.print(card + " ");
}
System.out.println(); // Start a new line for the next suit
}
System.out.println("Score: " + score);
System.out.println("Moves: " + moves);
System.out.println("Draw Pile: " + drawPile.size() + " cards");
System.out.println("Uncovered Pile: " + uncoveredPile.size() + " cards");
for (int i = 0; i < NUM_LANES; i++) {
System.out.println(
"Lane " + (i + 1) + ": " + lanes.get(i).size() + " cards"
);
}
for (int i = 0; i < NUM_SUITS; i++) {
System.out.println(
"Suit " +
Card.SUIT_LABELS[i] +
": " +
suitPiles.get(i).size() +
" cards"
);
}
}
private void drawCard() {
if (!drawPile.isEmpty()) {
Card card = drawPile.pop();
uncoveredPile.push(card);
moves++;
} else {
System.out.println("Draw pile is empty. Cannot draw a card.");
}
}
private void moveCard(String command) {
char fromLabel = command.charAt(0);
char toLabel = command.charAt(1);
int fromIndex = getLabelIndex(fromLabel);
int toIndex = getLabelIndex(toLabel);
if (fromIndex != -1 && toIndex != -1) {
if (fromLabel == 'P') {
moveUncoveredCardToSuit(toLabel);
} else if (fromLabel >= '1' && fromLabel <= '7') {
Card card = lanes.get(fromIndex).peek();
if (canMoveCard(card, toIndex)) {
lanes.get(toIndex).push(lanes.get(fromIndex).pop());
calculateScore(card, fromLabel, toLabel);
moves++;
} else {
System.out.println("Invalid move. Check the destination pile.");
}
}
} else {
System.out.println("Invalid labels.");
}
}
private void moveMultipleCards(String command) {
char fromLabel = command.charAt(0);
char toLabel = command.charAt(1);
int number = Character.getNumericValue(command.charAt(2));
int fromIndex = getLabelIndex(fromLabel);
int toIndex = getLabelIndex(toLabel);
if (fromIndex != -1 && toIndex != -1) {
if (lanes.get(fromIndex).size() >= number) {
List<Card> cardsToMove = new ArrayList<>();
for (int i = 0; i < number; i++) {
cardsToMove.add(
lanes.get(fromIndex).get(lanes.get(fromIndex).size() - 1 - i)
);
}
if (canMoveCards(cardsToMove, toIndex)) {
for (Card card : cardsToMove) {
lanes.get(toIndex).push(lanes.get(fromIndex).pop());
calculateScore(card, fromLabel, toLabel);
}
moves++;
} else {
System.out.println("Invalid move. Check the destination pile.");
}
} else {
System.out.println("Not enough cards in the source lane.");
}
} else {
System.out.println("Invalid labels.");
}
}
private int getLabelIndex(char label) {
if (label == 'P') {
return NUM_LANES;
} else if (label >= '1' && label <= '7') {
return Character.getNumericValue(label) - 1;
} else if (label >= 'D' && label <= 'S') {
return NUM_LANES + label - 'D';
}
return -1;
}
private boolean canMoveCard(Card card, int toIndex) {
if (lanes.get(toIndex).isEmpty()) {
return card.getValue() == 13; // Only Kings can start a new pile
} else {
Card topCard = lanes.get(toIndex).peek();
return topCard.isOneValueHigher(card) && topCard.isOppositeColor(card);
}
}
private boolean canMoveCards(List<Card> cards, int toIndex) {
if (lanes.get(toIndex).isEmpty()) {
return cards.get(0).getValue() == 13; // Only Kings can start a new pile
} else {
Card topCard = lanes.get(toIndex).peek();
return (
topCard.isOneValueHigher(cards.get(cards.size() - 1)) &&
topCard.isOppositeColor(cards.get(cards.size() - 1))
);
}
}
private void calculateScore(Card card, char fromLabel, char toLabel) {
if (fromLabel == 'P' && (toLabel >= 'D' && toLabel <= 'S')) {
score += 10; // From uncovered pile to suit
} else if (
fromLabel >= '1' && fromLabel <= '7' && (toLabel >= 'D' && toLabel <= 'S')
) {
score += 20; // From lane to suit
} else if (
(fromLabel >= '1' && fromLabel <= '7') &&
(toLabel >= '1' && toLabel <= '7')
) {
score += 5; // Between lanes
}
}
private boolean isGameOver() {
for (Stack<Card> suitPile : suitPiles) {
if (suitPile.size() < NUM_CARDS_PER_SUIT) {
return false;
}
}
return true;
}
public static void main(String[] args) {
PatienceGame game = new PatienceGame();
game.playGame();
}
}
class Card {
public static final String[] SUIT_LABELS = {"D", "H", "C", "S"};
private int suit;
private int value;
public Card(int suit, int value) {
this.suit = suit;
this.value = value;
}
public int getSuit() {
return suit;
}
public int getValue() {
return value;
}
public char getLabel() {
return SUIT_LABELS[suit].charAt(0);
}
public boolean isOneValueHigher(Card other) {
return this.value - other.value == 1;
}
public boolean isOppositeColor(Card other) {
return (this.suit < 2 && other.suit >= 2) || (this.suit >= 2 && other.suit < 2);
}
}