C# Rooms
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:
I can see nothing 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
0
Comments
Might help to provide a clip of the text file you are importing.
Not sure if it will help but good hunting!
The strings are definately not the same.
It's really wierd isn't it?
I did the build solution and then moved the files around a bit and voila.