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).
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.