Question
I need help with my assignment package mines; /** * minesweeper class provides the data * and methods for a minesweper game * * Level
I need help with my assignment
package mines;
/** * minesweeper class provides the data * and methods for a minesweper game * * Level 1 - data & methods to implement * Mines, Clues, Tiles, basic Board characters, * and opening/marking tiles
* Level 2 - Additional data & methods to support game status * and extended Board characters *
* minesweeper.java * spring 2004 * */ public class Minesweeper {
/** mine and clue values, 9 - mine, 0-8 clue values * */ public int[][] mines;
/** tile values 0 - open, 1 - closed, * 2 - question, 3 - mine */ public int[][] tiles;
/** Level 2 - game status win, lose, play */ private String status;
/** default constructor * board size 9 x 9 * create mines and tile arrays * place mines * calculate clues * (*)set game status to play */ public minesweeper() { initGame(9, 9); }
/** alternate constructor * use specifies board size * create mines and tile arrays * place mines * calculate clues * (*)set game status to play * @param newRows number of rows for grid * @param newCols number of columns for grid */ public minesweeper(int newRows, int newCols) { initGame(newRows, newCols); }
/** Level 2 - game status * @return "play", "win", or "lose" */ public String getStatus() { return status; }
/** number of rows for board * @return number of rows */ public int getRows() { return mines.length; }
/** number of columns for board * @return number of columns */ public int getCols() { return mines[0].length; }
/** value of the mines array at r,c * -1 is returned if invalid r,c * @param r row index * @param c column index * @return value of mines array, -1 if invalid */ public int getMines(int r, int c) { if (validIndex(r, c)) { return mines[r][c]; } else { return -1; } }
/** value of the tiles array at r,c * -1 is returned if invalid r,c * @param r row index * @param c column index * @return value of tiles array, -1 if invalid */ public int getTiles(int r, int c) { if (validIndex(r, c)) { return tiles[r][c]; } else { return -1; } }
/** mark tile - open tile, close tile, * flag tile as mine, set tile as question mark, close tile * * Level 1 - Requirements * - invalid r,c values must be ignored * - a tile that is opened must stay open * - a tile that is marked as a flag (ie. tile[][] value 3) can not be opened * * Level 2 - Requirements * - tile values can only change when game status is "play" * - game status must be updated after a tile is opened * * @param r row index * @param c column index * @param t 0 - open, 1 - close, 2 - question, 3 - mine */ public void markTile(int r, int c, int t) { // add your code here }
/** mines array as String * @return mines array as a String */ public String toStringMines() { String result = " ";
for (int r = 0; r < mines.length; r++) { for (int c = 0; c < mines[r].length; c++) result = result + mines[r][c];
result += " "; }
return result; }
/** tiles array as String * @return mines array as a String */ public String toStringTiles() { String result = " ";
for (int r = 0; r < mines.length; r++) { for (int c = 0; c < mines[r].length; c++) result = result + tiles[r][c];
result += " "; }
return result; }
/** game board array as String * @return game board as String */ public String toStringBoard() { String result = "";
for (int r = 0; r < tiles.length; r++) { for (int c = 0; c < tiles[r].length; c++) { result += this.getBoard(r, c); } result += " "; //advance to next line }
return result; }
/** getBoard - determines current game board character for r,c position * using the value of the mines[][] and tiles[][]array * Note: Level 2 values are returned when * game is over (ie. status is "win" or "lose") * * Level 1 values * '1'-'8' opened tile showing clue value * ' ' opened tile blank * 'X' tile closed * '?' tile closed marked with ? * 'F' tile closed marked with flag * '*' mine * * Level 2 values * '-' if game lost, mine that was incorrectly flagged * '!' if game lost, mine that ended game * 'F' if game won, all mines returned with F * * @return char representing game board at r,c */ public char getBoard(int r, int c) { // add your code here return 'X'; //this line must be modified }
/** create mines & tiles array * place mines * update clues * @param newRows number of rows for grid * @param newCols number of columns for grid */ private void initGame(int newRows, int newCols) { //allocate space for mines and tiles array if ((newRows >= 1) && (newCols >= 1)) { mines = new int[newRows][newCols]; tiles = new int[newRows][newCols];
//init tiles array resetTiles();
//place mines placeMines();
//update clues calculateClues();
//set game status status = "play"; } }
/** Sets all tiles to 1 - closed */ private void resetTiles() { // add your code here }
/** places mines randomly on grid * integer value 9 represents a mine * number of mines = (1 + number of columns * number rows) / 10 * minimum number of mines = 1 */ private void placeMines() { // add your code here }
/** calculates clue values and updates * clue values in mines array * integer value 9 represents a mine * clue values will be 0 ... 8 */ private void calculateClues() { // add your code here }
/** determines if x,y is valid position * @param x row index * @param y column index * @return true if valid position on board, * false if not valid board position */ private boolean validIndex(int x, int y) { //add your code here
return true; //this line must be modified }
/** Level 2 - game won status * @return true if game won * false if game not won */ private boolean gameWon() { //add your code here return false; //this line must be modified }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started