Blixinator
03-02-2009, 11:32 AM
Hey there, I just MADE minesweeper.
Well, the basics behind it. It generates the field.
But I've always sucked with Applets. We've done hardly anything with them in my Java class.
So what I want to do is have the program bring up the app and put down all the buttons, but I don't know where to start. The problem is, I'm in an online class so I don't have much teacher assistance and I teach myself most of the stuff.
Do the buttons each have the value assigned to it or are they all generic buttons which refer to the array I have of the values of the field?
Also, how do I generate the buttons without naming each one individually?
Here's the console code which outputs the mine field.
*s are mines and _s are blank spaces. Each number refers to the number of mines in the 8 squares surrounding it
import java.util.Random;
public class Minesweeper
{
public Minesweeper()
{}
public String difficulty = "INTERMEDIATE";
//-------~*~BEGINNER~*~-------
/*
public int width = 9;
public int height = 9;
public int nMines = 10;
*/
//-------~*~INTERMEDIATE~*~-------
public int width = 16;
public int height = 16;
public int nMines = 40;
//-------~*~EXPERT~*~-------
/*
public int width = 30;
public int height = 16;
public int nMines = 99;
*/
public boolean minePositions[][] = new boolean[height+1][width+1];
public void newline(int num)
{
for(int a=0; a<num; a++)
System.out.println();
}
public int getRand(int num)
{
Random r = new Random();
return Math.abs((r.nextInt() % num))+0;
}
public int getMines(int x, int y)
{
int n=0;
for(int a=-1; a<=1; a++)
{
for(int b=-1; b<=1; b++)
{
try{
if(minePositions[x+a][y+b]==true)
n++;
}
catch( ArrayIndexOutOfBoundsException e)
{}
}
}
return n;
}
public static void main(String args[])
{
Minesweeper ms = new Minesweeper();
for(int a=0; a<ms.height; a++)
{
for(int b=0; b<ms.width; b++)
{
ms.minePositions[a][b] = false;
}
}
int x=0;
int y=0;
int n=0;
while(n<ms.nMines)
{
x=ms.getRand(ms.height);
y=ms.getRand(ms.width);
if(ms.minePositions[x][y]==false)
{
ms.minePositions[x][y] = true;
n++;
}
}
char mineField2[][] = new char[ms.height+1][ms.width+1];
for(int a=0; a<ms.height; a++)
{
for(int b=0; b<ms.width; b++)
{
mineField2[a][b] = '_';
}
}
for(int a=0; a<ms.height; a++)
{
for(int b=0; b<ms.width; b++)
{
if(ms.minePositions[a][b])
mineField2[a][b] = '*';
else
{
mineField2[a][b] = Integer.valueOf(ms.getMines(a,b)).toString().charA t(0);
if(mineField2[a][b]=='0')
mineField2[a][b] = '_';
}
}
}
for(int a=0; a<ms.height; a++)
{
for(int b=0; b<ms.width; b++)
{
System.out.print(mineField2[a][b] + " ");
}
ms.newline(1);
}
}
}
Well, the basics behind it. It generates the field.
But I've always sucked with Applets. We've done hardly anything with them in my Java class.
So what I want to do is have the program bring up the app and put down all the buttons, but I don't know where to start. The problem is, I'm in an online class so I don't have much teacher assistance and I teach myself most of the stuff.
Do the buttons each have the value assigned to it or are they all generic buttons which refer to the array I have of the values of the field?
Also, how do I generate the buttons without naming each one individually?
Here's the console code which outputs the mine field.
*s are mines and _s are blank spaces. Each number refers to the number of mines in the 8 squares surrounding it
import java.util.Random;
public class Minesweeper
{
public Minesweeper()
{}
public String difficulty = "INTERMEDIATE";
//-------~*~BEGINNER~*~-------
/*
public int width = 9;
public int height = 9;
public int nMines = 10;
*/
//-------~*~INTERMEDIATE~*~-------
public int width = 16;
public int height = 16;
public int nMines = 40;
//-------~*~EXPERT~*~-------
/*
public int width = 30;
public int height = 16;
public int nMines = 99;
*/
public boolean minePositions[][] = new boolean[height+1][width+1];
public void newline(int num)
{
for(int a=0; a<num; a++)
System.out.println();
}
public int getRand(int num)
{
Random r = new Random();
return Math.abs((r.nextInt() % num))+0;
}
public int getMines(int x, int y)
{
int n=0;
for(int a=-1; a<=1; a++)
{
for(int b=-1; b<=1; b++)
{
try{
if(minePositions[x+a][y+b]==true)
n++;
}
catch( ArrayIndexOutOfBoundsException e)
{}
}
}
return n;
}
public static void main(String args[])
{
Minesweeper ms = new Minesweeper();
for(int a=0; a<ms.height; a++)
{
for(int b=0; b<ms.width; b++)
{
ms.minePositions[a][b] = false;
}
}
int x=0;
int y=0;
int n=0;
while(n<ms.nMines)
{
x=ms.getRand(ms.height);
y=ms.getRand(ms.width);
if(ms.minePositions[x][y]==false)
{
ms.minePositions[x][y] = true;
n++;
}
}
char mineField2[][] = new char[ms.height+1][ms.width+1];
for(int a=0; a<ms.height; a++)
{
for(int b=0; b<ms.width; b++)
{
mineField2[a][b] = '_';
}
}
for(int a=0; a<ms.height; a++)
{
for(int b=0; b<ms.width; b++)
{
if(ms.minePositions[a][b])
mineField2[a][b] = '*';
else
{
mineField2[a][b] = Integer.valueOf(ms.getMines(a,b)).toString().charA t(0);
if(mineField2[a][b]=='0')
mineField2[a][b] = '_';
}
}
}
for(int a=0; a<ms.height; a++)
{
for(int b=0; b<ms.width; b++)
{
System.out.print(mineField2[a][b] + " ");
}
ms.newline(1);
}
}
}