// // actor.java // // Robert Harrison 2001 // // actor.java describes the class for an agent in a simple computer game // // the actor can have a name and type // he/she/it has a health and knapsack // different kinds of actors can have different privleges // for example a mere mortal can't use a wand, but is real good with a sword // while a wizard might be able to use a wand, but not able to shoot a bow. // the privledge will be encapsulated in the class that contains the small_object // import java.lang.*; import java.util.Vector; public class actor extends Object { // these state variables are all private to discourage cheating // it makes it a little hard for there to be a magic class private String name; private String type; private Vector knapsack; private boolean magical; private double capacity,total; private boolean hungry; private double health; private int which_type; private static String[] species={"human","elf","wizard","troll"}; private static boolean[] m_attribute={false,true,true,false}; private static boolean[] smart={true,true,true,false}; private static boolean[] flies={false,true,false,false}; private static boolean[] eat_poison={false,false,false,true}; private static double[] c_attribute={50.,10.,50.,500.}; private static double[] metabolic_rate={0.01,0.02,0.01,0.05}; public actor( String name, String type) { this.name = name; this.type = type; knapsack = new Vector(10); capacity = 50.; total = 0.; health = 1.; hungry = true; for( int i=0; i< species.length; i++) { if( species[i].equalsIgnoreCase(type)) { magical = m_attribute[i]; capacity = c_attribute[i]; which_type = i; break; } } zork.message("\n as a " + type +" you can"); if(magical) zork.message(" use magic,"); if( am_smart()) zork.message(" read,"); if( can_fly()) zork.message(" fly,"); if( eat_anything()) zork.message(" eat (almost) anything,"); if( magical || am_smart() || can_fly() || eat_anything()) zork.message(" and"); zork.message(" carry "+String.valueOf(capacity)+" pounds in your knapsack"); } public String name() {return name;} public void live() { health -= metabolic_rate[which_type]; if( health < 0.5) hungry = true; if( health < 0.) { zork.message(" Sorry you just died"); System.exit(0); } } public static void kinds() { zork.message("\nyou can be a "); for(int i=0; i< species.length-1; i++) { zork.message(species[i]+", "); } zork.message("or a "+species[species.length-1]); } public static boolean valid_kind(String maybe) { for( int i=0; i< species.length; i++) { if( species[i].equalsIgnoreCase(maybe)) return true; } kinds(); return false; } public boolean am_hungry() { return hungry;} public boolean am_magical() { return magical;} public boolean am_smart() { return smart[which_type];} public boolean can_fly() { return flies[which_type];} public boolean eat_anything() { return eat_poison[which_type];} public String kind() { return species[which_type];} public boolean pickup( Object o) { if( o instanceof small_object) { small_object s = (small_object)o; if( (s.weight() + total) <= capacity) { total += s.weight(); knapsack.addElement(s); return true; }else{ zork.message("You need to drop something first because your pack is too full\n"); return false; } }else{ zork.message("you can't pick that up\n"); return false; } } // // drop puts an object back into a scene // public boolean drop( String name, scene where) { int i; small_object o; for( i= 0; i< knapsack.size(); i++) { o = (small_object)knapsack.elementAt(i); if( name.equalsIgnoreCase(o.what_i_am())) { where.add_to_contents(o); total -= o.weight; knapsack.removeElementAt(i); return true; } } return false; } // drop without a scene discards the object public boolean drop( String name) { int i; small_object o; for( i=0; i< knapsack.size(); i++) { o = (small_object)knapsack.elementAt(i); if( name.equalsIgnoreCase(o.what_i_am())) { total -= o.weight; knapsack.removeElementAt(i); return true; } } return false; } public void inventory() { small_object o; int i; if( knapsack.size() < 1) { zork.message("You aren't carrying anything. \n"); return; } zork.message("\n You are carrying "); for( i=0; i< knapsack.size(); i++) { o = (small_object)knapsack.elementAt(i); zork.message(o.what_i_am()+" weighing " + String.valueOf(o.weight())+" pounds\n"); } zork.message("\n"); } public void eat( String name) { small_object o; if( knapsack.size() < 1) { zork.message("You have to pick something up to eat it\n"); return; } for( int i =0; i< knapsack.size(); i++) { o = (small_object)knapsack.elementAt(i); if( name.equalsIgnoreCase(o.what_i_am())) { zork.message("\nYou open your mouth to eat "+name+" and "); if( !o.edible() && !o.poisonous() && !eat_anything() ) { zork.message(" you can't eat a "+ o.what_i_am() + "\n"); return;} if( o.poisonous() && !eat_anything()) { health -= o.poison(0.5); hungry = true; zork.message("\nThat was poison, you are sick\n"); if( health <= 0.) {zork.message("You're dead\n"); System.exit(0);} return; } else if( !o.edible() && eat_anything() ) { health += 0.5; drop(o.what_i_am()); zork.message("\nYum Yum Eat 'em up\n"); return ; } else { total -= o.weight(); o.eat(0.1); total += o.weight(); health += 0.5; hungry = false; if( which_type == 3) zork.message("\n Braaaack Snort Sniffle\n"); else zork.message("\nThat was good.\n"); return ; } } } zork.message("\n You need to pick up the "+ name +" first to eat it!\n"); } /** become (string) * change to another kind of actor * for use in scenes */ public boolean become( String type) { for( int i=0; i< species.length; i++) { if( species[i].equalsIgnoreCase(type)) { magical = m_attribute[i]; capacity = c_attribute[i]; which_type = i; break; } } return true; } }// end of the class actor