Hello, I'm new to these forums, so I don't know if any of you are familiar with the Gridworld framework, which is required by the AP Computer Science curriculum. Basically, it is literally a grid where "actors" are placed, and coded to do various tasks.
Anyway, my Minesweeper project fills up the grid with Mine objects. These Mines are managed by the Minefield class. Finally, the game is executed by running the MineSweeper class.
Everything works fine, except there is no "game over". What I don't know how to do is two things:
1. The game becomes aware that a mine has been revealed
2. All mine objects reveal themselves
"Revealing" means making the mine object return the proper string from getImageSuffix(). Here is the source code:
Mine.java:
//import java.awt.Color;
import info.gridworld.actor.ActorWorld;
import info.gridworld.actor.Rock;
import info.gridworld.actor.Actor;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
public class Mine extends Rock
{
private boolean _hasBomb = true;
private boolean _isFlagged = false;
private boolean _isRevealed = false;
private int _numberOfAdjacentBombCounter = 0;
private int _numberOfAdjacentBombs = 0;
private double rand = Math.random();
public Mine(MineField F)
{
if(rand >= .1)
_hasBomb = false;
}
public void setNumberOfAdjacentBombs(int n)
{
_numberOfAdjacentBombs = n;
}
// access to private variables
public int numberOfAdjacentBombs()
{
return _numberOfAdjacentBombs;
}
//public boolean gameOver()
//{
// return _gameOver;
//}
public boolean hasBomb()
{
return _hasBomb;
}
public void Reveal()
{
_isRevealed = true;
if(!_hasBomb && adjacentBombCounter() == 0)
{
for(Actor neighbor : getGrid().getNeighbors(getLocation()))
{
Mine neighborMine = ((Mine) neighbor);
if(!neighborMine._isRevealed && !neighborMine._hasBomb)
neighborMine.Reveal();
}
}
}
public void flag()
{
_isFlagged = true;
}
public void unflag()
{
_isFlagged = false;
}
public int adjacentBombCounter()
{
_numberOfAdjacentBombCounter = 0;
for(Actor neighbor : getGrid().getNeighbors(getLocation()))
{
Mine neighborMine = ((Mine) neighbor);
if(neighborMine._hasBomb)
_numberOfAdjacentBombCounter++;
}
return _numberOfAdjacentBombCounter;
}
public String getImageSuffix()
{
if(_isRevealed && _hasBomb)
return "Explode";
if(_isRevealed && !_hasBomb)
return "" + adjacentBombCounter();
if(_isFlagged)
return "Flag";
return "NoClick";
}
}
MineField.java:
//import java.awt.Color;
import info.gridworld.actor.ActorWorld;
import info.gridworld.actor.Actor;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
public class MineField extends ActorWorld
{
public boolean _gameOver = false;
public MineField()
{
//super(new BoundedGrid<Actor> (rows, columns));
Grid<Actor> gr = getGrid();
int row = gr.getNumRows();
int column = gr.getNumCols();
//fill the grid with mines
for (int r = 0; r < row; r++)
{
for (int c = 0; c < column; c++)
{
Mine currentMine = new Mine(this);
Location loc = new Location(r, c);
add(loc, currentMine);
currentMine.setNumberOfAdjacentBombs(currentMine.adjacentBombCounter());
}
}
}
}
MineSweeper.java:
//import java.awt.Color;
import info.gridworld.actor.Rock;
import info.gridworld.actor.Actor;
import info.gridworld.actor.ActorWorld;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
public class MineSweeper
{
public static void main(String[] args)
{
ActorWorld world = new MineField();
//world.add(new MineField());
world.show();
}
}