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; } }
public boolean isFull() { for (XY xy : XY.allXYs()) { if (this.get(xy) == ScribeMark.EMPTY) return false; } return true; }
/** @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; }
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; }
public void set(int x, int y, ScribeMark mark) { set(XY.at(x, y), mark); }
MiniGrid() { data = new ScribeMark[3][3]; for (XY xy : XY.allXYs()) { data[xy.y][xy.x] = ScribeMark.EMPTY; } }
@Override public int hashCode() { return XY.hash(this.x, this.y); }
public static XY at(int x, int y) { return map.get(XY.hash(x, y)); }
static { for (XY xy : createAllXYs()) { map.put(xy.hashCode(), xy); } }