Results 1 to 4 of 4
Thread: Logic Error: simulated accuracy
- 10-27-2009, 04:07 AM #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; } }
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"); } } } }
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
- 10-27-2009, 07:07 AM #2
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 11
isnt it working fine? or am i missing something
- 10-27-2009, 11:59 AM #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.
- 10-27-2009, 02:39 PM #4
Senior Member
- Join Date
- Feb 2009
- Posts
- 312
- Rep Power
- 11
Similar Threads
-
Dispatch simulated events
By kusanagi97 in forum AWT / SwingReplies: 1Last Post: 10-12-2009, 03:20 PM -
[SOLVED] need help with logic operator
By auralius in forum New To JavaReplies: 10Last Post: 12-25-2008, 11:01 PM -
Logic to generate a pattern
By vijay_2008 in forum Advanced JavaReplies: 3Last Post: 11-23-2008, 03:40 AM -
Logic Error: Not Writing To File
By JDCAce in forum Advanced JavaReplies: 6Last Post: 10-21-2008, 03:13 AM -
Cant get the logic right
By jermaindefoe in forum New To JavaReplies: 4Last Post: 03-11-2008, 01:22 AM
Bookmarks