Probability Programming Problem

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
natecuz
Forum Newbie
Posts: 2
Joined: Tue Mar 10, 2009 6:35 pm

Probability Programming Problem

Post 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
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Probability Programming Problem

Post 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 ;-) )
natecuz
Forum Newbie
Posts: 2
Joined: Tue Mar 10, 2009 6:35 pm

Re: Probability Programming Problem

Post by natecuz »

I changed up the code and used a more object oriented approach. And now is working.

Thanks for the response.
Post Reply