/src/edu/calpoly/Stone.java

http://cpe480-gobot.googlecode.com/ · Java · 178 lines · 116 code · 22 blank · 40 comment · 19 complexity · 7a9f1c383495a885b46c2fd008cb342f MD5 · raw file

  1. package edu.calpoly;
  2. public class Stone {
  3. public static final int EMPTY = 0;
  4. public static final int WHITE = 1;
  5. public static final int BLACK = 2;
  6. private int state;
  7. private boolean inAtari;
  8. private boolean isLegal;
  9. private int initSides;
  10. private int freeSides;
  11. private int blacks; //Number of its neighbors that are black
  12. private int whites; //Number of its neighbors that are white
  13. // These 2 ints above are only updated in getFreeSidesReverse since we don't care for neighbor colors for regular stones/groups.
  14. private int x,y;
  15. private Stone[] neighbors = new Stone[4];
  16. public void setNeighbors(Stone[] neigh){
  17. neighbors = neigh;
  18. }
  19. public int getX(){
  20. return x;
  21. }
  22. public int getY(){
  23. return y;
  24. }
  25. public Stone(int x, int y){
  26. this.x = x;
  27. this.y = y;
  28. state = EMPTY;
  29. inAtari = false;
  30. isLegal = true;
  31. initSides = 4;
  32. freeSides = 4;
  33. }
  34. public String toString()
  35. {
  36. if(state == EMPTY) return ".";
  37. else if(state == WHITE) return "w";
  38. else return "b";
  39. }
  40. /**
  41. * Checks whether or not a stone is of the state empty
  42. * @return true if the stone is in empty state
  43. */
  44. public boolean isEmpty() {
  45. return state == EMPTY;
  46. }
  47. /**
  48. * Returns the state, Stone.EMPTY, Stone.BLACK, or Stone.WHITE
  49. * @return the state of the stone
  50. */
  51. public int getState() {
  52. return state;
  53. }
  54. /**
  55. * Sets whether or not a stone is a legal position to play on
  56. * @param isLegal true if the position is legal
  57. */
  58. public void setLegal(boolean isLegal) {
  59. this.isLegal = isLegal;
  60. }
  61. /**
  62. * Returns whether or not a stone is a legal position to play on.
  63. * @return true if the position is legal
  64. */
  65. public boolean isLegal() {
  66. return isLegal;
  67. }
  68. /**
  69. * Sets whether or not a stone/position is in atari
  70. * @param inAtari true if the stone/position is in atari
  71. */
  72. public void setInAtari(boolean inAtari) {
  73. this.inAtari = inAtari;
  74. }
  75. /**
  76. * Returns whether or not the stone/position is in atari
  77. * @return true if the stone/position is in atari
  78. */
  79. public boolean isInAtari() {
  80. return inAtari;
  81. }
  82. /**
  83. * Sets the number of free sides for the stone/position
  84. *
  85. * @param The number of free sides the stone/position has.
  86. */
  87. public void setInitSides(int sides){
  88. freeSides = initSides = sides;
  89. }
  90. public void setFreeSides(int sides){
  91. freeSides = sides;
  92. }
  93. /**
  94. * @return The number of free sides for the stone/position.
  95. */
  96. public int getFreeSides(){
  97. int freeSides = 4;
  98. for(int i = 0; i < 4; i++){
  99. if(neighbors[i] == null || !neighbors[i].isEmpty())
  100. freeSides--;
  101. }
  102. return freeSides;
  103. }
  104. /**
  105. * This is backwards. Empty spaces don't account for free sides, colored intersections do.
  106. *
  107. * @return The number of free sides for the stone/position.
  108. */
  109. public int getFreeSidesReverse(){
  110. int freeSides = 4;
  111. blacks = 0;
  112. whites = 0;
  113. for(int i = 0; i < 4; i++){
  114. if(neighbors[i] == null || neighbors[i].isEmpty())
  115. freeSides--;
  116. else{
  117. if (neighbors[i].getState() == WHITE){
  118. whites++;
  119. }
  120. else{
  121. blacks++;
  122. }
  123. }
  124. }
  125. return freeSides;
  126. }
  127. // Two very racist sounding methods.
  128. public int getBlackNeighbors(){
  129. return blacks;
  130. }
  131. public int getWhiteNeighbors(){
  132. return whites;
  133. }
  134. public Stone getNeighbor(int index){
  135. return neighbors[index];
  136. }
  137. public void setColor(String color){
  138. if (0 == color.compareToIgnoreCase("w")) {
  139. state = WHITE;
  140. }
  141. else{
  142. state = BLACK;
  143. }
  144. }
  145. public void subSides(){
  146. freeSides--;
  147. }
  148. public void addSides(){
  149. freeSides++;
  150. }
  151. public void resetStone(){
  152. //freeSides = initSides;
  153. state = EMPTY;
  154. isLegal = true;
  155. }
  156. }