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

![this.numRows = rows; this.numColumns = columns; this.bombGrid = new boolean[rows][columns]; this.countGrid = new int[rows][co](http://img.homeworklib.com/questions/22f91da0-8952-11eb-8bae-f1bf3d7e2d43.png?x-oss-process=image/resize,w_560)
![109 110 111 if (row > 0 && column > 0 && bombGrid[row - 1][column - 1] == true) { bombCount++; if (column > 0 && bombGrid[row](http://img.homeworklib.com/questions/237856a0-8952-11eb-8bae-0f0c5cfd1dc0.png?x-oss-process=image/resize,w_560)
![* returns countGrid * @return 163 164 165 166 167 168 169 170 171 public int[][] getCountGrid() { return this.countGrid; 172](http://img.homeworklib.com/questions/23d8b010-8952-11eb-bb0a-e980c96a4f08.png?x-oss-process=image/resize,w_560)

![269 * main method starts a game 272 putih public static void main(String[] args) { createGame(); tekem volta main(stringt ara](http://img.homeworklib.com/questions/24cc9bb0-8952-11eb-8e7d-5b6fb617d6d1.png?x-oss-process=image/resize,w_560)
Output

Hope this helps!
Please let me know if any changes needed.
Thank you!
I hope you're safe during the pandemic.
Please use my code to implement the above instructions. My Grid class: import java.util.ArrayList; import java.util.Collections;...
I need help with the last method listed in the problem: Implement a class Grid that stores measurements in a rectangular grid. The grid has a given number of rows and columns, and a description string can be added for any grid location. Supply the following constructor and methods: public Grid(int numRows, int numColumns) public void add(int row, int column, String description) public String getDescription(int row, int column) public ArrayList getDescribedLocations() Here, Location is an inner class that encapsulates the...
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...
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...
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:...
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...
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...
(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 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 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; /** * 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...