public class HexBoard extends CharMatrix{ public HexBoard(int rows, int cols) { super(rows,cols); } public boolean isBlack(int r, int c) { return charAt(r,c) == 'B'; } public boolean isWhite(int r, int c) { return charAt(r,c) == 'W'; } public boolean isGray(int r, int c) { return charAt(r,c) == 'X'; } public void setBlack(int r, int c) { setCharAt(r,c,'B'); } public void setWhite(int r, int c) { setCharAt(r,c,'W'); } public void setGray(int r, int c) { setCharAt(r,c,'X'); } /** * Returns true if there is a contiguous chain of black stones * that starts in col 0 and ends in the last column of the board; * otherwise returns false. */ public boolean blackHasWon() { int i, j; boolean ret = false; for(i = 0; i < numRows(); i++) { areaFill(i,0); } for(i = 0; i < numRows(); i++) { if(isGray(i,numCols()-1)) ret = true; } for(i = 0; i < numRows(); i++) for(j = 0; j < numCols(); j++) if(isGray(i,j)) setBlack(i,j); return ret; } /** * Fills the contiguous area that contains r,c with gray color. * Does nothing if r, c is out of bounds or is not black. */ public void areaFill(int r, int c) { if(isInBounds(r,c) && isBlack(r,c)) { setGray(r,c); areaFill(r-1,c-1); areaFill(r-1,c); areaFill(r,c-1); areaFill(r,c+1); areaFill(r+1,c); areaFill(r+1,c+1); } } public String toString() { String s = ""; for (int r = 0; r < numRows(); r++) { for (int c = 0; c < numCols(); c++) { if (isBlack(r, c)) s += 'B'; else if (isWhite(r, c)) s += 'W'; else if (isGray(r, c)) s += 'X'; else s += '.'; } s += '\n'; } return s; } //**************************************************************** private boolean isInBounds(int row, int col) { if(row < 0 || col < 0 || row >= numRows() || col >= numCols()) return false; return true; }}