Name: failer of c 2012-05-22 22:44
For some reason will not list the contents of the backpack when Look backpack is used as a command
_
using System;
namespace MazeGame.Engine.Entity
{
using System.Collections.Generic;
using System.Text;
public class Inventory
{
private Dictionary<string, List<Item>> itemList;
private int totalWeight;
public int TotalWeight
{
get { return totalWeight; }
set { totalWeight = value; }
}
public Inventory()
{
itemList = new Dictionary<string, List<Item>>();
totalWeight = 0;
}
public void AddItem(Item theItem)
{
if (itemList.ContainsKey(theItem.Label))
{
itemList[theItem.Label].Add(theItem);
}
else
{
itemList.Add(theItem.Label, new List<Item>());
itemList[theItem.Label].Add(theItem);
}
totalWeight += theItem.Weight;
}
public Item GetItem(string label)
{
Item returningItem;
try
{
returningItem = itemList[label][0];
}
catch
{
returningItem = null;
//throw new KeyNotFoundException();
}
return returningItem;
}
public void RemoveItem(Item theItem)
{
//Item theItem = itemList[label][0];
string label = theItem.Label;
if (theItem != null)
itemList[label].Remove(theItem);
//return theItem;
}
public int GetSize()
{
return itemList.Count;
}
public override string ToString()
{
StringBuilder returnMsg = new StringBuilder();
returnMsg.Append("Items Available in you backpack :: ");
foreach (KeyValuePair<string, List<Item>> kvp in itemList)
{
returnMsg.Append("'" + kvp.Value.Count + " " + kvp.Key);
if (kvp.Value.Count > 1)
returnMsg.Append("s");
returnMsg.Append("' ");
}
return returnMsg.ToString();
}
public Item HasItem(string key)
{
StringBuilder returnMsg = new StringBuilder();
foreach (KeyValuePair<string, List<Item>> kvp in itemList)
{
if (kvp.Value.Count > 0)
{
if (key == "weapon")
{
if (kvp.Value[0] is Weapon)
{
return kvp.Value[0];
}
}
if(key == "armour")
{
if (kvp.Value[0] is Armor)
{
return kvp.Value[0];
}
}
if(key == "gp")
{
if(kvp.Value[0].Label == "gp")
{
return kvp.Value[0];
}
}
if (key == "key")
{
if (kvp.Value[0].Label == "key")
{
return kvp.Value[0];
}
}
}
}
return null;
}
}//end Inventory
}
//Start LookCommand
using System;
using System.Collections.Generic;
using System.Text;
using MazeGame.Engine.Entity;
namespace MazeGame.Engine.Control
{
public class LookCommand : Command
{
public override CommandResponse Execute(ParsedInput userInput,
PlayerCharacter thePlayer)
{
CommandResponse returnMsg = new CommandResponse("Can't find that to look at", false);
// does the player want to look at their location?
if (LookLocation(userInput, thePlayer, returnMsg))
return returnMsg;
// does player want to look in their backpack
if (LookBackpackItem(userInput, thePlayer, returnMsg))
return returnMsg;
// does the player want to look at an item "ie: look 1"
if (LookItem(userInput, thePlayer, returnMsg))
return returnMsg;
// does the player want to look at an exit
LookExit(userInput, thePlayer, returnMsg);
return returnMsg;
}
private bool LookItem(ParsedInput userInput, PlayerCharacter thePlayer, CommandResponse returnMsg)
{
Item theItem = thePlayer.CurrentLocation.StockAtLocation.GetItem(userInput.Arguments[0]);
if (theItem != null)
{
returnMsg.Message = theItem.Description;
return true;
}
return false;
}
private bool LookExit(ParsedInput userInput, PlayerCharacter thePlayer, CommandResponse returnMsg)
{
Exit theExit = thePlayer.CurrentLocation.GetExit(userInput.Arguments[0]);
if (theExit != null)
{
returnMsg.Message = theExit.Description;
return true;
}
return false;
}
private bool LookLocation(ParsedInput userInput, PlayerCharacter thePlayer, CommandResponse returnMsg)
{
if (userInput.Arguments.Count == 0 ||
userInput.Arguments.Contains("here") ||
userInput.Arguments.Contains("location") ||
userInput.Arguments.Contains(thePlayer.CurrentLocation.Label))
{
returnMsg.Message = thePlayer.CurrentLocation.ToString();
return true;
}
return false;
}
private bool LookBackpackItem(ParsedInput userInput, PlayerCharacter thePlayer, CommandResponse returnMsg)
{
Item theItem;
// does player want to look in their backpack
if (userInput.Arguments.Contains("backpack"))
{
//userInput.Arguments.Remove("backpack");
theItem = thePlayer.Backpack.HasItem(userInput.Arguments[0]); // <<<<<<<<<<<<
System.Console.Write(thePlayer.CurrentLocation.StockAtLocation.ToString()); // >> prints contents
if (theItem != null)
{
returnMsg.Message = theItem.Description;
return true;
}
}
return false;
}
}
}//end LookComand
_
using System;
namespace MazeGame.Engine.Entity
{
using System.Collections.Generic;
using System.Text;
public class Inventory
{
private Dictionary<string, List<Item>> itemList;
private int totalWeight;
public int TotalWeight
{
get { return totalWeight; }
set { totalWeight = value; }
}
public Inventory()
{
itemList = new Dictionary<string, List<Item>>();
totalWeight = 0;
}
public void AddItem(Item theItem)
{
if (itemList.ContainsKey(theItem.Label))
{
itemList[theItem.Label].Add(theItem);
}
else
{
itemList.Add(theItem.Label, new List<Item>());
itemList[theItem.Label].Add(theItem);
}
totalWeight += theItem.Weight;
}
public Item GetItem(string label)
{
Item returningItem;
try
{
returningItem = itemList[label][0];
}
catch
{
returningItem = null;
//throw new KeyNotFoundException();
}
return returningItem;
}
public void RemoveItem(Item theItem)
{
//Item theItem = itemList[label][0];
string label = theItem.Label;
if (theItem != null)
itemList[label].Remove(theItem);
//return theItem;
}
public int GetSize()
{
return itemList.Count;
}
public override string ToString()
{
StringBuilder returnMsg = new StringBuilder();
returnMsg.Append("Items Available in you backpack :: ");
foreach (KeyValuePair<string, List<Item>> kvp in itemList)
{
returnMsg.Append("'" + kvp.Value.Count + " " + kvp.Key);
if (kvp.Value.Count > 1)
returnMsg.Append("s");
returnMsg.Append("' ");
}
return returnMsg.ToString();
}
public Item HasItem(string key)
{
StringBuilder returnMsg = new StringBuilder();
foreach (KeyValuePair<string, List<Item>> kvp in itemList)
{
if (kvp.Value.Count > 0)
{
if (key == "weapon")
{
if (kvp.Value[0] is Weapon)
{
return kvp.Value[0];
}
}
if(key == "armour")
{
if (kvp.Value[0] is Armor)
{
return kvp.Value[0];
}
}
if(key == "gp")
{
if(kvp.Value[0].Label == "gp")
{
return kvp.Value[0];
}
}
if (key == "key")
{
if (kvp.Value[0].Label == "key")
{
return kvp.Value[0];
}
}
}
}
return null;
}
}//end Inventory
}
//Start LookCommand
using System;
using System.Collections.Generic;
using System.Text;
using MazeGame.Engine.Entity;
namespace MazeGame.Engine.Control
{
public class LookCommand : Command
{
public override CommandResponse Execute(ParsedInput userInput,
PlayerCharacter thePlayer)
{
CommandResponse returnMsg = new CommandResponse("Can't find that to look at", false);
// does the player want to look at their location?
if (LookLocation(userInput, thePlayer, returnMsg))
return returnMsg;
// does player want to look in their backpack
if (LookBackpackItem(userInput, thePlayer, returnMsg))
return returnMsg;
// does the player want to look at an item "ie: look 1"
if (LookItem(userInput, thePlayer, returnMsg))
return returnMsg;
// does the player want to look at an exit
LookExit(userInput, thePlayer, returnMsg);
return returnMsg;
}
private bool LookItem(ParsedInput userInput, PlayerCharacter thePlayer, CommandResponse returnMsg)
{
Item theItem = thePlayer.CurrentLocation.StockAtLocation.GetItem(userInput.Arguments[0]);
if (theItem != null)
{
returnMsg.Message = theItem.Description;
return true;
}
return false;
}
private bool LookExit(ParsedInput userInput, PlayerCharacter thePlayer, CommandResponse returnMsg)
{
Exit theExit = thePlayer.CurrentLocation.GetExit(userInput.Arguments[0]);
if (theExit != null)
{
returnMsg.Message = theExit.Description;
return true;
}
return false;
}
private bool LookLocation(ParsedInput userInput, PlayerCharacter thePlayer, CommandResponse returnMsg)
{
if (userInput.Arguments.Count == 0 ||
userInput.Arguments.Contains("here") ||
userInput.Arguments.Contains("location") ||
userInput.Arguments.Contains(thePlayer.CurrentLocation.Label))
{
returnMsg.Message = thePlayer.CurrentLocation.ToString();
return true;
}
return false;
}
private bool LookBackpackItem(ParsedInput userInput, PlayerCharacter thePlayer, CommandResponse returnMsg)
{
Item theItem;
// does player want to look in their backpack
if (userInput.Arguments.Contains("backpack"))
{
//userInput.Arguments.Remove("backpack");
theItem = thePlayer.Backpack.HasItem(userInput.Arguments[0]); // <<<<<<<<<<<<
System.Console.Write(thePlayer.CurrentLocation.StockAtLocation.ToString()); // >> prints contents
if (theItem != null)
{
returnMsg.Message = theItem.Description;
return true;
}
}
return false;
}
}
}//end LookComand