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.

158 lines
4.2 KiB

8 years ago
  1. package net.jrtechs.setgame;
  2. public class Card
  3. {
  4. //color,number, shape, shading
  5. private final int number; //0,1,2
  6. private final int color; //0-red,1-blue,2-green
  7. private final int shape; //0-square,1-triangle,2-circle
  8. private final int shading; //0-solid, 1-striped, 2-clear
  9. private boolean inUse = true;
  10. //constructor
  11. Card(int num, int col, int shap, int shad)
  12. {
  13. number = num;
  14. color = col;
  15. shape = shap;
  16. shading = shad;
  17. }
  18. public int getNumber()
  19. {
  20. return number;
  21. }
  22. public int getColor()
  23. {
  24. return color;
  25. }
  26. public int getShape()
  27. {
  28. return shape;
  29. }
  30. public int getShading()
  31. {
  32. return shading;
  33. }
  34. public boolean getInUse()
  35. {
  36. return inUse;
  37. }
  38. public void setInUse(boolean change)
  39. {
  40. inUse = change;
  41. }
  42. public String toString()
  43. {
  44. String dis = new String();
  45. switch(number)
  46. {
  47. case 0: dis = "1";
  48. break;
  49. case 1: dis = "2";
  50. break;
  51. case 2: dis = "3";
  52. }
  53. switch(color)
  54. {
  55. case 0: dis += " red";
  56. break;
  57. case 1: dis += " blue";
  58. break;
  59. case 2: dis += " green";
  60. }
  61. switch(shape)
  62. {
  63. case 0: dis += " square";
  64. break;
  65. case 1: dis += " triangle";
  66. break;
  67. case 2: dis += " circle";
  68. }
  69. switch(shading)
  70. {
  71. case 0: dis += " solid";
  72. break;
  73. case 1: dis += " striped";
  74. break;
  75. case 2: dis += " clear";
  76. }
  77. return dis;
  78. }
  79. public boolean equals(Card check)
  80. {
  81. if(check.shape == this.shape && check.color == this.color && check.number == this.number && check.shading == this.shading)
  82. {
  83. return true;
  84. }
  85. return false;
  86. }
  87. public static boolean equals(Card one, Card two, Card three)
  88. {
  89. if(one.equals(two) || one.equals(three) || two.equals(three))
  90. {
  91. return false;
  92. }
  93. return true;
  94. }
  95. //compares the colors of three cards
  96. public static boolean compare_color(Card one, Card two, Card three)
  97. {
  98. if(one.color == two.color && one.color == three.color)
  99. {
  100. return true;
  101. }
  102. else if(one.color != two.color && one.color != three.color && two.color != three.color)
  103. {
  104. return true;
  105. }
  106. return false;
  107. }
  108. //compares the number of three cards
  109. public static boolean compare_number(Card one, Card two, Card three)
  110. {
  111. if(one.number == two.number && one.number == three.number)
  112. {
  113. return true;
  114. }
  115. else if(one.number != two.number && one.number != three.number && two.number != three.number)
  116. {
  117. return true;
  118. }
  119. return false;
  120. }
  121. //compares the shading of the three cards
  122. public static boolean compare_shading(Card one, Card two, Card three)
  123. {
  124. if(one.shading == two.shading && one.shading == three.shading)
  125. {
  126. return true;
  127. }
  128. else if(one.shading != two.shading && one.shading != three.shading && two.shading != three.shading)
  129. {
  130. return true;
  131. }
  132. return false;
  133. }
  134. //checks to see if three cards have the same shale
  135. public static boolean compare_shape(Card one, Card two, Card three)
  136. {
  137. if(one.shape == two.shape && one.shape == three.shape)
  138. {
  139. return true;
  140. }
  141. else if(one.shape != two.shape && one.shape != three.shape && two.shape != three.shape)
  142. {
  143. return true;
  144. }
  145. return false;
  146. }
  147. //checks to see if three cards are a set
  148. public static boolean isSet(Card one, Card two, Card three)
  149. {
  150. if(Card.compare_color(one, two, three) && Card.compare_number(one, two, three) && Card.compare_shading(one, two, three) && Card.compare_shape(one, two, three))
  151. {
  152. return true;
  153. }
  154. return false;
  155. }
  156. }