C# How to make an object from class Person kill another?










-1














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;












share|improve this question























  • 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















-1














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;












share|improve this question























  • 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













-1












-1








-1


1





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;












share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












2 Answers
2






active

oldest

votes


















0














You need 2 methods in the Person class.



  1. Hit -> This method reduce the HitPoints on self object with each hit. When the HitPoints become Zero, status Alive is set to false.


  2. Kill -> This will take person as a parameter and call Hit method on that person till that person is Alive.


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!






share|improve this answer




























    0














    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);





    share|improve this answer






















    • @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










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    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

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You need 2 methods in the Person class.



    1. Hit -> This method reduce the HitPoints on self object with each hit. When the HitPoints become Zero, status Alive is set to false.


    2. Kill -> This will take person as a parameter and call Hit method on that person till that person is Alive.


    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!






    share|improve this answer

























      0














      You need 2 methods in the Person class.



      1. Hit -> This method reduce the HitPoints on self object with each hit. When the HitPoints become Zero, status Alive is set to false.


      2. Kill -> This will take person as a parameter and call Hit method on that person till that person is Alive.


      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!






      share|improve this answer























        0












        0








        0






        You need 2 methods in the Person class.



        1. Hit -> This method reduce the HitPoints on self object with each hit. When the HitPoints become Zero, status Alive is set to false.


        2. Kill -> This will take person as a parameter and call Hit method on that person till that person is Alive.


        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!






        share|improve this answer












        You need 2 methods in the Person class.



        1. Hit -> This method reduce the HitPoints on self object with each hit. When the HitPoints become Zero, status Alive is set to false.


        2. Kill -> This will take person as a parameter and call Hit method on that person till that person is Alive.


        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!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 5:17









        dj79

        1,311213




        1,311213























            0














            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);





            share|improve this answer






















            • @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















            0














            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);





            share|improve this answer






















            • @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













            0












            0








            0






            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);





            share|improve this answer














            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);






            share|improve this answer














            share|improve this answer



            share|improve this answer








            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
















            • @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

















            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            27

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            Category:Rhetoric