Skip to content

Instantly share code, notes, and snippets.

@nelsonlaquet
Created May 18, 2012 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nelsonlaquet/2722703 to your computer and use it in GitHub Desktop.
Save nelsonlaquet/2722703 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
interface IItem
{
string Name { get; }
}
interface IUsable : IItem
{
void Use();
}
interface IExaminable : IItem
{
void Examine();
}
abstract class Key : IItem, IUsable, IExaminable
{
public string Name { get; private set; }
public Key(string name)
{
Name = name;
}
public abstract void Use();
public void Examine()
{
Console.WriteLine("Looking at a key!");
}
}
class RedKey : Key
{
public RedKey()
: base("Red")
{
}
public override void Use()
{
Console.WriteLine("Unlocking red doors");
}
}
class GreenKey : Key
{
public GreenKey()
: base("Green")
{
}
public override void Use()
{
Console.WriteLine("Unlocking green doors");
}
}
class Mural : IItem, IExaminable
{
public string Name { get; private set; }
public Mural(string name)
{
Name = name;
}
public void Examine()
{
Console.WriteLine("Looking at a mural!");
}
}
class Program
{
static void Main(string[] args)
{
var inventory = new List<IItem>
{
new RedKey(),
new GreenKey(),
new Mural("Whoa"),
new Mural("Hey There!"),
};
while (true)
{
Console.Clear();
Console.WriteLine("INVENTORY:");
for (var i = 0; i < inventory.Count; i++)
{
Console.WriteLine("[{0}] - {1}", i + 1, inventory[i].Name);
}
var input = Console.ReadLine();
var parts = input.Split(' ');
if (parts.Length == 2)
{
int itemIndex;
if (!int.TryParse(parts[1], out itemIndex))
continue;
if (itemIndex <= 0 || itemIndex > inventory.Count)
continue;
var item = inventory[itemIndex - 1];
if (parts[0] == "use")
{
var useable = item as IUsable;
if (useable != null)
useable.Use();
else
Console.WriteLine("{0} is not useable!", item.Name);
}
if (parts[0] == "examine")
{
var examineable = item as IExaminable;
if (examineable != null)
examineable.Examine();
else
Console.WriteLine("{0} is not examinable!", item.Name);
}
}
Console.WriteLine("Press any key");
Console.ReadKey();
}
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment