Maze By Steve And Joe
<< Find the Box by Nick | OldProjectsTrailIndex | Hit the Mole by Noah >>
Runtime errors can happen if next is null and you have a call to MakeMaze.next.is()
Rocket.java
/**
* Write a description of class Rocket here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Rocket extends Cell
{
public Rocket(int r, int c)
{
super(r,c);
this.removeWalls();
}
public void removeSelfFromGrid(Cell[][] m)
{
Cell replacement = new Cell(this.getRow(),this.getCol());
replacement.removeWalls();
m[this.getRow()][this.getCol()] = replacement;
}
}
Box.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Box extends Rectangle
{
private Color color;
public Box(int x, int y,Color c){
super(x,y,100, 20);
color=c;
}
public Box(int x, int y, int width, int height, Color c){
super(x,y,width, height);
color=c;
}
public void setColor(Color c)
{
color = c;
}
public void draw(Graphics g){
g.setColor(color);
g.fillRect((int)this.getX(), (int)this.getY(),width,height);
}
}
Cell.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
public class Cell
{
public static final int GAP=10, SIZE=40, LEFT=40, TOP=60;
public static final int NORTH=0, SOUTH=1, EAST=2, WEST=3;
private int visits, row, col, rockets;
private boolean[] wall;
private Rectangle box;
private Color color;
public Cell(int r, int c)
{
rockets = 0;
visits=-1;
row=r;
col=c;
wall=new boolean[4];
box = new Rectangle (LEFT+col*(SIZE+GAP), TOP+row*(SIZE+GAP), SIZE, SIZE);
for (int i=0;i<4;i++)
wall[i]=true;
}
/**
*
* @return a random neighboring Cell, or null if all neighbors have been visited
*/
public Cell randomNeighbor(Cell[][] m){
ArrayList<Cell> n = new ArrayList<Cell>();
//NORTH
if (row>0 && m[row-1][col].getVisits()<0)
n.add(m[row-1][col]);
//SOUTH
if (row<m.length-1 && m[row+1][col].getVisits()<0)
n.add(m[row+1][col]);
//WEST
if (col>0 && m[row][col-1].getVisits()<0)
n.add(m[row][col-1]);
//EAST
if (col<m[0].length-1 && m[row][col+1].getVisits()<0)
n.add(m[row][col+1]);
if (n.size()<1)
return null;
return n.get((int)(n.size()*Math.random()));
}
public void draw(Graphics g, Color c)
{
Color oldColor=g.getColor();
g.setColor(c);
if (visits==1)
g.setColor(Color.GREEN);
if (visits>1)
g.setColor(Color.RED);
g.fillRect(box.x, box.y, box.width, box.height);
if (!wall[SOUTH])
g.fillRect(box.x, box.y+SIZE, box.width, GAP);
if (!wall[EAST])
g.fillRect(box.x+SIZE, box.y, GAP, box.width);
g.setColor(oldColor);
}
public void drawP(Graphics g, Color c)
{
g.setColor(c);
g.fillRect(box.x, box.y, box.width, box.height);
if (!wall[SOUTH])
g.fillRect(box.x, box.y+SIZE, box.width, GAP);
if (!wall[EAST])
g.fillRect(box.x+SIZE, box.y, GAP, box.width);
}
public Rectangle getBox(){return box;}
public int getRow(){return row;}
public int getCol(){return col;}
public int getVisits(){return visits;}
public Color getColor(){return color;}
public void setVisits(int i)
{
visits=i;
if (visits%3==0)
color=Color.YELLOW;
if (visits%3==1)
color=Color.GREEN;
if (visits%3==2)
color=Color.CYAN;
}
public void incVisits(){visits++;}
public void setWall(int direction, boolean value){
wall[direction]=value;
}
public boolean isWall(int direction)
{
return wall[direction];
}
public void setColor(Color c){color = c;}
/**
* removes the walls between this cell and the other cell
* @param other
*/
public void removeWall(Cell other) {
if (other.getRow()>row){//other cell is below this one
this.setWall(SOUTH, false);
other.setWall(NORTH, false);
}
if (other.getRow()<row){//other cell is above this one
this.setWall(NORTH, false);
other.setWall(SOUTH, false);
}
if (other.getCol()>col){//other cell is east of this one
this.setWall(EAST, false);
other.setWall(WEST, false);
}
if (other.getCol()<col){//other cell is west this one
this.setWall(WEST, false);
other.setWall(EAST, false);
}
}
public void removeWalls()
{
for(int i = 0; i < 4; i++)
wall[i] = false;
}
public boolean canMoveUp()
{
if(isWall(NORTH))
return false;
return true;
}
public boolean canMoveDown()
{
if(isWall(SOUTH))
return false;
return true;
}
public boolean canMoveLeft()
{
if(isWall(WEST))
return false;
return true;
}
public boolean canMoveRight()
{
if(isWall(EAST))
return false;
return true;
}
public boolean is(Cell c)
{
if(row == c.getRow() && col == c.getCol())
return true;
return false;
}
public String toString()
{
return "" + getRow() + "" + getCol() + ".";
}
public int getRockets()
{
return rockets;
}
public void loseRocket(int r)
{
if(rockets > 0)
{
rockets = rockets - r;
}
else{return;}
}
public void setRockets(int r)
{
rockets = r;
}
}
MakeMaze.java
import java.applet.Applet;
import java.awt.*;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.*;
import java.awt.event.MouseListener;
import java.util.ArrayList;
public class MakeMaze extends Applet implements KeyListener, MouseListener
{
public static final int ROWS=10, COLS=14;
public static final int WIDTH=800, HEIGHT=600;
private Cell[][] m;
String message="Making Maze";
String message1 = "new Maze";
String message2 = "On";
String message3 = "Off";
private Image imgBuffer;
private Graphics gBuffer;
private Cell player;
private Box newMaze, activateRocket;
private Cell finish, rocket;
private int maze, score, overAll, rAmmo;
private boolean activeRocket;
public void init()
{
imgBuffer = createImage(WIDTH,HEIGHT);
gBuffer = imgBuffer.getGraphics();
message="Making First Maze";
makeMaze();
message="First Maze done.";
newMaze = new Box(500, 30, Color.ORANGE);
activateRocket = new Box(750, 200, 24, 24, Color.BLUE);
overAll = 0;
maze = 1;
rAmmo = 0;
activeRocket = false;
this.addKeyListener(this);
this.addMouseListener(this);
}
public void makeMaze()
{
m = new Cell[ROWS][COLS];
for (int r=0;r<ROWS;r++)
for(int c=0;c<COLS;c++)
m[r][c]=new Cell(r,c);
player = m[0][0];
player.setColor(Color.BLUE);
finish = m[ROWS - 1][COLS -1];
finish.setColor(Color.CYAN);
rocket = m[(int)(Math.random() * 9) + 1][(int)(Math.random() * 13) + 1];
rocket.setRockets(1);
Cell currentCell=m[ROWS-1][0];
currentCell.incVisits();
ArrayList<Cell> stack=new ArrayList<Cell>();
while (unvisitedCells()){
Cell choice=currentCell.randomNeighbor(m);
if (choice!=null){
stack.add(0, currentCell);
currentCell.removeWall(choice);
currentCell=choice;
currentCell.incVisits();
}else{
stack.remove(0);
currentCell=stack.get(0);
}
repaint();
}
if(maze > 2)
{
m[(int)(Math.random() * 9) + 1][(int)(Math.random() * 13) + 1]
= new Rocket((int)(Math.random() * 9) + 1, ((int)Math.random() * 13) + 1);
repaint();
}
} private boolean unvisitedCells(){
int r=0;
while(r<ROWS){
int c=0;
while(c<COLS)
if(m[r][c].getVisits()<0){
return true;
}else{
c++;
}
r++;
}
return false;
}
public void paint(Graphics g)
{
gBuffer.setColor(Color.WHITE);
gBuffer.fillRect(0, 0, WIDTH, HEIGHT);
gBuffer.setColor(Color.BLACK);
gBuffer.drawString(message, 20, 20);
gBuffer.drawString(maze + "", 200, 20);
gBuffer.drawString(score + " points",200,30);
gBuffer.drawString(overAll + "Total points",300,30);
gBuffer.drawString(rAmmo + " Rockets",400,30);
gBuffer.drawString(message1, 525, 30);
if(activeRocket)
gBuffer.drawString(message2, 750, 200);
else{
gBuffer.drawString(message3, 750, 200);
}
gBuffer.fillRect(Cell.LEFT-Cell.GAP, Cell.TOP-Cell.GAP,
COLS*(Cell.SIZE+Cell.GAP)+Cell.GAP, ROWS*(Cell.SIZE+Cell.GAP)+Cell.GAP);
for (int r=0;r<ROWS;r++)
for(int c=0;c<COLS;c++)
m[r][c].draw(gBuffer, Color.YELLOW);
rocket.drawP(gBuffer, Color.ORANGE);
player.drawP(gBuffer, Color.BLUE);
finish.drawP(gBuffer, Color.CYAN);
newMaze.draw(gBuffer);
activateRocket.draw(gBuffer);
g.drawImage(imgBuffer,0,0,this);
}
public void update(Graphics g)
{
paint(g);
}
@Override
public void mousePressed(MouseEvent e)
{
}
@Override
public void mouseReleased(MouseEvent e)
{
}
@Override
public void mouseEntered(MouseEvent e)
{
}
@Override
public void mouseExited(MouseEvent e)
{
}
@Override
public void mouseClicked(MouseEvent e)
{
if(newMaze.contains(e.getX(), e.getY()))
{
makeMaze();
}
if(activateRocket.contains(e.getX(), e.getY()))
{
if(hasRockets())
{
activeRocket = !activeRocket;
if(activeRocket)
activateRocket.setColor(Color.RED);
if(!activeRocket)
activateRocket.setColor(Color.BLUE);
}
else
{
activateRocket.setColor(Color.BLUE);
}
}
repaint();
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode=e.getKeyCode();
int r=player.getRow();
int c=player.getCol();
Cell next=null;
if(keyCode==38 && r>0)
{ //up
if(player.canMoveUp())
next=m[r-1][c];
} else if (keyCode==40 && r<ROWS-1){
if(player.canMoveDown())//down
next=m[r+1][c];
} else if (keyCode==37 && c>0){
if(player.canMoveLeft())//left
next=m[r][c-1];
} else if (keyCode==39 && c<COLS-1){
if(player.canMoveRight())//right
next=m[r][c+1];
}
if(keyCode==38 && r>0)
{ //up
if(activeRocket && !player.canMoveUp())
{
next=m[r-1][c];
player.removeWall(next);
rAmmo--;
if(rAmmo == 0)
{
activeRocket = !activeRocket;
activateRocket.setColor(Color.BLUE);
}
}
} else if (keyCode==40 && r<ROWS-1){
if(activeRocket && !player.canMoveDown())//down
{
next=m[r+1][c];
player.removeWall(next);
rAmmo--;
if(rAmmo == 0)
{
activeRocket = !activeRocket;
activateRocket.setColor(Color.BLUE);
}
}
} else if (keyCode==37 && c>0){
if(activeRocket && !player.canMoveLeft())//left
{
next=m[r][c-1];
player.removeWall(next);
rAmmo--;
if(rAmmo == 0)
{
activeRocket = !activeRocket;
activateRocket.setColor(Color.BLUE);
}
}
} else if (keyCode==39 && c<COLS-1){
if(activeRocket && !player.canMoveRight())//right
{
next=m[r][c+1];
player.removeWall(next);
rAmmo--;
if(rAmmo == 0)
{
activeRocket = !activeRocket;
activateRocket.setColor(Color.BLUE);
}
}
}
if (next!=null ){
player.incVisits();
player=next;
player.setColor(Color.BLUE);
message="Current Tile: "+player.toString();
}
if(next.is(finish))
{
overAll += getScore();
maze++;
makeMaze();
}
if(next.is(rocket))
{
rAmmo+= rocket.getRockets();
rocket.loseRocket(1);
}
getScore();
repaint();
}
public int getScore()
{
score = 0;
for (int i = 0; i < m.length; i++)
{
for(int j = 0; j < m[0].length; j++)
{
if(m[i][j].getVisits() > 1)
score --;
if(m[i][j].getVisits() == 1)
score++;
}
}
return score;
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
public boolean hasRockets()
{
return rAmmo > 0;
}
}
