forked from jacksonGiles/Team-3
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFlowerBed.java
More file actions
737 lines (650 loc) · 19.9 KB
/
Copy pathFlowerBed.java
File metadata and controls
737 lines (650 loc) · 19.9 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
// created by team-3
import java.awt.Container;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class FlowerBed extends GameMode
{
// CONSTANTS
public static final int TABLE_HEIGHT = Card.CARD_HEIGHT * 5;
public static final int TABLE_WIDTH = (Card.CARD_WIDTH * 12) + 100;
public static final int NUM_FINAL_DECKS = 4;
public static final int NUM_PLAY_DECKS = 7;
public static final Point DECK_POS = new Point(5, 500);
public static final Point SHOW_POS = new Point(DECK_POS.x + Card.CARD_WIDTH + 5, 5);
public static final Point FINAL_POS = new Point(SHOW_POS.x + Card.CARD_WIDTH + 650, 35);
public static final Point PLAY_POS = new Point(5, 35);
// GUI COMPONENTS (top level)
private JFrame frame;
protected JPanel table;
// other components
// GAMEPLAY STRUCTURES
private static FlowerBedFinalStack[] final_cards;// Foundation Stacks
private static FlowerBedCardStack[] playCardStack; // Tableau stacks
private static FlowerBedCardStack deck; // populated with standard 52 card deck
// CARD MOVEMENT
private Card prevCard = null;// tracking card for waste stack
private Card movedCard = null;// card moved from waste stack
private boolean sourceIsFinalDeck = false;
private boolean putBackOnDeck = true;// used for waste card recycling
private boolean checkForWin = false;// should we check if game is over?
private boolean gameOver = true;// easier to negate this than affirm it
private Point start = null;// where mouse was clicked
private Point stop = null;// where mouse was released
private Card card = null; // card to be moved
// used for moving single cards
private FlowerBedCardStack source = null;
private FlowerBedCardStack dest = null;
// used for moving a stack of cards
private FlowerBedCardStack transferStack = new FlowerBedCardStack(false);
public FlowerBed() {
gameName = "Flower Bed";
gameDesc = "Move cards one at a time onto stacks regardless of suit/color to fill foundations.";
gameRules = "<b>Klondike Solitaire Rules</b>"
+ "<br><br> (From Wikipedia) Taking a shuffled standard 52-card deck of playing cards (without Jokers),"
+ " one upturned card is dealt on the left of the playing area, then six downturned cards"
+ " (from left to right).<p> On top of the downturned cards, an upturned card is dealt on the "
+ "left-most downturned pile, and downturned cards on the rest until all piles have an "
+ "upturned card. The piles should look like the figure to the right.<p>The four foundations "
+ "(light rectangles in the upper right of the figure) are built up by suit from Ace "
+ "(low in this game) to King, and the tableau piles can be built down by alternate colors,"
+ " and partial or complete piles can be moved if they are built down by alternate colors also. "
+ "Any empty piles can be filled with a King or a pile of cards with a King.<p> The point of "
+ "the game is to build up a stack of cards starting with 2 and ending with King, all of "
+ "the same suit. Once this is accomplished, the goal is to move this to a foundation, "
+ "where the player has previously placed the Ace of that suit. Once the player has done this, "
+ "they will have \"finished\" that suit- the goal being, of course, to finish all suits, "
+ "at which time the player will have won.<br><br><b> Scoring </b><br><br>"
+ "Moving cards directly from the Waste stack to a Foundation awards 10 points. However, "
+ "if the card is first moved to a Tableau, and then to a Foundation, then an extra 5 points "
+ "are received for a total of 15. Thus in order to receive a maximum score, no cards should be moved "
+ "directly from the Waste to Foundation.<p> Time can also play a factor in Windows Solitaire, if the Timed game option is selected. For every 10 seconds of play, 2 points are taken away."
+ "<b><br><br>Notes On My Implementation</b><br><br>"
+ "Drag cards to and from any stack. As long as the move is valid the card, or stack of "
+ "cards, will be repositioned in the desired spot. The game follows the standard scoring and time"
+ " model explained above with only one waste card shown at a time."
+ "<p> The timer starts running as soon as "
+ "the game begins, but it may be paused by pressing the pause button at the bottom of"
+ "the screen. ";
}
// add/subtract points based on gameplay actions
protected void setScore(int deltaScore)
{
score += deltaScore;
//String newScore = "Score: " + score;
}
// GAME TIMER UTILITIES
public void updateTimer()
{
time += 1;
// every 10 seconds elapsed we take away 2 points
if (time % 10 == 0)
{
setScore(-2);
}
//String text = "Seconds: " + time;
}
// BUTTON LISTENERS
public void startMenu() {
updateScores();
score = 0;
time = 0;
mainMenu.returnToMenu();
}
public void saveGame() {
String cardList = "";
for (int x = 0; x < NUM_PLAY_DECKS; x++)
{
Vector<Card> stack = playCardStack[x].getStack();
for (int y = 0; y < stack.size(); y++)
{
Card c = (Card) stack.get(y);
cardList = cardList + c.getSuit() + "," + c.getValue() + ";";
}
if(stack.size() == 0) {
cardList = cardList + ";";
}
cardList = cardList + "\n";
}
for (int x = 0; x < NUM_FINAL_DECKS; x++)
{
Vector<Card> stack = final_cards[x].getStack();
for (int y = 0; y < stack.size(); y++)
{
Card c = (Card) stack.get(y);
cardList = cardList + c.getSuit() + "," + c.getValue() + ";";
}
if(stack.size() == 0) {
cardList = cardList + ";";
}
cardList = cardList + "\n";
}
Vector<Card> stack = deck.reverse().getStack();
for (int y = 0; y < stack.size(); y++)
{
Card c = (Card) stack.get(y);
cardList = cardList + c.getSuit() + "," + c.getValue() + ";";
}
if(stack.size() == 0) {
cardList = cardList + ";";
}
deck.reverse();
cardList = cardList + "\n" + score + "\n" + time;
mainMenu.saveGame(cardList);
}
public void loadGame() {
if (playCardStack != null && final_cards != null)
{
for (int x = 0; x < NUM_PLAY_DECKS; x++)
{
playCardStack[x].makeEmpty();
}
for (int x = 0; x < NUM_FINAL_DECKS; x++)
{
final_cards[x].makeEmpty();
}
}
deck.makeEmpty();
List<String> stacks = mainMenu.loadGame();
for (int x = 0; x < NUM_PLAY_DECKS; x++)
{
List<String> cardList = Arrays.asList(stacks.get(x).split(";"));
for (int y = 0; y < cardList.size(); y++)
{
List<String> cardInfo = Arrays.asList(cardList.get(y).split(","));
Card c = new Card(Card.Suit.valueOf(cardInfo.get(0)), Card.Value.valueOf(cardInfo.get(1)));
playCardStack[x].push(c.setFaceup());
}
playCardStack[x].repaint();
}
for (int x = 0; x < NUM_FINAL_DECKS; x++)
{
List<String> cardList = Arrays.asList(stacks.get(NUM_PLAY_DECKS + x).split(";"));
for (int y = 0; y < cardList.size(); y++)
{
List<String> cardInfo = Arrays.asList(cardList.get(y).split(","));
Card c = new Card(Card.Suit.valueOf(cardInfo.get(0)), Card.Value.valueOf(cardInfo.get(1)));
final_cards[x].push(c.setFaceup());
}
final_cards[x].repaint();
}
List<String> cardList = Arrays.asList(stacks.get(NUM_PLAY_DECKS + NUM_FINAL_DECKS).split(";"));
for (int y = 0; y < cardList.size(); y++)
{
List<String> cardInfo = Arrays.asList(cardList.get(y).split(","));
Card c = new Card(Card.Suit.valueOf(cardInfo.get(0)), Card.Value.valueOf(cardInfo.get(1)));
deck.push(c.setFaceup());
}
deck.reverse();
score = Integer.parseInt(stacks.get(NUM_PLAY_DECKS + NUM_FINAL_DECKS + 1));
time = Integer.parseInt(stacks.get(NUM_PLAY_DECKS + NUM_FINAL_DECKS + 2));
}
private boolean validPlayStackMove(Card source, Card dest)
{
int s_val = source.getValue().ordinal();
int d_val = dest.getValue().ordinal();
//Card.Suit s_suit = source.getSuit();
//Card.Suit d_suit = dest.getSuit();
// destination card should be one higher value
if ((s_val + 1) == d_val)
{
return true;
} else
return false;
}
private boolean validFinalStackMove(Card source, Card dest)
{
int s_val = source.getValue().ordinal();
int d_val = dest.getValue().ordinal();
Card.Suit s_suit = source.getSuit();
Card.Suit d_suit = dest.getSuit();
if (s_val == (d_val + 1)) // destination must one lower
{
if (s_suit == d_suit)
return true;
else
return false;
} else
return false;
}
public Card getTop(Vector<Card> stack, Point p) {
Card c = null;
for (int x = stack.size() - 1; x >= 0; x--)
{
Card temp = (Card) stack.get(x);
temp.getSuit();
if(temp.contains(p)) {
c = temp;
}
}
return c;
}
public void mousePressed(MouseEvent e)
{
start = e.getPoint();
boolean stopSearch = false;
//statusBox.setText("");
transferStack.makeEmpty();
/*
* Here we use transferStack to temporarily hold all the cards above
* the selected card in case player wants to move a stack rather
* than a single card
*/
for (int x = 0; x < NUM_PLAY_DECKS; x++)
{
if (stopSearch)
break;
source = playCardStack[x];
// pinpointing exact card pressed-
if(source.contains(start) && source.showSize() > 0) {
Card c = (Card) source.getStack().get(0);
if(c.contains(start)) {
transferStack.putFirst(c);
card = c;
stopSearch = true;
System.out.println("Transfer Size: " + transferStack.showSize());
}
}
}
if(card == null) {
source = deck;
// pinpointing exact card pressed
Vector<Card> stack = source.getStack();
for (int x = 0; x < source.showSize(); x++)
{
transferStack.makeEmpty();
Card c = (Card) stack.get(x);
if(source.contains(start) && c == getTop(source.getStack(), start)) {
transferStack.putFirst(c);
card = c;
stopSearch = true;
System.out.println("Transfer Size: " + transferStack.showSize());
break;
}
}
}
// FINAL (FOUNDATION) CARD OPERATIONS
for (int x = 0; x < NUM_FINAL_DECKS; x++)
{
if (final_cards[x].contains(start))
{
source = final_cards[x];
card = source.getLast();
transferStack.putFirst(card);
sourceIsFinalDeck = true;
break;
}
}
putBackOnDeck = true;
}
public void mouseReleased(MouseEvent e)
{
stop = e.getPoint();
// used for status bar updates
boolean validMoveMade = false;
// SHOW CARD MOVEMENTS
if (movedCard != null)
{
// Moving from SHOW TO PLAY
for (int x = 0; x < NUM_PLAY_DECKS; x++)
{
dest = playCardStack[x];
// to empty play stack, only kings can go
if (dest.empty() && movedCard != null && dest.contains(stop)
&& movedCard.getValue() == Card.Value.KING)
{
System.out.print("moving new card to empty spot ");
movedCard.setXY(dest.getXY());
table.remove(prevCard);
dest.putFirst(movedCard);
table.repaint();
movedCard = null;
putBackOnDeck = false;
if(putBackOnDeck) {
//TODO Remove this
}
setScore(5);
System.out.println("-3");
validMoveMade = true;
break;
}
// this moves stuff from the deck out on the field automatically
// to populated play stack
/*if (movedCard != null && dest.contains(stop) && !dest.empty() && dest.getFirst().getFaceStatus()
&& validPlayStackMove(movedCard, dest.getFirst()))
{
System.out.print("moving new card ");
movedCard.setXY(dest.getFirst().getXY());
table.remove(prevCard);
dest.putFirst(movedCard);
table.repaint();
movedCard = null;
putBackOnDeck = false;
setScore(5);
validMoveMade = true;
break;
}*/
}
// Moving from SHOW TO FINAL
for (int x = 0; x < NUM_FINAL_DECKS; x++)
{
dest = final_cards[x];
// only aces can go first
if (dest.empty() && dest.contains(stop))
{
if (movedCard.getValue() == Card.Value.ACE)
{
dest.push(movedCard);
table.remove(prevCard);
dest.repaint();
table.repaint();
movedCard = null;
putBackOnDeck = false;
setScore(10);
System.out.println("-2");
validMoveMade = true;
break;
}
}
if (!dest.empty() && dest.contains(stop) && validFinalStackMove(movedCard, dest.getLast()))
{
System.out.println("Destin" + dest.showSize());
dest.push(movedCard);
table.remove(prevCard);
dest.repaint();
table.repaint();
movedCard = null;
putBackOnDeck = false;
checkForWin = true;
setScore(10);
System.out.println("-1");
validMoveMade = true;
break;
}
}
}// END SHOW STACK OPERATIONS
// PLAY STACK OPERATIONS
if (card != null && source != null)
{ // Moving from PLAY TO PLAY
for (int x = 0; x < NUM_PLAY_DECKS; x++)
{
dest = playCardStack[x];
// MOVING TO POPULATED STACK
if (card.getFaceStatus() == true && dest.contains(stop) && source != dest && !dest.empty()
&& validPlayStackMove(card, dest.getFirst()) && transferStack.showSize() == 1)
{
Card c = card;
source.removeCard(card);
c.repaint();
// if playstack, turn next card up
if (source.getFirst() != null)
{
Card temp = source.getFirst().setFaceup();
temp.repaint();
source.repaint();
}
dest.setXY(dest.getXY().x, dest.getXY().y);
dest.putFirst(c);
dest.repaint();
table.repaint();
System.out.print("Destination ");
dest.showSize();
if (sourceIsFinalDeck)
setScore(15);
else
setScore(10);
System.out.println("0");
validMoveMade = true;
break;
} else if (dest.empty() && transferStack.showSize() == 1 && dest.contains(stop))
{// MOVING TO EMPTY STACK, ONLY KING ALLOWED
Card c = card;
source.removeCard(card);
c.repaint();
// if playstack, turn next card up
if (source.getFirst() != null)
{
Card temp = source.getFirst().setFaceup();
temp.repaint();
source.repaint();
}
dest.setXY(dest.getXY().x, dest.getXY().y);
dest.putFirst(c);
dest.repaint();
table.repaint();
System.out.print("Destination ");
dest.showSize();
setScore(5);
System.out.println("1");
validMoveMade = true;
break;
}
}
// from PLAY TO FINAL
for (int x = 0; x < NUM_FINAL_DECKS; x++)
{
dest = final_cards[x];
if (card.getFaceStatus() == true && source != null && dest.contains(stop) && source != dest)
{// TO EMPTY STACK
if (dest.empty())// empty final should only take an ACE
{
if (card.getValue() == Card.Value.ACE)
{
Card c = card;
source.removeCard(card);
c.repaint();
if (source.getFirst() != null)
{
Card temp = source.getFirst().setFaceup();
temp.repaint();
source.repaint();
}
dest.setXY(dest.getXY().x, dest.getXY().y);
dest.push(c);
dest.repaint();
table.repaint();
System.out.print("Destination ");
dest.showSize();
card = null;
setScore(10);
System.out.println("2");
validMoveMade = true;
break;
}// TO POPULATED STACK
} else if (validFinalStackMove(card, dest.getLast()))
{
Card c = card;
source.removeCard(card);
c.repaint();
if (source.getFirst() != null)
{
Card temp = source.getFirst().setFaceup();
temp.repaint();
source.repaint();
}
dest.setXY(dest.getXY().x, dest.getXY().y);
dest.push(c);
dest.repaint();
table.repaint();
System.out.print("Destination ");
dest.showSize();
card = null;
checkForWin = true;
setScore(10);
System.out.println("3");
validMoveMade = true;
break;
}
}
}
}// end cycle through play decks
// SHOWING STATUS MESSAGE IF MOVE INVALID
if (!validMoveMade && dest != null && card != null)
{
//statusBox.setText("That Is Not A Valid Move");
} else if(validMoveMade) {
playSound();
}
// CHECKING FOR WIN
if (checkForWin)
{
boolean gameNotOver = false;
// cycle through final decks, if they're all full then game over
for (int x = 0; x < NUM_FINAL_DECKS; x++)
{
dest = final_cards[x];
if (dest.showSize() != 13)
{
// one deck is not full, so game is not over
gameNotOver = true;
break;
}
}
if (!gameNotOver) {
gameOver = true;
}
}
if (checkForWin && gameOver)
{
mainMenu.updateScores(gameName, score, time, true);
JOptionPane.showMessageDialog(table, "Congratulations! You've Won!");
//statusBox.setText("Game Over!");
}
// RESET VARIABLES FOR NEXT EVENT
start = null;
stop = null;
source = null;
dest = null;
card = null;
sourceIsFinalDeck = false;
checkForWin = false;
gameOver = false;
}// end mousePressed()
public void newGame() {
updateScores();
playNewGame();
}
private void playNewGame()
{
score = 0;
time = 0;
deck = new FlowerBedCardStack(true); // deal 52 cards
deck.shuffle();
//table.removeAll();
// reset stacks if user starts a new game in the middle of one
if (playCardStack != null && final_cards != null)
{
for (int x = 0; x < NUM_PLAY_DECKS; x++)
{
playCardStack[x].makeEmpty();
}
for (int x = 0; x < NUM_FINAL_DECKS; x++)
{
final_cards[x].makeEmpty();
}
}
// initialize & place final (foundation) decks/stacks
final_cards = new FlowerBedFinalStack[NUM_FINAL_DECKS];
for (int x = 0; x < NUM_FINAL_DECKS; x++)
{
final_cards[x] = new FlowerBedFinalStack();
final_cards[x].setXY((FINAL_POS.x + (x * Card.CARD_WIDTH)) + 10, FINAL_POS.y);
table.add(final_cards[x]);
}
// place new card distribution button
// initialize & place play (tableau) decks/stacks
playCardStack = new FlowerBedCardStack[NUM_PLAY_DECKS];
for (int x = 0; x < NUM_PLAY_DECKS; x++)
{
playCardStack[x] = new FlowerBedCardStack(false);
playCardStack[x].setXY((DECK_POS.x + (x * (Card.CARD_WIDTH + 10))), PLAY_POS.y);
table.add(playCardStack[x]);
}
// Dealing new game
for (int x = 0; x < NUM_PLAY_DECKS; x++)
{
Card c = deck.pop().setFaceup();
//c.setImage(cardPath);
playCardStack[x].putFirst(c);
//here
for (int y = 0; y < 4; y++)
{
c = deck.pop().setFaceup();
playCardStack[x].push(c);
}
}
deck.setXY(DECK_POS.x, DECK_POS.y);
table.add(deck);
Vector<Card> stack = deck.getStack();
for (int x = 0; x < deck.showSize(); x++)
{
Card c = (Card) stack.get(x);
c.setFaceup();
}
// reset time
time = 0;
//scoreBox.setText("Score: 0");
//timeBox.setText("Seconds: 0");
/*table.add(statusBox);
table.add(toggleTimerButton);
table.add(saveButton);
table.add(loadButton);
table.add(optionsButton);
table.add(gameTitle);
table.add(timeBox);
table.add(mainMenuButton);
table.add(newGameButton);
table.add(showRulesButton);
table.add(scoreBox);*/
table.repaint();
}
public void refreshCards() {
for (int x = 0; x < NUM_FINAL_DECKS; x++)
{
for(int y = 0; y < final_cards[x].v.size(); y++)
{
Card c = (Card)final_cards[x].v.get(y);
c.refreshImage();
}
final_cards[x].repaint();
}
for (int x = 0; x < NUM_PLAY_DECKS; x++)
{
for(int y = 0; y < playCardStack[x].v.size(); y++)
{
Card c = (Card)playCardStack[x].v.get(y);
c.refreshImage();
}
playCardStack[x].repaint();
}
for (int x = 0; x < deck.v.size(); x++)
{
Card c = (Card)deck.v.get(x);
c.refreshImage();
}
deck.repaint();
}
public void execute(JPanel myTable, JFrame myFrame, StartMenu myMenu, String myCardPath) {
Container contentPane;
table = myTable;
frame = myFrame;
mainMenu = myMenu;
cardPath = myCardPath;
frame.setSize(TABLE_WIDTH, TABLE_HEIGHT);
table.setLayout(null);
contentPane = frame.getContentPane();
contentPane.add(table);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
playNewGame();
//addButtons();
//startTimer();
/*table.addMouseListener(new CardMovementManager());
table.addMouseMotionListener(new CardMovementManager());*/
frame.setVisible(true);
}
}