Page 1 of 1

Probability Programming Problem

Posted: Tue Mar 10, 2009 6:41 pm
by natecuz
Help me, please?

Probability Programming Problem

Help me, please?

Problem:

3 shooters: A, B, C

Each has their own shooting accuracy:

A - 0.33%
B - 0.50%
C - 1.00%

They will engage in a shooting duel until only one is left standing. One shoots at a time. To compensate for the accuracy inequities of A & B...A will shoot first, B will shoot second, then C...and cycle repeats until there is a winner. Also, each shooter will always shoot at the person with the highest shooting accuracy first.

This is basically what I have thus far...of course I left out the non-trivial stuff

Code: Select all

 
Psuedo Code:
 
   A.ShootsAtTarget(C);
 
   if (C.isAlive()) {
       B.ShootsAtTarget(C);       
 
       if (C.isAlive()) {
          C.ShootsAtTarget(B) // B is now dead since C never misses       
 
           // Duel is now between A & C 
           ... // more code
       }
       // C is DEAD...Duel is now between B & A
       else ... // more code
   }
 
   // C is DEAD...Duel is now between A & B
   else {
       B.ShootsAtTarget(A);
       ... // more code
   }
 
 
   /* this basically sets the target (dueler) as dead if the random number
       is less than or equal to the shooter's accuracy 
   */
   public function ShootsAtTarget(Obj target) {    
     ran_num = Math.random()  // generate number between 0 & 1     
     if (this.accuracy <= ran_num) target.isAlive(false)
   }
 
BTW...since there is no loop...this only simulates one round of game. If I can get the first round working properly, then I'll implement a loop to play a indefinite amount of rounds...total up each shooter's wins and see if their total wins are approximately equaled to their accuracy.

Am I going about this the right way?

Any help is appreciated.

Thanks

Re: Probability Programming Problem

Posted: Tue Mar 10, 2009 7:36 pm
by josh
Hmm why use procedural code? I would use something more extensible. You know this can be solved linearly? ( eg. if calculating the shot was CPU intensive or inconvenient you could use a formula to figure out the probabilities of certain events happening. That way you get a mathematically correct answer ;-) )

Re: Probability Programming Problem

Posted: Tue Mar 10, 2009 10:06 pm
by natecuz
I changed up the code and used a more object oriented approach. And now is working.

Thanks for the response.