cs501b:cs-501b-su13_2d_array_of_minesweeper_squares_gui
2D Array of Minesweeper Squares GUI Program
The following version of the MSGrid class is a GUI.
/**
* MSGrid class that contains a 2-D array of MineSquare objects
*
* @author J. Chung
* @version CS-501B
*/
// GUI imports:
import java.awt.*;
import javax.swing.*;
public class MSGrid extends JFrame
{
// instance variables - replace the example below with your own
// 2-D array of MineSquare objects:
private final int ROWS = 20;
private final int COLS = 20;
private MineSquare [][] grid = new MineSquare[ROWS][COLS];
// Actual size of grid that we use in rows and cols:
private int rows = 9;
private int cols = 9;
// Number of mines that go in grid:
private int mines = 10;
/**
* Constructor for objects of class MSGrid
*/
public MSGrid()
{
// 1 - Create and initialize components
JButton smileyBtn = new JButton(";-)");
// initialise the grid of MineSquare objects:
// (construct individual MineSquare objects within grid array)
for ( int r = 1; r <= rows; r++ ) {
for ( int c = 1; c <= cols; c++ ) {
grid[r][c] = new MineSquare();
}
}
// 2 - Create content panes and set their layouts:
JPanel smileyPanel = new JPanel();
smileyPanel.setLayout(new FlowLayout());
JPanel gridPanel = new JPanel();
gridPanel.setLayout(new GridLayout( rows, cols ));
// 3 - Add components to content panes:
smileyPanel.add(smileyBtn);
// Add the _button from each MineSquare to the gridPanel:
for ( int r = 1; r <= rows; r++ ) {
for ( int c = 1; c <= cols; c++ ) {
gridPanel.add(grid[r][c].getFace());
}
}
// 4 - Set window characteristics and pack it:
// Need to set layout for JFrame to arrange the smiley and grid panes.
setLayout(new BorderLayout());
add(smileyPanel, BorderLayout.NORTH);
add(gridPanel, BorderLayout.CENTER);
pack();
setTitle("Simple Java Minesweeper");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); // Center window.
}
/*
* MSGrid methods:
*
* - Set mines
* - Compute and set minecounts
*/
/**
* Set some number of mines at random within the grid.
*/
public void setMines()
{
// Choose random row, choose random col, place mine there:
for ( int i = 1; i <= mines; i++ )
{
int randomrow = randbetween( 1, rows );
int randomcol = randbetween( 1, cols );
// If square is already mined, do it again:
while ( grid[randomrow][randomcol].isMined() == true )
{
randomrow = randbetween( 1, rows );
randomcol = randbetween( 1, cols );
}
grid[randomrow][randomcol].setMined(true);
}
}
/*
* Compute and set square minecounts.
*/
public void setMinecounts()
{
// Approach #1: Visit each square in grid; examine all adjacent
// squares; for each mine found, increment minecount
// by 1.
// (Recommended)
// Approach #2: Visit each mined square in grid; increment minecount
// of all adjacent squares by 1.
// Note: In both approaches, must exclude squares that are not in grid.
// (See the isValidSquare() method.)
}
/*
* See if a square at some row, col is within the grid.
*
* @param rr row of square in question
* @param cc col of square in question
* @return True if square is in grid, false if square not in grid
*/
private boolean isValidSquare( int rr, int cc )
{
if ( rr >= 1 && rr <= rows && cc >= 1 && cc <= cols )
return true;
else
return false;
}
/**
* Show the grid, for testing purposes only.
*/
public void showMSGrid()
{
for ( int r = 1; r <= rows; r++ ) {
for ( int c = 1; c <= cols; c++ ) {
// Call a MineSquare method:
int mc = grid[r][c].getMinecount();
// Show a mine or a minecount number:
if ( grid[r][c].isMined() == true )
System.out.print(" " + "X" );
else
System.out.print(" " + mc);
} // end of column
System.out.println(); // line break
} // end of row
}
/**
* randbetween: Return a random integer between low and high values
*
* @param: low - low value
* @param: high - high value
* @return: random integer b/w low and high
*/
private int randbetween( int low, int high ) {
// Make sure that low and high values are in correct positions:
// If low > high, swap low and high.
if ( low > high ) {
int temp = low;
low = high;
high = temp;
}
int scale = high - low + 1;
int shift = low;
int randnum = (int)(Math.random() * scale) + shift;
return randnum;
}
public static void main(String[] args) {
MSGrid window = new MSGrid();
window.setVisible(true);
}
}
cs501b/cs-501b-su13_2d_array_of_minesweeper_squares_gui.txt · Last modified: by jchung
