Using logical operators in shooting robot program [closed]










-1















Here is the c# code that controls an artillery shooting robot.



static bool ShouldFire(bool enemyInFront, string enemyName, int robotHealth)

bool shouldFire = true;
if (enemyInFront == true)

if (enemyName == "boss")

if (robotHealth < 50) shouldFire = false;
if (robotHealth > 100) shouldFire = true;


else

return false;

return shouldFire;



The code looks awkward somewhat and could be re-written significantly shorter. Can the community at stackoverflow suggest a c# method, that would do the same as in the code above, but using one operation? I kindly ask you to make your proposals. Here is some template of what one is looking for:



static bool ShouldFire2(bool enemyInFront, string enemyName, int robotHealth)

return enemyInFront && (...);



I would appreciate some explanation, on how to achieve the desired outcome.










share|improve this question















closed as off-topic by nilsK, Daniel A. White, Mong Zhu, Amit Joshi, Pontus Gagge Nov 13 '18 at 15:22



  • This question does not appear to be about programming within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.











  • 8





    Hi @shepengauer, welcome to SO! This question is probably better suited for Code Review. SO is for asking about problems with your code and/or why something isn't working

    – Greg
    Nov 13 '18 at 13:32






  • 3





    return enemyInFront && (enemyName != "boss" || robotHealth >= 50);?

    – Damien_The_Unbeliever
    Nov 13 '18 at 13:33






  • 3





    I'm voting to close this question as off-topic because it is asking for a code review.

    – Daniel A. White
    Nov 13 '18 at 13:41






  • 1





    @Greg, thanks for your comment. I will take this into account that such questions do not belong to SO. I will use Code Review instead next time. Kind regards, Shepengauer

    – Shepenkov Valeriy
    Nov 13 '18 at 13:41






  • 3





    to me this is a perfect case to leave the code as is. its keeping it clear and extensible.

    – Daniel A. White
    Nov 13 '18 at 13:41















-1















Here is the c# code that controls an artillery shooting robot.



static bool ShouldFire(bool enemyInFront, string enemyName, int robotHealth)

bool shouldFire = true;
if (enemyInFront == true)

if (enemyName == "boss")

if (robotHealth < 50) shouldFire = false;
if (robotHealth > 100) shouldFire = true;


else

return false;

return shouldFire;



The code looks awkward somewhat and could be re-written significantly shorter. Can the community at stackoverflow suggest a c# method, that would do the same as in the code above, but using one operation? I kindly ask you to make your proposals. Here is some template of what one is looking for:



static bool ShouldFire2(bool enemyInFront, string enemyName, int robotHealth)

return enemyInFront && (...);



I would appreciate some explanation, on how to achieve the desired outcome.










share|improve this question















closed as off-topic by nilsK, Daniel A. White, Mong Zhu, Amit Joshi, Pontus Gagge Nov 13 '18 at 15:22



  • This question does not appear to be about programming within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.











  • 8





    Hi @shepengauer, welcome to SO! This question is probably better suited for Code Review. SO is for asking about problems with your code and/or why something isn't working

    – Greg
    Nov 13 '18 at 13:32






  • 3





    return enemyInFront && (enemyName != "boss" || robotHealth >= 50);?

    – Damien_The_Unbeliever
    Nov 13 '18 at 13:33






  • 3





    I'm voting to close this question as off-topic because it is asking for a code review.

    – Daniel A. White
    Nov 13 '18 at 13:41






  • 1





    @Greg, thanks for your comment. I will take this into account that such questions do not belong to SO. I will use Code Review instead next time. Kind regards, Shepengauer

    – Shepenkov Valeriy
    Nov 13 '18 at 13:41






  • 3





    to me this is a perfect case to leave the code as is. its keeping it clear and extensible.

    – Daniel A. White
    Nov 13 '18 at 13:41













-1












-1








-1








Here is the c# code that controls an artillery shooting robot.



static bool ShouldFire(bool enemyInFront, string enemyName, int robotHealth)

bool shouldFire = true;
if (enemyInFront == true)

if (enemyName == "boss")

if (robotHealth < 50) shouldFire = false;
if (robotHealth > 100) shouldFire = true;


else

return false;

return shouldFire;



The code looks awkward somewhat and could be re-written significantly shorter. Can the community at stackoverflow suggest a c# method, that would do the same as in the code above, but using one operation? I kindly ask you to make your proposals. Here is some template of what one is looking for:



