I tried to make a minesweeper game and everything works fine except for one thing. Normally if you click a square with and value of 0, all the squares around that squarey should also be pressed, this works also, but if one of the squares that are automaticly pressed is also a square with an value of 0, that square should also be pressed, this is the thing that doesnt work.
(the game is based on an 2D Array in the MyWorld class)
There is also some code in the MyWorld class, but that is only for preparing the playingField and such things.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List; //wird für die ".getNeighbours()" methode benötigt
/**
* Write a description of class blankSquare here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class blankSquare extends Actor
{
boolean flag = false;
int x = 0;
int y = 0;
/**
* Act - do whatever the blankSquare wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
MouseInfo mi = Greenfoot.getMouseInfo();
if(Greenfoot.mouseClicked(this) && mi.getButton() == 1){
x = this.getX();
y = this.getY();
if(MyWorld.playingField[x][y] == 0){
setImage("square_0.png");
clickTouching(false,0,0);
}
if(MyWorld.playingField[x][y] == 1){
setImage("square_1.png");
}
if(MyWorld.playingField[x][y] == 2){
setImage("square_2.png");
}
if(MyWorld.playingField[x][y] == 3){
setImage("square_3.png");
}
if(MyWorld.playingField[x][y] == 4){
setImage("square_4.png");
}
if(MyWorld.playingField[x][y] == 5){
setImage("square_5.png");
}
if(MyWorld.playingField[x][y] == 6){
setImage("square_6.png");
}
if(MyWorld.playingField[x][y] == 7){
setImage("square_7.png");
}
if(MyWorld.playingField[x][y] == 8){
setImage("square_8.png");
}
if(MyWorld.playingField[x][y] == -1){
setImage("mine.png");
Greenfoot.stop();
}
}
if(Greenfoot.mouseClicked(this) && mi.getButton() == 3){
if(flag == false){
setImage("flag.png");
}
if(flag == true){
setImage("square.png");
}
if(flag == true){
flag = false;
}
else if(flag == false){
flag = true;
}
}
}
//everything above is for "flaging", clicking the squares and giving the squares the correct picture/number. The code abover works perfectly!
private void clickTouching(boolean chainReaction, int x2, int y2){ //chainReaction = if true the coordinates x2 and y2 will be used for the neighbour list; x2 = only used if chainReaction = true; x2 = only used if chainReaction = true
List neighbours = this.getNeighbours(1, true, blankSquare.class);
if(chainReaction == true){
neighbours.clear();
neighbours = ((blankSquare)getOneObjectAtOffset(x2, y2, blankSquare.class)).getNeighbourList(); //casts an actor at the position x,y to an object so it is possible to call a method with it
}
for(int i = 0; i < neighbours.size(); i++){
Actor a = (Actor) neighbours.get(i);
int x = a.getX();
int y = a.getY();
if(MyWorld.playingField[x][y] == 0){ //if the square has the value 0, normaly every other square with the value 0 should be clicked, like a chain reaction (thats the reason for the name of the boolean varible). Because of this, if the square is 0 the click touching mehod is called, but with chainReaction = true and x2 and y2 as the possition of the square.
a.setImage("square_0.png");
clickTouching(true,a.getX(),a.getY()); //throws null pointer exception, outcomment if you want to try the game at its current status
}
if(MyWorld.playingField[x][y] == 1){ //the game is based on an static 2d Array which is in the MyWorld class, the if's look which value the 2d Array has on the position x,y and then gives the square the matching picture.
a.setImage("square_1.png");
}
if(MyWorld.playingField[x][y] == 2){
a.setImage("square_2.png");
}
if(MyWorld.playingField[x][y] == 3){
a.setImage("square_3.png");
}
if(MyWorld.playingField[x][y] == 4){
a.setImage("square_4.png");
}
if(MyWorld.playingField[x][y] == 5){
a.setImage("square_5.png");
}
if(MyWorld.playingField[x][y] == 6){
a.setImage("square_6.png");
}
if(MyWorld.playingField[x][y] == 7){
a.setImage("square_7.png");
}
if(MyWorld.playingField[x][y] == 8){
a.setImage("square_8.png");
}
if(MyWorld.playingField[x][y] == -1){
a.setImage("mine.png");
Greenfoot.stop();
}
}
}
public List getNeighbourList(){ //is called if chainReaction = true. Returns a list of neighbours.
return this.getNeighbours(1,true,blankSquare.class);
}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
{
static int[][] playingField = new int[10][10];
/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
super(10, 10, 16);
prepare();
}
/**
* Prepare the world for the start of the program.
* That is: create the initial objects and add them to the world.
*/
private void prepare()
{
for(int i = 0; i <= 10; i++){
for(int a = 0; a <= 10; a++){
addObject(new blankSquare(), i,a);
}
}
preparePlayingField();
}
private void preparePlayingField(){
for(int i = 0; i < 10; i++){
int x = (int)(Math.random() * 10);
int y = (int)(Math.random() * 10);
if(playingField[y][x] != -1){
playingField[y][x] = -1;
try{
if(playingField[y-1][x-1] != -1){
playingField[y-1][x-1] += 1;
}
}
catch(Exception e){
}
try{
if(playingField[y-1][x-1] != -1){
playingField[y-1][x-1] += 1;
}
}
catch(Exception e){
}
try{
if(playingField[y-1][x] != -1){
playingField[y-1][x] += 1;
}
}
catch(Exception e){
}
try{
if(playingField[y-1][x+1] != -1){
playingField[y-1][x+1] += 1;
}
}
catch(Exception e){
}
try{
if(playingField[y][x-1] != -1){
playingField[y][x-1] += 1;
}
}
catch(Exception e){
}
try{
if(playingField[y][x] != -1){
playingField[y][x] += 1;
}
}
catch(Exception e){
}
try{
if(playingField[y][x+1] != -1){
playingField[y][x+1] += 1;
}
}
catch(Exception e){
}
try{
if(playingField[y+1][x-1] != -1){
playingField[y+1][x-1] += 1;
}
}
catch(Exception e){
}
try{
if(playingField[y+1][x] != -1){
playingField[y+1][x] += 1;
}
}
catch(Exception e){
}
try{
if(playingField[y+1][x+1] != -1){
playingField[y+1][x+1] += 1;
}
}
catch(Exception e){
}
}
else{
i--;
}
}
}
}

