Advertisement
mmayoub

Solitaire, MainDeck

Jul 30th, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. import unit4.collectionsLib.Stack;
  2.  
  3. public class MainDeck {
  4. Stack<Card> faceDown;
  5. Stack<Card> faceUp;
  6.  
  7. public MainDeck(Card[] cardsArr) {
  8. faceDown = new Stack<Card>();
  9. faceUp = new Stack<Card>();
  10.  
  11. for (Card c : cardsArr) {
  12. faceDown.push(c);
  13. }
  14.  
  15. showCard();
  16. }
  17.  
  18. public void showCard() {
  19. if (faceDown.isEmpty()) {
  20. // return all faceUp cards
  21. while (!faceUp.isEmpty())
  22. faceDown.push(faceUp.pop());
  23. }
  24.  
  25. // show one card
  26. if (!faceDown.isEmpty())
  27. faceUp.push(faceDown.pop());
  28. }
  29.  
  30. // move card to final deck
  31. public boolean moveToFinalDeck(FinalDeck finalDeck) {
  32. if (!faceUp.isEmpty()) {
  33. if (finalDeck.add(faceUp.top())) {
  34. faceUp.pop();
  35.  
  36. if (faceDown.isEmpty()) {
  37. showCard();
  38. }
  39.  
  40. return true;
  41. }
  42. }
  43.  
  44. return false;
  45. }
  46.  
  47. // move card to column deck
  48. public boolean moveToColumn(Column col) {
  49. if (!faceUp.isEmpty()) {
  50. if (col.add(faceUp.top())) {
  51. faceUp.pop();
  52.  
  53. if (faceDown.isEmpty()) {
  54. showCard();
  55. }
  56.  
  57. return true;
  58. }
  59. }
  60.  
  61. return false;
  62. }
  63.  
  64. @Override
  65. public String toString() {
  66. String hidden, shown;
  67. if (faceDown.isEmpty())
  68. hidden = " ";
  69. else
  70. hidden = "[( )]";
  71.  
  72. if (faceUp.isEmpty())
  73. shown = " ";
  74. else
  75. shown = "[" + faceUp.top() + "]";
  76.  
  77. return String.format("%s %s", hidden, shown);
  78. }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement