Question
2. (20 pts) When a user clicks on a cell that has a 0 count reveal adjacent cells as follows: a. (10 pts) All adjacent and co
Please use my code to implement the above instructions.
My Grid class:
import java.util.ArrayList;
import java.util.Collections;

class Grid {
private boolean bombGrid[][];
private int countGrid[][];
private int numRows;
private int numColumns;
private int numBombs;

public Grid() {
this(10, 10, 25);
}
  
public Grid(int rows, int columns) {
this(rows, columns, 25);
}
  
public Grid(int rows, int columns, int numBombs) {
this.numRows = rows;
this.numColumns = columns;
this.numBombs = numBombs;

createBombGrid();
createCountGrid();
}
  
public int getNumRows() {
return numRows;
}

public int getNumColumns() {
return numColumns;
}

public int getNumBombs() {
return numBombs;
}

public boolean[][] getBombGrid() {
boolean[][] result = new boolean[numRows][numColumns];
for (int i = 0; i < numRows; i++) {
System.arraycopy(bombGrid[i], 0, result[i], 0, bombGrid[i].length);
}
return result;
}

public int[][] getCountGrid() {
int[][] result = new int[numRows][numColumns];
for (int i = 0; i < numRows; i++) {
System.arraycopy(countGrid[i], 0, result[i], 0, countGrid[i].length);
}
return result;
}

public boolean IsBombAtLocation(int row, int column) {
return (bombGrid[row][column]);
}

public int getCountAtLocation(int row, int column) {
int count = 0;
if (IsBombAtLocation(row, column)) {
count++;
}

if (row + 1 < numRows) {
if (IsBombAtLocation(row + 1, column))
count++;
if (column + 1 < numColumns) {
if (IsBombAtLocation(row + 1, column + 1))
count++;
}

if (column - 1 >= 0) {
if (IsBombAtLocation(row + 1, column - 1))
count++;
}
}

if (row - 1 >= 0) {
if (IsBombAtLocation(row - 1, column))
count++;
if (column - 1 >= 0) {
if (IsBombAtLocation(row - 1, column - 1))
count++;
}

if (column + 1 < numColumns) {
if (IsBombAtLocation(row - 1, column + 1))
count++;
}

}

if (column + 1 < numColumns) {
if (IsBombAtLocation(row, column + 1))
count++;
}

if (column - 1 >= 0) {
if (IsBombAtLocation(row, column - 1))
count++;
}
return count;
}

private void createBombGrid() {
bombGrid = new boolean[numRows][numColumns];
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numColumns; j++) {
bombGrid[i][j] = false;
}
}

ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 1; i < numRows * numColumns; i++) {
list.add(new Integer(i));
}

Collections.shuffle(list);
for (int i = 0; i < numBombs; i++) {
int number = (list.get(i));
int row = new Integer(number / numColumns);
int column = new Integer(number % numColumns);

bombGrid[row][column] = true;
}
}

private void createCountGrid() {
countGrid = new int[numRows][numColumns];
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numColumns; j++) {
countGrid[i][j] = getCountAtLocation(i, j);
}
}
};


public static void main(String args[]) {
Grid minesweep = new Grid();

int rows = minesweep.getNumRows();

int columns = minesweep.getNumColumns();

boolean[][] bombgrid = minesweep.getBombGrid();

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

System.out.print(bombgrid[i][j] ? "T" : "F");

}

System.out.println("");

}

System.out.println("");

int[][] countgrid = minesweep.getCountGrid();

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

System.out.print(countgrid[i][j] + "");

}

System.out.println("");
}
};

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program File

filename: Grid.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.ImageIcon;
import java.awt.Color;
import javax.swing.*;

public class Grid {

// data members
private boolean[][] bombGrid;
private int[][] countGrid;
private int numRows;
private int numColumns;
private int numBombs;
static int numToWin = 0;
static int numFlags = -1;
static int lost = 0;
static boolean playAgain = true;
static int numWins = 0;

/**
* Constructor
*/
public Grid() {
this.numRows = 10;
this.numColumns = 10;
this.bombGrid = new boolean[10][10];
this.countGrid = new int[10][10];
this.numBombs = 25;
createBombGrid();
createCountGrid();
}

/**
* Constructor
*/
public Grid(int rows, int columns) {
this.numRows = rows;
this.numColumns = columns;
this.bombGrid = new boolean[rows][columns];
this.countGrid = new int[rows][columns];
this.numBombs = 25;
createBombGrid();
createCountGrid();
}

/**
* Constructor
*/
public Grid(int rows, int columns, int numBombs) {
this.numRows = rows;
this.numColumns = columns;
this.bombGrid = new boolean[rows][columns];
this.countGrid = new int[rows][columns];
this.numBombs = numBombs;
createBombGrid();
createCountGrid();
}

/**
* creates a bomb grid
*/
public void createBombGrid() {
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numColumns; j++) {
bombGrid[i][j] = false;
}
}
for (int i = 0; i < numBombs; i++) {
int rand1 = (int) (Math.random() * numRows);
int rand2 = (int) (Math.random() * numColumns);
if (bombGrid[rand1][rand2]) {
i--;
} else {
bombGrid[rand1][rand2] = true;
}
}
}

/**
* returns count at a given location
* @param row
* @param column
* @return
*/
public int getCountAtLocation(int row, int column) {
int bombCount = 0;
if (row > 0 && column < numColumns - 1 && bombGrid[row - 1][column + 1] == true) {
bombCount++;
}
if (column < numColumns - 1 && bombGrid[row][column + 1] == true) {
bombCount++;
}
if (row < numRows - 1 && column < numColumns - 1 && bombGrid[row + 1][column + 1] == true) {
bombCount++;
}
if (row > 0 && bombGrid[row - 1][column] == true) {
bombCount++;
}
if (bombGrid[row][column] == true) {
bombCount++;
}
if (row < numRows - 1 && bombGrid[row + 1][column] == true) {
bombCount++;
}
if (row > 0 && column > 0 && bombGrid[row - 1][column - 1] == true) {
bombCount++;
}
if (column > 0 && bombGrid[row][column - 1] == true) {
bombCount++;
}
if (row < numRows - 1 && column > 0 && bombGrid[row + 1][column - 1] == true) {
bombCount++;
}
return bombCount;
}

/**
* creates count grid
*/
public void createCountGrid() {
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numColumns; j++) {
countGrid[i][j] = getCountAtLocation(i, j);
}
}
}

/**
* returns numRows
* @return
*/
public int getNumRows() {
return this.numRows;
}

/**
* returns numColumns
* @return
*/
public int getNumColumns() {
return this.numColumns;
}

/**
* returns numBombs
*/
public int getNumBombs() {
return this.numBombs;
}

/**
* returns bombGrid
* @return
*/
public boolean[][] getBombGrid() {
return this.bombGrid;
}

/**
* returns countGrid
* @return
*/
public int[][] getCountGrid() {
return this.countGrid;
}

/**
* return whether bomb is at a given location
* @param row
* @param column
* @return
*/
public boolean isBombAtLocation(int row, int column) {
return bombGrid[row][column];
}

