Minesweeper


Minesweeper

I am creating Minesweeper using Greenfoot. Minesweeper is a puzzle game where you must clear all the blocks except the ones with mines beneath them. The way you do this is by clicking blocks to see what is beneath them. They can have nothing or a number of how many mines are near them, underneath.


What I Got Done:

I started by creating a background with a 10 by 10 grid, and then making an actor, Mine, which is added by the world. The world creates 10 mines in random locations.

 ArrayList mines = new ArrayList();
for(int i = 0; i < 10; i++)
{
 mines.add(new Mine());
 addObject(mines.get(i),Greenfoot.getRandomNumber(10),Greenfoot.getRandomNumber(10));
}

The only issue I’ve had with that so far is sometimes mines are placed on top of each other. I haven’t tried to fix it yet since I want to get most of the game done first.

To click and look for mines I made a cell class that when clicked it changes the image to match whatever is needed to indicate its distance to a mine. It checks the eight cells around the cell to check for mines.

int z = 0;
Actor a = getOneObjectAtOffset(1,0, Mine.class);
if(a != null)
{
 z = z + 1;
} 

I did this same thing but changed the getOneObjectAtOffset to check all the cells around it. If there is a mine in this cell it will increase int z.

int cellX = ((Cell) getWorld().getObjects(Cell.class).get(1)).getX();
int cellY = ((Cell) getWorld().getObjects(Cell.class).get(1)).getY();

int width = 20;
int height = 20;

if(z == 1)
{
 GreenfootImage image1 = new GreenfootImage("one.jpg");
 image1.scale(width,height);
 setImage(image1);
          
}

if(z == 2)
{
 GreenfootImage image2 = new GreenfootImage("two.png");
 image2.scale(width,height);
 setImage(image2);
          
}
if(z == 0)
{
 GreenfootImage image0 = new GreenfootImage("zero.jpg");
 image0.scale(width,height);
 setImage(image0);          
}

This code use the integer z to change the image of the cell to whatever integer it is. When there is a mine nearby, depending on how many there are, it changes to an image with the corresponding number.

The game is far from done, however, what I have done looks like this.

This image shows how my game works so far. It does have 10 mines, they didn’t get created on top of each other in this picture. There are numbers on top of bombs as well. This is because the bombs are under the cells, so the cell still changes it’s image based on the other cells around them. In the end I would like the cells with no mines nearby to be transparent however I wasn’t able to get to that.


Conclusion:

In conclusion I should’ve been able to get farther with this program. I was unsure what I wanted my final project to be when the class began so I wasted a lot of time figuring out what I wanted to do and messing around with different languages. I have zipped a file in my drive so I can finish it over the summer. I can definitely see myself finishing this project over the summer when my computer is repaired.


Summer Goals:

When a mine is clicked a lose message would pop up.
Flags can be added to signal where a mine is.
A win message when all cells without mines are clicked.
Clearing all neighboring empty cells when an empty cell is clicked.
More polish and finishing touches…

Leave a Reply

Your email address will not be published. Required fields are marked *