PDA

View Full Version : C# Rooms


patrickcabenjamin
17 Jun 2009, 4:59pm
This time I have a real problem. I have checked the whole bloody thing and can't find anything wrong.
It's a room based Program and it reads a txt file. The commands are QUIT, LOOK, NORTH, EAST, SOUTH and WEST. With them it was working fine. I added a USE command which should work however whenever I use it, it says the description for LOOK instead.

Here it is:
namespace AdventureGame
{
class Room
{
public string description;
public string north;
public string east;
public string south;
public string west;
public string use;
public Room(string d, string u, string n, string e, string s, string w)
{
this.description = d;
this.north = n;
this.east = e;
this.south = s;
this.west = w;
this.use = u;
}

}
class Program
{
static bool quit = false;

static Dictionary<STRING,ROOM> rooms = new Dictionary<STRING,ROOM>();

static string currentRoom = "";

static void Main(string[] args)
{
System.Console.WriteLine("WELCOME TO THE ADVENTURE GAME!");
System.Console.WriteLine("------------------------------");
System.Console.WriteLine("\nYour commands are:");
System.Console.WriteLine(" QUIT");
System.Console.WriteLine(" LOOK");
System.Console.WriteLine(" NORTH");
System.Console.WriteLine(" SOUTH");
System.Console.WriteLine(" EAST");
System.Console.WriteLine(" WEST");
System.Console.WriteLine(" USE\n");
System.Console.WriteLine("------------------------------\n\n");

MakeRooms(args[0]);

while (quit == false)
{
System.Console.Write("Enter your command: ");
string command = System.Console.ReadLine();

if (command.ToLower().Equals("quit"))
{
Quit();
}
else if (command.ToLower().Equals("east"))
{
Go(rooms[currentRoom].east, "east");
}
else if (command.ToLower().Equals("west"))
{
Go(rooms[currentRoom].west, "west");
}

else if (command.ToLower().Equals("north"))
{
Go(rooms[currentRoom].north, "north");
}

else if (command.ToLower().Equals("south"))
{
Go(rooms[currentRoom].south, "south");
}

else if (command.ToLower().Equals("look"))
{
Look();
}
else if (command.ToLower().Equals("use"))
{
Use();
}
else
{
UnknownCommand();
}
}
}

static void Go(string place, string direction)
{
if (place.Equals(""))
{
System.Console.WriteLine("\nYou cannot go " + direction + "\n");
return;
}

currentRoom = place;

System.Console.WriteLine("\nYou have gone " + direction + "\n");
}

static void Look()
{
System.Console.WriteLine("\nLocation:");
try
{
System.Console.WriteLine(rooms[currentRoom].description + "\n");
}
catch
{
System.Console.WriteLine("There is no room with the name: " + currentRoom + "\n. Something is wrong");
Quit();
}
System.Console.WriteLine(
"You can go " +
(rooms[currentRoom].north.Equals("") ? "" : "NORTH, ") +
(rooms[currentRoom].south.Equals("") ? "" : "SOUTH, ") +
(rooms[currentRoom].east.Equals("") ? "" : "EAST, ") +
(rooms[currentRoom].west.Equals("") ? "" : "WEST, ") +
"\n or you can USE\n\n");
}
static void Use()
{
System.Console.WriteLine("\n");
try
{
System.Console.WriteLine(rooms[currentRoom].use + "\n");
}
catch
{
System.Console.WriteLine("There is no room with the name: " + currentRoom + "\n. Something is wrong");
Quit();
}
System.Console.WriteLine("\n");
}

static void Quit()
{
System.Console.WriteLine("\n------------------------------\n");
System.Console.WriteLine("Press ENTER to quit");
System.Console.ReadLine();
quit = true;
}

static void UnknownCommand()
{
System.Console.WriteLine(
"\nUnknown command, your commands are: \n QUIT, \n LOOK, \n NORTH, \n SOUTH, \n EAST, \n WEST, \n USE\n"
);
}

static void MakeRooms(string filename)
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(filename))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
string name = line;
string description = sr.ReadLine();
string use = sr.ReadLine();
string north = sr.ReadLine();
string east = sr.ReadLine();
string south = sr.ReadLine();
string west = sr.ReadLine();
if (!sr.ReadLine().Equals("#"))
{
throw new Exception("uh oh");
}

rooms.Add(name, new Room(description, use, north, east, south, west));
}
// 00000000000
// 00 ..........00
// 00 ..0...0 .00
// 0 .....<..... 0
// 0..............0
// 00 ..____ .00
// .00..........00
// ..000000000
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
Quit();
}

string[] keys = rooms.Keys.ToArray<STRING>();
currentRoom = keys[0];
}
}
}


I can see nothing wrong

pragtastic
17 Jun 2009, 7:46pm
The code actually looks fine, are you sure the data file that you import from command line arguments is setup with "use" in the correct row so it is assigned to the proper Room variable?

Might help to provide a clip of the text file you are importing.

patrickcabenjamin
17 Jun 2009, 8:11pm
I'm pretty sure It's fine but here it is anyway

Room 1
This is room 1.
You have used something
Room 2

A BIG ROOM THAT DOESNT EXIST

#
Room 2
This is room 2.
You have used something


Room 1

#
A BIG ROOM THAT DOESNT EXIST
....

Room 1



#

pragtastic
17 Jun 2009, 9:36pm
Alright, then copy/paste a short run from the console that shows the problem when using your "USE" command, visually seeing the output might help diagnose the problem.

patrickcabenjamin
17 Jun 2009, 9:58pm
WELCOME TO THE ADVENTURE GAME!
-----------------------------

Your commands are:
QUIT
LOOK
NORTH
EAST
SOUTH
WEST
USE

-----------------------------

Enter your command: Look
Location:
This is room 1

You can go NORTH, EAST, WEST

Enter your command: Use

This is room 1

Enter your command: OMG WHY WON'T YOU WORK

Unknown command, Your commands are: QUIT, LOOK, NORTH, SOUTH, EAST, WEST, USE

Enter you command: quit

----------------------------
press ENTER to quit
no

Then it got a little hazy and I didn't kick my computer

BLuKnight
18 Jun 2009, 6:14am
Without calling use, what happens when you print out the contents of rooms[currentRoom].use? Could it be that the description and use in the room object contain the same string? I would think that when initializing the use variable, you wouldn't pull that in from the file, but rather, set it to false. I'd also recommend checking your input file.

Not sure if it will help but good hunting!

patrickcabenjamin
18 Jun 2009, 8:50am
They are definately different strings. Check. They are in the right order and everything

patrickcabenjamin
18 Jun 2009, 8:53am
I have underlined the bit that might be the problem.
The strings are definately not the same.

pragtastic
18 Jun 2009, 2:29pm
Does the same problem happen with the USE output on every room imported from the file?

patrickcabenjamin
18 Jun 2009, 3:49pm
yep

It's really wierd isn't it?

BLuKnight
20 Jun 2009, 7:41am
I guess I'm stumped. It's been two days. Every find the answer to the problem?

patrickcabenjamin
20 Jun 2009, 12:47pm
I've e-mailed my brother who is my programming guru and taught me everything. He says he'll re-write the whole thing for me or solve it.

patrickcabenjamin
23 Jun 2009, 5:07pm
I know the problem and it's fixed

BLuKnight
23 Jun 2009, 5:13pm
I'm curious. (that and I enjoy learning) What was the problem?

patrickcabenjamin
23 Jun 2009, 5:26pm
To be honest What I think was wrong is that I moved the program to a different place after it had 'compiled', so that I could get at it more easily. When I changed the code, the program was back in the old place.
I did the build solution and then moved the files around a bit and voila.