i'm relatively new to coding, i only started java this year and C++ last year, i decided i wanted to tackle minesweeper, my only issue is putting all the buttons on screen, my computer-science class uses java that's a little outdated so please excuse this,
here's my code, could someone please tell me what's wrong with it?
please note that it is nowhere near finnished

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math.*;

/*
*Author: John Pycroft
*Date CR: 15/6/2008
*Date LM: 15/6/2008
*Name of project: Minesweeper
*/

public class Minesweeper extends Applet implements ActionListener, KeyListener
{
    Button mineField[][];
    int numberMine[][];
    int xSize, ySize, mines;
    TextField X, Y, Mines;
    Button Expert, Intermediate, Easy, Submit,Restart;
    Label x, y, mInes;
    boolean secondTime;
    public void init()
    {
        if(secondTime)
        {
            remove(x);
            remove(X);
            remove(y);
            remove(Y);
            remove(mInes);
            remove(Mines);
            remove(Submit);
            remove(Easy);
            remove(Intermediate);
            remove(Expert);
            Button mineField[][]=new Button[xSize][ySize];
            int numberMine[][]=new int[xSize][ySize];
            for(int row=0; row<mineField.length; row++)
            {
                for(int collum=0; collum<mineField[0].length; collum++)
                {
                    mineField[row][collum]=new Button("");
                    mineField[row][collum].addActionListener(this);
                    add(mineField[row][collum]);
                }
            }
            for(int i=0; i<mines; i++)
            {
                numberMine[(int)Math.round(Math.random()*xSize)][(int)Math.round(Math.random()*ySize)]=42;
            }
        }
        else
        {
            X=new TextField(30);
            Y=new TextField(30);
            x=new Label("Custom X");
            y=new Label("Custom Y");
            mInes=new Label("Custom Mines");
            Mines=new TextField(30);
            Easy=new Button("Easy");
            Intermediate=new Button("Intermediate");
            Expert=new Button("Expert");
            Submit=new Button("Custom Field");
            Easy.addActionListener(this);
            Intermediate.addActionListener(this);
            Expert.addActionListener(this);
            Submit.addActionListener(this);
            add(x);
            add(X);
            add(y);
            add(Y);
            add(mInes);
            add(Mines);
            add(Submit);
            add(Easy);
            add(Intermediate);
            add(Expert);
        }
    }
    public void actionPerformed(ActionEvent e)
    {
        if(secondTime)
        {
        }
        else
        {
            if(e.getSource()==Submit)
            {
                xSize=Integer.parseInt(X.getText());
                ySize=Integer.parseInt(Y.getText());
                mines=Integer.parseInt(Mines.getText());
                secondTime=true;
                init();
            }
            else if(e.getSource()==Expert)
            {
                xSize=30;
                ySize=16;
                mines=99;
                secondTime=true;
                init();
            }
            else if(e.getSource()==Intermediate)
            {
                ySize=xSize=16;
                mines=40;
                secondTime=true;
                init();
            }
            else if(e.getSource()==Easy)
            {
                xSize=ySize=8;
                mines=10;
                secondTime=true;
                init();
            }
        }
    }
    public void keyPressed(KeyEvent e)
    {
    }
    public void keyReleased(KeyEvent e)
    {
    }
    public void keyTyped(KeyEvent e)
    {
    }
}

Recommended Answers

All 2 Replies

Instead of using the double generator from the Math class you can try using the Random class from java.util.Random extension. I modified your code to implement it--

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math.*;
import java.util.Random;

/*
*Author: John Pycroft
*Date CR: 15/6/2008
*Date LM: 15/6/2008
*Name of project: Minesweeper
*/

public class Minesweeper extends Applet implements ActionListener, KeyListener
{
	Random rgen = new Random();
    Button mineField[][];
    int numberMine[][];
    int xSize, ySize, mines;
    TextField X, Y, Mines;
    Button Expert, Intermediate, Easy, Submit,Restart;
    Label x, y, mInes;
    boolean secondTime;
    public void init()
    {
        if(secondTime)
        {
            remove(x);
            remove(X);
            remove(y);
            remove(Y);
            remove(mInes);
            remove(Mines);
            remove(Submit);
            remove(Easy);
            remove(Intermediate);
            remove(Expert);
            Button mineField[][]=new Button[xSize][ySize];
            int numberMine[][]=new int[xSize][ySize];
            for(int row=0; row<mineField.length; row++)
            {
                for(int collum=0; collum<mineField[0].length; collum++)
                {
                    mineField[row][collum]=new Button("");
                    mineField[row][collum].addActionListener(this);
                    add(mineField[row][collum]);
                }
            }
            for(int i=0; i<mines; i++)
            {
                numberMine[rgen.nextInt(xSize)][rgen.nextInt(ySize)]=42;
            }
        }
        else
        {
            X=new TextField(30);
            Y=new TextField(30);
            x=new Label("Custom X");
            y=new Label("Custom Y");
            mInes=new Label("Custom Mines");
            Mines=new TextField(30);
            Easy=new Button("Easy");
            Intermediate=new Button("Intermediate");
            Expert=new Button("Expert");
            Submit=new Button("Custom Field");
            Easy.addActionListener(this);
            Intermediate.addActionListener(this);
            Expert.addActionListener(this);
            Submit.addActionListener(this);
            add(x);
            add(X);
            add(y);
            add(Y);
            add(mInes);
            add(Mines);
            add(Submit);
            add(Easy);
            add(Intermediate);
            add(Expert);
        }
    }
    public void actionPerformed(ActionEvent e)
    {
        if(secondTime)
        {
        }
        else
        {
            if(e.getSource()==Submit)
            {
                xSize=Integer.parseInt(X.getText());
                ySize=Integer.parseInt(Y.getText());
                mines=Integer.parseInt(Mines.getText());
                secondTime=true;
                init();
            }
            else if(e.getSource()==Expert)
            {
                xSize=30;
                ySize=16;
                mines=99;
                secondTime=true;
                init();
            }
            else if(e.getSource()==Intermediate)
            {
                ySize=xSize=16;
                mines=40;
                secondTime=true;
                init();
            }
            else if(e.getSource()==Easy)
            {
                xSize=ySize=8;
                mines=10;
                secondTime=true;
                init();
            }
        }
    }
    public void keyPressed(KeyEvent e)
    {
    }
    public void keyReleased(KeyEvent e)
    {
    }
    public void keyTyped(KeyEvent e)
    {
    }
}

Edit: Unlike C++, you can initialize your variables upon declaration within field-scope.
It doesn't matter if you instantiate the Random class in the field or in the constructor, the result will be the same. Consider field initialization as a kind of "default value."

Instead of using the double generator from the Math class you can try using the Random class from java.util.Random extension. I modified your code to implement it--

thankyou, but this was not my problem, the Math.random() method works fine, my problem is instantiating a button and integer array within init() after having already declared it in the class body, i've discovered the problem is that the array became private to init() because that's where i've instantiated it, thus i cannot put the buttons on the screen.

Edit: Unlike C++, you can initialize your variables upon declaration within field-scope.

i already knew this, the reason i did it this way was because i want the variables to be initialized after user input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.