Results 1 to 7 of 7
  1. #1
    LuluMM is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Look over my code and give tips?

    This is my text based adventure game. I know there's something wrong with my items class. What should I do to fix it?



    Java Code:
    import java.util.Scanner;
    public class Game {
      public static void main(String[] args) {//Start of main program
        Scanner typed = new Scanner(System.in);//Create new scanner that reads what the player typed
        
        Player player = new Player(); // make player
        Room entryWay = buildWorld();
        player.moveTo(entryWay); // player starts at entryway
        printInstructions(); // prints instructions for players to read
        play(player); // allows game to be played
      }
      
      public static Room buildWorld(){
        
        Room pink = new Room("Pink Room"); // create room
        Room blue = new Room("Blue Room");
        Room purple = new Room("Purple Room");
        Room white = new Room("White Room");
        Room red = new Room("Red Room"); // create room
         
        pink.setNorth(blue); //set directions for pink room
        pink.setSouth(blue);
        pink.setWest(white);
        pink.setEast(purple);
        
        
        blue.setNorth(pink); //set directions for blue room
        blue.setSouth(pink);
        blue.setWest(red);
        blue.setEast(white);
        
        purple.setNorth(red); // set directions for purple room
        purple.setSouth(purple);
        purple.setWest(white);
        purple.setEast(pink);
        
        white.setNorth(white); // set directions for white room
        white.setSouth(red);
        white.setWest(white);
        white.setEast(blue);
        
        red.setNorth(red); // set directions for red room
        red.setSouth(pink);
        red.setWest(white);
        red.setEast(pink);
        
        return pink;
      }
        
      Item Gold = new Item("Gold",50);
        Item Silver = new Item ("Silver",50);
        Item Bronze = new Item ("Bronze",50);
    
        
    
      
      static void play(Player player){
        
        
        Scanner in = new Scanner(System.in); // creates scanner
        String output = new String(); // creates string
        
        while(in.hasNext()){ // as long as there is input from the person playing the game
          output = in.next();
          if (output.equals("North")){ //recognizes when player writes north
            player.moveTo(player.location.getNorth()); // moves player to the room north leads into
            System.out.println("You are now in the " + player.location.Name); // prints the players new location
          }
          if (output.equals("South")){ //recognizes when player writes south
            player.moveTo(player.location.getSouth()); // moves player to the room south leads into
            System.out.println("You are now in the " + player.location.Name); // prints the players new location
          }
          if (output.equals("West")){ //recognizes when player writes west
            player.moveTo(player.location.getWest()); // moves player to the room west leads into
            System.out.println("You are now in the " + player.location.Name); // prints the players new location
          }
          if (output.equals("East")){ //recognizes when player writes east
            player.moveTo(player.location.getEast()); // moves player to the room east leads into
            System.out.println("You are now in the " + player.location.Name); // prints the players new location
          }
        }
        
      }
      
      static void printInstructions(){
        System.out.println(" Welcome to Adventureland! In this game, there are 5 rooms: Pink, Blue, Red, White, and Purple. The rooms are interconnected and each have four directions. You can move to the north, south, west, or east in each room by typing out the direction you want to move in, with a capital first letter. Keep in mind that just because a certain direction leads you to a room, doesn't mean that it will take you back to the room you were previously in. Also, there are doors that lead you back into the same room you were just in. Have fun and try to collect as much gold as possible!!");
        
      }
      
      
    }//End of the Adventure class
    
    
    
    public class Item {
      String Name;
      Item Gold;
      Item Silver;
      Item Bronze;
      
      
      public Item(String name, int amount){
        Name = name;
      }
      
      void setGold(Item r){
        Gold = r;
      }
      Item getGold(){
        return Gold;
      }
      
      void setSilver(Item r){
        Silver = r;
      }
      Item getSilver(){
        return Silver;
      }
      
      void setBronze(Item r){
        Bronze = r;
      }
      Item getBronze(){
        return Bronze;
      }
      
    }
    
    
    public class Player {
      
      public Room location; //current location of the player
      void moveTo(Room c){ //changes the current location of the player to room c.
        location = c; // the current location is updated
      }
    }
    
    
    
    public class Room { //Start of Room class
      String Name; //room name
      Room North; //connects to other rooms
      Room South; // connects to other rooms
      Room West; // connects to other rooms
      Room East; // connects to other rooms
      
      public Room (String name){
        Name = name;
      }
      
      void setNorth(Room r){
        North = r;
      }
      Room getNorth(){
        return North;
      }
      
      void setSouth(Room r){
        South = r;
      }
      Room getSouth(){
        return South;
      }
      
      void setEast(Room r){
        East = r;
      }
      Room getEast(){
        return East;
      }
      
      
      void setWest(Room r){
        West = r;
      }
      Room getWest(){
        return West;
      } 
      
      Item Gold;
      Item Silver;
      Item Bronze;
      
    }//End of Room class
    Last edited by sunde887; 05-11-2012 at 05:22 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Look over my code and give tips?

    That's a lot of unformatted code to ask us to go through and a very vague question. Consider adding [code[i]] [/code[i]] tags around your code so we can read it, and also try to isolate your problem a bit so you can ask an answerable question.

  3. #3
    LuluMM is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Look over my code and give tips?

    So I attempted to add the code tags, but I wasn't able to it successfully. Here is the problem: I have a text-based game. The player can change rooms and move around the virtual world, but now I want to put items in the game that the player can pick up and drop (this is done in the item class). That's the part I can't do. So, if anyone has ever made a game like this, or knows about them, how should I go about doing this?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Look over my code and give tips?

    oops, the [i] business shouldn't be in there. The tags are:

    [code]
    your code goes here
    [/code]

    (I hope this works!)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Look over my code and give tips?

    Your Item class with its Item fields looks a bit strange to my eyes. What are you trying to do with this?

  6. #6
    LuluMM is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Look over my code and give tips?

    I'm trying to have the items be accessible by the player. I had a feeling the fields I was using were wrong, what should I use instead?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Look over my code and give tips?

    Quote Originally Posted by LuluMM View Post
    I'm trying to have the items be accessible by the player. I had a feeling the fields I was using were wrong, what should I use instead?
    Consider creating an Item class that doesn't hold Item variables but instead is nothing more than a blueprint for what an Item represents, it's name, its value, perhaps its weight, etc... And then create Item objects using this class.

Similar Threads

  1. look @ code and give ideas! Install new code....
    By turbowhat in forum New To Java
    Replies: 2
    Last Post: 04-27-2011, 03:08 AM
  2. Plz give me the code of following simple problem..
    By juhiswt in forum New To Java
    Replies: 3
    Last Post: 03-07-2011, 03:54 AM
  3. pleae give me a code
    By Quezza in forum New To Java
    Replies: 3
    Last Post: 01-07-2011, 01:59 AM
  4. give interview tips....
    By thirumurugan.sethu in forum New To Java
    Replies: 4
    Last Post: 10-03-2008, 11:45 AM
  5. I need some advice and tips about my code...
    By sukatoa in forum Advanced Java
    Replies: 3
    Last Post: 06-23-2008, 07:29 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
  •