Jump to content

[SOLVED] Is there a better way to do this?


woolyg

Recommended Posts

Hi all,

I'm writing part of a game engine that needs to define the outcome success (1 or 0) of a set of numbers that two teams are being compared on. I'm looking at making the last part of the process a bit more streamlined, and wonder if anyone can help me.

 

Current Process is as follows:

1. Collate Numeric Score for each team defined by team attributes

2. Compare the two scores, and the likelihood of success for a team

3. Taking the score comparisons into consideration, randomly choose whether the play was a success or a fail (1 or 0)

 

Here's the code:

 

<?php

// 1. Collate Numeric Score for each team defined by team attributes
// .. I'll just show the results of this section here, and assume the values are set

$team1_complete_total = "5367";
$team2_complete_total = "6192";


//2. Compare the two scores, and the likelihood of success for a team
// First, add the two scores up
$passage_magic_number = ($team1_complete_total + $team2_complete_total);

// Define a Success Array
$passage_success_or_fail_array = array();

//Assuming Team 1 is in possession, Place the likelihood of success or failure into the Success Array


for($t1=1;$t1<=$team1_complete_total;){ 
array_push($passage_success_or_fail_array, 1);
$t1++;
} 

for($t2=1;$t2<=$team2_complete_total;){ 
array_push($passage_success_or_fail_array, 0);
$t2++;
}


// NOW we have an array with heaps of 1's and 0's in it

//Shuffle the array
shuffle($passage_success_or_fail_array);

//Pick a random array placement number out of the current array
$passage_random_value = rand(1, $passage_magic_number);

// Define the outcome (1 or 0)
$action_outcome = $passage_success_or_fail_array[$passage_random_value];

// NOW we have a 1 or 0 to say whether the whole thing was a success or a fail.
// Crowd goes wild

?>

 

 

 

(..hopefully you're still with me)

My main worry is memory usage - if the $team1_complete_total & the $team2_complete_total values are MASSIVE (say around 100000), then the script above is attempting to load the sum of the two values, in 1's and 0's, into an array, which could well smash the memory of the host server machine, and the script might fall over.

 

Can anyone think of a better way to compare the two team totals, and compare them, and pop out a randomised outcome, using those weightings? The most important aspect of this script is that there is scope for both success AND failure (I don't want the team with more than 51% of the weight to always be successful, you know?) - as a play in a sports game can go both ways, no matter how good the teams are!

 

All input appreciated,

WoolyG

Link to comment
Share on other sites

Instead of creating a huge array, why not deal with percentages?  Sports logic dictates that the team in the lead has the advantage.  This advantage increases the larger the score gets.  So why not simply use the percentage of points (out of the total number) scored by the leading team to help determine success or failure?  That should save you from needing to deal with huge arrays.

 

A couple of syntax things I noticed:

 

1. Your scores are strings, not integers.  They may not behave the way you want them to (I don't know if automatic conversion will take place if both operands are strings).

 

2. Your for-loop uses irregular syntax.  It's normally:

 

for($t1 = 1; $t1 <= $team1_complete_total; $t1++)
{
   array_push($passage_success_or_fail_array, 1);
}

Link to comment
Share on other sites

Hey Nightslyr,

 

Thanks for the syntax fix - I'll go about correcting this in the code. Regarding the percentage idea, I did think of this, but my poor brain couldn't think of a way to do it, without placing it all into an array, and randomly picking the outcome from the array.

 

The main aspect I'm trying to maintain is weighting - if team 1 has 60% possiblity of success, and a 40% possibility of failure, I need to have the script pick a 1 or a 0 based on these weightings. Is there a way to do this without loading up the memory as much as I have?

 

This is probably the difference between the match engine taking 1 second to parse a match and 15 seconds to parse a match :)

WoolyG

 

 

Link to comment
Share on other sites

Hey Nightslyr,

 

Thanks for the syntax fix - I'll go about correcting this in the code. Regarding the percentage idea, I did think of this, but my poor brain couldn't think of a way to do it, without placing it all into an array, and randomly picking the outcome from the array.

 

The main aspect I'm trying to maintain is weighting - if team 1 has 60% possiblity of success, and a 40% possibility of failure, I need to have the script pick a 1 or a 0 based on these weightings. Is there a way to do this without loading up the memory as much as I have?

 

This is probably the difference between the match engine taking 1 second to parse a match and 15 seconds to parse a match :)

WoolyG

 

Why does it have to be either a 1 or 0?  Why not

 

if($successChance >= $target)
{
   //success
}
else
{
   //failure
}

 

Where $target is the finalized (meaning tweaked so that for large point differences it wouldn't be nearly impossible to fail) target percentage that would grant a success on the play?

 

To me, the simplest thing to do would be to randomly pick a number between 1 and 100, and test it against the percentage derived from the current game's score.  It keeps the random element of the play, and accurately follows the probability of success based on the score differential.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.