Jump to content

[SOLVED] Simple compare stats game


Schlo_50

Recommended Posts

Hello all,

 

I am trying to put together a script that works a bit like the game 'top trumps'.

 

I have an array containing car manufacturers, models and some stats such as acceleration. A function will randomly pick 2 cars out from the array and display them.

 

What I want to be able to do is compare the array elements (statistics) from the 2 cars randomly picked. I might be wrong but I think it would be more efficient to find the sum of the statistics for both cars and then whichever has the highest number wins, instead of creating a bunch of conditional statements?

 

Trouble is, I don't know how to use php to play with the stats from within the array. Does anyone have an example to show me how to reference the values or achieve a calculation to find the car with the best stats?

 

My code is as follows:

<?php

$carList = array( 
    array( 
        'manufacturer' => 'Mitsubishi', 
        'model' => 'Evolution', 
        'modexp' => '1200',
        'traction' => '6000',
        'aerodynamics' => '7000',
        'acceleration' => '10000',
        'styling' => '6000' 
    ), 
    array( 
        'manufacturer' => 'Toyota', 
        'model' => 'Celica', 
        'modexp' => '1800',
        'traction' => '2500',
        'aerodynamics' => '6000',
        'acceleration' => '5000',
        'styling' => '6000' 
    ), 	
    array( 
        'manufacturer' => 'Nissan', 
        'model' => 'Skyline', 
        'modexp' => '3000',
        'traction' => '5000',
        'aerodynamics' => '4000',
        'acceleration' => '9000',
        'styling' => '6000'
    ) 
);


function getRandomSubset($carList, $n) 
{ 
    /* 
     * If $n isn't greater than the number of entries, 
     * reset it to be the number of entries available. 
     */ 
    $n = (count($carList)<$n) ? count($carList) : $n; 
  
    $randList = array(); 

    // Pull the desired number of entry keys at random 
    $keys = array_rand($carList, $n); 

    // Loop through the keys 
    foreach($keys as $key) { 

        // Add the value to end of the return array 
        array_push($randList, $carList[$key]); 

    } 

    return $randList; 
} 



echo '<pre>'; 
print_r(getRandomSubset($carList, 2)); 
echo '</pre>'; 


?>

 

Thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/180646-solved-simple-compare-stats-game/
Share on other sites

try this out it will give you the winner on total stats or on one particular one

 

<?php

$carList = array( 
    array( 
        'manufacturer' => 'Mitsubishi', 
        'model' => 'Evolution', 
        'modexp' => '1200',
        'traction' => '6000',
        'aerodynamics' => '7000',
        'acceleration' => '10000',
        'styling' => '6000' 
    ), 
    array( 
        'manufacturer' => 'Toyota', 
        'model' => 'Celica', 
        'modexp' => '1800',
        'traction' => '2500',
        'aerodynamics' => '6000',
        'acceleration' => '5000',
        'styling' => '6000' 
    ),    
    array( 
        'manufacturer' => 'Nissan', 
        'model' => 'Skyline', 
        'modexp' => '3000',
        'traction' => '5000',
        'aerodynamics' => '4000',
        'acceleration' => '9000',
        'styling' => '6000'
    ) 
);


function getRandomSubset($carList, $n) 
{ 
    /* 
     * If $n isn't greater than the number of entries, 
     * reset it to be the number of entries available. 
     */ 
    $n = (count($carList)<$n) ? count($carList) : $n; 
  
    $randList = array(); 

    // Pull the desired number of entry keys at random 
    $keys = array_rand($carList, $n); 

    // Loop through the keys 
    foreach($keys as $key) { 

        // Add the value to end of the return array 
        array_push($randList, $carList[$key]); 

    } 

    return $randList; 
} 

function getWinner($chosen, $attr = null) {
    $index = $topStats = 0;

    foreach ($chosen as $key => $data) {
        if ($attr != null && $data[$attr] > $topStats) {
           $topStats = $data[$attr];
           $index = $key;
           continue;
        }
        if (array_sum($data) > $topStats) {
            $index = $key;
            $topStats = array_sum($data);
        }
    }
  return $chosen[$index];
}



echo '<pre>'; 
$randomSet = getRandomSubset($carList, 2);
$winner = getWinner($randomSet);
$winner_on_attribute = getWinner($randomSet, 'modexp');

print "<h1>Random Set</h1>";
print_r($randomSet);

print "<h1>Overall Winner</h1>";
print_r($winner);

print "<h1>modexp attribute Winner</h1>";
print_r($winner_on_attribute);

echo '</pre>'; 


?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.