The TextBoard class

  1: /**
  2:  * Simple extension of class Board that provides a textual representation of
  3:  * the MineSweeper game board on System.out.
  4:  */
  5: public class TextBoard extends Board {
  6: 
  7:   // Maximum length of string to hold column/row numbers
  8:   private int colLength, rowLength;
  9: 
 10:   // Printable versions of column and row numbers
 11:   private String[] colNums, rowNums;
 12: 
 13:   // Spacing for column-header lines
 14:   private String spacer;
 15: 
 16: 
 17:   /**
 18:    * Create a new TextBoard of size width*height and containing numMines
 19:    * mines.
 20:    */
 21:   public TextBoard(int width, int height, int numMines) {
 22:     // Don't forget to initialise the superclass!
 23:     super(width, height, numMines);
 24: 
 25:     // Allocate storage for column and row number, and work out how long the
 26:     // respective strings need to be.  Note that the largest column/row number 
 27:     // is actually one less than width/height, respectivaly.
 28:     colLength = Integer.toString(width-1).length();
 29:     rowLength = Integer.toString(height-1).length();
 30:     colNums = new String[width];
 31:     rowNums = new String[height];
 32: 
 33:     // Generate column numbers.  These are all padded out to colLength for
 34:     // ease of printing later on.  The numbers are right-justified within the
 35:     // string.
 36:     for (int i = 0; i < width; i++) {
 37:       StringBuffer col = new StringBuffer(Integer.toString(i));
 38:       while (col.length() < colLength) {
 39:         col.insert(0, ' ');
 40:       }
 41:       colNums[i] = col.toString();
 42:     }
 43: 
 44:     // Generate a spacer for column numbers.  Just a string of spaces that
 45:     // takes up the same space as a row number, for aligning the column
 46:     // headers correctly.
 47:     StringBuffer spaces = new StringBuffer();
 48:     for (int i = 0; i < rowLength + 2; i++) {
 49:       spaces.append(' ');
 50:     }
 51:     spacer = spaces.toString();
 52: 
 53:     // Generate row numbers.  Exactly the same procedure as the column
 54:     // numbers, except that we are extra spaces around the number to make the
 55:     // display more readable.
 56:     for (int i = 0; i < height; i++) {
 57:       StringBuffer row = new StringBuffer(Integer.toString(i));
 58:       while (row.length() <= rowLength) {
 59:         row.insert(0, ' ');
 60:       }
 61:       row.append(' ');
 62:       rowNums[i] = row.toString();
 63:     }
 64:   }
 65: 
 66: 
 67:   /**
 68:    * Draw the current state of the board on System.out, as follows:
 69:    *    - An unknown cell is indicated by '#'
 70:    *    - A marked cell is indicated by 'X'
 71:    *    - A revealed mine is indicated by '*'
 72:    *    - A revealed empty cell contains a digit indicating the number of
 73:    *      immediate neighbours containing mines, or '.' if there are no such
 74:    *      neighbours.
 75:    */
 76:   public void draw() {
 77: 
 78:     // Some space
 79:     System.out.println();
 80: 
 81:     // Do column numbers.  We print these out vertically, 'bottom-justified'.
 82:     // Note the use of the spacer defined in the constructor.
 83:     for (int i = 0; i < colLength; i++) {
 84:       System.out.print(spacer);
 85:       for (int j = 0; j < width; j++) {
 86:         System.out.print(colNums[j].charAt(i));
 87:       }
 88:       System.out.println();
 89:     }
 90: 
 91:     // Some more space
 92:     System.out.println();
 93: 
 94:     // Do rows, complete with numbers
 95:     for (int i = 0; i < height; i++) {
 96:       // Print row number.  We do this again at the end of the row in case the 
 97:       // board is huge and hard to read.
 98:       System.out.print(rowNums[i]);
 99: 
100:       // Print something appropriate for the cell
101:       for (int j = 0; j < width; j++) {
102:         switch (board[j][i]) {
103:          case UNKNOWN:
104:           System.out.print("#");
105:           break;
106:          case MARKED:
107:           System.out.print("X");
108:           break;
109:          case MINE:
110:           System.out.print("*");
111:           break;
112:          case 1:
113:          case 2:
114:          case 3:
115:          case 4:
116:          case 5:
117:          case 6:
118:          case 7:
119:          case 8:
120:          case 9:
121:           System.out.print(board[j][i]);
122:           break;
123:          case 0:
124:           System.out.print(".");
125:           break;
126:         }
127:       }
128:       System.out.println(rowNums[i]);
129:     }
130: 
131:     // Some more space
132:     System.out.println();
133: 
134:     // Do column numbers, again in case the board is humongous and hard to
135:     // read.
136:     for (int i = 0; i < colLength; i++) {
137:       System.out.print(spacer);
138:       for (int j = 0; j < width; j++) {
139:         System.out.print(colNums[j].charAt(i));
140:       }
141:       System.out.println();
142:     }
143: 
144:     // Some more space (surprise)
145:     System.out.println();
146: 
147:     // Display the number of mines left to find.  This number reflects the
148:     // number of marked mines, any of which might of course be wrong!
149:     System.out.println("Mines remaining: " + (getMines() - getMarked()));
150: 
151:     // Yet more spacing
152:     System.out.println();
153:   }
154: }
    

Scott Mitchell
Last modified: Thu Nov 5 21:34:29 GMT 1998