Using logical operators in shooting robot program [closed]
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#
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.
|
show 4 more comments
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#
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.
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
|
show 4 more comments
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#
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#
c#
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.
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.
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
|
show 4 more comments
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
|
show 4 more comments
2 Answers
2
active
oldest
votes
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.
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
add a comment |
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"
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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"
add a comment |
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"
add a comment |
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"
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"
answered Nov 13 '18 at 14:00
Robin BennettRobin Bennett
1,681312
1,681312
add a comment |
add a comment |
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