Programming Project Specification Boulder Dash
The programming project is to implement a simplified version of the 1980's classic computer puzzle game Boulder Dash.
For those not familiar with this fun puzzle classic, Boulder Dash consists of two-dimensional levels of tiles in which the player must collect a sufficient number of diamonds to be allowed to exit to the next level. Each level also has impenetrable walls, dirt that the player can dig through, and rocks that the player can push around. There are also fireflies and butterflies that move around that level (but who can't dig through the dirt) and whose touch is fatal to the player. The player can fight back by digging in suitable places to make a rock fall on a firefly or a butterfly, both of which explode if they are hit by a rock. Fireflies leave nothing behind when they explode, whereas the butterflies explode into a 3*3 grid of diamonds for the player to collect.
To get an intuitive idea of how the game works, you can watch some videos of the original C64 Boulder Dash on YouTube. Your program doesn’t need to have animated graphics or be able to scroll, but it can just show the entire gamefield on the component.
This project might seem daunting at first. However, it is nowhere as daunting as it might initially seem. Lab four consists of four initial tasks that should get you nicely started with the project, and after those, the rest of this document explains the rest of the code that you need to write. Furthermore, unlike some annoying projects that must be fully completed before they do anything visible, every improvement that you do can be written and tested piecemeal.

How things behave in the game
To get the fireflies and butterflies to move and the rocks and diamonds to fall, create an instance ofjavax.Swing.Timer and make it tick at some suitable speed, perhaps every 200 milliseconds. Every time your Timer object ticks, it generates an action event that you should listen to. In your action listener, you should loop through all tiles of the level, starting from the bottom row and proceeding towards the top. Inside the loop, depending on what the current tile (x,y) contains, you act according to the following rules. Rocks: Each rock inside the level always is in either one of the two states, stationary or falling. If a rock is currently stationary and the tile (x,y+1) directly under it is...
• empty: the rock becomes a falling rock. (But it doesn't move yet until the next tick.)
• wall, rock or diamond: if the tiles (x-1,y) and (x-1,y+1) immediately to the left and to the down-left are both empty, the rock “slips” into the tile (x-1, y) to the left and becomes a falling rock. Same way with right.
• anything else: the rock remains stationary.
If a rock is currently falling and the tile directly under it is...
• empty: the rock moves to the tile under it.
• player: the player explodes and dies.
• firefly: the firefly explodes so that everything in the surrounding 3*3 area that is not a wall turns into empty space. The explosions do not continue recursively even if there is another firefly or butterfly inside the surrounding area. (Hint: it is a good idea to write a private utility method void explode(int x, int y, BDTile filler) that does a 3*3 explosion centered in the coordinates (x,y) so that the non-wall tiles are filled with filler.)
• butterfly: the butterfly explodes so that everything in the surrounding 3*3 area that is not a wall turns into a diamond. (The explosions also do not continue recursively.)
• anything else: the rock becomes stationary.
Diamonds: Diamonds behave the same way as rocks so that each diamond is either stationary or falling, except that a diamond falling onto a player, firefly or butterfly is harmless and merely turns the diamond back into a stationary one.
Fireflies: If the player is in one of the four neighbouring tiles next to the firefly, the firefly explodes turning the surrounding 3*3 area into empty space, and the player dies. Otherwise the firefly tries to move. Each firefly has a current direction that determines which way it is heading; either up, right, down or left. If the next tile in that direction is empty, the firefly moves there. If the next tile is not empty, the firefly changes its direction turning clockwise so that for example, if its previous direction was up, its new direction is right. (The firefly doesn't try to move to the new direction until the next tick, since it takes one tick for it to turn.) Butterflies: Butterflies behave otherwise exactly like fireflies, except that they turncounterclockwise when their movement is blocked by something.
Amoeba: The amoeba doesn't move and its touch is harmless to everyone, but it slowly expands into surrounding empty spaces, eventually making it impossible for the player to move. During each tick, each tile of the amoeba has a small probability (experiment with values until you find a suitable one) to expand. The expansion looks at the neighbouring tile in a randomly chosen direction, and if that tile is currently empty or dirt, it also becomes an amoeba.
Also, for the testing purposes of the game, you should make your key listener behave so that when the user presses N, the game instantly moves to the next level.

Level files and provided classes
Each level of the game is a 40 * 22 grid of tiles. The x and y coordinates of each tile can therefore have values from the range 0,...,39 and 0,...,21. The x-coordinate represents the horizontal direction. The ycoordinates are vertical and increase downwards.
In your BoulderDash class, you shoulddeclare one BDLevelReader object as a field, and use its following methods to read the information about the current level:
• public int readLevels(String filename) throws Exception
Call this method only once in your constructor, passing the name of the level data field as parameter. This method reads the entire level data file and stores its contents to an internal XML document tree to be queried later. Returns the total number of levels provided in the data file.
• public void setCurrentLevel(int level) throws Exception
Tells the BDLevelReader that the accessor methods after this one should access the given current level. Remember that level numbering starts from 1, not 0.
• public BDTile getTile(int x, int y)
Returns the tile in the coordinates (x,y) in the current level. Each level is guaranteed to be exactly 40 tiles wide and 22 tiles tall, so the legal values for x are 0,...,39, and the legal values for y are 0,...,21. Each level is also guaranteed to have exactly one tile with the value PLAYER, the starting location of the player.
• public int getDiamondsNeeded()
Returns the number of diamonds that the player needs to collect to be allowed to exit the current level.

Extra credit
When the required things have been implemented so that they are working smoothly, here are some suggestions for how to get a higher grade:
• Improve the graphics, perhaps drawing everything with Image objects that you have drawn yourself and read from files.
• Add a clock and time limit to each level.
• Jazz up the graphics with animations. Simply create additional instances of Timer of different speeds and listen to their actions, and at each tick, change the values of some fields that determine how something is drawn.
• The rules listed above are simplified from the original Boulder Dash. For example, in the original game, the entire amoeba turns into diamonds if none of the amoeba tiles around the level has any room to expand. In addition to normal walls, there were also “magic walls” that otherwise behave like normal walls, but if a falling rock lands on top of a magic wall, the rock slowly slides through the wall and emerges from the other side as a diamond.

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;

public class BoulderDash extends JPanel {

* @param args
private static int WIDTH = 40;
private static int HEIGHT = 22;
private static int left=0, top=0;
private static int[] pos;
private static int currLevel=1;
private Random rd = new Random();
    private BDTile[][] allTiles = new BDTile[WIDTH][HEIGHT];
    private Image img, dirt, wall, diamond, img2, amoeba, ffly, bfly, exit;
    public ArrayList<Obstacle> obstacles;
    private static JFrame frame = new JFrame("Boulder Dash");
    private int level;
    private static int diamondsReqd;
private static int diamondCount=0;
    public BDLevelReader bdLvlReader = new BDLevelReader();
    private static JPanel scoreboard;
    private static JLabel score, timer, imagelabel, lvlLabel;
    private static boolean alive = true;
    public Obstacle ob;
    private int t;
    private static Timer tm, fall, movement;

    public BoulderDash(){

pos = new int[2]; //variable that will store player's current position

level = bdLvlReader.readLevels("levels.xml");
}catch(Exception e){
System.out.println("levels.xml file not found");
}catch(Exception e){
System.out.println("Unable to set level as "+ currLevel);

diamondsReqd = bdLvlReader.getDiamondsNeeded();   //number of diamonds to be collected to complete level

for (int i=0;i<WIDTH;i++) {
for (int j=0;j<HEIGHT;j++){
allTiles[i][j] = bdLvlReader.getTile(i, j);
obstacles = initObstacle();       //initialize obstacles like butterfly, firefly and amoeba if any

This is only a preview of the solution. Please use the purchase button to see the entire solution

Assisting Tutor

Related Homework Solutions

Money Problems in Java
Homework Solution
Recursion Methods
Cylinder Containers in Java
Homework Solution
Computer Science
Get help from a qualified tutor
Live Chats