Skip to content

Instantly share code, notes, and snippets.

@ScottLilly
Created November 4, 2013 14:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ScottLilly/7303388 to your computer and use it in GitHub Desktop.
Save ScottLilly/7303388 to your computer and use it in GitHub Desktop.
Sample of how to start building an RPG, with rooms that have different (and common) actions you can perform in them.
namespace BusinessObjects.RedditZorkQuestion
{
public class Bedroom : Room, ICanSleepHere
{
public Bedroom(string name) : base(name)
{
}
public void Sleep(Character character)
{
character.Heal(3);
}
}
}
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);
}
}
}
namespace BusinessObjects.RedditZorkQuestion
{
public interface ICanEatHere
{
void EatFood(Character character);
}
}
namespace BusinessObjects.RedditZorkQuestion
{
public interface ICanSleepHere
{
void Sleep(Character character);
}
}
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>();
}
}
}
namespace BusinessObjects.RedditZorkQuestion
{
public class Kitchen : Room, ICanEatHere
{
public Kitchen(string name) : base(name)
{
}
public void EatFood(Character character)
{
character.Heal(6);
}
}
}
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);
}
}
}
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;
}
}
}
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