• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ebrenjr

C# Text Adventure Design

9 posts in this topic

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.
0

Share this post


Link to post
Share on other sites
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[i];
else
{
room.Description = lines[i];
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);
}
}



0

Share this post


Link to post
Share on other sites
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.

01
first
First Room
This is the first room.
02
second
Second Room
This is the second room.


Thanks for the help, its much appreciated!
0

Share this post


Link to post
Share on other sites
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
}


0

Share this post


Link to post
Share on other sites
Thanks for clarifying that.

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

[/quote]
Would I have to store it in another structure or would I be able to manipulate it as Room.roomname.whatever?
0

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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[i].ToString());
}


0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0