static bool ShouldFire2(bool enemyInFront, string enemyName, int robotHealth)

return enemyInFront && (...);



I would appreciate some explanation, on how to achieve the desired outcome.










share|improve this question
















Here is the c# code that controls an artillery shooting robot.



static bool ShouldFire(bool enemyInFront, string enemyName, int robotHealth)

bool shouldFire = true;
if (enemyInFront == true)

if (enemyName == "boss")

if (robotHealth < 50) shouldFire = false;
if (robotHealth > 100) shouldFire = true;


else

return false;

return shouldFire;



The code looks awkward somewhat and could be re-written significantly shorter. Can the community at stackoverflow suggest a c# method, that would do the same as in the code above, but using one operation? I kindly ask you to make your proposals. Here is some template of what one is looking for:



static bool ShouldFire2(bool enemyInFront, string enemyName, int robotHealth)

return enemyInFront && (...);



I would appreciate some explanation, on how to achieve the desired outcome.







c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 13:59









Uwe Keim

27.5k32130212




27.5k32130212










asked Nov 13 '18 at 13:28









Shepenkov ValeriyShepenkov Valeriy

385




385




closed as off-topic by nilsK, Daniel A. White, Mong Zhu, Amit Joshi, Pontus Gagge Nov 13 '18 at 15:22



  • This question does not appear to be about programming within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.







closed as off-topic by nilsK, Daniel A. White, Mong Zhu, Amit Joshi, Pontus Gagge Nov 13 '18 at 15:22



  • This question does not appear to be about programming within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 8





    Hi @shepengauer, welcome to SO! This question is probably better suited for Code Review. SO is for asking about problems with your code and/or why something isn't working

    – Greg
    Nov 13 '18 at 13:32






  • 3





    return enemyInFront && (enemyName != "boss" || robotHealth >= 50);?

    – Damien_The_Unbeliever
    Nov 13 '18 at 13:33






  • 3





    I'm voting to close this question as off-topic because it is asking for a code review.

    – Daniel A. White
    Nov 13 '18 at 13:41






  • 1





    @Greg, thanks for your comment. I will take this into account that such questions do not belong to SO. I will use Code Review instead next time. Kind regards, Shepengauer

    – Shepenkov Valeriy
    Nov 13 '18 at 13:41






  • 3





    to me this is a perfect case to leave the code as is. its keeping it clear and extensible.

    – Daniel A. White
    Nov 13 '18 at 13:41












  • 8





    Hi @shepengauer, welcome to SO! This question is probably better suited for Code Review. SO is for asking about problems with your code and/or why something isn't working

    – Greg
    Nov 13 '18 at 13:32






  • 3





    return enemyInFront && (enemyName != "boss" || robotHealth >= 50);?

    – Damien_The_Unbeliever
    Nov 13 '18 at 13:33






  • 3





    I'm voting to close this question as off-topic because it is asking for a code review.

    – Daniel A. White
    Nov 13 '18 at 13:41






  • 1





    @Greg, thanks for your comment. I will take this into account that such questions do not belong to SO. I will use Code Review instead next time. Kind regards, Shepengauer

    – Shepenkov Valeriy
    Nov 13 '18 at 13:41






  • 3





    to me this is a perfect case to leave the code as is. its keeping it clear and extensible.

    – Daniel A. White
    Nov 13 '18 at 13:41







8




8





Hi @shepengauer, welcome to SO! This question is probably better suited for Code Review. SO is for asking about problems with your code and/or why something isn't working

– Greg
Nov 13 '18 at 13:32





Hi @shepengauer, welcome to SO! This question is probably better suited for Code Review. SO is for asking about problems with your code and/or why something isn't working

– Greg
Nov 13 '18 at 13:32




3




3





return enemyInFront && (enemyName != "boss" || robotHealth >= 50);?

– Damien_The_Unbeliever
Nov 13 '18 at 13:33





return enemyInFront && (enemyName != "boss" || robotHealth >= 50);?

– Damien_The_Unbeliever
Nov 13 '18 at 13:33




3




3





I'm voting to close this question as off-topic because it is asking for a code review.

– Daniel A. White
Nov 13 '18 at 13:41





I'm voting to close this question as off-topic because it is asking for a code review.

– Daniel A. White
Nov 13 '18 at 13:41




1




1





