C# How to make an object from class Person kill another?
In the following code, I'm trying to learn how to get objects to interact with each other, because I feel that's a bit more important than what I have done now which is simply collect a bunch of variables assign to each object.
For fun, what I want these different objects to do is to kill each other. The person named Jack can kill. The other two can not. What I want Jack to do is strike the other two, making them lose 1, 5 or 10 HitPoints multiple times until they are dead, and then set their Alive to false.
I don't know how to even start this, but I think it would be a very fun and interesting exercise.
The most important thing about doing this is learning how one object can directly change something about a different object, just because it can, and that the objects it has then changed, will then suffer a consequence from this action.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OOP_Learning
class Program
static void Main(string args)
Person p1;
p1 = new Person("Jack", 27, true, true, 10);
Person p2;
p2 = new Person("Vincent", 63, true, false, 10);
Person p3;
p3 = new Person("Tim", 13, true, false, 10);
Console.ReadLine();
public class Person
public string Name get; set;
public int Age get; set;
public bool Alive get; set;
public bool AbilityToKill get; set;
public int HitPoints get; set;
public Person(string name, int age, bool alive, bool abilityToKill, int hitPoints)
HitPoints = hitPoints;
AbilityToKill = abilityToKill;
Alive = alive;
Name = name;
Age = age;
c# oop object
add a comment |Â
In the following code, I'm trying to learn how to get objects to interact with each other, because I feel that's a bit more important than what I have done now which is simply collect a bunch of variables assign to each object.
For fun, what I want these different objects to do is to kill each other. The person named Jack can kill. The other two can not. What I want Jack to do is strike the other two, making them lose 1, 5 or 10 HitPoints multiple times until they are dead, and then set their Alive to false.
I don't know how to even start this, but I think it would be a very fun and interesting exercise.
The most important thing about doing this is learning how one object can directly change something about a different object, just because it can, and that the objects it has then changed, will then suffer a consequence from this action.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OOP_Learning
class Program
static void Main(string args)
Person p1;
p1 = new Person("Jack", 27, true, true, 10);
Person p2;
p2 = new Person("Vincent", 63, true, false, 10);
Person p3;
p3 = new Person("Tim", 13, true, false, 10);
Console.ReadLine();
public class Person
public string Name get; set;
public int Age get; set;
public bool Alive get; set;
public bool AbilityToKill get; set;
public int HitPoints get; set;
public Person(string name, int age, bool alive, bool abilityToKill, int hitPoints)
HitPoints = hitPoints;
AbilityToKill = abilityToKill;
Alive = alive;
Name = name;
Age = age;
c# oop object
Some parts of your question have simple answers. For example, p2.HitPoints -= 10 will take away hit points. And p3.Alive = false will kill someone off. But you could make it more complex, like Alive could be a calculated field: get return this.HitPoints > 0; . Stuff like that. Or you could have a TakeHit method, which reduces the hit count by the given number, or even calculates a random number. Dream big
â baileyrt
Nov 11 at 1:17
1
Also, I'd recommend you change the title of this question, because "kill" might be misinterpreted as terminating a process, which is also possible. You just want to make changes to your objects
â baileyrt
Nov 11 at 1:19
@baileyrt These are excellent points. I do like to dream big and get this close knitted and sophisticated with time, but I'd also like to start small to get a better fundamental understanding. I believe the first thing I have to do is learn about the this keyword. I edited the question a bit too so I think it will be easier to understand now
â Furnus
Nov 11 at 9:18
add a comment |Â
In the following code, I'm trying to learn how to get objects to interact with each other, because I feel that's a bit more important than what I have done now which is simply collect a bunch of variables assign to each object.
For fun, what I want these different objects to do is to kill each other. The person named Jack can kill. The other two can not. What I want Jack to do is strike the other two, making them lose 1, 5 or 10 HitPoints multiple times until they are dead, and then set their Alive to false.
I don't know how to even start this, but I think it would be a very fun and interesting exercise.
The most important thing about doing this is learning how one object can directly change something about a different object, just because it can, and that the objects it has then changed, will then suffer a consequence from this action.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OOP_Learning
class Program
static void Main(string args)
Person p1;
p1 = new Person("Jack", 27, true, true, 10);
Person p2;
p2 = new Person("Vincent", 63, true, false, 10);
Person p3;
p3 = new Person("Tim", 13, true, false, 10);
Console.ReadLine();
public class Person
public string Name get; set;
public int Age get; set;
public bool Alive get; set;
public bool AbilityToKill get; set;
public int HitPoints get; set;
public Person(string name, int age, bool alive, bool abilityToKill, int hitPoints)
HitPoints = hitPoints;
AbilityToKill = abilityToKill;
Alive = alive;
Name = name;
Age = age;
c# oop object
In the following code, I'm trying to learn how to get objects to interact with each other, because I feel that's a bit more important than what I have done now which is simply collect a bunch of variables assign to each object.
For fun, what I want these different objects to do is to kill each other. The person named Jack can kill. The other two can not. What I want Jack to do is strike the other two, making them lose 1, 5 or 10 HitPoints multiple times until they are dead, and then set their Alive to false.
I don't know how to even start this, but I think it would be a very fun and interesting exercise.
The most important thing about doing this is learning how one object can directly change something about a different object, just because it can, and that the objects it has then changed, will then suffer a consequence from this action.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OOP_Learning
class Program
static void Main(string args)
Person p1;
p1 = new Person("Jack", 27, true, true, 10);
Person p2;
p2 = new Person("Vincent", 63, true, false, 10);
Person p3;
p3 = new Person("Tim", 13, true, false, 10);
Console.ReadLine();
public class Person
public string Name get; set;
public int Age get; set;
public bool Alive get; set;
public bool AbilityToKill get; set;
public int HitPoints get; set;
public Person(string name, int age, bool alive, bool abilityToKill, int hitPoints)
HitPoints = hitPoints;
AbilityToKill = abilityToKill;
Alive = alive;
Name = name;
Age = age;
c# oop object
c# oop object
edited Nov 11 at 8:26
asked Nov 11 at 1:05
Furnus
93
93
Some parts of your question have simple answers. For example, p2.HitPoints -= 10 will take away hit points. And p3.Alive = false will kill someone off. But you could make it more complex, like Alive could be a calculated field: get return this.HitPoints > 0; . Stuff like that. Or you could have a TakeHit method, which reduces the hit count by the given number, or even calculates a random number. Dream big
â baileyrt
Nov 11 at 1:17
1
Also, I'd recommend you change the title of this question, because "kill" might be misinterpreted as terminating a process, which is also possible. You just want to make changes to your objects
â baileyrt
Nov 11 at 1:19
@baileyrt These are excellent points. I do like to dream big and get this close knitted and sophisticated with time, but I'd also like to start small to get a better fundamental understanding. I believe the first thing I have to do is learn about the this keyword. I edited the question a bit too so I think it will be easier to understand now
â Furnus
Nov 11 at 9:18
add a comment |Â
Some parts of your question have simple answers. For example, p2.HitPoints -= 10 will take away hit points. And p3.Alive = false will kill someone off. But you could make it more complex, like Alive could be a calculated field: get return this.HitPoints > 0; . Stuff like that. Or you could have a TakeHit method, which reduces the hit count by the given number, or even calculates a random number. Dream big
â baileyrt
Nov 11 at 1:17
1
Also, I'd recommend you change the title of this question, because "kill" might be misinterpreted as terminating a process, which is also possible. You just want to make changes to your objects
â baileyrt
Nov 11 at 1:19
@baileyrt These are excellent points. I do like to dream big and get this close knitted and sophisticated with time, but I'd also like to start small to get a better fundamental understanding. I believe the first thing I have to do is learn about the this keyword. I edited the question a bit too so I think it will be easier to understand now
â Furnus
Nov 11 at 9:18
Some parts of your question have simple answers. For example, p2.HitPoints -= 10 will take away hit points. And p3.Alive = false will kill someone off. But you could make it more complex, like Alive could be a calculated field: get return this.HitPoints > 0; . Stuff like that. Or you could have a TakeHit method, which reduces the hit count by the given number, or even calculates a random number. Dream big
â baileyrt
Nov 11 at 1:17
Some parts of your question have simple answers. For example, p2.HitPoints -= 10 will take away hit points. And p3.Alive = false will kill someone off. But you could make it more complex, like Alive could be a calculated field: get return this.HitPoints > 0; . Stuff like that. Or you could have a TakeHit method, which reduces the hit count by the given number, or even calculates a random number. Dream big
â baileyrt
Nov 11 at 1:17
1
1
Also, I'd recommend you change the title of this question, because "kill" might be misinterpreted as terminating a process, which is also possible. You just want to make changes to your objects
â baileyrt
Nov 11 at 1:19
Also, I'd recommend you change the title of this question, because "kill" might be misinterpreted as terminating a process, which is also possible. You just want to make changes to your objects
â baileyrt
Nov 11 at 1:19
@baileyrt These are excellent points. I do like to dream big and get this close knitted and sophisticated with time, but I'd also like to start small to get a better fundamental understanding. I believe the first thing I have to do is learn about the this keyword. I edited the question a bit too so I think it will be easier to understand now
â Furnus
Nov 11 at 9:18
@baileyrt These are excellent points. I do like to dream big and get this close knitted and sophisticated with time, but I'd also like to start small to get a better fundamental understanding. I believe the first thing I have to do is learn about the this keyword. I edited the question a bit too so I think it will be easier to understand now
â Furnus
Nov 11 at 9:18
add a comment |Â
2 Answers
2
active
oldest
votes
You need 2 methods in the Person class.
Hit-> This method reduce theHitPointson self object with each hit. When theHitPointsbecome Zero, statusAliveis set tofalse.Kill -> This will take person as a parameter and call
Hitmethod on that person till that person isAlive.
Add following methods to Person class:
public void Hit()
if(Alive)
if(HitPoints > 0)
HitPoints -= 1;
else
Alive = false;
public bool Kill(Person person)
if(!AbilityToKill)
Console.WriteLine("You don't have ability to kill! You cannont kill 0.", person.Name);
return false;
while(person.Alive)
person.Hit();
Console.WriteLine("0 is dead.", person.Name);
return true;
Call Kill method in Main method.
p2.Kill(p3);
p1.Kill(p2);
p1.Kill(p3);
Hope this helps!
add a comment |Â
Is this the kind of thing you mean?
public class Person
public string Name get; private set;
public int Age get; private set;
public bool Alive get; private set;
public bool AbilityToKill get; private set;
public int HitPoints get; private set;
public void Hit(int points)
this.HitPoints -= points;
if (this.HitPoints <= 0)
this.HitPoints = 0;
this.Alive = false;
public Person(string name, int age, bool alive, bool abilityToKill, int hitPoints)
this.HitPoints = hitPoints;
this.AbilityToKill = abilityToKill;
this.Alive = alive;
this.Name = name;
this.Age = age;
Now with the check for AbilityToKill:
public class Person
public string Name get; private set;
public int Age get; private set;
public bool Alive get; private set;
public bool AbilityToKill get; private set;
public int HitPoints get; private set;
public int Attack(Person victim, int points)
var hp = victim.HitPoints;
if (this.AbilityToKill)
victim.HitPoints -= points;
if (victim.HitPoints <= 0)
victim.HitPoints = 0;
victim.Alive = false;
hp -= victim.HitPoints;
return hp;
public Person(string name, int age, bool alive, bool abilityToKill, int hitPoints)
this.HitPoints = hitPoints;
this.AbilityToKill = abilityToKill;
this.Alive = alive;
this.Name = name;
this.Age = age;
This can be used like this:
var jack = new Person("Jack", 27, true, true, 10);
var vincent = new Person("Vincent", 63, true, false, 10);
var tim = new Person("Tim", 13, true, false, 10);
var damage_done = jack.Attack(vincent, 20);
Console.WriteLine(damage_done);
The Attack method returns the actual number of hits points reduced by the attack - the damage dealt.
And here's a more strongly-typed version. Using bool for properties isn't always the clearest way to code. Through in some enums and it's clearer.
public class Person
public string Name get; private set;
public int Age get; private set;
public Alive Alive get; private set;
public AbilityToKill AbilityToKill get; private set;
public int HitPoints get; private set;
public int Attack(Person victim, int points)
var hp = victim.HitPoints;
if (this.AbilityToKill == AbilityToKill.Yes)
victim.HitPoints -= points;
if (victim.HitPoints <= 0)
victim.HitPoints = 0;
victim.Alive = Alive.No;
hp -= victim.HitPoints;
return hp;
public Person(string name, int age, Alive alive, AbilityToKill abilityToKill, int hitPoints)
this.HitPoints = hitPoints;
this.AbilityToKill = abilityToKill;
this.Alive = alive;
this.Name = name;
this.Age = age;
public enum Alive Yes, No
public enum AbilityToKill Yes, No
It's used like this:
var jack = new Person("Jack", 27, Alive.Yes, AbilityToKill.Yes, 10);
var vincent = new Person("Vincent", 63, Alive.Yes, AbilityToKill.No, 10);
var tim = new Person("Tim", 13, Alive.Yes, AbilityToKill.No, 10);
var damage_done = jack.Attack(vincent, 20);
Console.WriteLine(damage_done);
@AntonÃnLejsek - I specifically went with a simple approach as the OP doesn't appear to have a lot of experience.
â Enigmativity
Nov 11 at 4:56
@AntonÃnLejsek - I've updated the code.
â Enigmativity
Nov 11 at 5:16
1
@AntonÃnLejsek - Please don't delete your comments. It makes understanding the conversation much harder.
â Enigmativity
Nov 11 at 5:25
As my comment related to state that no longer exists, I do not find cleaning up as a bad thing.
â AntonÃn Lejsek
Nov 11 at 5:32
1
@AntonÃnLejsek - But it does relate directly to my comment. So now my comment has no context. I do find that "cleaning up", as you call it, creates a big mess.
â Enigmativity
Nov 11 at 6:04
 |Â
