/** * This class implements the board for the game MineSweeper. * * @author John Paxton * @version 1.0 */ public class GameBoard { GameBoard () { board = new Tile [SIZE + 2][SIZE + 2]; } public void initialize() { for (int row = 0; row < SIZE + 2; row++) { for (int col = 0; col < SIZE + 2; col++) { board[row][col] = new Tile(); } } placeMines(); for (int row = 1; row <= SIZE; row++) { for (int col = 1; col <= SIZE; col++) { board[row][col].setSurroundingMines( calculateMines (row, col) ); } } } private void placeMines() { board[1][1].setMine(); board[2][2].setMine(); } private int calculateMines (int row, int col) { int answer = 0; for (int r = row - 1; r <= row + 1; r++) { for (int c = col -1; c <= col + 1; c++) { if ( board[r][c].getMine() ) { answer++; } } } return answer; } public void print () { for (int row = 1; row <= SIZE; row++) { printLine(); for (int col = 1; col <= SIZE; col++) { System.out.print("| " + board[row][col].getPrintString() + " "); } System.out.println("|"); } printLine(); } private void printLine() { for (int i = 0; i < SIZE; i++) { System.out.print("+---"); } System.out.println("+"); } private static final int SIZE = 3; private static final int MINES = 2; private Tile [][] board; }