@Greg, thanks for your comment. I will take this into account that such questions do not belong to SO. I will use Code Review instead next time. Kind regards, Shepengauer

– Shepenkov Valeriy
Nov 13 '18 at 13:41





@Greg, thanks for your comment. I will take this into account that such questions do not belong to SO. I will use Code Review instead next time. Kind regards, Shepengauer

– Shepenkov Valeriy
Nov 13 '18 at 13:41




3




3





to me this is a perfect case to leave the code as is. its keeping it clear and extensible.

– Daniel A. White
Nov 13 '18 at 13:41





to me this is a perfect case to leave the code as is. its keeping it clear and extensible.

– Daniel A. White
Nov 13 '18 at 13:41












2 Answers
2






active

oldest

votes


















2














My suggestion is this:



static bool ShouldFire2(bool enemyInFront, string enemyName, int robotHealth)

return enemyInFront && (enemyName == "boss" ? robotHealth >= 50 : true);



You first check in there is an enemy ahead. if yes, check if it is boss and if its hp greater than 50 then return true, otherwise false. If not a boss, always return true. And false if nothing in front of your character.






share|improve this answer

























  • Thanks. But, code approver says, that in that case: "Functions are different on the input (True, "boss", 10)"

    – Shepenkov Valeriy
    Nov 13 '18 at 13:44











  • sorry for the wrong condition. I fixed it

    – ghostbbbmt
    Nov 13 '18 at 13:51











  • Functions are the same, now! Thanks.

    – Shepenkov Valeriy
    Nov 13 '18 at 13:54


















0














The line 'if (robotHealth > 100) shouldFire = true;' does nothing. shouldFire is already true unless health is under 50.



So you can reduce that section to:



static bool ShouldFire(bool enemyInFront, string enemyName, int robotHealth)

bool shouldFire = true;
if (enemyInFront == true)

return !(enemyName != "boss" && robotHealth < 50);
else

return false;

return shouldFire;



Which could be reduced to:



static bool ShouldFire(bool enemyInFront, string enemyName, int robotHealth)

return enemyInFront && (enemyName != "boss"





share|improve this answer





























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    My suggestion is this:



    static bool ShouldFire2(bool enemyInFront, string enemyName, int robotHealth)

    return enemyInFront && (enemyName == "boss" ? robotHealth >= 50 : true);



    You first check in there is an enemy ahead. if yes, check if it is boss and if its hp greater than 50 then return true, otherwise false. If not a boss, always return true. And false if nothing in front of your character.






    share|improve this answer

























    • Thanks. But, code approver says, that in that case: "Functions are different on the input (True, "boss", 10)"

      – Shepenkov Valeriy
      Nov 13 '18 at 13:44











    • sorry for the wrong condition. I fixed it

      – ghostbbbmt
      Nov 13 '18 at 13:51











    • Functions are the same, now! Thanks.

      – Shepenkov Valeriy
      Nov 13 '18 at 13:54















    2














    My suggestion is this:



    static bool ShouldFire2(bool enemyInFront, string enemyName, int robotHealth)

    return enemyInFront && (enemyName == "boss" ? robotHealth >= 50 : true);



    You first check in there is an enemy ahead. if yes, check if it is boss and if its hp greater than 50 then return true, otherwise false. If not a boss, always return true. And false if nothing in front of your character.






    share|improve this answer

























    • Thanks. But, code approver says, that in that case: "Functions are different on the input (True, "boss", 10)"

      – Shepenkov Valeriy
      Nov 13 '18 at 13:44











    • sorry for the wrong condition. I fixed it

      – ghostbbbmt
      Nov 13 '18 at 13:51











    • Functions are the same, now! Thanks.

      – Shepenkov Valeriy
      Nov 13 '18 at 13:54













    2












    2








    2







    My suggestion is this:



    static bool ShouldFire2(bool enemyInFront, string enemyName, int robotHealth)

    return enemyInFront && (enemyName == "boss" ? robotHealth >= 50 : true);



    You first check in there is an enemy ahead. if yes, check if it is boss and if its hp greater than 50 then return true, otherwise false. If not a boss, always return true. And false if nothing in front of your character.






    share|improve this answer















    My suggestion is this:



    static bool ShouldFire2(bool enemyInFront, string enemyName, int robotHealth)

    return enemyInFront && (enemyName == "boss" ? robotHealth >= 50 : true);



    You first check in there is an enemy ahead. if yes, check if it is boss and if its hp greater than 50 then return true, otherwise false. If not a boss, always return true. And false if nothing in front of your character.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 '18 at 13:57

























    answered Nov 13 '18 at 13:39









    ghostbbbmtghostbbbmt

    888




    888












    • Thanks. But, code approver says, that in that case: "Functions are different on the input (True, "boss", 10)"

      – Shepenkov Valeriy
      Nov 13 '18 at 13:44











    • sorry for the wrong condition. I fixed it

      – ghostbbbmt
      Nov 13 '18 at 13:51











    • Functions are the same, now! Thanks.

      – Shepenkov Valeriy
      Nov 13 '18 at 13:54

















    • Thanks. But, code approver says, that in that case: "Functions are different on the input (True, "boss", 10)"

      – Shepenkov Valeriy
      Nov 13 '18 at 13:44











    • sorry for the wrong condition. I fixed it

      – ghostbbbmt
      Nov 13 '18 at 13:51











    • Functions are the same, now! Thanks.

      – Shepenkov Valeriy
      Nov 13 '18 at 13:54
















    Thanks. But, code approver says, that in that case: "Functions are different on the input (True, "boss", 10)"

    – Shepenkov Valeriy
    Nov 13 '18 at 13:44





    Thanks. But, code approver says, that in that case: "Functions are different on the input (True, "boss", 10)"

    – Shepenkov Valeriy
    Nov 13 '18 at 13:44













    sorry for the wrong condition. I fixed it

    – ghostbbbmt
    Nov 13 '18 at 13:51





    sorry for the wrong condition. I fixed it

    – ghostbbbmt
    Nov 13 '18 at 13:51













    Functions are the same, now! Thanks.

    – Shepenkov Valeriy
    Nov 13 '18 at 13:54





    Functions are the same, now! Thanks.

    – Shepenkov Valeriy
    Nov 13 '18 at 13:54













    0














    The line 'if (robotHealth > 100) shouldFire = true;' does nothing. shouldFire is already true unless health is under 50.



    So you can reduce that section to:



    static bool ShouldFire(bool enemyInFront, string enemyName, int robotHealth)

    bool shouldFire = true;
    if (enemyInFront == true)

    return !(enemyName != "boss" && robotHealth < 50);
    else

    return false;

    return shouldFire;



    Which could be reduced to:



    static bool ShouldFire(bool enemyInFront, string enemyName, int robotHealth)

    return enemyInFront && (enemyName != "boss"





    share|improve this answer



























      0














      The line 'if (robotHealth > 100) shouldFire = true;' does nothing. shouldFire is already true unless health is under 50.



      So you can reduce that section to:



      static bool ShouldFire(bool enemyInFront, string enemyName, int robotHealth)

      bool shouldFire = true;
      if (enemyInFront == true)

      return !(enemyName != "boss" && robotHealth < 50);
      else

      return false;

      return shouldFire;



      Which could be reduced to:



      static bool ShouldFire(bool enemyInFront, string enemyName, int robotHealth)

      return enemyInFront && (enemyName != "boss"





      share|improve this answer

























        0












        0








        0







        The line 'if (robotHealth > 100) shouldFire = true;' does nothing. shouldFire is already true unless health is under 50.



        So you can reduce that section to:



        static bool ShouldFire(bool enemyInFront, string enemyName, int robotHealth)

        bool shouldFire = true;
        if (enemyInFront == true)

        return !(enemyName != "boss" && robotHealth < 50);
        else

        return false;

        return shouldFire;



        Which could be reduced to:



        static bool ShouldFire(bool enemyInFront, string enemyName, int robotHealth)

        return enemyInFront && (enemyName != "boss"





        share|improve this answer













        The line 'if (robotHealth > 100) shouldFire = true;' does nothing. shouldFire is already true unless health is under 50.



        So you can reduce that section to:



        static bool ShouldFire(bool enemyInFront, string enemyName, int robotHealth)

        bool shouldFire = true;
        if (enemyInFront == true)

        return !(enemyName != "boss" && robotHealth < 50);
        else

        return false;

        return shouldFire;



        Which could be reduced to:



        static bool ShouldFire(bool enemyInFront, string enemyName, int robotHealth)

        return enemyInFront && (enemyName != "boss"






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 14:00









        Robin BennettRobin Bennett

        1,681312




        1,681312













            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo