You are correct. There's a logical error in the code that allows a player to attack even if they don't have enough resources, because the condition checking combines both players' resources in a way that can incorrectly evaluate to True. Specifically, the IF condition is using OR in a way that allows an attack to proceed if either player has enough resources, rather than the current player.
Here's why this happens:
The problematic condition is:
IF (player = 1 AND p1iron >= 3) OR (player = 2 AND p2iron >= 3) THEN
' Proceed with attack
Due to the way the logical operators are evaluated, this condition can incorrectly allow the attack to proceed. For example, if it's Player 2's turn (player = 2), and Player 2 has 0 iron (p2iron = 0), but Player 1 has 3 or more iron (p1iron >= 3), the condition (player = 1 AND p1iron >= 3) is False, but (player = 2 AND p2iron >= 3) is also False. However, due to the OR, the entire condition incorrectly becomes True because player = 2 is True and p1iron >= 3 is True, leading to (False AND True) OR (True AND False) which evaluates to True OR False, resulting in True.
To fix this issue, you need to ensure that the condition only checks the resources of the current player and not both players. Here's how you can modify the code for both the iron and wood attacks:
Fix for Iron Attack (Choice 4):
ELSEIF choice = 4 THEN
IF player = 1 THEN
IF p1iron >= 3 THEN
' Proceed with attack for Player 1
damage = INT(RND * 10) + 6
criticalHit = (RND < .2) ' 20% chance for critical hit
IF criticalHit THEN damage = damage * 2
p1iron = p1iron - 3
p2health = p2health - damage
PRINT "You dealt"; damage; "damage with iron!"
IF criticalHit THEN PRINT "CRITICAL HIT!"
PRINT "Remaining iron: "; p1iron
IF p2health <= 0 THEN
p2health = 0
GOTO GameOver
END IF
ELSE
PRINT "Not enough iron! You need 3 iron to attack. You have"; p1iron; "iron."
END IF
ELSE ' player = 2
IF p2iron >= 3 THEN
' Proceed with attack for Player 2
damage = INT(RND * 10) + 6
criticalHit = (RND < .2) ' 20% chance for critical hit
IF criticalHit THEN damage = damage * 2
p2iron = p2iron - 3
p1health = p1health - damage
PRINT "You dealt"; damage; "damage with iron!"
IF criticalHit THEN PRINT "CRITICAL HIT!"
PRINT "Remaining iron: "; p2iron
IF p1health <= 0 THEN
p1health = 0
GOTO GameOver
END IF
ELSE
PRINT "Not enough iron! You need 3 iron to attack. You have"; p2iron; "iron."
END IF
END IF
Fix for Wood Attack (Choice 3):
ELSEIF choice = 3 THEN
IF player = 1 THEN
IF p1wood >= 5 THEN
' Proceed with attack for Player 1
damage = INT(RND * 11)
criticalHit = (RND < .2) ' 20% chance for critical hit
IF criticalHit THEN damage = damage * 2
p1wood = p1wood - 5
p2health = p2health - damage
PRINT "You dealt"; damage; "damage with wood!"
IF criticalHit THEN PRINT "CRITICAL HIT!"
PRINT "Remaining wood: "; p1wood
IF p2health <= 0 THEN
p2health = 0
GOTO GameOver
END IF
ELSE
PRINT "Not enough wood! You need 5 wood to attack. You have"; p1wood; "wood."
END IF
ELSE ' player = 2
IF p2wood >= 5 THEN
' Proceed with attack for Player 2
damage = INT(RND * 11)
criticalHit = (RND < .2) ' 20% chance for critical hit
IF criticalHit THEN damage = damage * 2
p2wood = p2wood - 5
p1health = p1health - damage
PRINT "You dealt"; damage; "damage with wood!"
IF criticalHit THEN PRINT "CRITICAL HIT!"
PRINT "Remaining wood: "; p2wood
IF p1health <= 0 THEN
p1health = 0
GOTO GameOver
END IF
ELSE
PRINT "Not enough wood! You need 5 wood to attack. You have"; p2wood; "wood."
END IF
END IF
By restructuring the conditions to check the current player's resources separately, you prevent the wrong player's resources from affecting the ability to attack. This ensures that a player cannot attack if they do not have enough resources, and it fixes the issue where the attack could proceed incorrectly.
Explanation:
- Separate Conditions for Each Player: By using separate
IF statements for each player, you isolate the resource checks to the current player only.
- Avoid Logical Errors: The original condition combined both players' resources, leading to unintended true evaluations due to the
OR operator and operator precedence.
- Prevent Negative Resources: Ensure that resource deductions are correctly applied and that resources do not go negative by only deducting when the player has sufficient resources.
Final Note:
Always be cautious with logical conditions involving multiple variables and ensure that they are structured correctly to prevent unintended behavior due to operator precedence or logical grouping. It's often clearer and safer to write separate conditions for each case when dealing with multiple entities or players.