show 5 more comments
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need 2 methods in the Person class.
Hit-> This method reduce theHitPointson self object with each hit. When theHitPointsbecome Zero, statusAliveis set tofalse.Kill -> This will take person as a parameter and call
Hitmethod on that person till that person isAlive.
Add following methods to Person class:
public void Hit()
if(Alive)
if(HitPoints > 0)
HitPoints -= 1;
else
Alive = false;
public bool Kill(Person person)
if(!AbilityToKill)
Console.WriteLine("You don't have ability to kill! You cannont kill 0.", person.Name);
return false;
while(person.Alive)
person.Hit();
Console.WriteLine("0 is dead.", person.Name);
return true;
Call Kill method in Main method.
p2.Kill(p3);
p1.Kill(p2);
p1.Kill(p3);
Hope this helps!
add a comment |Â
You need 2 methods in the Person class.
Hit-> This method reduce theHitPointson self object with each hit. When theHitPointsbecome Zero, statusAliveis set tofalse.Kill -> This will take person as a parameter and call
Hitmethod on that person till that person isAlive.
Add following methods to Person class:
public void Hit()
if(Alive)
if(HitPoints > 0)
HitPoints -= 1;
else
Alive = false;
public bool Kill(Person person)
if(!AbilityToKill)
Console.WriteLine("You don't have ability to kill! You cannont kill 0.", person.Name);
return false;
while(person.Alive)
person.Hit();
Console.WriteLine("0 is dead.", person.Name);
return true;
Call Kill method in Main method.
p2.Kill(p3);
p1.Kill(p2);
p1.Kill(p3);
Hope this helps!
add a comment |Â
You need 2 methods in the Person class.
Hit-> This method reduce theHitPointson self object with each hit. When theHitPointsbecome Zero, statusAliveis set tofalse.Kill -> This will take person as a parameter and call
Hitmethod on that person till that person isAlive.
Add following methods to Person class:
public void Hit()
if(Alive)
if(HitPoints > 0)
HitPoints -= 1;
else
Alive = false;
public bool Kill(Person person)
if(!AbilityToKill)
Console.WriteLine("You don't have ability to kill! You cannont kill 0.", person.Name);
return false;
while(person.Alive)
person.Hit();
Console.WriteLine("0 is dead.", person.Name);
return true;
Call Kill method in Main method.
p2.Kill(p3);
p1.Kill(p2);
p1.Kill(p3);
Hope this helps!
You need 2 methods in the Person class.
Hit-> This method reduce theHitPointson self object with each hit. When theHitPointsbecome Zero, statusAliveis set tofalse.Kill -> This will take person as a parameter and call
Hitmethod on that person till that person isAlive.
Add following methods to Person class:
public void Hit()
if(Alive)
if(HitPoints > 0)
HitPoints -= 1;
else
Alive = false;
public bool Kill(Person person)
if(!AbilityToKill)
Console.WriteLine("You don't have ability to kill! You cannont kill 0.", person.Name);
return false;
while(person.Alive)
person.Hit();
Console.WriteLine("0 is dead.", person.Name);
return true;
Call Kill method in Main method.
p2.Kill(p3);
p1.Kill(p2);
p1.Kill(p3);
Hope this helps!
answered Nov 11 at 5:17
dj79
1,311213
1,311213
add a comment |Â
add a comment |Â
Is this the kind of thing you mean?
public class Person
public string Name get; private set;
public int Age get; private set;
public bool Alive get; private set;
public bool AbilityToKill get; private set;
public int HitPoints get; private set;
public void Hit(int points)
this.HitPoints -= points;
if (this.HitPoints <= 0)
this.HitPoints = 0;
this.Alive = false;
public Person(string name, int age, bool alive, bool abilityToKill, int hitPoints)
this.HitPoints = hitPoints;
this.AbilityToKill = abilityToKill;
this.Alive = alive;
this.Name = name;
this.Age = age;
Now with the check for AbilityToKill:
public class Person
public string Name get; private set;
public int Age get; private set;
public bool Alive get; private set;
public bool AbilityToKill get; private set;
public int HitPoints get; private set;
public int Attack(Person victim, int points)
var hp = victim.HitPoints;
if (this.AbilityToKill)
victim.HitPoints -= points;
if (victim.HitPoints <= 0)
victim.HitPoints = 0;
victim.Alive = false;
hp -= victim.HitPoints;
return hp;
public Person(string name, int age, bool alive, bool abilityToKill, int hitPoints)
this.HitPoints = hitPoints;
this.AbilityToKill = abilityToKill;
this.Alive = alive;
this.Name = name;
this.Age = age;
This can be used like this:
var jack = new Person("Jack", 27, true, true, 10);
var vincent = new Person("Vincent", 63, true, false, 10);
var tim = new Person("Tim", 13, true, false, 10);
var damage_done = jack.Attack(vincent, 20);
Console.WriteLine(damage_done);
The Attack method returns the actual number of hits points reduced by the attack - the damage dealt.
And here's a more strongly-typed version. Using bool for properties isn't always the clearest way to code. Through in some enums and it's clearer.
public class Person
public string Name get; private set;
public int Age get; private set;
public Alive Alive get; private set;
public AbilityToKill AbilityToKill get; private set;
public int HitPoints get; private set;
public int Attack(Person victim, int points)
var hp = victim.HitPoints;
if (this.AbilityToKill == AbilityToKill.Yes)
victim.HitPoints -= points;
if (victim.HitPoints <= 0)
victim.HitPoints = 0;
victim.Alive = Alive.No;
hp -= victim.HitPoints;
return hp;
public Person(string name, int age, Alive alive, AbilityToKill abilityToKill, int hitPoints)
this.HitPoints = hitPoints;
this.AbilityToKill = abilityToKill;
this.Alive = alive;
this.Name = name;
this.Age = age;
public enum Alive Yes, No
public enum AbilityToKill Yes, No
It's used like this:
var jack = new Person("Jack", 27, Alive.Yes, AbilityToKill.Yes, 10);
var vincent = new Person("Vincent", 63, Alive.Yes, AbilityToKill.No, 10);
var tim = new Person("Tim", 13, Alive.Yes, AbilityToKill.No, 10);
var damage_done = jack.Attack(vincent, 20);
Console.WriteLine(damage_done);
@AntonÃnLejsek - I specifically went with a simple approach as the OP doesn't appear to have a lot of experience.
â Enigmativity
Nov 11 at 4:56
@AntonÃnLejsek - I've updated the code.
â Enigmativity
Nov 11 at 5:16
1
@AntonÃnLejsek - Please don't delete your comments. It makes understanding the conversation much harder.
â Enigmativity
Nov 11 at 5:25
As my comment related to state that no longer exists, I do not find cleaning up as a bad thing.
â AntonÃn Lejsek
Nov 11 at 5:32
1
@AntonÃnLejsek - But it does relate directly to my comment. So now my comment has no context. I do find that "cleaning up", as you call it, creates a big mess.
â Enigmativity
Nov 11 at 6:04
 |Â
show 5 more comments
Is this the kind of thing you mean?
public class Person
public string Name get; private set;
public int Age get; private set;
public bool Alive get; private set;
public bool AbilityToKill get; private set;
public int HitPoints get; private set;
public void Hit(int points)
this.HitPoints -= points;
if (this.HitPoints <= 0)
this.HitPoints = 0;
this.Alive = false;
public Person(string name, int age, bool alive, bool abilityToKill, int hitPoints)
this.HitPoints = hitPoints;
this.AbilityToKill = abilityToKill;
this.Alive = alive;
this.Name = name;
this.Age = age;
Now with the check for AbilityToKill:
public class Person
public string Name get; private set;
public int Age get; private set;
public bool Alive get; private set;
public bool AbilityToKill get; private set;
public int HitPoints get; private set;
public int Attack(Person victim, int points)
var hp = victim.HitPoints;
if (this.AbilityToKill)
victim.HitPoints -= points;
if (victim.HitPoints <= 0)
victim.HitPoints = 0;
victim.Alive = false;
hp -= victim.HitPoints;
return hp;
public Person(string name, int age, bool alive, bool abilityToKill, int hitPoints)
this.HitPoints = hitPoints;
this.AbilityToKill = abilityToKill;
this.Alive = alive;
this.Name = name;
this.Age = age;
This can be used like this:
var jack = new Person("Jack", 27, true, true, 10);
var vincent = new Person("Vincent", 63, true, false, 10);
var tim = new Person("Tim", 13, true, false, 10);
var damage_done = jack.Attack(vincent, 20);
Console.WriteLine(damage_done);
The Attack method returns the actual number of hits points reduced by the attack - the damage dealt.
And here's a more strongly-typed version. Using bool for properties isn't always the clearest way to code. Through in some enums and it's clearer.
public class Person
public string Name get; private set;
public int Age get; private set;
public Alive Alive get; private set;
public AbilityToKill AbilityToKill get; private set;
public int HitPoints get; private set;
public int Attack(Person victim, int points)
var hp = victim.HitPoints;
if (this.AbilityToKill == AbilityToKill.Yes)
victim.HitPoints -= points;
if (victim.HitPoints <= 0)
victim.HitPoints = 0;
victim.Alive = Alive.No;
hp -= victim.HitPoints;
return hp;
public Person(string name, int age, Alive alive, AbilityToKill abilityToKill, int hitPoints)
this.HitPoints = hitPoints;
this.AbilityToKill = abilityToKill;
this.Alive = alive;
this.Name = name;
this.Age = age;
public enum Alive Yes, No
public enum AbilityToKill Yes, No
It's used like this:
var jack = new Person("Jack", 27, Alive.Yes, AbilityToKill.Yes, 10);
var vincent = new Person("Vincent", 63, Alive.Yes, AbilityToKill.No, 10);
var tim = new Person("Tim", 13, Alive.Yes, AbilityToKill.No, 10);
var damage_done = jack.Attack(vincent, 20);
Console.WriteLine(damage_done);
@AntonÃnLejsek - I specifically went with a simple approach as the OP doesn't appear to have a lot of experience.
â Enigmativity
Nov 11 at 4:56
@AntonÃnLejsek - I've updated the code.
â Enigmativity
Nov 11 at 5:16
1
@AntonÃnLejsek - Please don't delete your comments. It makes understanding the conversation much harder.
â Enigmativity
Nov 11 at 5:25
As my comment related to state that no longer exists, I do not find cleaning up as a bad thing.
â AntonÃn Lejsek
Nov 11 at 5:32
1
@AntonÃnLejsek - But it does relate directly to my comment. So now my comment has no context. I do find that "cleaning up", as you call it, creates a big mess.
â Enigmativity
Nov 11 at 6:04
 |Â
