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

2017/4/5

Minesweeper game help

BrownDwarf BrownDwarf

2017/4/5

#
I am making minesweeper, and so far I have made an array of the 'button' class (which is the part that covers the 'background' class, as in minesweeper), and I made a constructor in the button class, to identify whether there is a mine under it (true), or not (false).
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
 
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
    }
    public  int backAmount = 0;
    public  int maxBack = 256;
    public int num = 0;
    public int newn = 0;
 
    public int yBack = 392;
    Background[] b = new Background[256];
    button[] cover = new button[256];
    public int xBack = 592;
 
    public int xMine;
    public int yMine;
    public int mineLocation;
    public void act()
    {
        //xBack < 20
        //System.out.println("Inside act method");
        set();
 
        mine();
        butt();
    }  
 
    public void set()
    {
 
        for (backAmount = 1; backAmount <= maxBack; backAmount++)
        {
            if (backAmount % 16 == 0)
            {
                xBack = 592;
                yBack = yBack - 16;
                // backAmount = 1;
            }
            //System.out.println("num =" + num);
            if (num <= 255)
            {
                b[num] = new Background();
                addObject(b[num], xBack, yBack);
                num++;
            }
            else if (num >= 256)
            {
                break;
            }
 
            xBack -= 16;
        }
    }
 
    public void mine()
    {
        //System.out.println("Inside mine method");
        if (getObjects(Mine.class).size() <= 40)
        {
            mineLocation = Greenfoot.getRandomNumber(256);
            xMine = b[mineLocation].getX();
            yMine = b[mineLocation].getY();
 
            Mine mine = new Mine();
            addObject(mine, xMine, yMine);
            //button ba = new button(true);
            //addObject(ba, xMine, yMine);
 
        }
    }
 
    public void butt()
    {
        if (getObjects(Mine.class).size() >= 40)
        {
            xBack = 592;
            yBack = 392;
            for (backAmount = 1; backAmount <= maxBack; backAmount++)
            {
                if (backAmount % 16 == 0)
                {
                    xBack = 592;
                    yBack = yBack - 16;
                    // backAmount = 1;
                }
                //System.out.println("num =" + num);
                if (newn <= 255)
                {
                     
                    if (getObjectsAt(xBack, yBack, Mine.class) == null)
                    {
                        //System.out.println("here");
                        cover[newn] = new button(false);
                        addObject(cover[newn], xBack, yBack);
                    }
                    else if (getObjectsAt(xBack, yBack, Mine.class) != null)
                    {
                        cover[newn] = new button(true);
                        addObject(cover[newn], xBack, yBack);
                    }
                    newn++;
                }
                else if (newn >= 256)
                {
                   break;
                }
 
                xBack -= 16;
            }
        }
    }
}
This is the world class, and the problem is that when the world generates, the button class generates only with isKill being true. None of them are false. I need all ones not over mines to be false. Don't know why that code isn't running. Thanks.
Super_Hippo Super_Hippo

2017/4/5

#
I don't think that you should do the mine placement from the act method. You should do the creation of the mines and non-mines fields from the constructor. Why do you have this background class? I would do this: - add the 256 buttons - randomly choose 40 and set a variable in them to true (mine below) - when a button is clicked, show a mine or a number based on the variable and the variable from the neighbors
You need to login to post a reply.