text-based demo code

Started by
2 comments, last by obi-wan shinobi 19 years ago
This is my latest text-based work, which is somewhat based on my other thread a while ago, but with a few differences. To test it out, just copy and paste into your Java IDE/editor. Notes: -This is roughly 700 lines of Java (instead of c++) code done over 2 weeks -The code may very well be confusing; sorry for not commenting it yet -I realize now that there is a better way to do this, so I'm going to try that way next (using more than 1 class, inheritance, etc.) Any questions or comments?

import java.io.*;
import java.lang.*;

class areaone {
	BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
	int room;			String input;			int score;
	String o;			String i;				String t;
	int xaxis=5;		int yaxis=5;			int zaxis=2;			
	int[][] state=new int[50][4];	
	int[][] object=new int[50][4];	
	int[][] path=new int[50][4];
	int[] pitem=new int[16];
	String[] items=new String[50];
	String[] choice=new String[16];
	String wrongway="You cannot move in that direction here.\n";
	
	public areaone() {
		room=0;							score=0;
		choice[0]="Move North ";		choice[1]="Move East  ";
		choice[2]="Move West  ";		choice[3]="Move South ";
		choice[4]="Pull       ";		choice[5]="Push       ";
		choice[6]="Operate    ";		choice[7]="Inspect    ";
		choice[8]="Take       ";		choice[9]="Use item   ";
		choice[10]="Look Around";		choice[11]="Check Items";
		choice[12]="View Map   ";
		
		items[0]="";					items[1]="rusted key";
		items[2]="locked door";			items[3]="wall lever";
		items[4]="dead soldier";		items[5]="dead child";
		items[6]="dead scientist";		items[7]="cardboard box";
		items[8]="combination lock";	items[9]="fuse";
		items[10]="fuse box";			items[11]="chemical A1";
		items[12]="chemical B2";		items[13]="chemical C3";
		items[14]="chemical D4";		items[15]="flashing button";
		items[16]="red gauge";			items[17]="green gauge";
		items[18]="blue gauge";			items[19]="startup circuit";
		items[20]="red button";			items[21]="green button";
		items[22]="blue button";		items[23]="luminous rock";
		items[24]="elevator";			items[25]="dim rock";
		items[26]="crumbled note";		items[27]="card key";
		items[28]="turret";				items[29]="explosives";
		items[30]="rusty pick";			items[31]="dull pick";
		items[32]="unrefined coal";		items[33]="refined coal";
		items[34]="exposed wires";		items[35]="security door";
		items[36]="sharp pick";			items[37]="coal refiner";
		items[38]="coal furnace";		items[39]="mining lift";
		items[40]="radar jammer";		items[41]="command console";
		items[42]="sunbeam cover";		items[43]="sunbeam path";
		items[44]="giant door";
		
		object[2][0]=2;		object[2][1]=3;
		object[3][0]=23;
		object[4][0]=15;	object[4][1]=16;	object[4][2]=17;	object[4][3]=18;
		object[6][0]=4;		object[6][1]=5;		object[6][2]=6;
		object[7][0]=7;		object[7][1]=8;
		object[8][0]=10;	object[8][1]=24;
		object[9][0]=11;	object[9][1]=12;	object[9][2]=13;
		object[10][0]=41;	object[10][1]=26;
		object[12][0]=29;
		object[13][0]=19;	object[13][1]=20;	object[13][2]=21;	object[13][3]=22;
		object[14][0]=42;	object[14][1]=40;
		object[16][0]=35;
		object[17][0]=35;
		object[19][0]=35;
		object[20][0]=29;
		object[23][0]=39;
		
		object[27][0]=28;
		object[31][0]=37;
		object[30][0]=38;
		object[36][0]=28;
		object[40][0]=32;
		object[41][0]=44;
		object[43][0]=34;
		object[48][0]=39;
		object[49][0]=30;
		
		path[0][1]=1;		
		path[1][1]=1;		path[1][2]=1;
		path[2][2]=1;
		path[3][3]=1;
		path[4][3]=1;
		path[7][1]=1;
		path[8][0]=1;		path[8][1]=1;		path[8][2]=1;
		path[9][0]=1;		path[9][2]=1;
		path[10][1]=1;
		path[11][2]=1;		path[11][3]=1;
		path[15][3]=1;
		path[16][0]=1;		path[16][3]=1;
		path[17][1]=1;
		path[18][1]=1;		path[18][2]=1;
		path[19][2]=1;		path[19][3]=1;
		path[20][0]=1;
		path[21][0]=1;		path[21][1]=1;
		path[22][1]=1;		path[22][2]=1;
		path[23][1]=1;		path[23][2]=1;
		path[24][0]=1;		path[24][2]=1;
		
		path[26][3]=1;
		path[28][1]=1;		
		path[29][2]=1;		path[29][3]=1;
		path[30][3]=1;
		path[31][0]=1;
		path[32][3]=1;
		path[33][1]=1;
		path[34][0]=1;		path[34][2]=1;		path[34][3]=1;
		path[35][0]=1;		path[35][3]=1;
		path[37][0]=1;		
		path[38][1]=1;		path[38][3]=1;
		path[39][0]=1;		path[39][2]=1;		path[39][3]=1;
		path[40][0]=1;
		path[42][1]=1;		path[42][3]=1;
		path[43][0]=1;		path[43][2]=1;
		path[44][0]=1;		path[44][3]=1;		
		path[47][0]=1;		path[47][1]=1;
		path[48][2]=1;
		path[49][0]=1;
	}
	
