Original Post
[font="Trebuchet MS"]First of I'd like to say hello to everyone! ( Uh..Hello! )
I'm new to the forums so i do not know if this is the right place so please excuse me.
I have been working on a console based Adventure game in the style of god old Zork I.
I have been googling forever trying to find some good tutorials on how to write a more advanced text Adventure while having sofisticated programming(OOP) ( i.e avoiding hardcoding locations, items and so forth ).
I'v coded a DataParser class that reads the main Game.dat file that is going to contain all game information, from locations, items to game rules and so forth.
I'd like to get some feedback about my way of parsing data aswell as to help me with my newly found problem.
My main problem is how to solve the "Game Rules", and how to handle them? Like if you pour water on the ground in the Ashforest you die or if you feed the Troll the Garlic he moves out of the way ect ect.
So far this has been solved in EVERY tutorial I'v read ( Well they are for beginners aren't they? ) by hardcoding the ruleset with a big nasty If-else if.
And since i couldn't figure it out myself i thought i could get some help by more experienced users.
I'll post the DataParser Class:
Here is a class file that contains Item.cs, Direction.cs and Room.cs
NOTE: Fieldvars is public due to testing, i will make them private.
And last, the Game.dat file:
[/font]
I'm new to the forums so i do not know if this is the right place so please excuse me.
I have been working on a console based Adventure game in the style of god old Zork I.
I have been googling forever trying to find some good tutorials on how to write a more advanced text Adventure while having sofisticated programming(OOP) ( i.e avoiding hardcoding locations, items and so forth ).
I'v coded a DataParser class that reads the main Game.dat file that is going to contain all game information, from locations, items to game rules and so forth.
I'd like to get some feedback about my way of parsing data aswell as to help me with my newly found problem.
My main problem is how to solve the "Game Rules", and how to handle them? Like if you pour water on the ground in the Ashforest you die or if you feed the Troll the Garlic he moves out of the way ect ect.
So far this has been solved in EVERY tutorial I'v read ( Well they are for beginners aren't they? ) by hardcoding the ruleset with a big nasty If-else if.
And since i couldn't figure it out myself i thought i could get some help by more experienced users.
I'll post the DataParser Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Adventure
{
public class DataParser
{
private string[] data;
public List<Room> rooms = new List<Room>();
public void InitalizeGame(string filePath)
{
ReadDataFile(filePath);
ParseData();
// Free Memmory
data = null;
}
private void ReadDataFile(string filePath)
{
/* Load Data File into Memmory */
data = File.ReadAllLines(filePath);
}
private void ParseData()
{
/* Loops through each line of the data */
for (int i = 0; i < data.Length; i++)
{
/* We use a switch to use the right action on the right keyword */
switch (data)
{
case "@area":/* The keyword for an area */
CreateArea(i);
break;
default: /* Ignore null-lines and //-comments */
break;
}
}
}
private void CreateArea(int index)
{
rooms.Add(new Room());
int listIndex = (rooms.Count - 1);
/* Start reading the file from the keyword+1 */
index++;
string[] lineparser;
for (int i = index; i < data.Length; i++)
{
/* Split it by the #. 0 argument is the subword and 1 is the information */
lineparser = data.Split('#');
lineparser[0].Trim();
switch (lineparser[0])
{
case "name":
rooms[listIndex].SetNameOfArea(lineparser[1]);
break;
case "description":
rooms[listIndex].SetDescriptionOfArea(lineparser[1]);
break;
case "item":
lineparser = lineparser[1].Split(',');
rooms[listIndex].AddItem(lineparser[0], lineparser[1], lineparser[2], lineparser[3]);
break;
case "direction":
lineparser = lineparser[1].Split(',');
rooms[listIndex].AddDirection
(
lineparser[0],
lineparser[1],
lineparser[2],
lineparser[3],
lineparser[4],
lineparser[5]
);
break;
default:
return;
}
}
}
}
}
Here is a class file that contains Item.cs, Direction.cs and Room.cs
NOTE: Fieldvars is public due to testing, i will make them private.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Adventure
{
public class Direction
{
public string directionName;
public string directionDescription;
public bool isHidden = false;
public bool isLocked = false;
public string itemRequired;
public string travelLocation;
}
public class Item
{
public string itemName;
public string itemDesc;
public bool isHidden;
public string isFound;
public string located;
}
public class Room
{
private string roomName;
private string roomDesc;
protected const string _newline = "\r\n";
private Direction[] directions = new Direction[8];
public Item[] items = new Item[4];
public void AddItem(string name, string description, string hidden, string location )
{
for (int i = 0; i < items.Length; i++)
{
if (items == null)
{
items = new Item();
items.itemName = name;
items.itemDesc = description;
items.isHidden = bool.Parse(hidden);
items.located = location;
return;
}
}
Console.WriteLine("Couldn't add item to room: {0}", roomName);
}
public void AddDirection(string name, string pathdes, string destinationArea, string hidden, string locked, string requires )
{
for (int i = 0; i < directions.Length; i++)
{
if (directions == null)
{
directions = new Direction();
directions.directionName = name;
directions.directionDescription = pathdes;
directions.travelLocation = destinationArea;
directions.isHidden = bool.Parse(hidden);
directions.isLocked = bool.Parse(locked);
directions.itemRequired = requires;
return;
}
}
Console.WriteLine("Couldn't add direction to room: {0}", roomName);
}
public void SetNameOfArea(string name)
{
this.roomName = name;
}
public void SetDescriptionOfArea(string description)
{
this.roomDesc = description;
}
public string Name
{
get { return roomName; }
}
public string Description
{
get { return roomDesc; }
}
public void GetDirectionsInArea()
{
for (int i = 0; i < directions.Length; i++)
{
if (directions == null || directions.isHidden)
return;
else
Console.Write(directions.directionDescription);
}
}
public void GetItemsInArea()
{
for(int i = 0; i < items.Length; i++)
{
if(items == null || items.isHidden)
return;
else
Console.WriteLine(items.located);
}
}
public void DisplayArea()
{
Console.WriteLine(roomName);
Console.Write([/font]roomDesc[font="Trebuchet MS"]);
}
}
}
And last, the Game.dat file:
// Item:
// Item Name, Item Examine Description, Hidden, Location
// Direction:
// Direction Name, Path Description, Travel Location, Is Hidden, Is Locked, Item Required to Unlock
//============== AREA SETUP ==============\\
@area
name#Deep Dungeon
description#You stand infront of a big dark cave. From the inside you can hear small dripping sounds of water falling from the ceiling. Around you is a thick forest. You hear a bird sing from within the forest.
direction#North,Infront of you their is a long narrow tunnel that plunges deep into the dark cave,,false,true,torch
direction#South,To the south their is a small path leading back to the forest,Gloomy Forest,false,false,none
item#Rusty Key, A small rusty key that seam really old,true,On the ground their is a small key,none
@area
name#Gloomy Forest
description#You stand in the middle of the groovy forest of Ashevale. Trees is surrounding you.
direction#North,a road leading deeper inside the forest,Deeper Dungeon,false,false,none
direction#West,,Forest of the Damned,false,false,none
item#Torch, An old torch. It still looks like it's usuable.,false,On the ground a bit away from the clearing their is a Torch,none
@area
name#Wakky Forest
description#You stand in the middle of the groovy forest of Ashevale. Trees is surrounding you.
direction#North,a road leading deeper inside the forest,Deeper Dungeon,false,false,none
direction#West,,Forest of the Damned,false,false,none
item#Torch, An old torch. It still looks like it's usuable.,false,ground,none
[/font]