C# Game Set Bonuses

Started by
30 comments, last by raidzero 12 years, 8 months ago
Thank you, ApochiPiQ for your reply, really appreciate it.
I'm researching about enums, but can't really get my head around how to apply it to a game.
In my mind I keep wanting to use bools...


namespace TextBaseGame
{
public enum Equipment
{
EquipOne = 0,
EquipTwo = 1
}
Class Test
{
}
}


Then I have this
using System;
using System.Collections.Generic;

namespace TextBasedGame
{
public class Hero : Character
{
public List<string> items;


public Hero()
{
items = new List<string>();
}
Advertisement

Then I have this
using System;
using System.Collections.Generic;

namespace TextBasedGame
{
public class Hero : Character
{
public List<string> items;


public Hero()
{
items = new List<string>();
}



How about this:

namespace TextBasedGame
{
public class Hero : Character
{
public List<Equipment> items;


public Hero()
{
items = new List<Equipment>();
}
Now I'm a bit lost.
I currently have it like this.

Store.cs
const string devsword = "DevSword";
const string devbeanie = "DevBeanie";
const string devshirt = "DevShirt";
const string devpants = "DevPants";
const string devshoes = "DevShoes";
const string devmittens = "DevMittens";
const string devcloak = "DevCloak";
const string devpendant = "DevPendant";
const string devring = "DevRing";
const string pandabeanie = "PandaBeanie";
const string pandashield = "PandaShield";


When you purchase an item it adds it to the list.
case "DevBeanie":
case "devbeanie":
checkitem = hero.CheckItems(devbeanie);
if (checkitem == false && hasHeadgear == false)
{
if (hero.Gold >= 1)
{
hero.Gold -= 1;
hero.items.Add(devbeanie);
hasDevBeanie = true;
hasHeadgear = true;
Console.WriteLine("Thank you, anything else you want?");
Console.ReadLine();
}
else
{
Console.WriteLine("You currently have {0} gold, how do you expect to pay me?", hero.Gold);
Console.ReadLine();
}
}
else
{
Console.WriteLine("You already have a headgear");
Console.ReadLine();
}
break;


How would using enums instead improve what I have now? I can probably get rid of those bools..
Let me counter with another question: what do you gain by having strings everywhere with two copies of the item's name? (One for the variable name, one for the contents of the string.)

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]



How would using enums instead improve what I have now? I can probably get rid of those bools..




Truly, I don't think it will make that much of a difference, but it would be a tad easier for you, maybe.

What you can do is something like this:


public enum Items
{
DevSword,
DevBeanie,
DevShirt,
DevPants,
// and so on
}


Then have the player class have a list of Items, and when they buy something add whatever it is to that list

//buy sword
player.items.add(Items.DevSword);

Then to check if the player has the item

foreach(Item item in player.items)
{
if(item == Item.DevSword)
return true;
else
return false;
}



You could also make an Item class since your items have a price, it would be more code though, such as this:


public enum ItemType
{
DevSword,
DevBeanie,
DevShirt,
DevPants,
// and so on
}

abstract class Item
{
public int Cost { get; protected set; }
public ItemType Item { get; protected set;}

public Item(int cost, ItemType type)
{
Cost = cost;
Item = type;
}
}

class DevSword : Item
{
public DevSword()
: base(1, ItemType.DevSword) // cost and Type
{
}
}

Lots and lots of extra work to deal with.
So I setup all the equipment in Test class with all the enum..this is where I have trouble understanding/figuring out how can I use them.
I've spent all morning trying to figure/get it to work...frustrated now.
Tried adding it in to my current project, but to many errors occurred so I started a new project with only the necessary code.
Though, it's not working out like how I planned it...below I was just moving stuff around and removed some stuff...completely lost now
For the most part I have it like how beatlefan has it.

Hero.cs
using System;
using System.Collections.Generic;

namespace Test
{
public class Hero : Character
{
public List<Items> items;


public Hero()
{
items = new List<Items>();
}
public static void Initialize(Hero hero)
{
hero.Level = 1;
hero.CurrentHealth = 100;
hero.MaxHealth = 100;
hero.CurrentMagic = 20;
hero.MaxMagic = 20;
hero.baseStrength = 10;
hero.bonusStrength = 0;
hero.baseDefense = 5;
hero.bonusDefense = 0;
hero.bonusDamage = 0;
hero.Exp = 0;
hero.Gold = 0;
hero.isAlive = true;
}
public bool CheckItems(Items item, Hero hero)
{
foreach (Item item in hero.items)
{
if (item == Item.DevSword)
return true;
else
return false;
}
}
public static void CheckLevel(Hero hero)
{
if (hero.Exp >= 0 && hero.Exp < 50)
{
hero.Level = 1;
}
else if (hero.Exp >= 50 && hero.Exp < 250)
{
hero.Level = 2;
}
else if (hero.Exp >= 250 && hero.Exp < 2500)
{
hero.Level = 3;
}
else if (hero.Exp >= 2500 && hero.Exp < 10000)
{
hero.Level = 4;
}
else if (hero.Exp >= 10000)
{
hero.Level = 5;
}
}
public static void CheckAll(Hero hero)
{
hero.MaxHealth = 100;
hero.MaxMagic = 20;
hero.bonusStrength = 0;
hero.bonusDamage = 0;
hero.bonusDefense = 0;
Hero.CheckLevel(hero);
hero.Strength = hero.baseStrength + hero.bonusStrength;
hero.Defense = hero.baseDefense + hero.bonusDefense;
hero.PhysicalDamage = (hero.Strength * 3) + hero.bonusDamage;
if (hero.CurrentHealth > hero.MaxHealth)
{
hero.CurrentHealth = hero.MaxHealth;
}
if (hero.CurrentMagic > hero.MaxMagic)
{
hero.CurrentMagic = hero.MaxMagic;
}
}
}
}
In CheckItems do:


public bool CheckItems(Items hasThisItem, Hero hero)
{
foreach (Item item in hero.items)
{
if (item == hasThisItem) // changed part
return true;
else
return false;
}
}
Where exactly are you stuck? Compiler errors? Bugs? Can you describe exactly what's happening (or not happening) and how you expect it to work?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I'm getting:


Operator '==' cannot be applied to operands of type 'Test.Item' and 'Test.Items'
Cannot convert type 'Test.Items' to 'Test.Item'

This topic is closed to new replies.

Advertisement