Thread: Logic Error: simulated accuracy

1. Member
Join Date
Sep 2009
Posts
10
Rep Power
0

Logic Error: simulated accuracy

I've been thinking about making a simulated "battlefield" program, and i'm at the time now where I've created two separate soldiers (both from the same class), and they are "fighting" each other.

Infantry Class:
Java Code:
```//Infantry Class
//Includes Soldier, AntiTank, Sniper, CovertOps, Machine Gunner, and Elite
import java.util.Random;
public class infantry
{
//Declares variables
int health;
int attack;
double accuracy;
//Creates the getHealth method
public int getHealth()
{

health = 10;
return health;

}
//Sets up the attack number by deciding whether or not a random number is between
//zero and .6
public int getAttack()
{
Random gen = new Random();
accuracy = gen.nextDouble();

if (accuracy <= .6)
attack = 2;
else
attack = 0;

return attack;
}
//Returns the accuracy
public double getAccuracy()
{
return accuracy;
}
}```
Now, the BattleField class:
Java Code:
```//BattleField
//By: Jake Peshman
//Purpose: Create a simulated modern day war, and to Learn Java
//Date Created: 9/21/09
//Last Modified: 9/26/09
import java.util.Scanner;
public class BattleField
{

public static void main (String[] args)
{
//Declares variables and creates the two soldiers
double s1Acc, s2Acc, s1Atk, s2Atk, s1HP, s2HP;
int round = 0;
String s1name, s2name;
Scanner scan = new Scanner(System.in);
s1name = scan.next();
s2name = scan.next();
infantry side1 = new infantry();
infantry side2 = new infantry();
s1Atk = side1.getAttack();
s1HP = side1.getHealth();
s2Atk = side2.getAttack();
s2HP = side2.getHealth();
//Displays the stats of the soldiers
System.out.println(s1name + " stats - Attack " + s1Atk + ". Health " + s2HP + ".");
System.out.println(s2name + " stats - Attack " + s2Atk + ". Health " + s2HP + ".");

//**********************************************
//Battle Loop
//**********************************************
while (s2HP > 0 && s1HP > 0)
{
//Sets the accuracy and attack for side 1
s1Acc = side1.getAccuracy();
s1Atk = side1.getAttack();
//Prints the accuracy and attack for side 1
System.out.println(s1name + " Accuracy***************** : " + s1Acc);
System.out.println(s1name + " Attack: " + s1Atk);
//Sets the accuracy and attack for side 2
s2Acc = side2.getAccuracy();
s2Atk = side2.getAttack();
//Prints accuracy and attack for side 2
System.out.println(s2name + " Accuracy***************** : " + s2Acc);
System.out.println(s2name + " Attack: " + s2Atk);
//Subtracting health from the attack
s1HP -= (s2Atk);
s2HP -= (s1Atk);
//Prints the updated health
System.out.println(s1name + " Health: " + s1HP);
System.out.println(s2name + " Health: " + s2HP);
//Provides a clear separator per round
System.out.println("-------------------------------------------------------------");
//Increments the round counter
round++;
//Chooses if a side is dead
if (s1HP <= 0)
{
//Prints side 2's health and the number of rounds if side 2 wins
System.out.println(s2name + " wins!");
System.out.println(s2name + " health: " + s2HP);
System.out.println(round + " rounds");
}
else if (s2HP <= 0)
{
//Prints side 1's health and the number of rounds if side 2 wins
System.out.println(s1name + " wins!");
System.out.println(s2name + " health: " + s1HP);
System.out.println(round + " rounds");
}

}

}
}```
And, finally, the result:
Java Code:
```Allies
Axis
Allies stats - Attack 2.0. Health 10.0.
Axis stats - Attack 0.0. Health 10.0.
Allies Accuracy***************** : 0.10510401080009257
Allies Attack: 0.0
Axis Accuracy***************** : 0.9088780651608094
Axis Attack: 2.0
Allies Health: 8.0
Axis Health: 10.0
-------------------------------------------------------------
Allies Accuracy***************** : 0.7245537164782846
Allies Attack: 0.0
Axis Accuracy***************** : 0.4763987607903176
Axis Attack: 2.0
Allies Health: 6.0
Axis Health: 10.0
-------------------------------------------------------------
Allies Accuracy***************** : 0.6727377305162932
Allies Attack: 2.0
Axis Accuracy***************** : 0.18930255373352556
Axis Attack: 0.0
Allies Health: 6.0
Axis Health: 8.0
-------------------------------------------------------------
Allies Accuracy***************** : 0.59515596196702
Allies Attack: 2.0
Axis Accuracy***************** : 0.605454241709178
Axis Attack: 2.0
Allies Health: 4.0
Axis Health: 6.0
-------------------------------------------------------------
Allies Accuracy***************** : 0.1653979865429379
Allies Attack: 2.0
Axis Accuracy***************** : 0.5870144988656244
Axis Attack: 2.0
Allies Health: 2.0
Axis Health: 4.0
-------------------------------------------------------------
Allies Accuracy***************** : 0.5123892948439682
Allies Attack: 2.0
Axis Accuracy***************** : 0.3064485617912225
Axis Attack: 2.0
Allies Health: 0.0
Axis Health: 2.0
-------------------------------------------------------------
Axis wins!
Axis health: 2.0
6 rounds```
In a nutshell: if the random number is between 0 and .6, attack is supposed to be 2. Otherwise, its supposed to be zero, and that is supposed to update every round. However, it isn't doing so. I cannot seem to figure this out...can anyone see something i'm not?

2. Senior Member
Join Date
Oct 2009
Location
California,US
Posts
201
Rep Power
12
isnt it working fine? or am i missing something

3. Senior Member
Join Date
Jan 2009
Posts
119
Rep Power
0
It seems to be working just fine..though if it is a variable that is not getting updated, then try making it global.

4. Senior Member
Join Date
Feb 2009
Posts
312
Rep Power
12
The accuracy you are displaying is not the accuracy of the attack.

Since you are retrieving the accuracy before the attack, and the accuracy is being recalculated during the getAttack(), the accuracy value would have been changed.

Try changing to get the accuracy after you get the attack.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•