Source Code for Maze Applet

 

import java.io.*

import java.util.*;

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

import javagently.*;

 

 

 

public class Maze3 extends Applet{

 

/* class to offer GUI adventure game applet */

 

private static final int start = 0; //start position

private static final int no_of_directions = 4; // N,S E and W

private static final int no_of_rooms = 16; // 4 x 4 maze

private static final int horigap = 15;

private static final int vertigap = 10;

private int room = start;

private static int [] [] roomarray = new int [no_of_directions][no_of_rooms]; //map

String message = "Click on the direction buttons to move";

 

private static TextField StatusField, WallField, ExitField;

 

 

 

public void init(){

 

// The constructor is responsible for setting up the initial buttons text fields and colour background.

 

setBackground(Color.cyan);

setForeground(Color.black);

setLayout(new FlowLayout(FlowLayout.CENTER, horigap, vertigap));

add(new Label(message));

 

for(int roomindex = 0; roomindex < no_of_rooms; roomindex++)

for (int direction = 0; direction < no_of_directions; direction++)

roomarray[direction][roomindex]=-1;

 

roomarray[0][0] = 4;

roomarray[0][1] = 5;

roomarray[2][1] = 2;

roomarray[2][2] = 3;

roomarray[3][2] = 1;

roomarray[0][3] = 7;

roomarray[3][3] = 2;

roomarray[0][4] = 8;

roomarray[1][4] = 0;

roomarray[0][5] = 9;

roomarray[1][5] = 1;

roomarray[0][6] = 10;

roomarray[0][7] = 11;

roomarray[1][7] = 3;

roomarray[0][8] = 12;

roomarray[1][8] = 4;

roomarray[2][8] = 9;

roomarray[1][9] = 5;

roomarray[3][9] = 8;

roomarray[0][10] = 14;

roomarray[1][10] = 6;

roomarray[2][10] = 11;

roomarray[1][11] = 7;

roomarray[3][11] = 10;

roomarray[1][12] = 8;

roomarray[2][13] = 14;

roomarray[1][14] = 10;

roomarray[2][14] = 15;

roomarray[3][14] = 13;

roomarray[2][15] = 16;

roomarray[3][15] = 14;

 

Panel q = new Panel();

q.setLayout(new FlowLayout());

q.add (new Button("North"));

q.add(new Button("East"));

q.add(new Button("South"));

q.add ( new Button("West"));

add("North",q);

 

Panel p = new Panel();

p.setLayout(new FlowLayout());

StatusField = new TextField(20);

StatusField.setEditable(false);

p.add(StatusField);

ExitField = new TextField(25);

ExitField.setEditable(false);

p.add(ExitField);

WallField = new TextField(20);

WallField.setEditable(false);

p.add(WallField);

add("South", p);

 

showPosition(room);

showExits(room,roomarray);

 

}

 

public boolean action(Event evt, Object arg){

boolean reply = true;

if (arg.equals ("North")) {

walltest(room,0,roomarray);

room = newPosition(room,0,roomarray);

showPosition(room);

showExits(room,roomarray);

} else if (arg.equals ("East")){

walltest(room,2,roomarray);

room=newPosition(room,2,roomarray);

showPosition(room);

showExits(room,roomarray);

} else if (arg.equals ("South")){

walltest(room,1,roomarray);

room=newPosition(room,1,roomarray);

showPosition(room);

showExits(room,roomarray);

}else if (arg.equals ("West")) {

walltest(room,3,roomarray);

room=newPosition(room,3,roomarray);

showPosition(room);

showExits(room,roomarray);

}else if (room ==16) reply = false;

return reply ;

}

 

 

 

public static int newPosition (int room, int direction, int roomarray[][]){

 

// method to give new state from current room, direction to travel and map

int position;

position = roomarray[direction][room];

if ( position >= 0) return position;

else return room;

}

 

public static void showPosition(int room){

 

// method to print out current position

 

if (room != 16)StatusField.setText("You are now in in room " + room);

if (room == 16)StatusField.setText("Solved");

}

 

public static void showExits(int room,int roomarray[][]){

 

// method to show exits given room and map

 

String intro = "There are exits to the: ";

String n = " " ;

String s = " " ;

String e = " " ;

String w = " " ;

 

if (room != 16){

for(int counter = 0;counter <=3;counter++)

if (roomarray[counter][room]>0){

switch(counter){

case 0: n="N ";break;

case 1: s="S ";break;

case 2: e="E ";break;

case 3: w="W ";break;

}

ExitField.setText(intro + n + s + e + w);

}

};

}

 

public void walltest (int room, int direction, int roomarray[][]){

 

// method to test for wall given current room, direction to travel and map

 

if (roomarray[direction][room] < 0) {WallField.setText("You have hit a wall");

play(getCodeBase(), "ouch.au");}

else WallField.setText(" ");

}

 

 

}