Results 1 to 3 of 3
- 11-16-2007, 12:02 AM #1
Member
- Join Date
- Nov 2007
- Posts
- 1
- Rep Power
- 0
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
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:
Java Code://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(); } }
- 08-05-2010, 05:43 AM #2
Member
- Join Date
- Jul 2010
- Posts
- 21
- Rep Power
- 0
well, I didnt fully read through this, but basically... A mine should be a special value on an array. Your action listener would always check if the placement is a mine. Then basically say for(int i = 1; i<number_of_mines;i++) and then reveal your mines one by one.
-
There are many ways to achieve this solution, one being to create a non-GUI model to hold the logic of the program and then attach it to the "view" or GUI portion of the code. The cell can know what type it is without having to do any look up. But irregardless, the OP posted this question 2 years ago, and so given its age, I think it is proper to lock this thread.
Similar Threads
-
Hwlp with "Open", "Save", "Save as..."
By trill in forum New To JavaReplies: 3Last Post: 11-02-2010, 10:26 AM -
Roll 2-Dice "Pig" Game Help
By King8654 in forum AWT / SwingReplies: 7Last Post: 04-07-2008, 07:58 PM -
creating a text based game
By Phobos0001 in forum New To JavaReplies: 1Last Post: 02-12-2008, 05:35 PM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 11:55 PM -
ArrayList: Exception in thread "main" java.lang.NullPointerException
By susan in forum New To JavaReplies: 1Last Post: 07-16-2007, 07:32 AM
Bookmarks