/**
* creates a new game
*/
public static void createGame() {
Grid bombGrid = new Grid(14, 14, 40);
JFrame appFrame = new JFrame();
appFrame.setSize(bombGrid.getNumColumns() * 60 + 150, bombGrid.getNumRows() * 60);
appFrame.setTitle("Grid");
appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField tf = new JTextField();
for (int i = 0; i < bombGrid.getNumRows(); i++) {
for (int j = 0; j < bombGrid.getNumColumns(); j++) {
JButton label = new JButton();
label.setBounds(i * 50, j * 50, 50, 50);
label.setVisible(true);
if (i == bombGrid.getNumRows() || j == bombGrid.getNumColumns()) {
label.setVisible(false);
}
appFrame.add(label);
appFrame.setLayout(null);
label.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ImageIcon redFlag = new ImageIcon("redflag.png");
JButton flag = new JButton(redFlag);
flag.setIcon(redFlag);
flag.setBounds(label.getLocation().x, label.getLocation().y, 50, 50);
flag.setVisible(true);
if (lost == 0) {
appFrame.add(flag);
label.setVisible(false);
}
numFlags++;
System.out.println("Number of mines identified: " + numFlags + "/" + bombGrid.getNumBombs());
if (lost == 0) {
flag.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
flag.setVisible(false);
JButton dig = new JButton();
dig.setBounds(label.getLocation().x, label.getLocation().y, 50, 50);
dig.setVisible(true);
appFrame.add(dig);
if (bombGrid.isBombAtLocation((int) (label.getLocation().y / 50), (int) (label.getLocation().x / 50))) {
dig.setText("Bomb");
dig.setVisible(true);
dig.setBackground(Color.RED);
dig.setOpaque(true);
JFrame mainFrame = new JFrame("Java Swing Examples");
mainFrame.setSize(400,400);
JFrame f;
f=new JFrame();
int input = JOptionPane.showOptionDialog(null, "You lost. Would you like to play again?", "Game Over", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
if(input == JOptionPane.OK_OPTION)
{
appFrame.setVisible(false);
createGame();
}
} else {
numFlags--;
dig.setText(Integer.toString(bombGrid.getCountAtLocation((int) (label.getLocation().y / 50), (int) (label.getLocation().x / 50))));
dig.setBackground(Color.GREEN);
dig.setOpaque(true);
System.out.println("NumToWin = " + numToWin);
if (numToWin == bombGrid.getNumColumns() * bombGrid.getNumRows() - bombGrid.getNumBombs()) {
JFrame mainFrame = new JFrame("Java Swing Examples");
mainFrame.setSize(400,400);
JFrame f;
f=new JFrame();
int input = JOptionPane.showOptionDialog(null, "You won! Would you like to play again?", "Game Over", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
if(input == JOptionPane.OK_OPTION)
{
appFrame.setVisible(false);
createGame();
}
numWins++;
}
}
}
});
}
}
});
}
}
appFrame.setVisible(true);

}

/**
* main method starts a game
*/
public static void main(String[] args) {
createGame();
}

}

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.lang.*; import javax.swing.JButton; impo