	public void roomname() {
		System.out.print("Current Location: ");
		switch(room) {
			case 0:		System.out.println("[ Cave Entrance ]");		break;
			case 1:		System.out.println("[ Creepy Corridor ]");		break;
			case 2:		System.out.println("[ Scrap Dump ]");			break;
			case 3:		System.out.println("[ Illuminated Tunnel ]");	break;
			case 4:		System.out.println("[ Engine Chamber ]");		break;
			case 6:		System.out.println("[ Dungeon Cell ]");			break;
			case 7:		System.out.println("[ Guard Station A ]");		break;
			case 8:		System.out.println("[ Central Hub - B1 ]");		break;
			case 9:		System.out.println("[ Chemical Storage ]");		break;
			case 10:	System.out.println("[ Lab Control Center ]");	break;
			case 11:	System.out.println("[ Laboratory ]");			break;
			case 12:	System.out.println("[ Mining Workshop ]");		break;
			case 13:	System.out.println("[ Adjustment Room ]");		break;
			case 14:	System.out.println("[ Sunny Balcony ]");		break;
			case 15:	System.out.println("[ Weapon Cache - Guns ]");	break;
			case 16:	System.out.println("[ Hall Leading to Lab ]");	break;
			case 17:	System.out.println("[ Outside the Workshop ]");	break;
			case 18:	System.out.println("[ Workshop Path ]");		break;
			case 19:	System.out.println("[ Guard Station B ]");		break;
			case 20:	System.out.println("[ Weapon Cache - Bombs ]"); break;
			case 21:	System.out.println("[ Specimen Archives ]");	break;
			case 22:	System.out.println("[ Narrow Passage ]");		break;
			case 23:	System.out.println("[ Mining Lift - B1 ]");		break;
			case 24:	System.out.println("[ Monitored Hall ]");		break;
			case 26:	System.out.println("[ Waste Disposal Room ]");	break;
			case 27:	System.out.println("[ Stalagmite Junction ]");	break;
			case 28:	System.out.println("[ Bridge Over A Pit ]");	break;
			case 29:	System.out.println("[ Torch-Lit Passage ]");	break;
			case 30:	System.out.println("[ Production Room ]");		break;
			case 31:	System.out.println("[ Refinery ]");				break;
			case 32:	System.out.println("[ Straightforward Hall ]");	break;
			case 33:	System.out.println("[ Central Hub - B2 ]");		break;
			case 34:	System.out.println("[ Chilly Hallway ]");		break;
			case 35:	System.out.println("[ Fork in the Road ]");		break;
			case 36:	System.out.println("[ Stalactite Junction ]");	break;
			case 37:	System.out.println("[ Guard Station C ]");		break;
			case 38:	System.out.println("[ Just Another Hall ]");	break;
			case 39:	System.out.println("[ Chillier Hallway ]");		break;
			case 40:	System.out.println("[ Coal Mines ]");			break;
			case 41:	System.out.println("[ Silent Passageway ]");	break;
			case 42:	System.out.println("[ Guard Station D ]");		break;
			case 43:	System.out.println("[ Room Full of Wires ]");	break;
			case 44:	System.out.println("[ Chilliest Hallway ]");	break;
			case 46:	System.out.println("[ The Inner Sanctum ]");	break;
			case 47:	System.out.println("[ Resting Quarters ]");		break;
			case 48:	System.out.println("[ Mining Lift - B2 ]");		break;
			case 49:	System.out.println("[ Dusty Dead End ]");		break;
			default:	break;
		}
		System.out.println();
	}
	
	public String trimspace(String opt) {
		opt = opt.trim();
		opt = opt.toLowerCase();
		return opt;
	}
	
	public String choose() throws IOException {
		for(int i=0;i<13;i++) {
			System.out.print("\t*"+choice+"*");
			if(i%2==0)		System.out.print("\t");
			else			System.out.println();
		}
		
		System.out.print("\nWhat will you do?\t");
		input = in.readLine();
		for(int j=0;j<13;j++) {
			t = trimspace(choice[j]);
			if(input.compareToIgnoreCase(t)==0) {
				return input.toLowerCase();
			}
		}
		System.out.println("--Invalid choice.");
		return "void";
	}
	
	public int move(String d) throws IOException {
		int a=-1;
		for(int i=0;i<4;i++) {
			t = trimspace(choice);
			if(d.compareTo(t)==0 && path[room]==1)
				{	a=i;	break;	}
			if(d.compareTo(t)==0 && path[room]==0)
				{	System.out.println(wrongway);	return 0;	}
		}
		switch(a) {
			case 0:		{	score-=5;	return -xaxis;		}
			case 1:		{	score-=5;	return 1;			}
			case 2:		{	score-=5;	return -1;			}
			case 3:		{	score-=5;	return xaxis;		}
			default:	System.out.println("--Invalid direction.");
		}
		return 0;
	}
	
	public void process(String p) throws IOException {
		if(p.compareTo("pull")==0)			pull();
		if(p.compareTo("push")==0)			push();
		if(p.compareTo("operate")==0)		operate();
		if(p.compareTo("inspect")==0)		inspect();
		if(p.compareTo("take")==0)			take();
		if(p.compareTo("use item")==0)		useitem();
	}
	
	public void pull() throws IOException {
		System.out.print("What would you like to pull?\t");
		String s=in.readLine();
		s=s.toLowerCase();
		
		if(s.compareTo(items[3])==0 && room==2 && state[2][1]==0) {
			System.out.println("(!)\tThe lever is now in the up position.");
			state[2][1]=1;		path[1][3]=1;		path[6][0]=1;		return;
		}
		if(s.compareTo(items[3])==0 && room==2 && state[2][1]==1) {
			System.out.println("(!)\tThe lever is now in the down position.");
			state[2][1]=0;		path[1][3]=0;		path[6][0]=0;		return;
		}
		if(s.compareTo("exposed wires")==0 && room==43 && state[43][0]==0) {
			if(pitem[4]==29 && pitem[5]==29 && pitem[6]==23) {
			System.out.println("(!)\tPower deactivated.  All B2 activity suspended.");
			path[27][1]=1;		path[27][2]=1;		path[27][3]=1;		score+=75;
			path[28][2]=1;		path[26][1]=1;		path[32][0]=1;
			path[36][1]=1;		path[36][2]=1;		path[36][3]=1;
			path[37][2]=1;		path[35][1]=1;		path[41][0]=1;
			state[8][1]=0;		state[33][0]=0;		state[43][0]=1;
			}
			else		System.out.println("(!)\tYou're missing a few things...");
			return;
		}
	}
	
	public void push() throws IOException {
		System.out.print("What would you like to push?\t");
		String s=in.readLine();
		s=s.toLowerCase();
		int i;
		
		if(s.compareTo("flashing button")==0 && room==4 &&
			object[4][0]==15 && state[8][0]==1) {
			System.out.println("(!)\tThe monitor now reads: 'Adjustment Room open'.");
			object[4][0]=0;		path[8][3]=1;		path[13][0]=1;
			score+=25;			return;
		}
		if(s.compareTo("exposed wires")==0 && room==43 && state[43][0]==1) {
			if(object[27][0]==0 && object[36][0]==0) {
			System.out.println("(!)\tPower reactivated.  All B2 activity resumed.");
			state[8][1]=1;		state[33][0]=1;		state[43][0]=2;		score+=75;
			}
			else		System.out.println("(!)\tYou've still got work to do...");
			return;
		}
		if(s.compareTo("red button")==0 && room==13) {
			System.out.print("(?)\tHow far in will you push the button (0-4)?\t");
			i=Integer.parseInt(in.readLine());
			if(i<5 && i>-1)			state[13][0]=i;
			else		System.out.println("(!)--Out of range.");
			return;
		}
		if(s.compareTo("green button")==0 && room==13) {
			System.out.print("(?)\tHow far in will you push the button (0-5)?\t");
			i=Integer.parseInt(in.readLine());
			if(i<6 && i>-1)			state[13][1]=i;
			else		System.out.println("(!)--Out of range.");
			return;
		}
		if(s.compareTo("blue button")==0 && room==13) {
			System.out.print("(?)\tHow far in will you push the button (0-3)?\t");
			i=Integer.parseInt(in.readLine());
			if(i<4 && i>-1)			state[13][2]=i;
			else		System.out.println("(!)--Out of range.");
			return;
		}
		if(s.compareTo("giant door")==0 && room==41) {
			if(state[30][0]==1 && state[43][0]==2) {
				System.out.println("(?)\tYou push the giant door open.");
				path[41][3]=1;		path[46][0]=1;		score+=50;
			}
			else		System.out.println("(!)\tIt's locked.");
		}
	}
	
	public void operate() throws IOException {
		System.out.print("What would you like to operate?\t");
		String s=in.readLine();
		s=s.toLowerCase();
		
		if(s.compareTo("elevator")==0 && room==8 && state[8][1]==1) {
			System.out.print("(?)\tTake the elevator down?\t");
			s=in.readLine();
			if(s.compareToIgnoreCase("yes")==0)	{
				System.out.println("(!)\tGoing down...");		room+=25;
				}
			else		System.out.println("(!)\tElevator ride cancelled.");
			return;
		}
		if(s.compareTo("command console")==0 && room==10 && state[10][0]==0) {
			System.out.print("(?)\tEnter a command:\t");
			s=in.readLine();
			if(s.compareToIgnoreCase("eject ck")==0 && object[10][1]!=34) {
				System.out.println("(!)\tCard key ejected from console.");
				object[10][1]=27;		state[10][0]=1;			score+=25;
			}
			else		System.out.println("(!)\tCommand not recognized.");
			return;
		}
		if(s.compareTo("mining lift")==0 && room==23) {
			System.out.print("(?)\tTake the lift down?\t");
			s=in.readLine();
			if(s.compareToIgnoreCase("yes")==0) {
				System.out.println("(!)\tGoing down...");		room+=25;
			}
			else		System.out.println("(!)\tLift riding cancelled.");
			return;
		}
		if(s.compareTo("elevator")==0 && room==33 && state[33][0]==1) {
			System.out.print("(?)\tTake the elevator up?\t");
			s=in.readLine();
			if(s.compareToIgnoreCase("yes")==0)	{
				System.out.println("(!)\tGoing up...");		room-=25;
				}
			else		System.out.println("(!)\tElevator ride cancelled.");
			return;
		}
		if(s.compareTo("mining lift")==0 && room==48) {
			System.out.print("(?)\tTake the lift up?\t");
			s=in.readLine();
			if(s.compareToIgnoreCase("yes")==0) {
				System.out.println("(!)\tGoing up...");		room-=25;
			}
			else		System.out.println("(!)\tLift riding cancelled.");
			return;
		}
		if(s.compareTo("startup circuit")==0 && room==13 && state[13][0]==3 && 
			state[13][1]==4 && state[13][2]==1 && state[8][1]!=1) {
				System.out.println("(!)\tThe elevator is now operational.");
				state[8][1]=1;		state[33][0]=1;			score+=25;		return;
			}
	}
	
	public void inspect() throws IOException {
		System.out.print("What would you like to inspect?\t");
		String s=in.readLine();
		s=s.toLowerCase();
		
		if(s.compareTo(items[5])==0 && room==6 && object[6][1]==5 && pitem[0]!=1) {
			System.out.println("(!)\tTurning over the body, you see a rusted key");
			object[6][3]=1;		score+=25;		return;
		}
		if(s.compareTo("cardboard box")==0 && room==7 &&
		 	object[7][0]==7 && pitem[1]!=9) {
			System.out.println("(!)\tUnder a pile of blank paper, you find a fuse");
			object[7][0]=9;		score+=25;		return;
		}
		if(s.compareTo("sunbeam cover")==0 && room==14 && object[14][0]==42) {
			System.out.println("(!)\tPerhaps you can break it with something sharp.");
			return;
		}
		if(s.compareTo("chemical a1")==0 && room==9) {
			System.out.println("(!)\tIt's marked 'Cleanser'");
			return;
		}
		if(s.compareTo("chemical b2")==0 && room==9) {
			System.out.println("(!)\tIt's marked, 'Brightener'");
			return;
		}
		if(s.compareTo("chemical c3")==0 && room==9) {
			System.out.println("(!)\tIt's marked, 'Sharpener'");
			return;
		}
		if(s.compareTo("crumbled note")==0 && room==10) {
			System.out.println("(!)\tIt reads, 'eject ck'.");
			return;
		}
		if(s.compareTo("red gauge")==0 && room==4) {
			System.out.println("(!)\tThe meter reads, '0.75'.");
			return;
		}
		if(s.compareTo("green gauge")==0 && room==4) {
			System.out.println("(!)\tThe meter reads, '0.80'.");
			return;
		}
		if(s.compareTo("blue gauge")==0 && room==4) {
			System.out.println("(!)\tThe meter reads, '0.33'.");
			return;
		}
		if(s.compareTo("exposed wires")==0 && room==43 && state[43][0]==0) {
			System.out.println("(!)\tPulling these wires would shut down the power.");
			return;
		}
		if(s.compareTo("exposed wires")==0 && room==43 && state[43][0]==1) {
			System.out.println("(!)\tPushing these wires would restore the power.");
			return;
		}
		if(s.compareTo("coal furnace")==0 && room==30) {
			System.out.println("(!)\tA warning notice reads, 'refined coal only'.");
			if(state[30][0]==0)
				System.out.println("(!)\tIt's barely producing any power...");
			return;
		}
		if(s.compareTo("giant door")==0 && room==41 && object[41][0]==44) {
			System.out.println("(!)\tDespite its size, you can push this door open.");
			System.out.println("\tThat is, if two sources of power are supplied.");
		}
	}
	
	public void take() throws IOException {
		System.out.print("What would you like to take?\t");
		String s=in.readLine();
		s=s.toLowerCase();
		
		if(s.compareTo(items[1])==0 && room==6 && object[6][3]==1) {
			System.out.println("(!)\tRusted Key taken.\n");		object[6][3]=0;
			pitem[0]=1;			score+=10;		return;
		}
		if(s.compareTo("fuse")==0 && room==7 && object[7][0]==9) {
			System.out.println("(!)\tFuse taken.\n");			object[7][0]=0;
			pitem[1]=9;			score+=10;		return;	
		}
		if(s.compareTo("rusty pick")==0 && room==49 && object[49][0]==30) {
			System.out.println("(!)\tRusty pick taken.\n");		object[49][0]=0;
			pitem[2]=30;		score+=10;		return;
		}
		if(s.compareTo("card key")==0 && room==10 && object[10][1]==27) {
			System.out.println("(!)\tCard key taken.\n");		object[10][1]=0;
			pitem[3]=27;		score+=10;		return;
		}
		if(s.compareTo("explosives")==0 && room==12 && object[12][0]==29) {
			System.out.println("(!)\tExplosives taken.\n");		object[12][0]=0;
			pitem[4]=29;		score+=10;		return;
		}
		if(s.compareTo("explosives")==0 && room==20 && object[20][0]==29) {
			System.out.println("(!)\tExplosives taken.\n");		object[20][0]=0;
			pitem[5]=29;		score+=10;		return;
		}
		if(s.compareTo("dim rock")==0 && room==3 && object[3][1]==25) {
			System.out.println("(!)\tDim rock taken.\n");		object[3][1]=0;
			pitem[6]=25;		score+=10;		return;
		}
		if(s.compareTo("unrefined coal")==0 && room==40 && object[40][0]==32) {
			System.out.println("(!)\tUnrefined coal taken.\n");	object[40][0]=0;
			pitem[7]=32;		score+=10;		return;
		}
	}
	
	public boolean useprep() throws IOException {
		int c=0;	int d=0;
		for(int a=0;a<16;a++) {
			if(pitem[a]>0)		
				c++;				
			}
		if(c>0)		System.out.print("What item would you like to use?\t");
		else		{	
			System.out.print("You don't have any items to use.\n");
			return false;	
			}
		i=in.readLine();
		i=i.toLowerCase();
		
		for(int b=0;b<4;b++) {
			if(object[room]>0)	d++;
		}
		if(d>0)		System.out.print("What would you like to use this on?\t");
		else		{
			System.out.print("There's nothing here to try using this with.\n");
			return false;
		}
		o=in.readLine();
		o=o.toLowerCase();
		return true;
		}
		
	public void useitem() throws IOException {
		if(useprep()==false)		return;
		if(i.compareTo("rusted key")==0 && o.compareTo("locked door")==0 && 
			room==2 && object[2][0]==2 && pitem[0]==1) {
			System.out.println("(!)\tDoor unlocked with rusted key.\n");
			object[2][0]=0;		path[2][1]=1;		path[3][2]=1;		return;	
			}
		if(i.compareTo("fuse")==0 && o.compareTo("fuse box")==0 && room==8 && 
			object[8][0]==10 && pitem[1]==9) {
			System.out.println("(!)\tThe broken fuse was replaced.\n");
			object[8][0]=0;		state[8][0]=1;		pitem[1]=0;
			score+=25;			return;
			}
		if(i.compareTo("card key")==0 && o.compareTo("security door")==0 &&	
			room==16 && object[16][0]==35 && pitem[3]==27) {
			System.out.println("(!)\tSecurity door unlocked with card key.\n");
			object[16][0]=0;	path[16][2]=1;		path[15][1]=1;
			return;	
			}
		if(i.compareTo("card key")==0 && o.compareTo("security door")==0 &&	
			room==17 && object[17][0]==35 && pitem[3]==27) {
			System.out.println("(!)\tSecurity door unlocked with card key.\n");
			object[17][0]=0;	path[17][0]=1;		path[12][3]=1;
			return;	
			}
		if(i.compareTo("card key")==0 && o.compareTo("security door")==0 &&	
			room==19 && object[19][0]==35 && pitem[3]==27) {
			System.out.println("(!)\tSecurity door unlocked with card key.\n");
			object[19][0]=0;	path[19][0]=1;		path[14][3]=1;
			return;	
			}
		if(i.compareTo("rusty pick")==0 && o.compareTo("chemical a1")==0 &&
			room==9 && pitem[2]==30) {
			System.out.println("(!)\tThe rusty pick has become the dull pick.\n");
			pitem[2]=31;		score+=50;			return;
			}
		if(i.compareTo("dull pick")==0 && o.compareTo("chemical c3")==0 &&
			room==9 && pitem[2]==31) {
			System.out.println("(!)\tThe dull pick has become the sharp pick.\n");
			pitem[2]=36;		score+=50;			return;
			}
		if(i.compareTo("sharp pick")==0 && o.compareTo("luminous rock")==0 &&
			room==3 && pitem[6]!=25 && pitem[2]==36) {
			System.out.println("(!)\tA dim rock breaks off from the larger rock.\n");
			object[3][1]=25;	score+=25;			return;
			}
		if(i.compareTo("sharp pick")==0 && o.compareTo("sunbeam cover")==0 && 
			room==14 && pitem[2]==36 && object[14][0]==42) {
			System.out.println("(!)\tThe sunbeam cover has been broken,");
			System.out.println("\tRevealing a sunbeam path.\n");
			object[14][0]=0;	score+=25;			return;
			}
		if(i.compareTo("dim rock")==0 && o.compareTo("sunbeam path")==0 &&
			room==14 && pitem[6]==25) {
			System.out.println("(!)\tThe dim rock has become a luminous rock.\n");
			pitem[6]=23;		score+=25;			return;
			}
		if(i.compareTo("explosives")==0 && o.compareTo("turret")==0 && 
			room==27 && pitem[4]==29) {
			System.out.println("(!)\tThe turret has been destroyed.\n");
			pitem[4]=0;			object[27][0]=0;	score+=50;		return;
			}
		if(i.compareTo("explosives")==0 && o.compareTo("turret")==0 && 
			room==36 && pitem[5]==29) {
			System.out.println("(!)\tThe turret has been destroyed.\n");
			pitem[5]=0;			object[36][0]=0;	score+=50;		return;
			}
		if(i.compareTo("unrefined coal")==0 && o.compareTo("coal refiner")==0 &&
			room==31 && pitem[7]==32) {
			System.out.println("(!)\tThe refining process yields refined coal.\n");
			pitem[7]=33;		score+=25;			return;
			}
		if(i.compareTo("refined coal")==0 && o.compareTo("coal furnace")==0 &&
			room==30 && pitem[7]==33) {
			System.out.println("(!)\tThe furnace is supplying adequate power.");
			pitem[7]=0;			state[30][0]=1;		score+=25;		return;
			}
	}
	
	public void inventory() {
		System.out.println("-Items on hand:");
		int c=0;
		for(int i=0;i<16;i++) {
			if(pitem>0) {
				System.out.println("--"+items[pitem]);		c++;
			}
		}
		if(c==0)	System.out.println("--none");
	}
	
	public void map() {
		System.out.println("\n\t-Map-\n");
		int r=0;
		int s=0;
		for(int i=0;i<yaxis;i++) {
			for(int j=0;j<xaxis;j++) {
				if(room>24)
					r=25+((i*5)+j);
				else	r=(i*5)+j;
				if(r==room) 
						System.out.print("O");
				else	System.out.print("X");
				if(r==49)
					return;
				if(path[r][1]==1 && path[r+1][2]==1)
					System.out.print("--");
				if(path[r][1]==1 && path[r+1][2]==0)
					System.out.print("->");
				if(path[r][1]==0 && path[r+1][2]==1)
					System.out.print("<-");
				if(path[r][1]==0 && path[r+1][2]==0)
					System.out.print("  ");
					
			}
			System.out.println();
			for(int k=0;k<xaxis;k++) {
				if(room>24)
					s=25+((i*5)+k);
				else	s=(i*5)+k;
				if(path[<span class="cpp-number">3</span>]==<span class="cpp-number">1</span> &amp;&amp; path[s+<span class="cpp-number">5</span>][<span class="cpp-number">0</span>]==<span class="cpp-number">1</span>)
					System.out.print(<span class="cpp-literal">"|  "</span>);
				<span class="cpp-keyword">if</span>(path[<span class="cpp-number">3</span>]==<span class="cpp-number">1</span> &amp;&amp; path[s+<span class="cpp-number">5</span>][<span class="cpp-number">0</span>]==<span class="cpp-number">0</span>)
					System.out.print(<span class="cpp-literal">"v  "</span>);
				<span class="cpp-keyword">if</span>(path[<span class="cpp-number">3</span>]==<span class="cpp-number">0</span> &amp;&amp; path[s+<span class="cpp-number">5</span>][<span class="cpp-number">0</span>]==<span class="cpp-number">1</span>)
					System.out.print(<span class="cpp-literal">"^  "</span>);
				<span class="cpp-keyword">if</span>(path[<span class="cpp-number">3</span>]==<span class="cpp-number">0</span> &amp;&amp; path[s+<span class="cpp-number">5</span>][<span class="cpp-number">0</span>]==<span class="cpp-number">0</span>)
					System.out.print(<span class="cpp-literal">"   "</span>);
			}
			System.out.println();
		}
		System.out.println(<span class="cpp-literal">"'O':current '–':two way '-&gt;':one way"</span>);
	}
	
	<span class="cpp-keyword">public</span> <span class="cpp-keyword">void</span> look() {
		System.out.println(<span class="cpp-literal">"Items of interest:"</span>);
		<span class="cpp-keyword">int</span> c=<span class="cpp-number">0</span>;
		<span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> i=<span class="cpp-number">0</span>;i&lt;<span class="cpp-number">4</span>;i++) {
			<span class="cpp-keyword">if</span>(object[room]<span style="font-weight:bold;">!=<span class="cpp-number">0</span>) {
				System.out.println(<span class="cpp-literal">"\t*"</span>+items[object[room]<span style="font-weight:bold;">]);		c++;
			}
		}
		<span class="cpp-keyword">if</span>(c==<span class="cpp-number">0</span>)	System.out.println(<span class="cpp-literal">"\t*none"</span>);
	}
	
	<span class="cpp-keyword">public</span> <span class="cpp-keyword">void</span> intro() {
		System.out.println(<span class="cpp-literal">"\t\t\tIntroduction:\n"</span>);
		System.out.println(<span class="cpp-literal">"\tFor some time, there has been news of travellers"</span>);
		System.out.println(<span class="cpp-literal">"disappearing while out on the seas.  Strangely enough,"</span>);
		System.out.println(<span class="cpp-literal">"your investigations have led you to a cave near a local"</span>);
		System.out.println(<span class="cpp-literal">"lighthouse.  This particular cave has the scent of dead"</span>);
		System.out.println(<span class="cpp-literal">"bodies in the air and the atmosphere has a weird - if"</span>);
		System.out.println(<span class="cpp-literal">"not terrifying - feel to it.  It is this cave in which"</span>);
		System.out.println(<span class="cpp-literal">"your adventure will begin…\n"</span>);
	}
	
	<span class="cpp-keyword">public</span> <span class="cpp-keyword">void</span> conclude() {
		System.out.println(<span class="cpp-literal">"\tUpon entering, something immediately catches your"</span>);
		System.out.println(<span class="cpp-literal">"attention - a portal that seems to be trying to suck"</span>);
		System.out.println(<span class="cpp-literal">"you in.  Not only that, but there are more dead bodies"</span>);
		System.out.println(<span class="cpp-literal">"here than you've seen in all the other rooms combined."</span>);
		System.out.println(<span class="cpp-literal">"The bodies are obviously those of missing travellers,"</span>);
		System.out.println(<span class="cpp-literal">"and it appears that a few of them tried to fight off"</span>);
		System.out.println(<span class="cpp-literal">"some serious threat or another.  You've definitely"</span>);
		System.out.println(<span class="cpp-literal">"found what you were looking for, but the big question"</span>);
		System.out.println(<span class="cpp-literal">"is, 'What do you do now?'…\n"</span>);
		System.out.println(<span class="cpp-literal">"\t\tTo be continued?"</span>);
		System.out.println(<span class="cpp-literal">"\t\tYour score: "</span> + score);
	}
	
	<span class="cpp-keyword">public</span> <span class="cpp-keyword">void</span> loop() throws IOException {
		intro();
		<span class="cpp-keyword">while</span>(room&gt;-<span class="cpp-number">1</span>) {
			System.out.println();
			roomname();
			<span class="cpp-keyword">if</span>(room==<span class="cpp-number">46</span>) {
				conclude();			<span class="cpp-keyword">return</span>;
			}
			String s=choose();
			<span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> a=<span class="cpp-number">0</span>;a&lt;<span class="cpp-number">4</span>;a++) {
				t = trimspace(choice[a]);
				<span class="cpp-keyword">if</span>(s.compareToIgnoreCase(t)==<span class="cpp-number">0</span>)
					room+=move(t);
			}
			<span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> b=<span class="cpp-number">4</span>;b&lt;<span class="cpp-number">10</span>;b++) {
				t = trimspace(choice<span style="font-weight:bold;">);
				<span class="cpp-keyword">if</span>(s.compareToIgnoreCase(t)==<span class="cpp-number">0</span>)
					process(t);
			}
			<span class="cpp-keyword">if</span>(s.compareTo(<span class="cpp-literal">"check items"</span>)==<span class="cpp-number">0</span>)		inventory();
			<span class="cpp-keyword">if</span>(s.compareTo(<span class="cpp-literal">"view map"</span>)==<span class="cpp-number">0</span>)			map();
			<span class="cpp-keyword">if</span>(s.compareTo(<span class="cpp-literal">"look around"</span>)==<span class="cpp-number">0</span>)		look();
		}
	}
	
}

<span class="cpp-keyword">public</span> <span class="cpp-keyword">class</span> jtest3 {
	
	<span class="cpp-keyword">public</span> <span class="cpp-keyword">static</span> <span class="cpp-keyword">void</span> main(String[] args) throws IOException{
	areaone a=<span class="cpp-keyword">new</span> areaone();
	a.loop();
	}	
}


</pre></div><!–ENDSCRIPT–> 
Advertisement
Please use 'source' tags rather than 'code' tags for large blocks of code (Oh, looks like you fixed it :) ). And yeah, definitely look into some OOP design, and data-driven design (reading all of that hard-coded information in from a file instead).
Yeah, it looks like it could end up good in the end,
but DEFINATLY needs some DataDriven progaming so that updating your
game levels does not mean a recompile.

Though making it datadriven looks like it also means you need to implement
some sort of state-machine to remove all the variables used to keep track of
button presses/lights/locked doors/ect...
I've definetly learned that hard-coding is downright stressful when done on this scale. I'm starting on a way to read info from a text file into the program, but need a bit of help on the theory. I want to do something like have a text file with the following format for npc conversations for a particular location...

--"location.txt"
//comment
npc line 1
npc line 2
etc.

...and program code that tests whether the first character in the line is '/' like this:

--"txt2java.java"
int c=0;
String npcline[]=new npcline[8];
while(not end of file) {
.if(first character in line is '/')
..go to next line
.else {
..assign string on current line to npcline[c]
..c++ }
}

The only thing is that I'm not certain what methods there are in Java to move through a file and I find the documentation kinda confusing. For things like weapon/armor stats, could I read the numbers in a series as long as they're separated by a space, or would I have to put one value per line?

This topic is closed to new replies.

Advertisement