Posted by: techGuy July 28, 2008
Java help
Login in to Rate this Post:     0       ?        

I didnt quite went thru the algo but this is where u were doing wrong

while passing paremeters, it never changes the original. So your code never came out of the loop.

This explains more http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

I've done some modification to ur code.I'm not sure how correct is the changes..but u might want to do similar. Good Luck.

 

class Person{

public boolean alive;

public int number_alive;

public int wins;

public Person(boolean pAlive, int pWins)

{

alive=pAlive;

wins=pWins;

}

}

public class Duellist {

private String charlie;

private String bob;

private String aaron;

private int numberOfDuels = 10000;

int aaron_wins=0, bob_wins=0, charlie_wins=0;

boolean aaronAlive = true;

boolean bobAlive = true;

boolean charlieAlive = true;

int num_alive=3;

public void shoot(Person p, double accuracy)

{

double random_num = (double) (Math.random()*1);

if (random_num <= accuracy)

{

p.alive = false;

num_alive--;

}

}

public String startDuel()

{

Person aaron=new Person(true,0);

Person charlie=new Person(true,0);

Person bob = new Person(true,0);

do

{

if (aaron.alive)

{

if (charlie.alive)

shoot(charlie, 1/3.0);

else if (bob.alive)

shoot(bob, 1/3.0);

}

 

if (bob.alive)

{

if (charlie.alive)

shoot(charlie, 0.5);

else if (aaron.alive)

shoot(aaron, 0.5);

else

;

}

if(charlie.alive)

{

if (bob.alive)

shoot(bob, 1.0);

else if (aaron.alive)

shoot(aaron, 1.0);

else

;

}

 

}while(num_alive > 1);

if (aaron.alive)

return "Aaron";

else if(bob.alive)

return "Bob";

else

return "Charlie";

}

public int strategy1()

{

String winner="";

do

{

winner = startDuel();

if(winner == "Aaron")

aaron_wins++;

else if (winner == "Bob")

bob_wins++;

else

charlie_wins++;

numberOfDuels--;

}while (numberOfDuels >0);

return 0 ;

}

public void printOutput()

{

System.out.println("number of wins of charlie: " +charlie_wins);

System.out.println("number of wins of Aaron: " +aaron_wins);

System.out.println("number of wins of bob: " +bob_wins);

}

public static void main (String[] args) {

Duellist Duelist_Test = new Duellist();

Duelist_Test.startDuel();

Duelist_Test.strategy1();

Duelist_Test.printOutput();

}

 

}

 

I got following o/p

number of wins of charlie: 0

number of wins of Aaron: 8329

number of wins of bob: 1671

Not sure how correct the math is..

and also you might want to remove the variables that's not used on the modified one...:)

Last edited: 28-Jul-08 12:17 PM
Read Full Discussion Thread for this article