Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Looking for help to straiten out this Tile Method in the minesweeper Java game.

ID: 3716023 • Letter: L

Question

Looking for help to straiten out this Tile Method in the minesweeper Java game. I think a switch, case statement may work?? Could you please help with this method or at any case get me pointed in the right direction. Thank-you I am posting just the method and instruction for method up top and then full program below for reference.

Instructions

/** 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
Try using switch case statementlkk

* @param t 0 - open, 1 - close, 2 - question, 3 - mine

*/

public void markTile(int r, int c, int t)

{

//Validate r and c

//validate t

//set t eqaul r, c

if(validIndex(r, c))

{

if((tiles[r][c] == 0) || (t == 0 && tiles[r][c] == 3))

{

return;

}

// tile must stay open

if(t == 1)

if(!status.equals("play"))

{

tiles[r][c] = t;

// try to open a mine

if(mines[r][c] == 9 && t == 0)

{

status = "lose";

}

//recalculate status

gameWon();

}

}

}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// Full program for reference

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.util.Random;

public class minesweeper

{

public int[][] mines;

public int[][] tiles;

private String status;

//-------------------------------------------------------------

// Constructor

// create arrays,, place bombs,, init tiles,, set game status

//-------------------------------------------------------------

public minesweeper()

{

initGame(9, 9);

}

//--------------------------------------------------------------

// alternate constructor

//--------------------------------------------------------------

public minesweeper(int newRows, int newCols)

{

//if(newRows < 1)

// newRows = 1;

//if(newCols < 1)

// newCols = 1;

initGame(newRows, newCols);

}

//---------------------------------------------------------------

// returns the game status play, win, lose

//---------------------------------------------------------------

public String getStatus()

{

return status;

}

//---------------------------------------------------------------

// returns number of rows

//---------------------------------------------------------------

public int getRows()

{

return mines.length;

}

//---------------------------------------------------------------

//

//---------------------------------------------------------------

public int getCols()

{

return mines[0].length;

}

public int getMines(int r, int c)

{

if (validIndex(r, c))

{

return mines[r][c];

}

else

{

return -1;

}

}

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
Try using switch case statementlkk

* @param t 0 - open, 1 - close, 2 - question, 3 - mine

*/

public void markTile(int r, int c, int t)

{

//Validate r and c

//validate t

//set t eqaul r, c

if(validIndex(r, c))

{

if((tiles[r][c] == 0) || (t == 0 && tiles[r][c] == 3))

{

return;

}

// tile must stay open

if(t == 1)

if(!status.equals("play"))

{

tiles[r][c] = t;

// try to open a mine

if(mines[r][c] == 9 && t == 0)

{

status = "lose";

}

//recalculate status

gameWon();

}

}

}

//-------------------------------------------------------------------------------------------

// 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

//------------------------------------------------------------------------

public char getBoard(int r, int c)

{

if(status.equals("win"))

{

if(mines[r][c] == 9) //level 2

{

return 'F';

}

}

else if(status.equals("lose"))

{

if(mines[r][c]!= 9 && tiles[r][c] == 3) //level 2

return'-';

if(mines[r][c]!= 9 && tiles[r][c] == 0)

return'!';

}

else

{

if(tiles[r][c] == 0) //level 1

{

if(mines[r][c] == 0)

return' ';

else if(mines[r][c] == 9)

return'*';

else

return numToChar(mines[r][c]);

}

else if(tiles[r][c] == 1)

{

return'X';

}

else if(tiles[r][c] == 2)

{

return'?';

}

else

{

return'F';

}

}

return 0;

}

private char numToChar(int i) {

// TODO Auto-generated method stub

return 0;

}

//--------------------------------------------------------------------------------

// creates mines & tiles array,,, place mines,,, update clues

//--------------------------------------------------------------------------------

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()

{

for(int i = 0; i < tiles.length; i++)

{

for(int j = 0; j < tiles[i].length; j++)

{

tiles[i][j] = 1;

}

}

}

//----------------------------------------------------------------------------

// places mines randomly on grid integer value 9 represents a mine

//---------------------------------------------------------------------------

private void placeMines()

{

// Initialise mines to 0

int numOfMines = 0;

// While mines value does not hit the target value

while(numOfMines != (1 + getRows() * getCols()) / 10)

{

// Randomly gets a spot to put mine

int row = (int)(Math.random() * getRows());

int col = (int)(Math.random() * getCols());

// Check if spot is not already a mine

if(mines[row][col] != 9)

{

// sets spot to mine and increment mine counter

mines[row][col] = 9;

numOfMines++;

}

}

}

//------------------------------------------------------------------------------

// Calculate clue values and updates clue values in mines array

//------------------------------------------------------------------------------

private void calculateClues()

{

for(int i = 0; i < mines.length; i++)

{

for(int j = 0; j < mines[i].length; j++)

{

if(mines[i][j]!= 9)

{

int count = 0;

if(validIndex(i+1, j+1)&& mines[i+1][j+1] == 9)

count++;

if(validIndex(i, j+1)&& mines[i][j+1] == 9)

count++;

if(validIndex(i-1, j+1)&& mines[i-1][j+1] == 9)

count++;

if(validIndex(i-1, j)&& mines[i-1][j] == 9)

count++;

if(validIndex(i+1, j)&& mines[i+1][j] == 9)

count++;

if(validIndex(i-1, j-1)&& mines[i-1][j-1] == 9)

count++;

if(validIndex(i, j-1)&& mines[i][j-1] == 9)

count++;

if(validIndex(i+1, j-1)&& mines[i+1][j-1] == 9)

count++;

mines[i][j]=count;

}

}

}

}

//------------------------------------------------------------------------

// determines if x,y is valid position

//------------------------------------------------------------------------

private boolean validIndex(int x, int y)

{

if(x < 0 || x >= tiles.length || y < 0 || y >= tiles[0].length)

return false;

return true;

}

//----------------------------------------------------------------------------

// Game one status,,, return true "game won",,,, return false "not won"

//----------------------------------------------------------------------------

private boolean gameWon()

{

int mines = 0;

for(int i = 0; i < tiles[i].length; i++)

{

for(int j = 0; j < tiles[j].length; j++)

{

if(tiles[i][j] == 1 || tiles[i][j] == 2)

{

return false;

}

if(tiles[i][j] == 3)

{

mines++;

}

}

}

if(mines == (1 + tiles.length * tiles[0].length) / 10)

{

status = "win";

return true;

}

else

{

return false; //this line must be modified

}

}

}

