Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Randomizing cells in a grid

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Thanked 0 Times in 0 Posts

    Default Randomizing cells in a grid

    Hello all, happy holidays!

    I am here to seek some assistance in solving a homework problem. I know the code will not be written for me, I am just looking for a guiding hand. What my problem involves is designing a java applet that resembles minesweeper. I have made a 10x10 grid for the playing field. What I need to do is randomize the cells so that twenty of them are "mines" and the rest are "duds". I'm feeling very lost at the moment and any tips that would send me in the right direction would be great.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Thanked 619 Times in 561 Posts
    Blog Entries

    Default Re: Randomizing cells in a grid

    A simple method is to create a 10*10 array of booleans to false (being devoid of mines). Then you just need to pick 20 random (x,y) integer pairs (use the Random class or Math.random) and set those to spots to contain a mine by setting that coordinate to true. Make sure to check to see if that spot already contains a mine, otherwise you'll end up with a game map with possibly less than 20 mines (and the chances of this happening are quite high).

    A second alternative is to create a set of the coordinates which contains mines (see the HashSet Javadoc).

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 833 Times in 772 Posts
    Blog Entries

    Default Re: Randomizing cells in a grid

    The Random class provides the capability for pseudo-random values. Can be used to randomize in a grid by getting 2 random values between 1 and 10 (or 0 and 9 for an array) to randomize a piece.

  4. #4
    Junior Member
    Join Date
    Dec 2010
    Thanked 0 Times in 0 Posts

    Default Re: Randomizing cells in a grid

    Thanks, guys. Also, I'm having a bit of a problem trying to set out my initial layout again. I had the 10x10 grid set up, but I also needed to add three labels/buttons on top of the grid, and it's throwing me off a little bit.

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class Ex3_1 extends JApplet
      int numButtons;
      String s;
      private JButton b = new JButton("start over");
      private JLabel l1 = new JLabel("attempts: ");
      private JLabel l2 = new JLabel("found: ");
      private JPanel p1, p2;
      private GridLayout grid1;
      private Container pane;
      public void init()
        Container pane = getContentPane();
        pane.setLayout(new GridLayout(1, 1, 0, 5));
        JPanel top = new JPanel();
        top.setLayout(new FlowLayout());
        JPanel bottom = new JPanel();
        bottom.setLayout(new FlowLayout());
        s = getParameter("NUMBUTTONS");
        numButtons = Integer.valueOf(s).intValue();
        grid1 = new GridLayout(10, 10);
        for (int i=1; i<=numButtons; i++ )
        bottom.add(new Button("")  );    

    With this code, I have the three buttons on the left and a distorted grid on the right side of the panel. I think trying to put the grid on a panel is throwing it all off, am I going about this all wrong?

Similar Threads

  1. How to align the items in a form (grid layout)?
    By onlybarca in forum AWT / Java Swing
    Replies: 4
    Last Post: November 27th, 2010, 11:38 PM
  2. Counting cells
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2010, 05:04 PM
  3. Grid GUI Library
    By aussiemcgr in forum Java Theory & Questions
    Replies: 7
    Last Post: September 15th, 2010, 03:30 PM
  4. how to copy cells to a new sheet of excel with JexcelAPI??
    By 19world in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: June 15th, 2010, 03:49 AM

Tags for this Thread