This site requires JavaScript, please enable it in your browser!
Greenfoot back
mozrila
mozrila wrote ...

2013/6/13

Prompt for world size with JOptionPanel

mozrila mozrila

2013/6/13

#
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,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);
        
    }
     
}
and then
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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);
            }
        }
    }
}
mozrila mozrila

2013/6/13

#
edited code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 height of the board"));
        //Builds Board
   public Board(int width, int height, int numMines)
   {   
       super(width, height, 16);
       new BoardBuilder();
    }
     
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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);
            }
        }
    }
}
mozrila mozrila

2013/6/13

#
Updated Updated code...
1
2
3
4
5
6
7
8
9
10
11
12
13
import greenfoot.*;
import javax.swing.JOptionPane;
import javax.swing.JInternalFrame;
 
public class Board extends World {
           //Builds Board
   public Board(int width, int height, int numMines)
   {   
       super(width, height, 16);
       new BoardBuilder(width, height, numMines);
    }
     
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import greenfoot.*;
import javax.swing.JOptionPane;
import javax.swing.JInternalFrame;
 
public class BoardBuilder extends Board {
     
        //Builds Board
   public BoardBuilder(int width, int height, int numMines) {   
              
       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);
            }
        }
    }
}
No Compiler errors, But mad errors when running.
You need to login to post a reply.