Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Sample of how to start building an RPG, with rooms that have different (and common) actions you can perform in them.

View Bedroom.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14
namespace BusinessObjects.RedditZorkQuestion
{
public class Bedroom : Room, ICanSleepHere
{
public Bedroom(string name) : base(name)
{
}
 
public void Sleep(Character character)
{
character.Heal(3);
}
}
}
View Bedroom.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
using System;
 
namespace BusinessObjects.RedditZorkQuestion
{
public class Character
{
public string Name { get; set; }
public int MaximumHitPoints { get; set; } // When fully healed
public int CurrentHitPoints { get; set; }
public Room CurrentRoom { get; set; }
 
public Character(string name, int hitPoints, int currentHitPoints)
{
Name = name;
MaximumHitPoints = hitPoints;
CurrentHitPoints = currentHitPoints;
}
 
public void Heal(int numberOfHitPointsToHeal)
{
// Heal the character of 'numberOfHitPointsToHeal' hit points, but don't exceed their maximum hit points.
CurrentHitPoints = Math.Min(CurrentHitPoints + numberOfHitPointsToHeal, MaximumHitPoints);
}
}
}
View Bedroom.cs
1 2 3 4 5 6 7
namespace BusinessObjects.RedditZorkQuestion
{
public interface ICanEatHere
{
void EatFood(Character character);
}
}
View Bedroom.cs
1 2 3 4 5 6 7
namespace BusinessObjects.RedditZorkQuestion
{
public interface ICanSleepHere
{
void Sleep(Character character);
}
}
View Bedroom.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
using System.Collections.Generic;
 
namespace BusinessObjects.RedditZorkQuestion
{
public class Item
{
public string Name { get; set; }
public List<Item> Items { get; set; }
 
public Item(string name)
{
Name = name;
 
Items = new List<Item>();
}
}
}
View Bedroom.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14
namespace BusinessObjects.RedditZorkQuestion
{
public class Kitchen : Room, ICanEatHere
{
public Kitchen(string name) : base(name)
{
}
 
public void EatFood(Character character)
{
character.Heal(6);
}
}
}
View Bedroom.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
namespace BusinessObjects.RedditZorkQuestion
{
public class LivingRoom : Room, ICanSleepHere, ICanEatHere
{
public LivingRoom(string name) : base(name)
{
}
 
public void EatFood(Character character)
{
character.Heal(3);
}
 
public void Sleep(Character character)
{
character.Heal(3);
}
}
}
View Bedroom.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
using System.Collections.Generic;
 
namespace BusinessObjects.RedditZorkQuestion
{
public class Room
{
public string Name { get; set; }
public List<Item> Items { get; set; }
 
public Room North { get; set; }
public Room East { get; set; }
public Room South { get; set; }
public Room West { get; set; }
 
// Some boolean variables, to see which direction you can move.
public bool CanGoNorth { get { return North != null; } }
public bool CanGoEast { get { return East != null; } }
public bool CanGoSouth { get { return South != null; } }
public bool CanGoWest { get { return West != null; } }
 
public Room(string name)
{
Name = name;
Items = new List<Item>();
}
 
public string Look()
{
string whatYouSee = string.Format("You are in the {0}. ", Name);
 
foreach(Item item in Items)
{
whatYouSee += string.Format("There is a {0} here. ", item.Name);
}
 
return whatYouSee;
}
}
}
View Bedroom.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
using BusinessObjects.RedditZorkQuestion;
using Microsoft.VisualStudio.TestTools.UnitTesting;
 
namespace TestBusinessObjects.RedditZorkQuestion
{
[TestClass]
public class Test
{
[TestMethod]
public void TestMethod1()
{
// Set up the rooms
Bedroom masterBedroom = new Bedroom("Master bedroom");
// Add an item to the master bedroom
masterBedroom.Items.Add(new Item("Queen-sized bed"));
 
// Add an item that contains other items
Item dresser = new Item("Dresser");
dresser.Items.Add(new Item("Pants"));
masterBedroom.Items.Add(dresser);
 
LivingRoom livingRoom = new LivingRoom("Living Room");
livingRoom.Items.Add(new Item("Couch"));
livingRoom.Items.Add(new Item("Big-screen TV"));
 
Kitchen kitchen = new Kitchen("Kitchen");
kitchen.Items.Add(new Item("Fridge"));
kitchen.Items.Add(new Item("Stove"));
 
// 'Connect' the rooms.
// Here's one simple way, although there are certainly better ways
masterBedroom.West = livingRoom;
livingRoom.East = masterBedroom;
 
livingRoom.North = kitchen;
kitchen.South = livingRoom;
 
// Create the starting character
Character character = new Character("Joe Smith", 20, 15);
 
// Put the character in a room
character.CurrentRoom = masterBedroom;
 
// Check to see if the character can sleep here. If so, sleep (which will heal the character).
// You do this by checking if the room implements the ICanSleepHere interface.
// This will always be 'true' in this sample, but I'm showing how you'd check to see what extra thing you can do in the room.
if(character.CurrentRoom is ICanSleepHere)
{
((ICanSleepHere)character.CurrentRoom).Sleep(character);
}
 
// Here is how you'd see if you can eat in this room
if(character.CurrentRoom is ICanEatHere)
{
((ICanEatHere)character.CurrentRoom).EatFood(character);
}
 
// If the player typed "GO WEST", check to see if they can move West.
// If so, set the current room to the room to the West.
if(character.CurrentRoom.CanGoWest)
{
character.CurrentRoom = character.CurrentRoom.West;
}
 
// If the player typed "LOOK", display the room information.
string display = character.CurrentRoom.Look();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.