Page 1 of 1

Excluding value of $num from "randomiser"

Posted: Tue Jul 04, 2006 5:50 am
by mattcooper
Hi all,

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.

Code: Select all

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 ()
Thanks!

Posted: Tue Jul 04, 2006 6:50 am
by Jenk
You know.. you could try the crazy notion of not making the 'game' a predetermined slideshow..

anyway..

Code: Select all

<?php

$cards = array('jack', 'queen', 'king', 'joker');

shuffle($cards);

$_SESSION['card'] = $cards[0];

?>
or:

Code: Select all

<?php

$cards = array('jack', 'queen', 'king', 'joker');

$_SESSION['card'] = $cards[array_rand($cards)];

?>

Posted: Tue Jul 04, 2006 7:41 am
by mattcooper
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:

Code: Select all

$cards = array('jack', 'queen', 'king', 'joker');
shuffle($cards);
$_SESSION['card'] = $cards[0];
creates the same effect!

So how do I FORCE the $_SESSION['swf'] variable to be different to $num without making things predictable?

Thank you.

Posted: Tue Jul 04, 2006 8:09 am
by Jenk
try this, if I have got what you mean correct:

Code: Select all

<?php

$num = 1;

do {
    $_SESSION['swf'] = rand(1,4);
} while ($_SESSION['swf'] == $num);

?>

Posted: Tue Jul 04, 2006 8:18 am
by mattcooper
Yes, I see what you're getting at, but implementing that bit of code is causing an internal server error after a hage lag.

Why would that be, and is there a variation on your solution that will stop this? It looks like an endless loop ios happening!

Posted: Tue Jul 04, 2006 8:23 am
by Jenk
I did a ninja edit which you may have not noticed, originally it had the not equals operator (!=) instead of equals as it does now (==)

It's either that, or you were just unlucky and drew the same number thousands of times :p

An alternative, if I have got that wrong:

Code: Select all

<?php

$num = 1;

$_SESSION['swf'] = rand(1,4);

while ($num == $_SESSION['swf']) {
    $_SESSION['swf'] = rand(1,4);
}

?>

Posted: Tue Jul 04, 2006 12:21 pm
by mattcooper
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?

Example URL: http://www.coopermattic.co.uk/games/mtm

See what you think. I think it's slow, has too much JavaScript and is generally lacking pazazz... but it's only an experiment!

Cheers all :)