Click here to Skip to main content
15,892,005 members
Home / Discussions / C#
   

C#

 
GeneralRe: My thoughts on C# Pin
Richard MacCutchan2-Jun-19 21:45
mveRichard MacCutchan2-Jun-19 21:45 
GeneralRe: My thoughts on C# Pin
F-ES Sitecore2-Jun-19 22:11
professionalF-ES Sitecore2-Jun-19 22:11 
GeneralRe: My thoughts on C# Pin
#realJSOP3-Jun-19 0:10
mve#realJSOP3-Jun-19 0:10 
GeneralRe: My thoughts on C# Pin
Dave Kreskowiak3-Jun-19 2:41
mveDave Kreskowiak3-Jun-19 2:41 
GeneralRe: My thoughts on C# Pin
#realJSOP2-Jun-19 4:19
mve#realJSOP2-Jun-19 4:19 
GeneralRe: My thoughts on C# Pin
jschell2-Jun-19 8:49
jschell2-Jun-19 8:49 
GeneralRe: My thoughts on C# Pin
Brian_TheLion2-Jun-19 19:46
Brian_TheLion2-Jun-19 19:46 
GeneralRe: My thoughts on C# Pin
F-ES Sitecore2-Jun-19 23:19
professionalF-ES Sitecore2-Jun-19 23:19 
I've knocked up this basic template to give you an idea what people are talking about. There is a "global" object you need to track like the player and also the current location, so you can either create these as an instance of a variable and keep a hold of them, passing them to functions\events as needed, or you could create a "static" class that will hold a reference to your player object and current location object. As I said, it's the basics, you'd need to tweak for things like containers as game objects also so you could pick up a bag, or put a small bag inside a big bag etc.

public class GameObject
{
    public string Name { get; set; }
    public int Weight { get; set; }
}

public abstract class Container
{
    public int MaxWeight { get; set; }
    public int MaxItems { get; set; }

    public List<GameObject> Objects { get; private set; }

    public Container() : this(0, 0)
    {

    }

    public Container(int maxWeight, int maxItems)
    {
        this.Objects = new List<GameObject>();
        this.MaxItems = maxItems;
        this.MaxWeight = maxWeight;
    }

    public GameObject Find(string name)
    {
        return this.Objects.FirstOrDefault(o => o.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase));
    }

    public bool CanContain(GameObject gameObject)
    {
        // if max items is set make sure we have room
        if (this.MaxItems > 0 && Objects.Count >= this.MaxItems)
        {
            return false;
        }

        // if max weight is set make sure we have capacity
        if (this.MaxWeight > 0 && Objects.Sum(o => o.Weight) + gameObject.Weight > this.MaxWeight)
        {
            return false;
        }

        return true;
    }
}

public class Player : Container
{
    public bool Get(Container container, string name)
    {
        // rather than returning true\false you can return an enum that is specific to
        // why the get failed
        GameObject targetObject = container.Find(name);

        if (targetObject == null)
        {
            return false;
        }

        if (!this.CanContain(targetObject))
        {
            return false;
        }

        container.Objects.Remove(targetObject);
        this.Objects.Add(targetObject);

        return true;
    }
}

public class Room : Container
{
    public string Name { get; set; }

    public Dictionary<string, Room> Exits { get; set; }

    public Room()
    {
        this.Exits = new Dictionary<string, Room>();
    }
}

static void Main(string[] args)
{
    Player player = new Player();

    Room startRoom = new Room();
    startRoom.Name = "Hallway";
    startRoom.Objects.Add(new GameObject { Name = "Key", Weight = 1 });

    startRoom.Exits.Add("north", new Room
    {
        Name="Kitchen"
    });

    Room currentLocation = startRoom;

    // if player does "get key"

    player.Get(currentLocation, "key");

    // if player does "go north"

    Room room = currentLocation.Exits["north"];
    if (currentLocation.Exits.ContainsKey("north"))
    {
        currentLocation = currentLocation.Exits["north"];
    }
    else
    {
        // can't go that way
    }
}

GeneralRe: My thoughts on C# Pin
Brian_TheLion3-Jun-19 12:26
Brian_TheLion3-Jun-19 12:26 
GeneralRe: My thoughts on C# Pin
Nathan Minier4-Jun-19 1:31
professionalNathan Minier4-Jun-19 1:31 
GeneralRe: My thoughts on C# Pin
Brian_TheLion4-Jun-19 14:16
Brian_TheLion4-Jun-19 14:16 
GeneralRe: My thoughts on C# Pin
Nathan Minier4-Jun-19 14:26
professionalNathan Minier4-Jun-19 14:26 
GeneralRe: My thoughts on C# Pin
Brian_TheLion4-Jun-19 14:30
Brian_TheLion4-Jun-19 14:30 
GeneralRe: My thoughts on C# Pin
Nathan Minier4-Jun-19 14:40
professionalNathan Minier4-Jun-19 14:40 
GeneralRe: My thoughts on C# Pin
Brian_TheLion4-Jun-19 14:53
Brian_TheLion4-Jun-19 14:53 
GeneralRe: My thoughts on C# Pin
Nathan Minier4-Jun-19 14:47
professionalNathan Minier4-Jun-19 14:47 
GeneralRe: My thoughts on C# Pin
Brian_TheLion4-Jun-19 15:12
Brian_TheLion4-Jun-19 15:12 
GeneralRe: My thoughts on C# Pin
Nathan Minier4-Jun-19 15:29
professionalNathan Minier4-Jun-19 15:29 
GeneralRe: My thoughts on C# Pin
Brian_TheLion4-Jun-19 16:37
Brian_TheLion4-Jun-19 16:37 
GeneralRe: My thoughts on C# Pin
Brian_TheLion4-Jun-19 15:28
Brian_TheLion4-Jun-19 15:28 
GeneralRe: My thoughts on C# Pin
Brian_TheLion4-Jun-19 15:38
Brian_TheLion4-Jun-19 15:38 
GeneralRe: My thoughts on C# Pin
Nathan Minier4-Jun-19 15:41
professionalNathan Minier4-Jun-19 15:41 
GeneralRe: My thoughts on C# Pin
Gerry Schmitz4-Jun-19 6:53
mveGerry Schmitz4-Jun-19 6:53 
GeneralRe: My thoughts on C# Pin
Brian_TheLion4-Jun-19 18:44
Brian_TheLion4-Jun-19 18:44 
QuestionHow to perform the content of a variable as a C# instruction Pin
Member 133074841-Jun-19 10:02
Member 133074841-Jun-19 10:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.