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

2017/4/1

Array of Objects

BrownDwarf BrownDwarf

2017/4/1

#
I am trying to make a Minesweeper game. I Have made the grid (16 x 16), but now I am trying to place the mines randomly after each run. What I tried to do is create an array of 256 objects (the squares that make up the background), and have the program place each of those from the array to form the grid. This way when I need to find the x-y location of a random square on the grid I can just reference the array. My problem is, is that first of all the grid only forms after I manually place one square and then run the program. Also even when it makes the grid, I inspect, and the array is empty, so when I try to reference it later, nothing happens. Hope you can find the problem:
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
63
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Background here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Background extends Actor
{
    /**
     * Act - do whatever the Background wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public  int backAmount = 1;
    public  int maxBack = 256;
    public int num = 0;
 
    public int yBack = 392;
    Background[] b = new Background[256];
     
    public int xBack = 592;
 
    public int xMine;
    public int yMine;
    public void act()
    {
        //xBack < 20
        set();
        //mine();
    }  
 
    public void set()
    {
 
        for (backAmount = 1; backAmount <= maxBack; backAmount++)
        {
            if (backAmount > 16)
            {
                xBack = 592;
                yBack = yBack - 16;
                // backAmount = 1;
            }
            Background back = new Background();
           
            b[num] = back;
             
            getWorld().addObject(b[num], xBack, yBack);
            xBack -= 16;
            num++;
 
        }
    }
 
    public void mine()
    {
     
        xMine = b[0].getX();
        yMine = b[0].getY();
        Mine mine = new Mine();
        getWorld().addObject(mine, xMine, yMine);
    }
}
Super_Hippo Super_Hippo

2017/4/1

#
There is a logical error. The code is in a class called Background. For every Background object, line 20 creates an array which can hold 256 Background objects. Another problem is that you keep adding new background objects from the act method which themselves have the array of 256 Background objects and which also create new Background objects.... I am not sure what the Background Actor class should do and I didn't program a Minesweeper yet, but I would think of having this structure: When the world is created, it adds 256 instances of an Actor subclass to the world. It randomly chooses a given number of them to become mines. Have a boolean field in the Actor subclass. The Actor class itself checks for mouse clicks on itself. When it is clicked, it first checks if it is a mine (game lost) or if not, it checks its neighbors if they are mines and display the correct number.
You need to login to post a reply.