show 5 more comments
Is this the kind of thing you mean?
public class Person
public string Name get; private set;
public int Age get; private set;
public bool Alive get; private set;
public bool AbilityToKill get; private set;
public int HitPoints get; private set;
public void Hit(int points)
this.HitPoints -= points;
if (this.HitPoints <= 0)
this.HitPoints = 0;
this.Alive = false;
public Person(string name, int age, bool alive, bool abilityToKill, int hitPoints)
this.HitPoints = hitPoints;
this.AbilityToKill = abilityToKill;
this.Alive = alive;
this.Name = name;
this.Age = age;
Now with the check for AbilityToKill:
public class Person
public string Name get; private set;
public int Age get; private set;
public bool Alive get; private set;
public bool AbilityToKill get; private set;
public int HitPoints get; private set;
public int Attack(Person victim, int points)
var hp = victim.HitPoints;
if (this.AbilityToKill)
victim.HitPoints -= points;
if (victim.HitPoints <= 0)
victim.HitPoints = 0;
victim.Alive = false;
hp -= victim.HitPoints;
return hp;
public Person(string name, int age, bool alive, bool abilityToKill, int hitPoints)
this.HitPoints = hitPoints;
this.AbilityToKill = abilityToKill;
this.Alive = alive;
this.Name = name;
this.Age = age;
This can be used like this:
var jack = new Person("Jack", 27, true, true, 10);
var vincent = new Person("Vincent", 63, true, false, 10);
var tim = new Person("Tim", 13, true, false, 10);
var damage_done = jack.Attack(vincent, 20);
Console.WriteLine(damage_done);
The Attack method returns the actual number of hits points reduced by the attack - the damage dealt.
And here's a more strongly-typed version. Using bool for properties isn't always the clearest way to code. Through in some enums and it's clearer.
public class Person
public string Name get; private set;
public int Age get; private set;
public Alive Alive get; private set;
public AbilityToKill AbilityToKill get; private set;
public int HitPoints get; private set;
public int Attack(Person victim, int points)
var hp = victim.HitPoints;
if (this.AbilityToKill == AbilityToKill.Yes)
victim.HitPoints -= points;
if (victim.HitPoints <= 0)
victim.HitPoints = 0;
victim.Alive = Alive.No;
hp -= victim.HitPoints;
return hp;
public Person(string name, int age, Alive alive, AbilityToKill abilityToKill, int hitPoints)
this.HitPoints = hitPoints;
this.AbilityToKill = abilityToKill;
this.Alive = alive;
this.Name = name;
this.Age = age;
public enum Alive Yes, No
public enum AbilityToKill Yes, No
It's used like this:
var jack = new Person("Jack", 27, Alive.Yes, AbilityToKill.Yes, 10);
var vincent = new Person("Vincent", 63, Alive.Yes, AbilityToKill.No, 10);
var tim = new Person("Tim", 13, Alive.Yes, AbilityToKill.No, 10);
var damage_done = jack.Attack(vincent, 20);
Console.WriteLine(damage_done);
Is this the kind of thing you mean?
public class Person
public string Name get; private set;
public int Age get; private set;
public bool Alive get; private set;
public bool AbilityToKill get; private set;
public int HitPoints get; private set;
public void Hit(int points)
this.HitPoints -= points;
if (this.HitPoints <= 0)
this.HitPoints = 0;
this.Alive = false;
public Person(string name, int age, bool alive, bool abilityToKill, int hitPoints)
this.HitPoints = hitPoints;
this.AbilityToKill = abilityToKill;
this.Alive = alive;
this.Name = name;
this.Age = age;
Now with the check for AbilityToKill:
public class Person
public string Name get; private set;
public int Age get; private set;
public bool Alive get; private set;
public bool AbilityToKill get; private set;
public int HitPoints get; private set;
public int Attack(Person victim, int points)
var hp = victim.HitPoints;
if (this.AbilityToKill)
victim.HitPoints -= points;
if (victim.HitPoints <= 0)
victim.HitPoints = 0;
victim.Alive = false;
hp -= victim.HitPoints;
return hp;
public Person(string name, int age, bool alive, bool abilityToKill, int hitPoints)
this.HitPoints = hitPoints;
this.AbilityToKill = abilityToKill;
this.Alive = alive;
this.Name = name;
this.Age = age;
This can be used like this:
var jack = new Person("Jack", 27, true, true, 10);
var vincent = new Person("Vincent", 63, true, false, 10);
var tim = new Person("Tim", 13, true, false, 10);
var damage_done = jack.Attack(vincent, 20);
Console.WriteLine(damage_done);
The Attack method returns the actual number of hits points reduced by the attack - the damage dealt.
And here's a more strongly-typed version. Using bool for properties isn't always the clearest way to code. Through in some enums and it's clearer.
public class Person
public string Name get; private set;
public int Age get; private set;
public Alive Alive get; private set;
public AbilityToKill AbilityToKill get; private set;
public int HitPoints get; private set;
public int Attack(Person victim, int points)
var hp = victim.HitPoints;
if (this.AbilityToKill == AbilityToKill.Yes)
victim.HitPoints -= points;
if (victim.HitPoints <= 0)
victim.HitPoints = 0;
victim.Alive = Alive.No;
hp -= victim.HitPoints;
return hp;
public Person(string name, int age, Alive alive, AbilityToKill abilityToKill, int hitPoints)
this.HitPoints = hitPoints;
this.AbilityToKill = abilityToKill;
this.Alive = alive;
this.Name = name;
this.Age = age;
public enum Alive Yes, No
public enum AbilityToKill Yes, No
It's used like this:
var jack = new Person("Jack", 27, Alive.Yes, AbilityToKill.Yes, 10);
var vincent = new Person("Vincent", 63, Alive.Yes, AbilityToKill.No, 10);
var tim = new Person("Tim", 13, Alive.Yes, AbilityToKill.No, 10);
var damage_done = jack.Attack(vincent, 20);
Console.WriteLine(damage_done);
edited Nov 11 at 5:24
answered Nov 11 at 2:26
Enigmativity
75.1k864129
75.1k864129
@AntonÃnLejsek - I specifically went with a simple approach as the OP doesn't appear to have a lot of experience.
â Enigmativity
Nov 11 at 4:56
@AntonÃnLejsek - I've updated the code.
â Enigmativity
Nov 11 at 5:16
1
@AntonÃnLejsek - Please don't delete your comments. It makes understanding the conversation much harder.
â Enigmativity
Nov 11 at 5:25
As my comment related to state that no longer exists, I do not find cleaning up as a bad thing.
â AntonÃn Lejsek
Nov 11 at 5:32
1
@AntonÃnLejsek - But it does relate directly to my comment. So now my comment has no context. I do find that "cleaning up", as you call it, creates a big mess.
â Enigmativity
Nov 11 at 6:04
 |Â
