C# Text Adventure Design

Started by
8 comments, last by VizOne 15 years, 7 months ago
Hey everyone! This is my first post here on gamedev but I've been perusing the forums for about a month now. Basically the language I chose to start learning is C#. I'm using VS2005 and currently working on my first text adventure. My question is pretty basic and pertains to the actual way one would design a basic text adventure in C#.
using System;

class Program
{
    // basic structure for a room
    struct room
    {
        public int id;
        public string name;
        public string desc;
    }

    // function that will display room name and description
    static void enter(int pos)
    {
        location(loc);
        Console.WriteLine(loc.name);
        Console.WriteLine(loc.desc);
    }

    // function that checks current location and passes
    // variable name to enter function
    static string location(int pos)
    {
        string loc = "start";

        room start;
        start.id = 01;
        start.name = "Starting Location";
        start.desc = "This the the first room.";

        return loc;
    }

    static void Main()
    {
        enter(start.id);
    }
}

I know this isn't coded right but basically my thought process is the Main function passes the current location id to the enter function which then passes it to the location function where all the room data is stored. The location function will check the id against the ids that have been defined to determine the room variable name and then pass that back to the enter function. Then the enter function can display the room name and description once it knows the variable name of the room the player is currently in. This is very basic and eventually I plan to have the room data stored in a txt file a function will read from and not hardcoded into the program. But for the purpose of simplicity I'm trying to get this to work first. If anyone has any ideas on how I can do this or knows of a better way to implement this concept your suggestions will be much appreciated. Thanks! EDIT: Added comments in code.
Regards,Eric Brennanebrenjr@gmx.com
Advertisement
I'm a little confused about your current program, although I think you're on the right track. Here's a small example program that shows how I would do the overall structure for this sort of game. I also include the load from text file here, because it's actually much cleaner and easier to define everything in the file instead of trying to load all the rooms from code.

class Room{    public string Name;    public string Description;}class Program{    static List<Room> rooms = new List<Room>();    static Room currentRoom;    public static void Main()    {        string[] lines = File.ReadAllLines("rooms.txt");        Room room;        for(int i = 0; i < lines.Length; i++)        {            if (i % 2 == 0)                room.Name = lines;            else            {                room.Description = lines;                rooms.Add(room);            }        }        // now start the game with the first room loaded from the file        currentRoom = rooms[0];        while (currentRoom != null)        {            EnterRoom(currentRoom);            DoStuffInRoom();            // when we're ready to quit, call the following line            currentRoom = null;        }    }    static void EnterRoom(Room room)    {        Console.WriteLine(room.Name);        Console.WriteLine(room.Description);    }}
Mike Popoloski | Journal | SlimDX
Hmmm, I don't completely understand all that code but I think I get the gist of it. If I wanted to store the game data as an array rather than a list (I'm not familiar with lists in C# yet), am I on the right track with this? For some reason the only output I'm getting is "System.String[,]". Are you unable to display entire arrays on the console?

using System;using System.IO;class Game{    public class Room    {        public int id;        public string name;        public string title;        public string desc;    }    public static void Main()    {        string[,] data = new string[4,2];        int c=1, r=1;        foreach (string line in File.ReadAllLines("data.gam"))        {            data[c, r] = line;            c = c++;            if (c == 5)            {                r = r++;                c = 1;            }        }        Console.WriteLine(data);    }}

Here's my data.gam file.

01firstFirst RoomThis is the first room.02secondSecond RoomThis is the second room.

Thanks for the help, its much appreciated!
Regards,Eric Brennanebrenjr@gmx.com
Be aware that array indices start at 0 and go up to theArray.Length - 1. They do NOT go from 1 to theArray.Length.

Personally I find it a bit overcomplicated to use a 2D array. Just as Mike did, you can simply load all lines and then iterate over them, like this:
string[] lines = File.ReadAllLines("game.data");int lineIndex = 0; // which line are we currently reading?for(int i = 0; i < lines.Length/4; ++i) { // 4 lines per room. so there are lines.Length/4 rooms in the file  Room room = new Room();  room.ID = int.Parse(lines[lineIndex++]); //lineIndex++ increases lineIndex after use  room.Name = lines[lineIndex++];  room.Title = lines[lineIndex++];  room.Description = lines[lineIndex++];    // room is completely loaded, store it in whatever structure you like,   // e.g. a List<Room> like Mike did}
Andre Loker | Personal blog on .NET
Thanks for clarifying that.

Original post by VizOne
  // room is completely loaded, store it in whatever structure you like,   // e.g. a List<Room> like Mike did

Would I have to store it in another structure or would I be able to manipulate it as Room.roomname.whatever?
Regards,Eric Brennanebrenjr@gmx.com
i remember my first text adventure game, you have a much better start then i did hard coding all the rooms menus and everything in qbasic, using goto to move around... it was a long time ago and i got better :)

about the only thing i see with your program is if you use arrays instead of list you make it harder to expand your game later on, i'm working on a similar style game but multiplayer, i would expand on your rooms structure a little bit more so that they know where other rooms are in relation to each other. :) but a good start





0))))))>|FritzMar>
Quote:Original post by ebrenjr
Would I have to store it in another structure or would I be able to manipulate it as Room.roomname.whatever?

No need to copy the room data itself into a different structure. But you will need some kind of container that holds all rooms.

You can put the loaded rooms for example in a list:
class Game{ List<Room> rooms = new List<Room>(); public static void Main(){ ...  for(int i = 0; i < lines.Length/4; ++i) {    Room room = new Room();   ... // parse room as shown in previous post   rooms.Add(room);  }  // all rooms are available in rooms now  // find a room by name (C# 3 style)  Room firstRoom = rooms.Find(r => r.name == "first");  // or C# 2 style:  Room secondRoom = rooms.Find(delegate(Room r){return r.name == "second"});  // or find it by name:  firstRoom = rooms.Find(r => r.id = 1);   }}

Or you could put them into a dictionary to find a room by name:
class Game{ Dictionary<string, Room> rooms = new Dictionary<string, Room>(); public static void Main(){ ...  for(int i = 0; i < lines.Length/4; ++i) {    Room room = new Room();   ... // parse room as shown in previous post   rooms[room.name] = room.  }  // all rooms are available in rooms now and they can be looked up like  Room someRoom = rooms["first"]; }}


Alternatively you can use a Dictionary<int, Room> to store the rooms by their ID instead of name. Whatever you like and is more convenient for you.

Whatever method you use, if you have found a room in the list/dictionary/whatever you can of course use its properties (name, id, etc.) any way you like.
Andre Loker | Personal blog on .NET
Quote:Original post by ebrenjr
Hmmm, I don't completely understand all that code but I think I get the gist of it. If I wanted to store the game data as an array rather than a list (I'm not familiar with lists in C# yet), am I on the right track with this? For some reason the only output I'm getting is "System.String[,]". Are you unable to display entire arrays on the console?

CODE: Console.WriteLine(data);



You aren't able to display arrays like that. When you tell the console to 'write the data variable to a line' it replies with what data is, in your case it is a multi-dimensional array of strings so it returns System.String[,]. To display everything in the array you'd need something like:

for (int i = 0; i < data.Length; i++){    Console.WriteLine(data.ToString());}

=============================RhinoXNA - Easily start building 2D games in XNA!Projects

Hey guys, I hate to keep posting about the same stupid problem so this should be the last time. I must be an idiot but I can't get this to work, here's the code in its entirety.

using System;using System.IO;using System.Collections.Generic;class Game{    public class Room    {        public int id;        public string name;        public string title;        public string desc;    }    Dictionary<int, Room> loc = new Dictionary<int, Room>();    public static void Main()    {        string[] lines = File.ReadAllLines("data.gam");        int lineIndex = 0;        for (int i = 0; i < lines.Length / 4; i++)        {            Room rooms = new Room();            rooms.id = int.Parse(lines[lineIndex++]);            rooms.name = lines[lineIndex++];            rooms.title = lines[lineIndex++];            rooms.desc = lines[lineIndex++];            loc[rooms.id] = rooms;        }        Console.WriteLine(loc[01]);    }}

Basically once I get this to work I should be able to refer to rooms as loc[id] right? So if I wanted to display the title and description I could just do

Console.WriteLine(loc[01].title);Console WriteLine(loc[01].desc);

right? Thanks again I know this is probably annoying.
Regards,Eric Brennanebrenjr@gmx.com
Two things:

1. Main is a static method, which means that only static members can be accessed from there. loc is a non-static (or instance) field and is therefore not accessible from Main. Two solutions:
a) make loc static:
Dictionary<int, Room> loc = new Dictionary<int, Room>();
b) Create an instance of Game in Main and let the rest of the code run inside an instance of main:
class Game{...    Dictionary<int, Room> loc = new Dictionary<int, Room>();    public void Run()    {        string[] lines = File.ReadAllLines("data.gam");        int lineIndex = 0;        for (int i = 0; i < lines.Length / 4; i++)        {            Room rooms = new Room();            rooms.id = int.Parse(lines[lineIndex++]);            rooms.name = lines[lineIndex++];            rooms.title = lines[lineIndex++];            rooms.desc = lines[lineIndex++];            loc[rooms.id] = rooms;        }        Console.WriteLine(loc[01]);    }    public static void Main()    {           Game game = new Game();      game.Run();       }}


2) For a reason I don't know you are prefixing every id with a zero (like 01, 02 etc). The compiler will probably handle it anyway, but this is neither necessary nor good style. The 0 prefix has no meaning for an integer, so you should get rid of it in code and in the data file. If you really insist on having IDs like 01 and 02 with a meaningful 0 prefix you should rather use strings for IDs instead of ints.
Andre Loker | Personal blog on .NET

This topic is closed to new replies.

Advertisement