Page 3 of 3 FirstFirst 123
Results 41 to 44 of 44
Like Tree1Likes

Thread: How do I cycle through objects, then use a method from the found object?

  1. #41
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: How do I cycle through objects, then use a method from the found object?

    I didn't call the next exit in the iterator, did I? xD
    Well, I fixed it. But now how exactly am I supposed to set the found dest to the player object in my main class, I could just pass the player object to player.move() and then to room.move(), but that doesn't seem appropriate.

    EDIT: I just tried that, it works, but even if I put in a wrong direction it still changes rooms m(_ _)m
    Last edited by Darkzombies; 12-07-2012 at 03:33 AM.

  2. #42
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: How do I cycle through objects, then use a method from the found object?

    Never mind, I fixed it, yay!

    EDIT: Ok maybe not I thought I did, but I tried it out and even though it works, it only swaps between two rooms. EX. I type north and I go to area 1, even though north was supposed to go to area 2, and of course, if I type north in area 1, it tells me there is no direction, because there is no exit that way. So the exit part works, just not the room switching. I'll try to fix it again. If you think you know whats wrong please tell, because I got really happy for a second there xD

    EDIT 2: Fixed it again, feel so smart, even though it was probably just stupid to not notice it in the first place.
    Last edited by Darkzombies; 12-07-2012 at 06:58 AM.

  3. #43
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: How do I cycle through objects, then use a method from the found object?

    I just can't let this thread die, can I? Anyways, now that I'm all happy, I started making enemies to fight. But it seems like I just made a frankenstein instead of a program, any suggestions?

    Java Code:
    import java.util.*;
    	
    public class Practice{
    	
    	private static String name;
    	private static Room currLocation;
    	static Scanner sc = new Scanner(System.in);
    	
    	public static void main(String[] args){
    		
    		System.out.print("What is your name? ");
    		name = sc.nextLine();
    		
    		//Make rooms
    		Room library = new Room("Library", "A dark library illuminated by flickering candles. \nHuge dusty bookshelves line the floor. \nTo the north a steel door beckons. \nTo the west you see a sparkle.");
    		Room darkHallway = new Room("Dark Hallway", "A long passage looms ahead, or what seems to be one. \nYou cannot see the end of the corridor.");
    		Room crevace = new Room("Stone wall", "A dull stone wall. A small drawer is resting upon it. \nYou decide to move it, and reveal a small crevice. \nDo you wish to go west into the crevice? \nOr east back to the main room?");
    		
    		//Create player object
    		Player player = new Player(name, library);
    		player.setLocation(library);
    		currLocation = library;
    		
    		//Make exits
    		library.addExit(new Exit(MyDirection.NORTH, darkHallway));
    		library.addExit(new Exit(MyDirection.WEST, crevace));
    		crevace.addExit(new Exit(MyDirection.WEST, darkHallway));
    		crevace.addExit(new Exit(MyDirection.EAST, library));
    		darkHallway.addExit(new Exit(MyDirection.SOUTH, library));
    		
    		//Make monsters
    		crevace.addMonster(new Monster(Type.ZOMBIE, 10, 10, 10));
    		darkHallway.addMonster(new Monster(Type.SKELETON, 10, 10, 10));
    		library.addMonster(new Monster(Type.VAMPIRE, 10, 10, 10));
    		
    		//Show location info
    		showLocation(player);
    		
    	}
    	
    	public static void showLocation(Player player) {
    		
    		System.out.println("\n" + currLocation.getTitle() + "\n");
    		System.out.println(currLocation.getDesc());
    		for(ListIterator<Monster> e = currLocation.getMonster().listIterator();e.hasNext();){
    			
    			Monster currMonster = e.next();
    			
    			System.out.println("\n" + currMonster.getType() + " APPEARS\n");
    			
    			System.out.println("Health: " + currMonster.getStam());
    			System.out.println("Attack: " + currMonster.getAttack());
    			System.out.println("Defence: " + currMonster.getDef());
    			
    		}
    		
    		currLocation = player.getLocation();
    		movePlayer(player);
    	}
    	
    	public static void movePlayer(Player player) {
    		  boolean moveOK = false;
    		  while (!moveOK) {
    		    String dirString = promptPlayerForDirection();
    		    try {
    		      MyDirection dir = getMyDirFromString(dirString, player);
    		      if (player.move(dir, player)) { // if Room has exit, this moves Player and returns true
    		        currLocation = player.getLocation();
    		        showLocation(player);
    		        //moveOK = true; // or return
    		      } else {
    		        // warn Player that Room doesn't have this exit, to try another
    		    	  System.out.print("Current location has no \'" + dirString + "\' exit.\n");
    		      }
    		    } catch (InvalidMyDirectionStringException e) {
    		      // warn player that direction String dirString was not valid
    		    	System.out.println("Not a valid command!\n");
    		    }
    		}
    		showLocation(player);
    	}
    	
    	private static String promptPlayerForDirection() {
    		Scanner sc = new Scanner(System.in);
    		System.out.print("Type a command: ");
    		String input = sc.nextLine();
    		return input;
    	}
    
    	public static MyDirection getMyDirFromString(String dirString, Player player) throws InvalidMyDirectionStringException{
    		  for(MyDirection dir : MyDirection.values()){
    	      for(Commands com : Commands.values()){
    		     if (dir.getDirName().equalsIgnoreCase(dirString) || dir.getShortDirName().equalsIgnoreCase(dirString)){
    		       return dir;
    		     }else if(com.getComName().equalsIgnoreCase(dirString) || com.getShortComName().equalsIgnoreCase(dirString)){
    		       parseCommand(dirString, player);
    		     }
    	      	}
    		  }
    		  // if were here, then no dir was found
    		  throw new InvalidMyDirectionStringException(dirString);
    	}
    	
    	public static void parseCommand(String comString, Player player){
    		boolean commandOK = false;
    		while (!commandOK){
    			try{
    				if(player.command(comString, player)){
    					
    				}
    			}catch(Exception e){
    				System.out.println("Invalid command!");
    			}
    	    }
    	}
    }
    Java Code:
    import java.util.*;
    
    
    public class Room {
    	
    	private String title;
    	private String desc;
    	public static int roomAmount;
    	private List<Exit> exits = new ArrayList<Exit>();
    	private List<Monster> monsters = new ArrayList<Monster>();
    	
    	public Room(String title, String desc){
    		this.title = title;
    		this.desc = desc;
    		roomAmount += 1;
    	}
    	
    	public String getTitle(){
    		return title;
    	}
    	
    	public String getDesc(){
    		return desc;
    	}
    	
    	public void addExit(Exit exit){
    		exits.add(exit);
    	}
    	
    	public void removeExit(Exit exit){
    		if(exits.contains(exit)){
    			exits.remove(exit);
    		}
    	}
    	
    	public List<Exit> getExits (){
    		return exits;
    	}
    	
    	public void addMonster(Monster monster){
    		monsters.add(monster);
    	}
    	
    	public void removeExit(Monster monster){
    		if(monsters.contains(monster)){
    			monsters.remove(monster);
    		}
    	}
    	
    	public List<Monster> getMonster(){
    		return monsters;
    	}
    	
    	public static boolean canMove(MyDirection dir, Room currLocation){
    		boolean temp = false;
    		for(ListIterator<Exit> e = currLocation.getExits().listIterator();e.hasNext();){
    			if(dir.equals(e.next().getDir())){
    				temp = true;
    			}
    		}
    		return temp;
    	}
    
    	public static void move(MyDirection dir, Room currLocation, Player player) {
    		for(ListIterator<Exit> e = currLocation.getExits().listIterator();e.hasNext();){
    			if(dir.equals(e.next().getDir())){
    				e.previous();
    				Room dest = e.next().getDest();
    				player.setLocation(dest);
    			}
    		}
    	}
    }
    Java Code:
    import java.util.ListIterator;
    
    
    public class Player {
    	
    	private String name;
    	private Room currLocation;
    	private int stamina = 10;
    	private int attack = 10;
    	private int defence = 10;
    	
    	public Player(String name, Room currLocation){
    		this.name = name;
    		this.currLocation = currLocation;
    		System.out.println("Welcome " + name + "\n");
    	}
    	
    	public Room getLocation(){
    		return currLocation;
    	}
    	
    	public void setLocation(Room currLocation){
    		this.currLocation = currLocation;
    	}
    	
    	public boolean move(MyDirection dir, Player player) {
    		   // first ask room if it is possible to move in direction
    		   if (Room.canMove(dir, currLocation)) {
    		      //if so, then move into new room
    			   //It should be returning true
    		      Room.move(dir, currLocation, player);
    		      return true; 
    		   } else {
    		      // else stay where we are and return false
    			   System.out.println("\nInvalid Direction! Returning to room\n");
    		      return false;
    		  }
    	}
    	public boolean command(String com, Player player) {
    		for(ListIterator<Monster> e = currLocation.getMonster().listIterator();e.hasNext();){
    		if(com.equals(Commands.ATTACK)){
    			
    			int currStam = e.next().getStam();
    			int futureStam = currStam -= player.attack;
    			e.next().setStam(futureStam);
    			
    		}else if(com.equals(Commands.DEFEND)){
    			//Defense code, raise players defense
    			
    			
    		}else if(com.equals(Commands.ITEMS)){
    			//Inventory, implement later
    			
    			
    		}
    	}
    	return false;
    	}
    }
    Java Code:
    public enum MyDirection {
    	NORTH("north", "n"), SOUTH("south", "s"), EAST("east", "e"), WEST("west", "w");
    		    
    		   private String dirName;
    		   private String shortDirName;
    		   
    		   private MyDirection(String dirName, String shortDirName) {
    		      this.dirName = dirName;
    		      this.shortDirName = shortDirName;
    		   }
    		   public String getDirName() {
    		      return dirName;
    		   }
    		   public String getShortDirName() {
    		      return shortDirName;
    		   }
    		   public static MyDirection getValue(String input) {
    			    for (MyDirection dir : values()) {
    			        if (dir.dirName.equals(input) || dir.shortDirName.equals(input)) {
    			            return dir;
    			        }
    			    }
    			    return null;
    			}
    		    
    	}
    Java Code:
    public class InvalidMyDirectionStringException extends Exception {
      public InvalidMyDirectionStringException(String text) { 
        super(text);
      }
    }
    Java Code:
    public class Monster {
    	
    	private Type type;
    	private int stamina;
    	private int attack;
    	private int defence;
    	
    	public Monster(Type type, int stamina, int attack, int defence){
    		this.type = type;
    		this.stamina = stamina;
    		this.attack = attack;
    		this.defence = defence;
    	}
    	
    	public Type getType(){
    		return type;
    	}
    	
    	public int getStam(){
    		return stamina;
    	}
    	
    	public void setStam(int stamina){
    		this.stamina = stamina;
    	}
    	
    	public int getAttack(){
    		return attack;
    	}
    	
    	public void setAttack(int attack){
    		this.attack = attack;
    	}
    	
    	public int getDef(){
    		return defence;
    	}
    	
    	public void setDef(int defence){
    		this.defence = defence;
    	}
    	
    }
    Java Code:
    public enum Commands {
    	ATTACK("attack", "a"), DEFEND("defend", "d"), ITEMS("item","i");
    	
    	private String comName;
    	private String shortComName;
    	
    	private Commands(String comName, String shortComName){
    		this.comName = comName;
    		this.shortComName = shortComName;
    	}
    	
    	public String getComName(){
    		return comName;
    	}
    	
    	public String getShortComName(){
    		return shortComName;
    	}
    	
    }
    Java Code:
    public enum Type {
    	ZOMBIE("zombie"), SKELETON("skeleton"), BANSHEE("banshee"), VAMPIRE("vampire");
    		    
    		   private String monsterName;
    		   
    		   private Type(String monsterName) {
    		      this.monsterName = monsterName;
    		   }
    		    
    		   public String getName(){
    			   return monsterName;
    		   }
    		   
    	}
    EDIT: Fixed it up a bit, but now it says its an invalid command even if its right.

    Java Code:
    import java.util.*;
    	
    public class Practice{
    	
    	private static String name;
    	private static Room currLocation;
    	static Scanner sc = new Scanner(System.in);
    	
    	public static void main(String[] args){
    		
    		System.out.print("What is your name? ");
    		name = sc.nextLine();
    		
    		//Make rooms
    		Room library = new Room("Library", "A dark library illuminated by flickering candles. \nHuge dusty bookshelves line the floor. \nTo the north a steel door beckons. \nTo the west you see a sparkle.");
    		Room darkHallway = new Room("Dark Hallway", "A long passage looms ahead, or what seems to be one. \nYou cannot see the end of the corridor.");
    		Room crevace = new Room("Stone wall", "A dull stone wall. A small drawer is resting upon it. \nYou decide to move it, and reveal a small crevice. \nDo you wish to go west into the crevice? \nOr east back to the main room?");
    		
    		//Create player object
    		Player player = new Player(name, library);
    		player.setLocation(library);
    		currLocation = library;
    		
    		//Make exits
    		library.addExit(new Exit(MyDirection.NORTH, darkHallway));
    		library.addExit(new Exit(MyDirection.WEST, crevace));
    		crevace.addExit(new Exit(MyDirection.WEST, darkHallway));
    		crevace.addExit(new Exit(MyDirection.EAST, library));
    		darkHallway.addExit(new Exit(MyDirection.SOUTH, library));
    		
    		//Make monsters
    		crevace.addMonster(new Monster(Type.ZOMBIE, 100, 10, 10));
    		darkHallway.addMonster(new Monster(Type.SKELETON, 100, 10, 10));
    		library.addMonster(new Monster(Type.VAMPIRE, 100, 10, 10));
    		
    		//Show location info
    		showLocation(player);
    		
    	}
    	
    	public static void showLocation(Player player) {
    		
    		System.out.println("\n" + currLocation.getTitle() + "\n");
    		System.out.println(currLocation.getDesc());
    		for(ListIterator<Monster> e = currLocation.getMonster().listIterator();e.hasNext();){
    			
    			Monster currMonster = e.next();
    			
    			System.out.println("\n" + currMonster.getType() + " APPEARS\n");
    			
    			System.out.println("Health: " + currMonster.getStam());
    			System.out.println("Attack: " + currMonster.getAttack());
    			System.out.println("Defence: " + currMonster.getDef());
    			
    		}
    		
    		currLocation = player.getLocation();
    		movePlayer(player);
    	}
    	
    	public static void movePlayer(Player player) {
    		  boolean moveOK = false;
    		  while (!moveOK) {
    		    String dirString = promptPlayerForDirection();
    		    try{
    		      MyDirection dir = getMyDirFromString(dirString);
    		      Commands com = getCommandFromString(dirString);
    		      if(player.move(dir, player)){ // if Room has exit, this moves Player and returns true
    		        currLocation = player.getLocation();
    		        showLocation(player);
    		      }else if(player.command(com, player)){
    		    	  showLocation(player);
    		      }else{
    		    	  System.out.print("Current location has no \'" + dirString + "\' exit.\n");
    		      }
    		    }catch (Exception e) {
    		      // warn player that direction String dirString was not valid
    		    	System.err.println("\nNot a valid command!");
    		    }
    		}
    		showLocation(player);
    	}
    	
    	private static String promptPlayerForDirection() {
    		Scanner sc = new Scanner(System.in);
    		System.out.print("Type a command: ");
    		String input = sc.nextLine();
    		return input;
    	}
    
    	public static MyDirection getMyDirFromString(String dirString) throws InvalidMyDirectionStringException{
    		  for(MyDirection dir : MyDirection.values()){
    		     if (dir.getDirName().equalsIgnoreCase(dirString) || dir.getShortDirName().equalsIgnoreCase(dirString)){
    		       return dir;
    		     }
    		  }
    		  // if were here, then no dir was found
    		  throw new InvalidMyDirectionStringException(dirString);
    	}
    	
    	public static Commands getCommandFromString(String dirString) throws InvalidCommandsStringException{
    	      for(Commands com : Commands.values()){
    		     if(com.getComName().equalsIgnoreCase(dirString) || com.getShortComName().equalsIgnoreCase(dirString)){
    		       return com;
    		     }
    		  }
    		  // if were here, then no dir was found
    		  throw new InvalidCommandsStringException(dirString);
    	}
    }
    Last edited by Darkzombies; 12-08-2012 at 08:51 AM.

  4. #44
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: How do I cycle through objects, then use a method from the found object?

    Put some debug in getMyDirFromString to see what string it is working with, and what the values (short and long) it is trying to compare it against as it loops over the enums.

    I suspect it could be the Scanner.
    I notice you create a new one in promptPlayerForDirection, when you already have a perfectly good one as an attribute of Practice.
    Please do not ask for code as refusal often offends.

Page 3 of 3 FirstFirst 123

Similar Threads

  1. Main Method not found in class
    By HinaKhan in forum New To Java
    Replies: 6
    Last Post: 06-22-2012, 02:34 PM
  2. Symbol not found - object invisible outside of class
    By stesteve in forum New To Java
    Replies: 5
    Last Post: 02-15-2012, 09:57 PM
  3. Method not found
    By fleg in forum New To Java
    Replies: 8
    Last Post: 10-12-2010, 05:02 PM
  4. incompatible types found object expect integer
    By bflhr673 in forum New To Java
    Replies: 3
    Last Post: 07-24-2010, 03:10 PM
  5. JButtonGroup; add() method not found?
    By Moddy in forum AWT / Swing
    Replies: 3
    Last Post: 10-15-2008, 11:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •