Im making a minesweeper game and I would like to prompt for the size of the board initially. Is this possible? If so, how? So basically,
and then
import greenfoot.*;
import javax.swing.JOptionPane;
import javax.swing.JInternalFrame;
public class Board extends World {
int width = Integer.parseInt(JOptionPane.showInputDialog("Please input the width of the board"));
int height= Integer.parseInt(JOptionPane.showInputDialog("Please input the height of the board"));
int mines= Integer.parseInt(JOptionPane.showInputDialog("Please input the mines of the board"));
//Builds Board
public Board(int width, int height, int numMines)
{
super(width, height, 16);
}
}import greenfoot.*;
import javax.swing.JOptionPane;
import javax.swing.JInternalFrame;
public class BoardBuilder extends Board {
//Builds Board
public BoardBuilder() {
super(width, height, 16);
addMines(numMines);
addNumbers();
addTiles();
}
//Continuous check for end game
public void act() {
checkGameOver();
}
//Checks for game over
//Is the numbr of mines equal to the number of tiles?
//AND is the number of tiles equal to the number of flags? If so, you win!
public void checkGameOver() {
int numMines = getObjects(Mine.class).size();
int numTiles = getObjects(Tile.class).size();
int numFlags = getObjects(Flag.class).size();
if (numMines == numTiles && numTiles == numFlags) {
JOptionPane.showMessageDialog(new JInternalFrame(), "Congrats! You just won!","WINNER!", JOptionPane.INFORMATION_MESSAGE);
Greenfoot.stop();
}
}
private void addMines(int count) {
for (int i = 0; i < count; i++) {
int x = -1, y = -1;
do {
x = Greenfoot.getRandomNumber(getWidth());
y = Greenfoot.getRandomNumber(getHeight());
} while (getObjectsAt(x, y, Mine.class).size() > 0);
addObject(new Mine(), x, y);
}
}
private void addNumbers() {
for (int x = 0; x < getWidth(); x++) {
for (int y = 0; y < getHeight(); y++) {
if (getObjectsAt(x, y, Mine.class).size() == 0) {
addObject(new Number(), x, y);
}
}
}
}
private void addTiles() {
for (int x = 0; x < getWidth(); x++) {
for (int y = 0; y < getHeight(); y++) {
addObject(new Tile(), x, y);
}
}
}
}