Explanation / Answer

package listcode;
import java.util.HashSet;
import java.util.Random;
/**
* minesweeper class provides the data<br>
* and methods for a minesweper game<br>
* <br>
* Level 1 - data & methods to implement <br>
* Mines, Clues, Tiles, basic Board characters,<br>
* and opening/marking tiles
* <p>
* Level 2 - Additional data & methods to support game status<br>
* and extended Board characters<br>
* <p>
* minesweeper.java<br>
* spring 2004<br>
* <br>
*/
public class Minesweeper
{
/**
* mine and clue values, 9 - mine, 0-8 clue values
*
*/
public int[][] mines;
/**
* tile values 0 - open, 1 - closed,<br>
* 2 - question, 3 - mine
*/
public int[][] tiles;
/**
* Level 2 - game status win, lose, play
*/
private String status;
/**
* default constructor<br>
* board size 9 x 9<br>
* create mines and tile arrays<br>
* place mines<br>
* calculate clues<br>
* (*)set game status to play<br>
*/
public Minesweeper() {
initGame(9, 9);
}
/**
* alternate constructor use specifies board size<br>
* create mines and tile arrays<br>
* place mines<br>
* calculate clues<br>
* (*)set game status to play<br>
*
* @param newRows
* number of rows for grid<br>
* @param newCols
* number of columns for grid<br>
*/
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<br>
* -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<br>
*
* @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, <br>
* flag tile as mine, set tile as question mark, close tile<br>
* <br>
* Level 1 - Requirements<br>
* - invalid r,c values must be ignored<br>
* - a tile that is opened must stay open<br>
* - a tile that is marked as a flag (ie. tile[][] value 3) can not be
* opened<br>
* <br>
* Level 2 - Requirements<br>
* - tile values can only change when game status is "play"<br>
* - game status must be updated after a tile is opened<br>
* <br>
*
* @param r
* row index<br>
* @param c
* column index<br>
* @param t
* 0 - open, 1 - close, 2 - question, 3 - mine<br>
*/
public void markTile(int r, int c, int t) {

if (validIndex(r, c)) {
if ((tiles[r][c] == 0) || (t == 0 && tiles[r][c] == 3)) {
return;
}

// no tile can be closed
if(t == 1)
return;

if (!status.equals("play")) {
tiles[r][c] = t;

// if we try to open a mine
if(mines[r][c] == 9 && t==0) {
status = "lose";
return;
}
// recalculate status
gameWon();
}
}
}
/**
* 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 <br>
* using the value of the mines[][] and tiles[][]array<br>
* Note: Level 2 values are returned when <br>
* game is over (ie. status is "win" or "lose")<br>
* <br>
* <br>
* Level 1 values<br>
* '1'-'8' opened tile showing clue value<br>
* ' ' opened tile blank<br>
* 'X' tile closed<br>
* '?' tile closed marked with ?<br>
* 'F' tile closed marked with flag<br>
* '*' mine<br>
* <br>
* <br>
* Level 2 values<br>
* '-' if game lost, mine that was incorrectly flagged<br>
* '!' if game lost, mine that ended game<br>
* 'F' if game won, all mines returned with F <br>
*
* @return char representing game board at r,c
*/
public char getBoard(int r, int c) {
if (status.equals("win")) {
// level 2
if(mines[r][c] == 9) {
return 'F';
}
} else if (status.equals("lose")) {
// level 2
if(mines[r][c] != 9 && tiles[r][c] == 3)
return '-';
if(mines[r][c] != 9 && tiles[r][c] == 0)
return '!';

} else {
// level 1
if (tiles[r][c] == 0) {
if (mines[r][c] == 0)
return ' ';
else if (mines[r][c] == 9)
return '*';
else
return numToChar(mines[r][c]);
} else if (tiles[r][c] == 1) {
return 'X';
} else if (tiles[r][c] == 2) {
return '?';
} else {
return 'F';
}
}
return ' ';
}
/**
* create mines & tiles array place mines<br>
* update clues<br>
*
* @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() {
for (int i = 0; i < tiles.length; i++) {
for (int j = 0; j < tiles[i].length; j++) {
tiles[i][j] = 1;
}
}
}
/**
* places mines randomly on grid integer value 9 represents a mine<br>
* number of mines = (1 + number of columns * number rows) / 10<br>
* minimum number of mines = 1<br>
*/
private void placeMines() {
int n = (1 + tiles.length * tiles[0].length) / 10;
HashSet<Integer> set = getUniqueRandom(n, tiles.length
* tiles[0].length);
for (Integer i : set) {
int r = i / tiles[0].length;
int c = i % tiles[0].length;
mines[r][c] = 9;
}
}
private HashSet<Integer> getUniqueRandom(int n, int range) {
Random r = new Random();
HashSet<Integer> set = new HashSet<>();
while (set.size() < n) {
set.add(r.nextInt(range));
}
return set;
}
/**
* calculates clue values and updates clue values in mines array<br>
* integer value 9 represents a mine<br>
* clue values will be 0 ... 8<br>
*/
private void calculateClues() {
for (int i = 0; i < mines.length; i++) {
for (int j = 0; j < mines[i].length; j++) {
if(mines[i][j] != 9) {
int count = 0;
if(validIndex(i+1, j + 1) && mines[i+1][j+1] == 9)
count++;
if(validIndex(i, j + 1) && mines[i][j+1] == 9)
count++;
if(validIndex(i-1, j + 1) && mines[i-1][j+1] == 9)
count++;
if(validIndex(i-1, j) && mines[i-1][j] == 9)
count++;
if(validIndex(i+1, j) && mines[i+1][j] == 9)
count++;
if(validIndex(i-1, j - 1) && mines[i-1][j-1] == 9)
count++;
if(validIndex(i, j - 1) && mines[i][j-1] == 9)
count++;
if(validIndex(i+1, j - 1) && mines[i+1][j-1] == 9)
count++;

mines[i][j] = count;
}
}
}
}
/**
* 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) {
if(x < 0 || x>= tiles.length || y<0 || y >= tiles[0].length)
return false;
return true;
}
/**
* Level 2 - game won status
*
* @return true if game won false if game not won
*/
private boolean gameWon() {
int mines = 0;
for (int i = 0; i < tiles.length; i++) {
for (int j = 0; j < tiles[i].length; j++) {
if(tiles[i][j] == 1 || tiles[i][j] == 2) {
return false;
}
if(tiles[i][j] == 3) {
mines++;
}

}
}
if(mines == (1 + tiles.length * tiles[0].length) / 10) {
status = "win";
return true;
} else {
return false;
}
}
/*
* Given num between 0 and 9, return the character representation of the
* number
*/
private char numToChar(int num) {
return (char) ((char) num + '0');

}
public static void main(String args[]){
   Minesweeper m = new Minesweeper();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote