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); } } |