this.numRows = rows; this.numColumns = columns; this.bombGrid = new boolean[rows][columns]; this.countGrid = new int[rows][co

109 110 111 if (row > 0 && column > 0 && bombGrid[row - 1][column - 1] == true) { bombCount++; if (column > 0 && bombGrid[row

* returns countGrid * @return 163 164 165 166 167 168 169 170 171 public int[][] getCountGrid() { return this.countGrid; 172

public void actionPerformed(ActionEvent e) { flag.setVisible(false); JButton dig = new JButton(); dig.setBounds (label.getLoc

269 * main method starts a game 272 putih public static void main(String[] args) { createGame(); tekem volta main(stringt ara

Output

Grid Game Over (i You lost. Would you like to play again? Ok Cancel

Hope this helps!

Please let me know if any changes needed.

Thank you!

I hope you're safe during the pandemic.

Add a comment
Know the answer?
Add Answer to:
Please use my code to implement the above instructions. My Grid class: import java.util.ArrayList; import java.util.Collections;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private...

    import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private ArrayList<String> words; private HashSet<String> foundWords = new HashSet<String>(); public FindWordInMaze(char[][] grid) { this.grid = grid; this.words = new ArrayList<>();    // add dictionary words words.add("START"); words.add("NOTE"); words.add("SAND"); words.add("STONED");    Collections.sort(words); } public void findWords() { for(int i=0; i<grid.length; i++) { for(int j=0; j<grid[i].length; j++) { findWordsFromLocation(i, j); } }    for(String w: foundWords) { System.out.println(w); } } private boolean isValidIndex(int i, int j, boolean visited[][]) { return...

  • draw a flew chart for this code // written by Alfuzan Mohammed package matreix; import java.util.Scanner;...

    draw a flew chart for this code // written by Alfuzan Mohammed package matreix; import java.util.Scanner; public class Matrix {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);    System.out.print("Enter number of rows: first matrix ");    int rows = scanner.nextInt();    System.out.print("Enter number of columns first matrix: ");    int columns = scanner.nextInt();    System.out.print("Enter number of rows: seconed matrix ");    int rowss = scanner.nextInt();    System.out.print("Enter number of columns seconed matrix:...

  • In Java; Given numRows and numColumns, print a list of all seats in a theater. Rows...

    In Java; Given numRows and numColumns, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E. Print a space after each seat, including after the last. Use separate print statements to print the row and column. Ex: numRows = 2 and numColumns = 3 prints: 1A 1B 1C 2A 2B 2C import java.util.Scanner; public class NestedLoops { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); int...

  • Can you help me with this code in Java??? import java.util.Scanner; public class Main { public...

    Can you help me with this code in Java??? import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int rows = 3; int columns = 4; int[][] arr = new int[rows][columns]; for(int i = 0; i < rows; ++i) { for(int j = 0; j < columns; ++j) { System.out.println("Enter a value: "); arr[i][j] = scan.nextInt(); } } System.out.println("The entered matrix:"); for(int i = 0; i < rows; ++i) { for(int j...

  • (Java Zybooks) Given numRows and numColumns, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E. Print a space after each seat, including after the last. Use s...

    (Java Zybooks) Given numRows and numColumns, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E. Print a space after each seat, including after the last. Use separate print statements to print the row and column. Ex: numRows = 2 and numColumns = 3 prints: 1A 1B 1C 2A 2B 2C import java.util.Scanner; public class NestedLoops { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); int...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • JAVA QUESTION!!! please help! The following codes are a Maze program. There are TWO traversable paths...

    JAVA QUESTION!!! please help! The following codes are a Maze program. There are TWO traversable paths possible in this maze. The program uses recursion. Can someone please explain to me why the program will pick one path if multiple traversable paths are available? Why does the program choose one path over a different path? (I believe it has something to do with the recursion terminating when a solution is found but I'm not sure.) Thank you!!!! The codes: public class...

  • cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /**...

    cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /** * default constructor -- * Creates an matrix that has 2 rows and 2 columns */ public Matrix(int [][] m) { boolean valid = true; for(int r = 1; r < m.length && valid; r++) { if(m[r].length != m[0].length) valid = false; } if(valid) matrix = m; else matrix = null; } public String toString() { String output = "[ "; for (int i...

  • JAVA Only Help on the sections that say Student provide code. The student Provide code has...

    JAVA Only Help on the sections that say Student provide code. The student Provide code has comments that i put to state what i need help with. import java.util.Scanner; public class TicTacToe {     private final int BOARDSIZE = 3; // size of the board     private enum Status { WIN, DRAW, CONTINUE }; // game states     private char[][] board; // board representation     private boolean firstPlayer; // whether it's player 1's move     private boolean gameOver; // whether...

  • Solver.java package hw7; import java.util.Iterator; import edu.princeton.cs.algs4.Graph; import edu.princeton.cs.algs4.BreadthFirstPaths; public class Solver {    public static...

    Solver.java package hw7; import java.util.Iterator; import edu.princeton.cs.algs4.Graph; import edu.princeton.cs.algs4.BreadthFirstPaths; public class Solver {    public static String solve(char[][] grid) {        // TODO        /*        * 1. Construct a graph using grid        * 2. Use BFS to find shortest path from start to finish        * 3. Return the sequence of moves to get from start to finish        */               // Hardcoded solution to toyTest        return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT