예제 #1
0
  private void updateRegions(XY xy, ScribeMark mark) {
    List<Region> regionsContainingNewSquare = new ArrayList<Region>();

    for (Region region : this.regions) {
      for (XY neighbor : xy.neighbors()) {
        if (this.get(neighbor) == mark && region.contains(neighbor)) {
          region.add(xy);
          regionsContainingNewSquare.add(region);
          break;
        }
      }
    }

    switch (regionsContainingNewSquare.size()) {
      case 0:
        Region newRegion = new Region(xy, mark);
        this.regions.add(newRegion);
        break;
      default:
        /*
         * merge all the regions into one
         */
        Region mergedRegion = regionsContainingNewSquare.remove(0);
        for (Region region : regionsContainingNewSquare) {
          this.regions.remove(region);
          mergedRegion.addAll(region);
        }
        break;
    }
  }
예제 #2
0
  public boolean isFull() {
    for (XY xy : XY.allXYs()) {
      if (this.get(xy) == ScribeMark.EMPTY) return false;
    }

    return true;
  }
예제 #3
0
 /** @return A list of all the positions on this MiniGrid that are still empty. */
 public List<XY> getEmptyCells() {
   List<XY> emptyCells = new ArrayList<XY>();
   for (XY xy : XY.allXYs()) {
     if (this.get(xy) == ScribeMark.EMPTY) {
       emptyCells.add(xy);
     }
   }
   return emptyCells;
 }
예제 #4
0
파일: XY.java 프로젝트: rvganesh/Scribe4
 public Collection<XY> neighbors() {
   Collection<XY> neighbors = new ArrayList<XY>(4);
   for (int i : new int[] {-1, 0, 1}) {
     for (int j : new int[] {-1, 0, 1}) {
       if (i == 0 ^ j == 0) {
         XY neighbor = XY.at(x + i, y + j);
         if (neighbor != null) neighbors.add(neighbor);
       }
     }
   }
   return neighbors;
 }
예제 #5
0
 public void set(int x, int y, ScribeMark mark) {
   set(XY.at(x, y), mark);
 }
예제 #6
0
 MiniGrid() {
   data = new ScribeMark[3][3];
   for (XY xy : XY.allXYs()) {
     data[xy.y][xy.x] = ScribeMark.EMPTY;
   }
 }
예제 #7
0
파일: XY.java 프로젝트: rvganesh/Scribe4
 @Override
 public int hashCode() {
   return XY.hash(this.x, this.y);
 }
예제 #8
0
파일: XY.java 프로젝트: rvganesh/Scribe4
 public static XY at(int x, int y) {
   return map.get(XY.hash(x, y));
 }
예제 #9
0
파일: XY.java 프로젝트: rvganesh/Scribe4
 static {
   for (XY xy : createAllXYs()) {
     map.put(xy.hashCode(), xy);
   }
 }