Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
***I have managed to fix this issue. I created a bool called RoomChallange_ItemNeeded in static void Main(string[] args), and set it to false. Then in the case "take-key" I set it to true, and in the case "drop-key" I set it to false. Finally in the case "use", the if statement is this:
if (currentRoom.RoomChallange != null && RoomChallange_ItemNeeded == true)
{
   Console.WriteLine("currentRoom.RoomChallange.SuccessText");
   playeritems.Remove(currentRoom.RoomChallange.ItemNeeded);
   currentRoom.RoomChallange = null;
}
***I will leave this string if you have any questions, as I saw no questions on this subject. Good luck programming.

Hello, I am trying to create a chest for my text based rpg in a Console Application. My problem is that when you try to unlock the chest, even if you have the specifications asked for, the ability to access it will not trigger. I have looked online for help on this but have found no such help. My question is why is my code not working, and is there a way to impliment the chest if my will simply not work? I have recived no error messages, warnings and I have tryed the debuger to no avail. Here is my code:
CSS
#region challenge
class Challenge
{
    public Items ItemNeeded;
    public string ChallengeText;
    public string SuccessText;
    public string BlockedExits;

    public Challenge(string challenge, string success, Items needed, string blocked)
    {
        ChallengeText = challenge;
        SuccessText = success;
        ItemNeeded = needed;
        BlockedExits = blocked;
    }
}
#endregion challenge
#region class items
class Items
{
    public string Name;
    public string Description;
    public decimal Wieght;
    public Items (string name, string description, decimal wieght)
    {
        Name = name;
        Description = description;
        Wieght = wieght;
    }
}
#endregion class items
#region class room
class Room
{
    public string Title;
    public string Description;
    public string map_location;
    public Room Warn;
    public Room Dwarn;
    public Room Swarn;
    public Room Awarn;
    public Items RoomItem;
    public Items RoomItem2;
    public Items RoomItem3;
    public Challenge RoomChallange;

    public Room(string title, string description, string maplocation)
    {
        Title = title;
        Description = description;
        map_location = maplocation;
    }
}
#endregion class room

*This part is in the static void Main(string[] args);
*I have initialized these next three chunks to allow specific options such as "take-key" instead of just "take". I am not sure if the rooms and challange had to be initialized in here. :/
Items key = new Items("the Pit Boss Key", "This is the key for the pit boss chest.", 1);

Room darkcave32a45 = new Room("Dark Cave", "This is the treasure room. A large chest sits in the center with a lock on it. At the back you see an Elvish Axe, and a Bronze Hammer. Five orcics are at the edge of the chest, and ten are at your feet. You can go Swarn.", "You are underground, and cannot tell your location.");
Room darkcave32achest = new Room("Chest", "You have opened the chest, as you rummage around through it you find a sack of fifty Orcics, an iron dagger, and a frost staff. The chest vomits out ancient dust when you opened it. The chest has been plastered together to just stay in the condition that it is. What a strange paradox between the bandits wealth and items they house themselves with.", "There is no point in trying to find your location down here.");

Challenge Room_Challange = new Challenge("The chest is locked!", "The rusty key unlocks the chest.", key, "w");

*When I attempt to test the "use" option the "success text" or allowing you to open the chest do not occur.
#region rooms
Room currentRoom = SetUpMap();
string userChoice = "";
List<Items> playeritems = new List<Items>();
while (userChoice != "quite")
{
    DescribeRoom(currentRoom);
    userChoice = Console.ReadLine().ToLower();
    switch (userChoice)
    {
        case "w":
            currentRoom = GoTo(currentRoom.Warn, currentRoom, userChoice, playeritems);
            break;
        case "take-key":
            if (currentRoom.RoomItem != null)
            {
                Console.WriteLine("You have taken the {0}.", key.Name);
                playeritems.Add(key);
                playerwieght = playerwieght + key.Wieght;
                key = null;
            }
            break;
        case "drop-key":
            if (currentRoom.RoomItem == null)
            {
                Console.WriteLine("You have droped {0}", key.Name);
                playeritems.Remove(key);
                playerwieght = playerwieght - key.Wieght;
            }
            break;
        case "use":
            if (currentRoom.RoomChallange != null && playeritems.Contains(currentRoom.RoomChallange.ItemNeeded))
            {
                Console.WriteLine(currentRoom.RoomChallange.SuccessText);
                playeritems.Remove(currentRoom.RoomChallange.ItemNeeded);
                currentRoom.RoomChallange = null;
            }
            break;
        default:
            Console.WriteLine("Sorry, but I don't understand that. Type in help for a list of commands.");
            break;
    }
}
#endregion rooms

