Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. public class ArrayStack<E> implements Stack<E> {
  2. private E[] data;
  3. private int size;
  4. public ArrayStack() {
  5. data = (E[])(new Object[1]);
  6. size = 0;
  7. }
  8.  
  9. public boolean isEmpty() {
  10. return size == 0;
  11. }
  12.  
  13. public E pop() {
  14. size--;
  15. return data[size-1];
  16. }
  17.  
  18. protected boolean isFull() {
  19. return size == data.length;
  20. }
  21.  
  22. public void push (E target) {
  23. if (isFull()) {
  24. stretch();
  25. }
  26. data[size] = target;
  27. size++;
  28. }
  29.  
  30. public void stretch() {
  31. E[] newData = (E[])(new Object[data.length * 2]);
  32. for (int i = 0; i < data.length; i++) {
  33. newData[i] = data[i];
  34. }
  35. data = newData;
  36. }
  37. }
  38. ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  39. public class Card {
  40. private int rank;
  41. private int suit;
  42. public static final int SPADES = 0;
  43. public static final int HEARTS = 1;
  44. public static final int DIAMONDS = 2;
  45. public static final int CLUBS = 3;
  46. public static final int ACE = 1;
  47. public static final int JACK = 11;
  48. public static final int QUEEN = 12;
  49. public static final int KING = 13;
  50.  
  51. public Card(int rank, int suit) {
  52. this.rank = rank;
  53. this.suit = suit;
  54. }
  55. public int getRank() {
  56. return rank;
  57. }
  58.  
  59. public int getSuit() {
  60. return suit;
  61. }
  62.  
  63. public boolean equals(Object that) {
  64. if (this == that) {
  65. return true;
  66. }
  67. if (that == null) {
  68. return false;
  69. }
  70. if (getClass() != that.getClass()) {
  71. return false;
  72. }
  73. Card thatCard = (Card)that;
  74. return rank == thatCard.rank;
  75. }
  76.  
  77. public String toString() {
  78. return ""+ "-A23456789TJQK".charAt(rank) + "shdc".charAt(suit);
  79. }
  80. }
  81. ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  82. public class Deck {
  83.  
  84. private Card[] cards;
  85.  
  86. private int size;
  87.  
  88. public Deck() {
  89. cards = new Card[52];
  90. size = 0;
  91. for (int suit = Card.SPADES; suit <= Card.CLUBS; suit++) {
  92. for (int rank = Card.ACE; rank <= Card.KING; rank++) {
  93. cards [size] = new Card(rank, suit);
  94. size +=1;
  95. }
  96. }
  97. }
  98.  
  99. public Card deal() {
  100. size--;
  101. return cards[size];
  102. }
  103.  
  104. public void shuffle() {
  105. for (int i = size - 1; i>0; i--) {
  106. swap(i, (int)(Math.random() * (i+1)));
  107. }
  108. }
  109.  
  110. public void swap (int i, int j) {
  111. Card temp = cards[i];
  112. cards[i] = cards[j];
  113. cards[j] = temp;
  114. }
  115.  
  116. public boolean isEmpty() {
  117. return size == 0;
  118. }
  119.  
  120. public int size() {
  121. return size;
  122. }
  123. }
  124. ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  125. import java.util.Scanner;
  126.  
  127. public class IdiotsDelight {
  128.  
  129. public static final Scanner INPUT = new Scanner(System.in);
  130. private Stack<Card>[] stacks;
  131. private Deck deck;
  132.  
  133. public IdiotsDelight() {
  134. deck = new Deck();
  135. deck.shuffle();
  136. stacks = new Stack[4];
  137. for (int i = 0; i < 4; i++) {
  138. stacks[i] = new ArrayStack<Card>();
  139. }
  140.  
  141. deal();
  142. }
  143.  
  144. public void deal() {
  145. for (Stack<Card> s : stacks) {
  146. s.push(deck.deal());
  147. }
  148. }
  149.  
  150. public void play() {
  151. while (true) {
  152. System.out.println("\n" + this);
  153.  
  154. boolean done = true;
  155. for (Stack<Card> s : stacks) {
  156. if (!(s.isEmpty())) {
  157. done = false;
  158. break;
  159. }
  160. }
  161. if (done) {
  162. System.out.println("You win!");
  163. return;
  164. }
  165.  
  166. System.out.print("Your command (pair, suit, deal, or quit)? " );
  167. String command = INPUT.nextLine();
  168.  
  169. if (command.equals("pair")) {
  170. removePair();
  171. } else if (command.equals("suit")) {
  172. removeLowCard();
  173. } else if (command.equals("deal")) {
  174. deal();
  175. } else {
  176. return;
  177. }
  178. }
  179. }
  180.  
  181. public void removeLowCard() {
  182. System.out.print("Location (1-4) of low card? ");
  183. int i = INPUT.nextInt();
  184. System.out.print("Location (1-4) of high card? ");
  185. int j = INPUT.nextInt();
  186. INPUT.nextLine();
  187. Card lowCard = stacks[i-1].peek();
  188. Card highCard = stacks[j-1].peek();
  189.  
  190. stacks[i-1].pop();
  191. stacks[j-1].pop();
  192. }
  193.  
  194. public String toString() {
  195. String result = "";
  196. for (int i = 0; i < 4; i++) {
  197. if (stacks[i].isEmpty()) {
  198. result += "-- ";
  199. } else {
  200. result += stacks[i].peek() + " ";
  201. }
  202. }
  203. return result + "\n" + deck.size() + " cards left in the deck.";
  204. }
  205.  
  206. public static void main (String[] args) {
  207. System.out.println("Welcome to Idiot's Delight.");
  208. IdiotsDelight game = new IdiotsDelight();
  209. game.play();
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement