PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I'm using the function below to set a game environment. It works, but is predictable - I would like it to be more random in terms of the mismatch between cards if $outcome = 0.
Can anyone suggest a better way to acheive this? It would be better to not see joker.swf each time $outcome = 0 and the King card has been set... it's obvious to the player that the outcome has been predetermined!
If I were to create an array of all possible cards, how would I exclude the value of $num from that array? I'm thinking that I could use the values that are left over in a function that would then go on to choose one of them in full knowledge that the movie chosen wouldnot match the card.
function set_Cards () {
// Create the correct set of cards to use for the game
// based on the value of $_SESSION['outcome']
// CREATES THE SESSION ENVIRONMENT FOR THE FLASH...
if ( $_SESSION['outcome'] == 0 ) {
// Aaaaaaaah, they're gonna lose...
$num = rand(1,3); // Generate a number to represent a card
$movie = $num+1; // The outcome will be mismatched cards
switch ( $num ) { // Defines the card that the player will "choose"
case 1 : $_SESSION['card'] = "jack"; break;
case 2 : $_SESSION['card'] = "queen"; break;
case 3 : $_SESSION['card'] = "king"; break;
}
switch ( $movie ) { // Defines the movie that the player will see
case 1 : $_SESSION['swf'] = "queen"; break;
case 2 : $_SESSION['swf'] = "king"; break;
case 3 : $_SESSION['swf'] = "jack"; break;
case 4 : $_SESSION['swf'] = "joker"; break; // This extra card caters for the fact that the result of
// the randomiser above may be "4", which causes problems
}
} // end of first "if" statement
if ( $_SESSION['outcome'] == 1 ) {
// Oooooooooh, they're gonna win!!
$num = rand(1,3);
$movie = $num;
switch ( $num ) { // Defines the card that the player will "choose"
case 1 : $_SESSION['card'] = "jack"; break;
case 2 : $_SESSION['card'] = "queen"; break;
case 3 : $_SESSION['card'] = "king"; break;
}
switch ( $movie ) { // Defines the movie that the player will see
case 1 : $_SESSION['swf'] = "jack"; break;
case 2 : $_SESSION['swf'] = "queen"; break;
case 3 : $_SESSION['swf'] = "king"; break;
}
} // end of second "if" statement
} // End of function set_Cards ()
Jenk wrote:You know.. you could try the crazy notion of not making the 'game' a predetermined slideshow..
Apologies, but aren't most 'games' of this kind "predetermined slideshows"? The PHP environment that creates this one does mean that the win/lose outcome is truly random, and that the card "chosen" is in fact random.
What I am trying to do is randomise the movie that plays in response the card chosen by the script - so, in essence, your script:
Thanks, that cured the lag - kind of, but I know that all random gens are slow. trouble is, it seems to get stuck on a particular result pattern. Is this a common problem with rand()? I've also tried mt_rand(), same problem occurs.
This is a particularly frustrating problem, is there an easier way? Anyone had to do this kind of thing before?