Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Text Based Adventure Game (Inventory related)

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Text Based Adventure Game (Inventory related)

    Hey all,

    im quite new to Java but i'm trying to create a text based adventure game. Where as you control a main character and pick up items along the way in order to finish the game.
    The main plot would be the character moves from room to room, picking up items which unlock "abilities" allowing you to enter the next room.

    eg. picking up a key to open a locked door.

    mind you this is a text based game...

    I'm almost finished a version 1 of the game and Currently the player starts with an inventory allowing them to carry 6 items, but i am wanting them to be able to pick up a BAG
    which enables the inventory to increase to 8, allowing them to pick up more items before game can be finished.

    My coding is as follows:


    int MAX_ITEMS = 6



    public void pickUp(Item item, Location location)
    {
    if(youHave(item))
    {
    System.out.println("You already have the " + item + "!");
    }
    else if(!location.isHere(item))
    {
    System.out.println("There is no " + item + " here!");
    }
    else if(numItems < MAX_ITEMS)
    {
    location.removeItem(item);
    hasItem[item.ordinal()] = true;
    numItems++;
    System.out.println("ok");
    }
    else
    {
    System.out.println("You can't carry any more!");
    }



    How am i able to code to increase MAX ITEMS when i pick up the BAG

    Thank you. if you want to see more of the code i can post, was just hard putting it all in this initial post


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,470
    My Mood
    Grumpy
    Thanks
    0
    Thanked 159 Times in 150 Posts

    Default Re: Text Based Adventure Game (Inventory related)

    The first thing you would need to do is test if the item picked up was the "bag". If it was then increase the inventory size.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Text Based Adventure Game (Inventory related)

    yeah thats the part i'm having trouble with, how do i reference this?

    Here is my coding for the items i've created


    /**
    * Enumeration class Item - things that can be picked up/used etc



    public enum Item
    {
    TORCH("torch"), WALLET("wallet"),
    KEYS("keys"), RING("ring"), USB("USB");

    /**
    * name of this item
    */
    private String name;

    /**
    * Constructor for an item
    *
    * @param name - the name of this item
    */
    private Item(String name)
    {
    this.name = name;
    }

    /**
    * Convert the item to a String, e.g. for printing
    *
    * @return the String
    */
    public String toString()
    {
    return name;
    }

    /**
    * Get the item by name
    *
    * @param name - the name to look for
    * @return the item wit this name, or null if no item has this name
    */
    public static Item getItem(String name)
    {
    Item result = null;

    for(Item item: Item.values())
    {
    if(name.equalsIgnoreCase(item.name))
    {
    result = item;
    }
    }

    return result;
    }
    }

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,470
    My Mood
    Grumpy
    Thanks
    0
    Thanked 159 Times in 150 Posts

    Default Re: Text Based Adventure Game (Inventory related)

    public void pickUp(Item item, Location location) {
    // code for other checks
    if item equals bag {
        inventory size increased;
    }
    How you determine if item equals bag is up to you and will depend upon how you have implemented bag.
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Text Based Adventure Game (Inventory related)

    the above code is how i have entered the BAG as an item, same as all the other items i have TORCH, USB etc.

    just dontk now the line i would need to write to say if item equals bag

Similar Threads

  1. Text adventure game help
    By Death_The_Kid in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 26th, 2013, 05:45 AM
  2. Carving data from text file based on expressions
    By fortune2k in forum Paid Java Projects
    Replies: 1
    Last Post: March 4th, 2011, 04:07 PM
  3. Text Based RPG Framework Questions
    By Kristler in forum Java Theory & Questions
    Replies: 0
    Last Post: December 21st, 2010, 08:39 AM
  4. beginner w/ text based RPG
    By rbread80 in forum Java Theory & Questions
    Replies: 12
    Last Post: December 4th, 2010, 01:39 PM