Reputation: 119
I am struggling with the this for a few hours now. I only want to use a get in my property. When I call a function, it gets a number from that property(10) and should substract 1 from the 10, which makes 9. But I have no idea how to save this 9, and do it minus 1 everytime, so the next time I call it, it becomes 8. I Hope this is clear enough.
main.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
do
{
Zwakmonster zwakmonster = new Zwakmonster();
Sterkmonster sterkmonster = new Sterkmonster();
List<IMonster> monster = new List<IMonster>();
string lijn;
string test = "exit";
string nieuwelijn;
string zwak = "zwakmonster";
string sterk = "sterkmonster";
string groen = "groen";
string geel = "geel";
Sterkmonster henk = new Sterkmonster();
henk.Naam = sterk;
henk.Kleur = groen;
Zwakmonster piet = new Zwakmonster();
piet.Kleur = geel;
piet.Naam = zwak;
Console.WriteLine("Schrijf zwakmonster_iemand of sterkmonster_iemand");
lijn = Console.ReadLine();
if (lijn == test)
{
Environment.Exit(0);
}
else if (lijn.StartsWith("hit"))
{
if (lijn.StartsWith("hit "))
{
nieuwelijn = lijn.Remove(0, 4);
if (nieuwelijn == sterk)
{
henk.Hit();
Console.WriteLine(sterkmonster);
}
else if (nieuwelijn == zwak)
{
piet.Hit();
Console.WriteLine(zwakmonster);
}
else
{
Console.WriteLine("iets ging er fout");
}
}
else
{
Console.WriteLine("Ben je misschien een spatie vergeten na ''hit''?\n");
}
}
else
{
Console.WriteLine("Verkeerd commando ingevoerd\n");
}
} while (1 == 1);
}
}
}
IMonster.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
interface IMonster
{
string Naam
{
get;
set;
}
string Kleur
{
get;
set;
}
int Levens
{
get;
}
void Hit();
}
}
zwakmonster.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Zwakmonster : IMonster
{
private string mijnNaam;
private string mijnKleur;
private int mijnLevens = 10;
private int nieuwelevens;
public string Naam
{
get { return mijnNaam; }
set { mijnNaam = value; }
}
public string Kleur
{
get { return mijnKleur; }
set { mijnKleur = value; }
}
public int Levens
{
get { return mijnLevens; }
}
public void Hit()
{
mijnLevens = mijnLevens - 1;
nieuwelevens = mijnLevens;
Console.WriteLine(nieuwelevens);
}
}
}
Upvotes: 0
Views: 72
Reputation: 4094
Change this
public void Hit()
{
newlife = mijnLevens - 1;
}
to this
public void Hit()
{
mijnLevens = mijnLevens - 1;
newlife = mijnLevens;
}
this will make sure mijnLevens is decremented. Currently, mijnLevens is always 10.
Change this:
do
{
Zwakmonster zwakmonster = new Zwakmonster();
To this
Zwakmonster zwakmonster = new Zwakmonster();
do
{
So that you don't create a new object for every iteration.
Upvotes: 2
Reputation: 483
Allan Elder already gave the right answer, to make things less complicated i would use only one var for your lives count
class Zwakmonster : IMonster
{
private string mijnNaam;
private string mijnKleur;
private int lives = 10;
public string Naam
{
get { return mijnNaam; }
set { mijnNaam = value; }
}
public string Kleur
{
get { return mijnKleur; }
set { mijnKleur = value; }
}
public int Levens
{
get { return lives; }
}
public void Hit()
{
lives = lives - 1;
}
}
Upvotes: 0
Reputation: 541
mijnLevens will always have the value 10. You need to assign the reduced value back to mijnLevens
Upvotes: 0