Java Implementation of the game set.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.1 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package net.jrtechs.setgame;
  2. public class Hand
  3. {
  4. private Card[] hand;
  5. Hand(Card[] temp)
  6. {
  7. hand= temp;
  8. }
  9. public void resize(int newSize, Deck deckT)
  10. {
  11. Card[] transfer = new Card[newSize];
  12. int count = 0;
  13. for(Card c: hand)
  14. {
  15. if(c != null)
  16. {
  17. transfer[count] = c;
  18. count++;
  19. }
  20. }
  21. for(int i = count; i < newSize; i++)
  22. {
  23. transfer[i] = deckT.deal();
  24. }
  25. hand = transfer;
  26. }
  27. public Card getCard(int e)
  28. {
  29. return hand[e];
  30. }
  31. public void display()
  32. {
  33. for(Card elem : hand)
  34. System.out.print(elem + " ");
  35. System.out.println("");
  36. }
  37. public Card[] getHand()
  38. {
  39. return hand;
  40. }
  41. public Card[] findSet()
  42. {
  43. Card[] match = new Card[3];
  44. //checks each possible combination of three on the hand to find a set
  45. for(Card one: hand)
  46. for(Card two: hand)
  47. for(Card three : hand)
  48. if(Card.isSet(one, two, three) && Card.equals(one, two, three))
  49. if(one.getInUse() && two.getInUse() && three.getInUse())
  50. {
  51. match[0] = one;
  52. match[1] = two;
  53. match[2] = three;
  54. return match;
  55. }
  56. return match;
  57. }
  58. public int setsLeft()
  59. {
  60. int count = 0;
  61. for(Card one: hand)
  62. for(Card two: hand)
  63. for(Card three : hand)
  64. if(Card.isSet(one, two, three) && Card.equals(one, two, three))
  65. if(one.getInUse() && two.getInUse() && three.getInUse())
  66. count++;
  67. //gets rid of all permutations
  68. return count/6;
  69. }
  70. public int location(Card find)
  71. {
  72. for(int i = 0; i < hand.length; i++)
  73. {
  74. if(hand[i] == null)
  75. {
  76. }
  77. else if(find.equals(hand[i]))
  78. {
  79. return i;
  80. }
  81. }
  82. return -1;
  83. }
  84. public void removePair(Card[] remove, Deck deckT)
  85. {
  86. if(deckT.cardsLeft() < 2)
  87. {
  88. //not enough cards in deck to replace w/ new cards
  89. for(Card c : remove)
  90. {
  91. hand[this.location(c)] = null;
  92. }
  93. }
  94. else if(hand.length > 12)
  95. {
  96. for(Card c : remove)
  97. {
  98. hand[location(c)] = null;
  99. }
  100. //condense the deck
  101. this.resize(hand.length -3, deckT);
  102. }
  103. else
  104. {
  105. //removes a set and replaces it with new cards
  106. int i = 0;
  107. Card[] replace = deckT.dealMany(remove.length);
  108. for(Card c : remove)
  109. {
  110. hand[this.location(c)] = replace[i];
  111. i++;
  112. }
  113. }
  114. }
  115. public void disableCard(Card c)
  116. {
  117. hand[this.location(c)].setInUse(false);
  118. }
  119. }