Can someone make some precise comment on this code and help me understand it: it
ID: 3582243 • Letter: C
Question
Can someone make some precise comment on this code and help me understand it: it is for minesweeper:
public void mousePressed(MouseEvent evt) {
int row, col;
row = findR( evt.getY() );
col = findCol( evt.getX() );
if(evt.isMetaDown()){
if(color[row][col] == clicked){
color[row][col] = bomb;
flaggedSpace++;
}
else if(color[row][col] == bomb){
color[row][col] =clicked;
flaggedSpace--;
}
}
else if(bombs[row][col]==true){
JOptionPane.showMessageDialog(this, "You Loss!");
lose=true;
}
else{
if(color[row][col] !=safe){
safeSpace++;
}
color[row][col] =safe;
}
if(safeSpace+flaggedSpace==100){
if(flaggedSpace<=10){
JOptionPane.showMessageDialog(this, "You Won!");
}
private int countMines(int r, int c) {
int counter;
counter = 0;
if(r>=0 && r<=9 && c<=9 && c>=0){
if(r-1>=0 && c-1>=0){
if (bombs[r-1][c-1]==true){
counter++;
}
}
if(r-1>=0 && c+1<=9){
if (bombs[r-1][c+1]==true){
counter++;
}
}
if(r-1>=0){
if (bombs[r-1][c]==true){
counter++;
}
}if(c-1>=0){
if (bombs[r][c-1]==true){
counter++;
}
}
if(c+1<=9){
if (bombs[r][c+1]==true){
counter++;
}
}
if(r+1<=9 && c-1>=0){
if (bombs[r+1][c-1]==true){
counter++;
}
}
if(r+1<=9 &&c+1<=9){
if (bombs[r+1][c+1]==true){
counter++;
}
}
if(r+1<=9){
if (bombs[r+1][c]==true){
counter++;
}
}
}
return counter;
}
private void defineMines(){
int counter = 0;
while (counter<10){
int r = (int)(10*Math.random());
int c = (int)(10*Math.random());
if(bombs[r][c] == false){
counter++;
bombs[r][c] = true;
}
}
for (int i=0; i<10; i++){
for(int c=0; c<10;c++){
color[i][c] =clicked;
}
}
}
public void mouseClicked(MouseEvent evt) { }
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
public void mouseReleased(MouseEvent evt) { }
}
repaint();
}
Explanation / Answer
Also there are two boards- color and bombs.
Color: denotes the positions of all colored cells ( safe moves ).
Bombs: denotes the positions of all bombs ( unsafe moves ).
We also assign the moves using the function mousePressed().
Then the game is played till the user either wins (when the user never steps/clicks on a mine-containing cell) or lose (when the user steps/clicks on a mine-containing cell). This is checked by the positions of bombs and current position.
This function returns a true if the user steps/clicks on a mine and hence he loses else if he step/click on a safe cell, then we get the count of mines surrounding that cell. We use the function countAdjacentMines() to calculate the adjacent mines. Since there can be maximum 8 surrounding cells, so we check for all 8 surrounding cells.
If there are no adjacent mines to this cell, then we recursively click/step on all the safe adjacent cells (hence reducing the time of the game-play). And if there is atleast a single adjacent mine to this cell then that count is displayed on the current cell. This is given as a hint to the player so that he can avoid stepping/clicking on the cells having mines by logic.
Also if you click on a cell having no adjacent mines (in any of the surrounding eight cells) then all the adjacent cells are automatically cleared, thus saving our time.