Advertisement
Guest User

war/crazy8

a guest
Apr 28th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static Deck deck = new Deck();
  5. public static boolean peace = true;
  6. public static Player player1 = new Player(); //player1's hand
  7. public static Player player2 = new Player(); //player2's hand
  8. public static Board board1 = new Board(); //player1's board
  9. public static Board board2 = new Board(); //player2's board
  10. public static Player[] players = {player1, player2};
  11. public static Board[] boards = {board1, board2};
  12.  
  13.  
  14. private static void deal(){
  15. for(int i = 1; i <= 26; i++){
  16. Card c1 = deck.getCard();
  17. player1.put(c1);
  18. Card c2 = deck.getCard();
  19. player2.put(c2);
  20. }
  21. }
  22.  
  23. private static void playRound(){
  24. Card play1 = player1.get();
  25. Card play2 = player2.get();
  26. if (play1.compareTo(play2) > 0 //player1 gets the card
  27. player1.put(play1);
  28. );
  29. if (play1.compareTo(play2) < 0 //player2 gets the card
  30. );
  31. if (play1.compareTo(play2) == 0 //war
  32. );
  33.  
  34. }
  35.  
  36. private static void war() {
  37.  
  38. }
  39.  
  40. public static void main(String[] args) {
  41. deal();
  42. System.out.println(player1);
  43.  
  44. }
  45. }
  46.  
  47. // Card.java
  48.  
  49. public class Card implements Comparable<Card> {
  50. private String mySymbol;
  51. private int myRank;
  52. private char mySuit;
  53.  
  54. public Card (char rank, char suit) {
  55. // Note how switch works. It's really useful.
  56. // Note also the character arithmetic. Remember, a char has a Unicode
  57. // value and that needs to be converted.
  58. switch (rank) {
  59. case 'A': myRank = 14; break;
  60. case 'K': myRank = 13; break;
  61. case 'Q': myRank = 12; break;
  62. case 'J': myRank = 11; break;
  63. case 'T': myRank = 10; break;
  64. default: myRank = rank - '0';
  65. }
  66. mySuit = suit;
  67. mySymbol = "" + rank + suit;
  68. }
  69.  
  70. public int getRank() { return myRank; }
  71.  
  72. public char getSuit() { return mySuit; }
  73.  
  74. public String getSymbol() { return mySymbol; }
  75.  
  76. public int compareTo(Card card) {
  77. {
  78. if (getRank() < card.getRank())
  79. return -1;
  80. if (getRank() > card.getRank())
  81. return 1;
  82. else return 0;
  83. }
  84. }
  85.  
  86. public boolean equals(Card card) {
  87. if (getRank() == card.getRank())
  88. return true;
  89. else return false;
  90.  
  91. }
  92.  
  93. public String toString() {
  94. return mySymbol;
  95. }
  96. }
  97.  
  98.  
  99. public class Player extends Queue<Card>{
  100. public Player(){
  101. super();
  102. }
  103.  
  104. }
  105.  
  106. // Deck.java
  107.  
  108. import java.util.*;
  109.  
  110. public class Deck { // This is slow--can you think of a better way? (There are many...)
  111. HashSet myCards = new HashSet();
  112. char[] suits = { 'S', 'H', 'C', 'D' };
  113. char[] ranks = { 'A', 'K', 'Q', 'J', 'T',
  114. '9', '8', '7', '6', '5',
  115. '4', '3', '2' };
  116. Iterator cardGetter; // This is used in a really ugly way
  117. Random random = new Random();
  118.  
  119. public Deck() {
  120. init();
  121. }
  122.  
  123. private void init() {
  124. for (int i = 0; i < suits.length; i++)
  125. for (int j = 0; j < ranks.length; j++)
  126. myCards.add(new Card(ranks[j], suits[i]));
  127. cardGetter = myCards.iterator();
  128. }
  129.  
  130. // Note why we need to adhere to abstractions. The shuffle method
  131. // doesn't really shuffle at all upon inspection. However, in conjunction
  132. // with getCard(), the behavior of shuffling and getting cards from the
  133. // deck is perfectly reasonable.
  134. //
  135. // While I would not recommend writing shuffle() this way in general,
  136. // the point is that the Deck abstraction should be treated like a black
  137. // box, not an open book.
  138. public void shuffle() {
  139. myCards.clear();
  140. init();
  141. }
  142.  
  143. public Card getCard() {
  144. Card c;
  145. cardGetter = myCards.iterator(); // Start from beginning each time
  146.  
  147. int next = random.nextInt(size()) + 1;
  148. c = (Card) cardGetter.next();
  149. for (int i = 1; i < next; i++) {
  150. c = (Card) cardGetter.next();
  151. }
  152. cardGetter.remove(); // Note that if a card is dealt,
  153. return c; // it is taken out of the deck
  154. }
  155.  
  156. public boolean isEmpty() {
  157. return myCards.isEmpty();
  158. }
  159.  
  160. public int size() {
  161. return myCards.size();
  162. }
  163. }
  164. import java.util.LinkedList;
  165.  
  166. public class Queue<E> extends LinkedList<E> {
  167. public Queue() {}
  168.  
  169. public void put(E o) {
  170. addLast(o);
  171. }
  172.  
  173. public E get() {
  174. if (!this.isEmpty()) {
  175. return removeFirst();
  176. } else {
  177. System.err.println("You can\'t do that!");
  178. return null;
  179. }
  180. }
  181.  
  182. public E peek() {
  183. return getFirst();
  184. }
  185. }
  186. import java.util.LinkedList;
  187.  
  188. public class Stack<E> extends LinkedList<E> {
  189. public Stack() {}
  190.  
  191. public void put(E o) {
  192. addFirst(o);
  193. }
  194.  
  195. public E get() {
  196. if (!this.isEmpty()) {
  197. return removeFirst();
  198. } else {
  199. System.err.println("You can\'t do that!");
  200. return null;
  201. }
  202. }
  203.  
  204. public E peek() {
  205. return getFirst();
  206. }
  207. }
  208.  
  209.  
  210. public class Board extends Stack {
  211. public Board(){
  212. super();
  213. }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement