User Tools

Site Tools


cs501b:cs-501b-su13_2d_array_of_minesweeper_squares_example

2D Array of Minesweeper Squares Example

The following MSGrid class creates and uses a two-dimensional array of the previously-given Minesweeper Square class objects.

The BlueJ project (in zip format) of the entire MineSweeper program to date (MineSquare and MSGrid) is at http://tinyurl.com/mwvvrls.


/**
 * MSGrid class that contains a 2-D array of MineSquare objects
 * 
 * @author J. Chung
 * @version CS-501B
 */
public class MSGrid
{
    // 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()
    {
        // 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();
            }
        }
    }

    
    /*
     * 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.
        //       (Must use the isValidSquare() method below.)
    }
    
    

    
    
    /*
     * 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;
    }

    
    
}

cs501b/cs-501b-su13_2d_array_of_minesweeper_squares_example.txt · Last modified: 2013/06/20 19:54 by jchung

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki