Created
October 4, 2016 13:15
-
-
Save ivstuart/50112a31a6657001ca52c2d94c30d4b4 to your computer and use it in GitHub Desktop.
HauntedHouse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
/** | |
* Haunted House Adventure | |
* Original BASIC version from the 1983 Usborne computer book | |
* Write Your Own Adventure Programs for Your Microcomputer | |
* by Jenny Tyler && Les Howarth (ISBN 0-86020-741-2) | |
* Adapted from lau version by Marc Lepage (May 2014) | |
* Also see version in Ruby by JOSHUA M. CLULOW (2010) | |
* Created by Ivan Stuart on 03/10/2016. | |
*/ | |
public class HauntedHouse { | |
private int V = 25; | |
private int W = 36; | |
private int G = 18; | |
private int[] C = new int[W+1]; | |
private int[] L = {46, 38, 35, 50, 13, 18, 28, 42, 10, 25, 26, 4, 2, 7, 47, 60, 43, 32}; | |
private String[] AS = { | |
"HELP", "CARRYING", "GO", "N", "S", "W", "E", "U", "D", "GET", "TAKE", "OPEN", "EXAMINE", "READ", "SAY", | |
"DIG", "SWING", "CLIMB", "LIGHT", "UNLIGHT", "SPRAY", "USE", "UNLOCK", "LEAVE", "SCORE" | |
}; | |
private String[] RS = { | |
"SE", "WE", "WE", "SWE", "WE", "WE", "SWE", "WS", | |
"NS", "SE", "WE", "NW", "SE", "W", "NE", "NSW", | |
"NS", "NS", "SE", "WE", "NWUD", "SE", "WSUD", "NS", | |
"N", "NS", "NSE", "WE", "WE", "NSW", "NS", "NS", | |
"S", "NSE", "NSW", "S", "NSUD", "N", "N", "NS", | |
"NE", "NW", "NE", "W", "NSE", "WE", "W", "NS", | |
"SE", "NSW", "E", "WE", "NW", "S", "SW", "NW", | |
"NE", "NWE", "WE", "WE", "WE", "NWE", "NWE", "W" | |
}; | |
private String[] DS = {"DARK CORNER", "OVERGROWN GARDEN", "BY LARGE WOODPILE", "YARD BY RUBBISH", | |
"WEED PATCH", "FOREST", "THICK FOREST", "BLASTED TREE", | |
"CORNER OF HOUSE", "ENTRANCE TO KITCHEN", "KITCHEN AND GRIMY COOKER", "SCULLERY DOOR", | |
"ROOM WITH INCHES OF DUST", "REAR TURRET ROOM", "CLEARING BY HOUSE", "PATH", | |
"SIDE OF HOUSE", "BACK OF HALLWAY", "DARK ALCOVE", "SMALL DARK ROOM", | |
"BOTTOM OF SPIRAL STAIRCASE", "WIDE PASSAGE", "SLIPPERY STEPS", "CLIFFTOP", | |
"NEAR CRUMBLING WALL", "GLOOMY PASSAGE", "POOL OF LIGHT", "IMPRESSIVE VAULTED HALLWAY", | |
"HALL BY THICK WOODEN DOOR", "TROPHY ROOM", "CELLAR WITH BARRED WINDOW", "CLIFF PATH", | |
"CUPBOARD WITH HANGING COAT", "FRONT HALL", "SITTING ROOM", "SECRET ROOM", | |
"STEEP MARBLE STAIRS", "DINING ROOM", "DEEP CELLAR WITH COFFIN", " CLIFF PATH", | |
"CLOSET", "FRONT LOBBY", "LIBRARY OF EVIL BOOKS", "STUDY WITH DESK & HOLE IN WALL", | |
"WEIRD COBWEBBY ROOM", "VERY COLD CHAMBER", "SPOOKY ROOM", "CLIFF PATH BY MARSH", | |
"RUBBLE-STREWN VERANDA", "FRONT PORCH", "FRONT TOWER", "SLOPING CORRIDOR", | |
"UPPER GALLERY", "MARSH BY WALL", "MARSH", "SOGGY PATH", | |
"BY TWISTED RAILING", "PATH THROUGH IRON GATE", "BY RAILINGS", "BENEATH FRONT TOWER", | |
"DEBRIS FROM CRUMBLING FACADE", "LARGE FALLEN BRICKWORK", "ROTTING STONE ARCH", "CRUMBLING CLIFFTOP" | |
}; | |
private String[] OS = { | |
"PAINTING", "RING", "MAGIC SPELLS", "GOBLET", "SCROLL", "COINS", "STATUE", "CANDLESTICK", | |
"MATCHES", "VACUUM", "BATTERIES", "SHOVEL", "AXE", "ROPE", "BOAT", "AEROSOL", "CANDLE", "KEY", | |
"NORTH", "SOUTH", "WEST", "EAST", "UP", "DOWN", | |
"DOOR", "BATS", "GHOSTS", "DRAWER", "DESK", "COAT", "RUBBISH", | |
"COFFIN", "BOOKS", "XZANFAR", "WALL", "SPELLS" | |
}; | |
private int LL = 60; | |
private int RM = 57; | |
private String MS = "OK"; | |
private int VB = 0; | |
private int OB = 0; | |
private String VS = ""; | |
private String WS = ""; | |
private int[] F = new int[W+1]; | |
public HauntedHouse() { | |
F[18] = 1; | |
F[17] = 1; | |
F[2] = 1; | |
F[26] = 1; | |
F[28] = 1; | |
F[23] = 1; | |
} | |
public void help() { | |
System.out.println("WORDS I KNOW:"); | |
for (String verb : AS) { | |
System.out.println(verb); | |
} | |
} | |
public void carrying() { | |
System.out.println("YOU ARE CARRYING:"); | |
for (int i = 1; i < C.length; i++) { | |
if (C[i] == 1) { | |
System.out.println(OS[i-1]); | |
} | |
} | |
} | |
public void go() { | |
int D = 0; | |
if (OB == 0) { | |
D = VB - 3; | |
} | |
if (OB == 19) { | |
D = 1; | |
} | |
if (OB == 20) { | |
D = 2; | |
} | |
if (OB == 21) { | |
D = 3; | |
} | |
if (OB == 22) { | |
D = 4; | |
} | |
if (OB == 23) { | |
D = 5; | |
} | |
if (OB == 24) { | |
D = 6; | |
} | |
if (RM == 20 && D == 5) { | |
D = 1; | |
} | |
if (RM == 20 && D == 6) { | |
D = 3; | |
} | |
if (RM == 22 && D == 6) { | |
D = 2; | |
} | |
if (RM == 22 && D == 5) { | |
D = 3; | |
} | |
if (RM == 36 && D == 6) { | |
D = 1; | |
} | |
if (RM == 36 && D == 5) { | |
D = 2; | |
} | |
if (F[14] == 1) { | |
MS = "CRASH! YOU FELL OUT OF THE TREE!"; | |
F[14] = 0; | |
return; | |
} | |
if (F[27] == 1 && RM == 52) { | |
MS = "GHOSTS WILL NOT LET YOU MOVE"; | |
return; | |
} | |
if (RM == 45 && C[1] == 1 && F[34] == 0) { | |
MS = "A MAGICAL BARRIER TO THE WEST"; | |
return; | |
} | |
if ((RM == 26 && F[0] == 0) && (D == 1 || D == 4)) { | |
MS = "YOU NEED A LIGHT"; | |
return; | |
} | |
if (RM == 54 && C[15] != 1) { | |
MS = "YOU'RE STUCK!"; | |
return; | |
} | |
if (C[15] == 1 && !(RM == 53 || RM == 54 || RM == 55 || RM == 47)) { | |
MS = "YOU CAN'T CARRY A BOAT!"; | |
return; | |
} | |
if (RM > 26 && RM < 30 && F[0] == 0) { | |
MS = "TOO DARK TO MOVE"; | |
return; | |
} | |
F[35] = 0; | |
for (int i = 0; i < (RS[RM]).length(); i++) { | |
String US = RS[RM].substring(i, i + 1); | |
if (US.equalsIgnoreCase("N") && D == 1 && F[35] == 0) { | |
RM = RM - 8; | |
F[35] = 1; | |
break; | |
} | |
if (US.equalsIgnoreCase("S") && D == 2 && F[35] == 0) { | |
RM = RM + 8; | |
F[35] = 1; | |
break; | |
} | |
if (US.equalsIgnoreCase("W") && D == 3 && F[35] == 0) { | |
RM = RM - 1; | |
F[35] = 1; | |
break; | |
} | |
if (US.equalsIgnoreCase("E") && D == 4 && F[35] == 0) { | |
RM = RM + 1; | |
F[35] = 1; | |
break; | |
} | |
} | |
MS = "OK"; | |
if (F[35] == 0) { | |
MS = "CAN'T GO THAT WAY!"; | |
} | |
if (D < 1) { | |
MS = "GO WHERE?"; | |
} | |
if (RM == 41 && F[23] == 1) { | |
RS[49] = "SW"; | |
MS = "THE DOOR SLAMS SHUT!"; | |
F[23] = 0; | |
} | |
} | |
public void get() { | |
if (OB > G || OB==0) { | |
MS = "I CAN'T GET " + WS; | |
return; | |
} | |
if (L[OB-1] != RM) { | |
MS = "IT ISN'T HERE"; | |
} | |
if (F[OB] != 0) { | |
MS = "WHAT " + WS + "?"; | |
} | |
if (C[OB] == 1) { | |
MS = "YOU ALREADY HAVE IT"; | |
} | |
if (OB > 0 && L[OB-1] == RM && F[OB] == 0) { | |
C[OB] = 1; | |
L[OB-1] = 65; | |
MS = "YOU HAVE THE " + WS; | |
} | |
} | |
public void open() { | |
if (RM == 43 && (OB == 28 || OB == 29)) { | |
F[17] = 0; | |
} | |
MS = "DRAWER OPEN"; | |
if ((RM == 28) && (OB == 25)) { | |
MS = "IT'S LOCKED"; | |
} | |
if ((RM == 38) && (OB == 32)) { | |
MS = "THAT'S CREEPY!"; | |
} | |
F[2] = 0; | |
} | |
public void examine() { | |
if (OB == 30) { | |
F[18] = 0; | |
MS = "SOMETHING HERE!"; | |
} | |
if (OB == 31) { | |
MS = "THAT'S DISGUSTING!"; | |
} | |
if (OB == 28 || OB == 29) { | |
MS = "THERE IS A DRAWER"; | |
} | |
if ((OB == 33) || (OB == 5)) { | |
read(); | |
} | |
if ((RM == 43) && (OB == 35)) { | |
MS = "THERE IS SOMETHING BEYOND..."; | |
} | |
if (OB == 32) { | |
open(); | |
} | |
} | |
public void read() { | |
if (RM == 42 && OB == 33) { | |
MS = "THEY ARE DEMONIC WORKS"; | |
} | |
if ((OB == 3 || OB == 36) && (C[3] == 1 && F[34] == 0)) { | |
MS = "USE THIS WORD WITH CARE 'XZANFAR'"; | |
} | |
if (C[5] == 1 && OB == 5) { | |
MS = "THE SCRIPT IS IN AN ALIEN TONGUE"; | |
} | |
} | |
public void say() { | |
MS = "OK '" + WS + "'"; | |
if ((C[3] == 1) && (OB == 34)) { | |
MS = "*MAGIC OCCURS*"; | |
} | |
if (RM != 45) { | |
RM = random(63); | |
} | |
if (C[3] == 1 && OB == 34 && RM == 45) { | |
F[34] = 1; | |
} | |
} | |
public void dig() { | |
if (C[12] == 1) { | |
MS = "YOU MADE A HOLE"; | |
} | |
if (C[12] == 1 && RM == 30) { | |
MS = "DUG THE BARS OUT"; | |
DS[RM] = "HOLE IN WALL"; | |
RS[RM] = "NSE"; | |
} | |
} | |
public void swing() { | |
if (C[14] != 1 && RM == 7) { | |
MS = "THIS IS NO TIME TO PLAY GAMES"; | |
} | |
if (OB == 14 && C[14] == 1) { | |
MS = "YOU SWUNG IT"; | |
} | |
if (OB == 13 && C[13] == 1) { | |
MS = "WHOOSH"; | |
} | |
if (OB == 13 && C[13] == 1 && RM == 43) { | |
RS[RM] = "WN"; | |
DS[RM] = "STUDY WITH SECRET ROOM"; | |
MS = "YOU BROKE THE THIN WALL"; | |
} | |
} | |
public void climb() { | |
if (OB == 14 && C[14] == 1) { | |
MS = "IT ISN'T ATTACHED TO ANYTHING!"; | |
} | |
if (OB == 14 && C[14] != 1 && RM == 7 && F[14] == 0) { | |
MS = "YOU SEE THICK FOREST AND CLIFF SOUTH"; | |
F[14] = 1; | |
return; | |
} | |
if (OB == 14 && C[14] != 1 && RM == 7 && F[14] == 1) { | |
MS = "GOING DOWN!"; | |
F[14] = 0; | |
} | |
} | |
public void light() { | |
if (OB == 17 && C[17] == 1 && C[8] == 0) { | |
MS = "IT WILL BURN YOUR HANDS"; | |
} | |
if (OB == 17 && C[17] == 1 && C[9] == 0) { | |
MS = "NOTHING TO LIGHT IT WITH"; | |
} | |
if (OB == 17 && C[17] == 1 && C[9] == 1 && C[8] == 1) { | |
MS = "IT CASTS A FLICKERING LIGHT"; | |
F[0] = 1; | |
} | |
} | |
public void unlight() { | |
if (F[0] == 1) { | |
F[0] = 0; | |
MS = "EXTINGUISHED"; | |
} | |
} | |
public void spray() { | |
if (OB == 26 && C[16] == 1) { | |
MS = "HISSSS"; | |
} | |
if (OB == 26 && C[16] == 1 && F[26] == 1 && RM == 13) { | |
F[26] = 0; | |
MS = "PFFT! GOT THEM"; | |
} | |
} | |
public void use() { | |
if (OB == 10 && C[10] == 1 && C[11] == 1) { | |
MS = "SWITCHED ON"; | |
F[24] = 1; | |
} | |
if (F[27] == 1 && F[24] == 1) { | |
MS = "WHIZZ- VACUUMED THE GHOSTS UP!"; | |
F[27] = 0; | |
} | |
} | |
public void unlock() { | |
if (RM == 43 && (OB == 27 || OB == 28)) { | |
open(); | |
} | |
if (RM == 28 && OB == 25 && F[25] == 0 && C[18] == 1) { | |
F[25] = 1; | |
RS[RM] = "SEW"; | |
DS[RM] = "HUGE OPEN DOOR"; | |
MS = "THE KEY TURNS!"; | |
} | |
} | |
public void leave() { | |
if (C[OB] == 1) { | |
C[OB] = 0; | |
L[OB-1] = RM; | |
MS = "DONE"; | |
} | |
} | |
public void score() { | |
int S = 0; | |
for (int i = 0; i < C.length; i++) { | |
if (C[i] == 1) { | |
S++; | |
} | |
} | |
if (S == 17 && C[15] != 1 && RM != 57) { | |
System.out.println("YOU HAVE EVERYTHING"); | |
System.out.println("RETURN TO THE GATE FOR FINAL SCORE"); | |
} | |
if (S == 17 && RM == 57) { | |
System.out.println("DOUBLE SCORE FOR REACHING HERE!"); | |
S = S * 2; | |
} | |
System.out.println("YOUR SCORE=" + S); | |
if (S > 18) { | |
System.out.println("WELL DONE! YOU FINISHED THE GAME"); | |
System.exit(0); | |
} | |
} | |
public static void main(String args[]) throws IOException { | |
System.out.println(); | |
System.out.println(); | |
System.out.println("HAUNTED HOUSE"); | |
System.out.println("--------------"); | |
HauntedHouse hauntedHouse = new HauntedHouse(); | |
for (; ; ) { | |
hauntedHouse.main(); | |
} | |
} | |
public void main() throws IOException { | |
System.out.println("YOUR LOCATION"); | |
System.out.println((DS[RM])); | |
System.out.println("EXITS:"); | |
System.out.println((RS[RM])); | |
for (int i = 0; i < G; i++) { | |
if (L[i] == RM && F[i+1] == 0) { | |
System.out.println("YOU CAN SEE " + OS[i] + " HERE"); | |
} | |
} | |
System.out.println("============================"); | |
System.out.println((MS)); | |
MS = "WHAT"; | |
System.out.println("WHAT WILL YOU DO NOW "); | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String QS = br.readLine(); | |
VS = ""; | |
WS = ""; | |
VB = 0; | |
OB = 0; | |
String words[] = QS.split(" ", 2); | |
VS = words[0]; | |
if (words.length == 2) { | |
WS = words[1]; | |
} | |
if (words.length == 1) { | |
VS = QS; | |
} | |
for (int i = 0; i < AS.length; i++) { | |
if (VS.equalsIgnoreCase(AS[i])) { | |
VB = i+1; | |
break; | |
} | |
} | |
for (int i = 0; i < OS.length; i++) { | |
if (WS.equalsIgnoreCase(OS[i])) { | |
OB = i+1; | |
break; | |
} | |
} | |
if (WS.length() > 0 && OB == 0) { | |
MS = "THAT'S SILLY"; | |
} | |
if (VB == 0) { | |
VB = V + 1; | |
} | |
if (WS.equals("")) { | |
MS = "I NEED TWO WORDS"; | |
} | |
if (VB > V && OB > 0) { | |
MS = "YOU CAN'T '" + QS + "'"; | |
} | |
if (VB > V && OB == 0) { | |
MS = "YOU DON'T MAKE SENSE"; | |
return; | |
} | |
if (VB < V && OB > 0 && C[OB] == 0) { | |
MS = "YOU DON'T HAVE '" + WS + "'"; | |
} | |
if (F[26] == 1 && RM == 13 && random(3) != 3 && VB != 21) { | |
MS = "BATS ATTACKING!"; | |
return; | |
} | |
if (RM == 44 && random(2) == 1 && F[24] != 1) { | |
F[27] = 1; | |
} | |
if (F[0] == 1) { | |
LL = LL - 1; | |
} | |
if (LL < 1) { | |
F[0] = 0; | |
} | |
// System.out.println("VB="+VB+"OB="+OB); | |
if (VB > 2 && VB < 9) { | |
go(); | |
} else if (VB == 10 || VB == 11) { | |
get(); | |
} else { | |
if (VB > 0) { | |
Class<?> c = HauntedHouse.class; | |
try { | |
Method method = c.getMethod(VS.toLowerCase(), null); | |
method.invoke(this, null); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} catch (InvocationTargetException e) { | |
e.printStackTrace(); | |
} catch (NoSuchMethodException e) { | |
MS = "YOU CAN'T '" + QS + "'"; | |
} | |
} | |
} | |
if (LL == 10) { | |
MS = "YOUR CANDLE IS WANING!"; | |
} | |
if (LL == 1) { | |
MS = "YOUR CANDLE IS OUT!"; | |
} | |
} | |
public int random(int range) { | |
return (int) (1 + (Math.random() * range)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment