PageRenderTime 29ms CodeModel.GetById 2ms app.highlight 21ms RepoModel.GetById 2ms app.codeStats 0ms

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