Can someone help me figure out the methods: private boolean gameWon() public cha
ID: 3540799 • Letter: C
Question
Can someone help me figure out the methods:
private boolean gameWon()
public char getBoard(int r, int c)
public void markTile(int r, int c, int t)
/**
* 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) {
// 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 <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) {
// add your code here
return 'X'; //this line must be modified
}
/** 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() {
// add your code here
}
/** 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() {
// add your code here
}
/** 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() {
// 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
}
}
Explanation / Answer
package mines;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Board extends JPanel {
private final int NUM_IMAGES = 13;
private final int CELL_SIZE = 15;
private final int COVER_FOR_CELL = 10;
private final int MARK_FOR_CELL = 10;
private final int EMPTY_CELL = 0;
private final int MINE_CELL = 9;
private final int COVERED_MINE_CELL = MINE_CELL + COVER_FOR_CELL;
private final int MARKED_MINE_CELL = COVERED_MINE_CELL + MARK_FOR_CELL;
private final int DRAW_MINE = 9;
private final int DRAW_COVER = 10;
private final int DRAW_MARK = 11;
private final int DRAW_WRONG_MARK = 12;
private int[] field;
private boolean inGame;
private int mines_left;
private Image[] img;
private int mines = 40;
private int rows = 16;
private int cols = 16;
private int all_cells;
private JLabel statusbar;
public Board(JLabel statusbar) {
this.statusbar = statusbar;
img = new Image[NUM_IMAGES];
for (int i = 0; i < NUM_IMAGES; i++) {
img[i] =
(new ImageIcon(this.getClass().getResource((i)
+ ".png"))).getImage();
}
setDoubleBuffered(true);
addMouseListener(new MinesAdapter());
newGame();
}
public void newGame() {
Random random;
int current_col;
int i = 0;
int position = 0;
int cell = 0;
random = new Random();
inGame = true;
mines_left = mines;
all_cells = rows * cols;
field = new int[all_cells];
for (i = 0; i < all_cells; i++)
field[i] = COVER_FOR_CELL;
statusbar.setText(Integer.toString(mines_left));
i = 0;
while (i < mines) {
position = (int) (all_cells * random.nextDouble());
if ((position < all_cells) &&
(field[position] != COVERED_MINE_CELL)) {
current_col = position % cols;
field[position] = COVERED_MINE_CELL;
i++;
if (current_col > 0) {
cell = position - 1 - cols;
if (cell >= 0)
if (field[cell] != COVERED_MINE_CELL)
field[cell] += 1;
cell = position - 1;
if (cell >= 0)
if (field[cell] != COVERED_MINE_CELL)
field[cell] += 1;
cell = position + cols - 1;
if (cell < all_cells)
if (field[cell] != COVERED_MINE_CELL)
field[cell] += 1;
}
cell = position - cols;
if (cell >= 0)
if (field[cell] != COVERED_MINE_CELL)
field[cell] += 1;
cell = position + cols;
if (cell < all_cells)
if (field[cell] != COVERED_MINE_CELL)
field[cell] += 1;
if (current_col < (cols - 1)) {
cell = position - cols + 1;
if (cell >= 0)
if (field[cell] != COVERED_MINE_CELL)
field[cell] += 1;
cell = position + cols + 1;
if (cell < all_cells)
if (field[cell] != COVERED_MINE_CELL)
field[cell] += 1;
cell = position + 1;
if (cell < all_cells)
if (field[cell] != COVERED_MINE_CELL)
field[cell] += 1;
}
}
}
}
public void find_empty_cells(int j) {
int current_col = j % cols;
int cell;
if (current_col > 0) {
cell = j - cols - 1;
if (cell >= 0)
if (field[cell] > MINE_CELL) {
field[cell] -= COVER_FOR_CELL;
if (field[cell] == EMPTY_CELL)
find_empty_cells(cell);
}
cell = j - 1;
if (cell >= 0)
if (field[cell] > MINE_CELL) {
field[cell] -= COVER_FOR_CELL;
if (field[cell] == EMPTY_CELL)
find_empty_cells(cell);
}
cell = j + cols - 1;
if (cell < all_cells)
if (field[cell] > MINE_CELL) {
field[cell] -= COVER_FOR_CELL;
if (field[cell] == EMPTY_CELL)
find_empty_cells(cell);
}
}
cell = j - cols;
if (cell >= 0)
if (field[cell] > MINE_CELL) {
field[cell] -= COVER_FOR_CELL;
if (field[cell] == EMPTY_CELL)
find_empty_cells(cell);
}
cell = j + cols;
if (cell < all_cells)
if (field[cell] > MINE_CELL) {
field[cell] -= COVER_FOR_CELL;
if (field[cell] == EMPTY_CELL)
find_empty_cells(cell);
}
if (current_col < (cols - 1)) {
cell = j - cols + 1;
if (cell >= 0)
if (field[cell] > MINE_CELL) {
field[cell] -= COVER_FOR_CELL;
if (field[cell] == EMPTY_CELL)
find_empty_cells(cell);
}
cell = j + cols + 1;
if (cell < all_cells)
if (field[cell] > MINE_CELL) {
field[cell] -= COVER_FOR_CELL;
if (field[cell] == EMPTY_CELL)
find_empty_cells(cell);
}
cell = j + 1;
if (cell < all_cells)
if (field[cell] > MINE_CELL) {
field[cell] -= COVER_FOR_CELL;
if (field[cell] == EMPTY_CELL)
find_empty_cells(cell);
}
}
}
public void paint(Graphics g) {
int cell = 0;
int uncover = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cell = field[(i * cols) + j];
if (inGame && cell == MINE_CELL)
inGame = false;
if (!inGame) {
if (cell == COVERED_MINE_CELL) {
cell = DRAW_MINE;
} else if (cell == MARKED_MINE_CELL) {
cell = DRAW_MARK;
} else if (cell > COVERED_MINE_CELL) {
cell = DRAW_WRONG_MARK;
} else if (cell > MINE_CELL) {
cell = DRAW_COVER;
}
} else {
if (cell > COVERED_MINE_CELL)
cell = DRAW_MARK;
else if (cell > MINE_CELL) {
cell = DRAW_COVER;
uncover++;
}
}
g.drawImage(img[cell], (j * CELL_SIZE),
(i * CELL_SIZE), this);
}
}
if (uncover == 0 && inGame) {
inGame = false;
statusbar.setText("Game won");
} else if (!inGame)
statusbar.setText("Game lost");
}
class MinesAdapter extends MouseAdapter {
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
int cCol = x / CELL_SIZE;
int cRow = y / CELL_SIZE;
boolean rep = false;
if (!inGame) {
newGame();
repaint();
}
if ((x < cols * CELL_SIZE) && (y < rows * CELL_SIZE)) {
if (e.getButton() == MouseEvent.BUTTON3) {
if (field[(cRow * cols) + cCol] > MINE_CELL) {
rep = true;
if (field[(cRow * cols) + cCol] <= COVERED_MINE_CELL) {
if (mines_left > 0) {
field[(cRow * cols) + cCol] += MARK_FOR_CELL;
mines_left--;
statusbar.setText(Integer.toString(mines_left));
} else
statusbar.setText("No marks left");
} else {
field[(cRow * cols) + cCol] -= MARK_FOR_CELL;
mines_left++;
statusbar.setText(Integer.toString(mines_left));
}
}
} else {
if (field[(cRow * cols) + cCol] > COVERED_MINE_CELL) {
return;
}
if ((field[(cRow * cols) + cCol] > MINE_CELL) &&
(field[(cRow * cols) + cCol] < MARKED_MINE_CELL)) {
field[(cRow * cols) + cCol] -= COVER_FOR_CELL;
rep = true;
if (field[(cRow * cols) + cCol] == MINE_CELL)
inGame = false;
if (field[(cRow * cols) + cCol] == EMPTY_CELL)
find_empty_cells((cRow * cols) + cCol);
}
}
if (rep)
repaint();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.