Ejemplo n.º 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;
    }
  }
Ejemplo n.º 2
0
  public boolean isFull() {
    for (XY xy : XY.allXYs()) {
      if (this.get(xy) == ScribeMark.EMPTY) return false;
    }

    return true;
  }
Ejemplo n.º 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;
 }
Ejemplo n.º 4
0
 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;
 }
Ejemplo n.º 5
0
 public void set(int x, int y, ScribeMark mark) {
   set(XY.at(x, y), mark);
 }
Ejemplo n.º 6
0
 MiniGrid() {
   data = new ScribeMark[3][3];
   for (XY xy : XY.allXYs()) {
     data[xy.y][xy.x] = ScribeMark.EMPTY;
   }
 }
Ejemplo n.º 7
0
 @Override
 public int hashCode() {
   return XY.hash(this.x, this.y);
 }
Ejemplo n.º 8
0
 public static XY at(int x, int y) {
   return map.get(XY.hash(x, y));
 }
Ejemplo n.º 9
0
 static {
   for (XY xy : createAllXYs()) {
     map.put(xy.hashCode(), xy);
   }
 }