*This is where the rooms are created and linked together. Room 46 comes before Room 45. I had made that mistake and left it seeing as it did no harm. I had made the "chest" a room becuase I could easiy create the effect of a chest without having to do anything adventureaus. I used the "w" command as a placeholder.
Room darkcave32a46 = new Room("Dark Cave", "Welcome to the pit bosses thrown. He lifts his eyebrow questioning "Well?" .He sits on his large thrown chair barely about to buckle. The chair has a few rusted swords sticking out of it in an uneven curved pattern. Two guard stand in front of the sovereign, relaxed. A few stacks of ten Orcics lye in the back corner. You can go Warn and Dwarn.", "You are underground and cannot find your location");
Room darkcave32a45 = new Room("Dark Cave", "This is the treasure room. A large chest sits in the center with a lock on it. At the back you see an Elvish Axe, and a Bronze Hammer. Five orcics are at the edge of the chest, and ten are at your feet. You can go Swarn.", "You are underground and cannot find your location");
Room darkcave32achest = new Room("Chest", "You have opened the chest, as you rummage around through it you find a sack of fifty Orcics, an iron dagger, and a frost staff. The chest vomits out ancient dust when you opened it. The chest has been plastered together to just stay in the condition that it is. What a strange paradox between the bandits wealth and items they house themselves with.", "You are underground and cannot find your location");
Items Orcics_50 = new Items("50 Orcics", "A pile of 50 gold coins.", 5);
Items Iron_Dagger = new Items("Iron Dagger", "A sharp iron dagger. Short Blade, Strength 50, Damage 1-5", 3);
Items Frost_Staff = new Items("Frost Staff", "A frost staff. Staff, Strength 100, Frost Damage 10", 10);
Items key = new Items("Pit Boss Key", "This is the key to the Pit Boss's Chest.", 2);
Challenge Room_Challange = new Challenge("The chest is locked!", "You have unlocked the chest!", key, "w");
darkcave32a46.RoomItem = key;
darkcave32achest.RoomItem = Orcics_50;
darkcave32achest.RoomItem2 = Iron_Dagger;
darkcave32achest.RoomItem3 = Frost_Staff;
darkcave32a45.RoomChallange = Room_Challange;

*This is what tells the player they cant go this way or that lets say a door is locked. The challange text is showed every time I try to enter with the key and entering the use command.
C#
static Room GoTo(Room nextRoom, Room currentRoom, string direction, List<Items> playeritems)
{
    if (nextRoom != null)
    {
        if (currentRoom.RoomChallange != null && currentRoom.RoomChallange.BlockedExits.Contains(direction))
            Console.WriteLine(currentRoom.RoomChallange.ChallengeText);
        else
            currentRoom = nextRoom;

    }
    else
    {
        Console.WriteLine("There is no exit in that direction.");
    }
    return currentRoom;
}

Thanks for answering and looking at my question in advance. Your answer is greatly appreciated.
Posted
Updated 17-Jun-13 8:49am
v2
Comments
R.B. 13-Jun-13 17:49pm    
you should use the debugger to find out why your contains ItemNeeded is coming out false. Also, does this loop forever? If the user types in "quit" it won't stop because your loop is checking for "quite".
A Fellow Coder 15-Jun-13 12:13pm    
The game does not forever loop. Any other ideas?
Ezra Neil 13-Jun-13 23:02pm    
What happen at the function SetUpMap()? I think what happen at that function will determine the problem. I have replicated your code and I can unlock the chest.
A Fellow Coder 15-Jun-13 12:11pm    
When you say replicate do you meen that you that made an exact copy? If not then will you write it out to me. Thankyou. One more thing, what problem do you think is happening in the setup function?

1 solution

This is wildly complicated. Your chest should be a class. It should have a property that says if it's locked or not. Attempts to interact with it should check if it's locked, or at least behave differently if it's locked or not.

Your room should have an array of items, not a fixed number. Your code limits the items to a room right now, and means you need to write code to deal with each one, instead of iterating over a collection.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900