Guest User

Untitled

a guest
Jul 25th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. package runner;
  2. import java.util.*;
  3. class Card{
  4. private int rank, suit;
  5. private String[] suitStrings = {"Hearts", "Spades", "Clubs", "Diamonds",""};
  6. private String[] rankStrings = {"2","3","4","5","6","7","8","9","10","J","Q","K","A",""}; //ints correspond with each string in array.
  7. Card(int rank, int suit){
  8. this.rank = rank;
  9. this.suit = suit;
  10. }
  11. Card(){
  12. rank = 13;
  13. suit = 4;
  14. }
  15. public int getRank(){
  16. return rank;
  17. }
  18. public int getSuit(){
  19. return suit;
  20. }
  21. public String readCard(){
  22. return (rankStrings[rank]+" of "+suitStrings[suit]);
  23. }
  24. }
  25. class DeckClass{
  26. Card[] deck = new Card[52];
  27. int counter = 0;
  28. DeckClass(){
  29. for (int i=0;i<4;i++){
  30. for (int j = 0; j<13;j++){
  31. deck[counter] = new Card(j,i);
  32. counter++;
  33. }
  34. }
  35. }
  36. public void shuffle(){
  37. Collections.shuffle(Arrays.asList(deck));
  38. }
  39. }
  40. class Player{
  41. private ArrayList<Card> hand = new ArrayList<Card>();
  42. public void addCard(Card cardToAdd){
  43. hand.add(cardToAdd);
  44. }
  45. public void removeCard(int indexOfCard){
  46. hand.remove(indexOfCard);
  47. }
  48. public int lengthOfHand(){
  49. return hand.size();
  50. }
  51. public void displayCards(){
  52. System.out.println("=================================================================");
  53. int counter = 0;
  54. for (int i = 0;i<hand.size();i++){
  55. System.out.print(hand.get(i).readCard()+"\t"); //read the cards in each hand as a test.
  56. counter++;
  57. if (counter==4){
  58. System.out.println();
  59. counter = 0;
  60. }
  61. }
  62. System.out.println("\n=================================================================");
  63. }
  64. public int handIndexOf(String card){
  65. int index = -1;
  66. for (int i = 0;i<hand.size();i++){
  67. if (hand.get(i).readCard().equals(card)){
  68. index = i;
  69. }
  70. }
  71. return index;
  72. }
  73. public Card getCard(int index){
  74. return hand.get(index);
  75. }
  76.  
  77. }
  78. public class Hearts {
  79. Random r = new Random();
  80. private static DeckClass mainDeck = new DeckClass();
  81. private static Player one = new Player();
  82. private static Player two = new Player(); //init deck and players
  83. private static Player three = new Player();
  84. private static Player four = new Player();
  85.  
  86. public static void dealCardsFromDeck(){
  87. mainDeck.shuffle();
  88. int player = 1;
  89. for (int i = 0; i<mainDeck.deck.length;i++){
  90. switch (player){
  91. case 1:
  92. one.addCard(mainDeck.deck[i]);
  93. player++;
  94. break;
  95. case 2:
  96. two.addCard(mainDeck.deck[i]);
  97. player++;
  98. break;
  99. case 3:
  100. three.addCard(mainDeck.deck[i]);
  101. player++;
  102. break;
  103. case 4:
  104. four.addCard(mainDeck.deck[i]);
  105. player = 1;
  106. break;
  107. }
  108. }
  109. }
  110.  
  111. public static void main(String[] args){
  112. Random r = new Random();
  113. Scanner s = new Scanner(System.in);
  114. dealCardsFromDeck();
  115. System.out.println("Cards have been shuffled and dealt. You are player one.\n");
  116. System.out.println("Select three cards to swap with player two.");
  117. one.displayCards();
  118.  
  119. Card[][] cardsToAdd = new Card[4][3];
  120.  
  121. //This array stores the cards that will be swapped to the next hand.
  122. //The cards are first stored in this array, and then swapped all at once
  123. //This prevents cards from being cycled around the table
  124.  
  125. System.out.print("First card: ");
  126. cardsToAdd[0][0] = one.getCard(one.handIndexOf(s.nextLine()));
  127. System.out.print("Second card: ");
  128. cardsToAdd[0][1] = one.getCard(one.handIndexOf(s.nextLine()));
  129. System.out.print("Third card: ");
  130. cardsToAdd[0][2] = one.getCard(one.handIndexOf(s.nextLine()));
  131.  
  132. for (int i = 0;i<3;i++){
  133. cardsToAdd[1][i] = two.getCard(r.nextInt(two.lengthOfHand()));
  134. }
  135. for (int i = 0;i<3;i++){
  136. cardsToAdd[2][i] = three.getCard(r.nextInt(three.lengthOfHand())); //Collecting random cards to swap
  137. }
  138. for (int i = 0;i<3;i++){
  139. cardsToAdd[3][i] = four.getCard(r.nextInt(four.lengthOfHand()));
  140. }
  141. for (int i = 0; i<3;i++){
  142. two.addCard(cardsToAdd[0][i]);
  143. //System.out.println(cardsToAdd[0][i].readCard());
  144. one.removeCard(one.handIndexOf(cardsToAdd[0][i].readCard()));
  145. }
  146. for (int i = 0; i<3;i++){
  147. three.addCard(cardsToAdd[1][i]);
  148. two.removeCard(two.handIndexOf(cardsToAdd[1][i].readCard()));
  149. }
  150. for (int i = 0; i<3;i++){
  151. four.addCard(cardsToAdd[2][i]);
  152. three.removeCard(three.handIndexOf(cardsToAdd[2][i].readCard()));
  153. }
  154. for (int i = 0; i<3;i++){
  155. one.addCard(cardsToAdd[3][i]);
  156. four.removeCard(four.handIndexOf(cardsToAdd[3][i].readCard()));
  157. }
  158. one.displayCards();
  159. }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment