/** twisty.java * * @author Robert W. Harrison * * example scene for zork * * use the abstract class scene */ import java.lang.Math; import java.lang.Thread; import java.util.Vector; public class twisty extends scene { private boolean come_from_left; public twisty( Vector v, actor who) { super( v,who); come_from_left = false; } public twisty( Vector v, actor who, boolean come_from_left) { super(v,who); this.come_from_left = come_from_left; } /** The version of run in great_hall is inherited from scene. * */ public void set_the_scene() {// my is an instance variable that is defined in the abstract class scene zork.message("\n" +my.name()+", you are in a maze of twisty little passages all alike \n"); zork.message("There is a door on the left and on the right \n"); if( Math.random() < 0.1) zork.message(" There is the sound of heavy breathing nearby which may be the minotaur\n"); what_is_here(); if( my.kind().equalsIgnoreCase("troll")) zork.message("\n You are very hungry\n"); else if( me.am_hungry()) zork.message("\n You are hungry\n"); } public boolean action() { String inputline; // these are the simple way to implment these. There are cleverer ways String [][] pickup = {{"pick"},{"pickup"}}; String [] eat = {"eat"}; String [][] walk_left = {{"walk","left"},{"run","left"}}; String [][] walk_right = {{"walk","right"},{"run","right"}}; String [] open={"open"}; String [] drink = {"drink"}; String [] drop = {"drop"}; String [] look = {"look"}; // initially you can pickup or eat stuff boolean right = false; boolean left = false; boolean not_moved = true; move_on: do{ inputline = zork.readLine(); if( inputline == null || inputline == "") continue; for(int i=0; i< 2; i++) { small_object o; if( zork.simple_NLP(inputline,walk_right[i])) {not_moved = false; right = true; break move_on;} else if( zork.simple_NLP(inputline,walk_left[i])) {not_moved = false; left = true; break move_on;} else if( zork.simple_NLP(inputline,pickup[i])) { what_is_here(); zork.message("pickup what ?\n"); inputline = zork.readLine(); inputline = inputline.trim(); o = pick_up(inputline); me.pickup(o); my.inventory(); } } if( zork.simple_NLP(inputline,look)) {what_is_here(); my.inventory();} if( zork.simple_NLP(inputline,drop)) { my.inventory(); zork.message("Drop what ?\n"); inputline = zork.readLine(); inputline = inputline.trim(); me.drop(inputline, this); what_is_here(); } if( zork.simple_NLP(inputline,eat)) { my.inventory(); zork.message("Eat what ?\n"); inputline = zork.readLine(); inputline = inputline.trim(); me.eat(inputline); } else zork.message("huhh?"); } while( not_moved); int count = 0; do{ if( count ++ > 0) zork.message("\n Open the door dummy!"); inputline = zork.readLine(); }while( !zork.simple_NLP(inputline, open)); not_moved = false; if( left || right ) for( count = 0; count < state_vector.size(); count ++) { Object what = state_vector.elementAt(count); if( what instanceof twisty && Math.random() < 0.1) { next_scene = (twisty)what; not_moved = true; break;} } if( come_from_left) for( count = 0; count < state_vector.size(); count ++) { Object what = state_vector.elementAt(count); if(Math.random() < 0.1 && !(what instanceof initial_scene)) { next_scene = (scene)what; return true; } } if( !not_moved) next_scene = new twisty(state_vector,me,left); // typically you would check that this exists before creating it. return true;} }// end of class