show 5 more comments
@AntonÃnLejsek - I specifically went with a simple approach as the OP doesn't appear to have a lot of experience.
â Enigmativity
Nov 11 at 4:56
@AntonÃnLejsek - I've updated the code.
â Enigmativity
Nov 11 at 5:16
1
@AntonÃnLejsek - Please don't delete your comments. It makes understanding the conversation much harder.
â Enigmativity
Nov 11 at 5:25
As my comment related to state that no longer exists, I do not find cleaning up as a bad thing.
â AntonÃn Lejsek
Nov 11 at 5:32
1
@AntonÃnLejsek - But it does relate directly to my comment. So now my comment has no context. I do find that "cleaning up", as you call it, creates a big mess.
â Enigmativity
Nov 11 at 6:04
@AntonÃnLejsek - I specifically went with a simple approach as the OP doesn't appear to have a lot of experience.
â Enigmativity
Nov 11 at 4:56
@AntonÃnLejsek - I specifically went with a simple approach as the OP doesn't appear to have a lot of experience.
â Enigmativity
Nov 11 at 4:56
@AntonÃnLejsek - I've updated the code.
â Enigmativity
Nov 11 at 5:16
@AntonÃnLejsek - I've updated the code.
â Enigmativity
Nov 11 at 5:16
1
1
@AntonÃnLejsek - Please don't delete your comments. It makes understanding the conversation much harder.
â Enigmativity
Nov 11 at 5:25
@AntonÃnLejsek - Please don't delete your comments. It makes understanding the conversation much harder.
â Enigmativity
Nov 11 at 5:25
As my comment related to state that no longer exists, I do not find cleaning up as a bad thing.
â AntonÃn Lejsek
Nov 11 at 5:32
As my comment related to state that no longer exists, I do not find cleaning up as a bad thing.
â AntonÃn Lejsek
Nov 11 at 5:32
1
1
@AntonÃnLejsek - But it does relate directly to my comment. So now my comment has no context. I do find that "cleaning up", as you call it, creates a big mess.
â Enigmativity
Nov 11 at 6:04
@AntonÃnLejsek - But it does relate directly to my comment. So now my comment has no context. I do find that "cleaning up", as you call it, creates a big mess.
â Enigmativity
Nov 11 at 6:04
 |Â
show 5 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid â¦
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid â¦
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244944%2fc-sharp-how-to-make-an-object-from-class-person-kill-another%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Some parts of your question have simple answers. For example, p2.HitPoints -= 10 will take away hit points. And p3.Alive = false will kill someone off. But you could make it more complex, like Alive could be a calculated field: get return this.HitPoints > 0; . Stuff like that. Or you could have a TakeHit method, which reduces the hit count by the given number, or even calculates a random number. Dream big
â baileyrt
Nov 11 at 1:17
1
Also, I'd recommend you change the title of this question, because "kill" might be misinterpreted as terminating a process, which is also possible. You just want to make changes to your objects
â baileyrt
Nov 11 at 1:19
@baileyrt These are excellent points. I do like to dream big and get this close knitted and sophisticated with time, but I'd also like to start small to get a better fundamental understanding. I believe the first thing I have to do is learn about the this keyword. I edited the question a bit too so I think it will be easier to understand now
â Furnus
Nov 11 at 9:18