SirChick Posted August 15, 2007 Share Posted August 15, 2007 Is there a way to do something like this using php? I dont know how to code this but this is the logic of my idea: If exp =< 100 rand(1,5) Include "You Loose".php Include "You Loose".php Include "You Win".php Include "You Loose".php Include "You Win".php If exp > 100 rand(1,5) Include "You Win".php Include "You Win".php Include "You Win".php Include "You Win".php Include "You Loose".php Quote Link to comment https://forums.phpfreaks.com/topic/65061-case-statement-with-rand-feature/ Share on other sites More sharing options...
thedarkwinter Posted August 15, 2007 Share Posted August 15, 2007 okay what exactly are you trying to do heres a go at it: <?php if ($exp <= 100) { } ?> Quote Link to comment https://forums.phpfreaks.com/topic/65061-case-statement-with-rand-feature/#findComment-324687 Share on other sites More sharing options...
thedarkwinter Posted August 15, 2007 Share Posted August 15, 2007 ... sorry, pushed something okay what exactly are you trying to do heres a go at it: <?php $val = rand(1,5); if ($exp <= 100) { if ($val == 1) { incluude_once("youwin.php"; } // else.... } else { if ($val == 1) { incluude_once("youwin.php"; } //... } ?> something along those lines? Quote Link to comment https://forums.phpfreaks.com/topic/65061-case-statement-with-rand-feature/#findComment-324689 Share on other sites More sharing options...
SirChick Posted August 15, 2007 Author Share Posted August 15, 2007 well ok ill explain. If a player have 100 exp or less and they attempt to do a mission on my game as its called there needs to be a random amount of times they will succeed. For example: Say they have less than 100 exp. Now they try mission 1. With less than 100 exp there is odds of like 2 in 5 chances of completion: rand(1,5) Include "You Loose".php Include "You Loose".php Include "You Win".php Include "You Loose".php Include "You Win".php like that ^ the rand feature picture one of them 5 each time the rand is started. But now say they have over 100 exp and do the same mission.... they now have 4 out of 5 chances of winning: If exp > 100 rand(1,5) Include "You Win".php Include "You Win".php Include "You Win".php Include "You Win".php Include "You Loose".php Basically the rand will select one of the 5 possible choices. So its not predictable but there is always still a chance of a loss. Quote Link to comment https://forums.phpfreaks.com/topic/65061-case-statement-with-rand-feature/#findComment-324697 Share on other sites More sharing options...
MadTechie Posted August 15, 2007 Share Posted August 15, 2007 try $chance = 20; //that % so 1 in 5 $gamble = rand(1, 100); if($gamble =< $chance) { echo "win"; $gamble2= rand(1, 5); switch($gamble2) { case 1: include "win1.php"; break; case 2: include "win2.php"; break; case 3: include "win3.php"; break; case 4: include "win4.php"; break; case 5: include "win5.php"; break; } }else{ echo "loss"; } Quote Link to comment https://forums.phpfreaks.com/topic/65061-case-statement-with-rand-feature/#findComment-324702 Share on other sites More sharing options...
Psycho Posted August 15, 2007 Share Posted August 15, 2007 It is "lose", not "loose" (i.e. not tight). And I would suggest not using php files with spaces in the names. It can all be done in three lines: <?php $chance = ($exp<100)?2:4; $inclFile = ($rand<=rand(1,5))?'You_Win.php':'You_Lose.php'; include($inclFile); ?> Quote Link to comment https://forums.phpfreaks.com/topic/65061-case-statement-with-rand-feature/#findComment-324703 Share on other sites More sharing options...
thedarkwinter Posted August 15, 2007 Share Posted August 15, 2007 okay so lets try this: <?php // whatever code is used to get the val of $exp $val = rand(1,5); // generate random number to five if ( ($exp < 100) and ($val > 3) ) { include ("win.php"); } // low experience, value is more than 3 = win if ( ($exp < 100) and ($val <= 3) ) { include ("lose.php"); } // low experience, value is less/equal to 3 = lose if ( ($exp >= 100) and ($val > 1) ) { include ("win.php"); } // high experience, value is more than 1 = win if ( ($exp >= 100) and ($val = 1) ) { include ("lose.php"); } // high experience, value is 1 = lose ?> hows that? Quote Link to comment https://forums.phpfreaks.com/topic/65061-case-statement-with-rand-feature/#findComment-324705 Share on other sites More sharing options...
SirChick Posted August 15, 2007 Author Share Posted August 15, 2007 woah there all so different! im confused on what each one is doing... whats the ($val = 1) all about on that last one ? Whats the ?2:4; all about on the end of the $chance line on the second code? And i dont think the % is a wise idea on the 3rd because that means till be every 5th time which makes it not random Quote Link to comment https://forums.phpfreaks.com/topic/65061-case-statement-with-rand-feature/#findComment-324711 Share on other sites More sharing options...
Barand Posted August 15, 2007 Share Posted August 15, 2007 <?php $win = ($exp <= 100) ? rand(1,5) <= 2 : rand(1,5) <= 4; include $win ? 'You Win.php' : 'You Lose.php'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/65061-case-statement-with-rand-feature/#findComment-324716 Share on other sites More sharing options...
Psycho Posted August 15, 2007 Share Posted August 15, 2007 <?php //If $exp<100 sets $chance to 2, else it is set to 4 //Chance represents the chance in 5 that the user will win $chance = ($exp<100)?2:4; //Generates a random number (1-5) and if it is equal to or less than //chance, sets the include file to the Win file, else sets it to the Lose file $inclFile = (rand(1,5)<=$chance)?'You_Win.php':'You_Lose.php'; //Includes the file include($inclFile); ?> Quote Link to comment https://forums.phpfreaks.com/topic/65061-case-statement-with-rand-feature/#findComment-324724 Share on other sites More sharing options...
thedarkwinter Posted August 15, 2007 Share Posted August 15, 2007 in mine i meant ($val == 1), sorry type but the last post looks more efficient Quote Link to comment https://forums.phpfreaks.com/topic/65061-case-statement-with-rand-feature/#findComment-324725 Share on other sites More sharing options...
NArc0t1c Posted August 15, 2007 Share Posted August 15, 2007 Ooh.. <?php $Number = rand(1,5); If ($exp =< 100){ if ($Number < 4){ Include "You Loose.php"; } else { Include "You Win.php"; } else { if ($Number < 2){ Include "You Loose.php"; } else { Include "You Win.php"; } } ?> Edit: Sorry, was afk, only saw the otehrs now. Quote Link to comment https://forums.phpfreaks.com/topic/65061-case-statement-with-rand-feature/#findComment-324731 Share on other sites More sharing options...
SirChick Posted August 15, 2007 Author Share Posted August 15, 2007 <?php //If $exp<100 sets $chance to 2, else it is set to 4 //Chance represents the chance in 5 that the user will win $chance = ($exp<100)?2:4; //Generates a random number (1-5) and if it is equal to or less than //chance, sets the include file to the Win file, else sets it to the Lose file $inclFile = (rand(1,5)<=$chance)?'You_Win.php':'You_Lose.php'; //Includes the file include($inclFile); ?> Ok but say 2 out of 5 chances. That still means every 5 times u will win 2 correct? How would i alter the chances for different amounts like if it was out 20 attempts? How come you all have different methods surely only one method should physically work :S Quote Link to comment https://forums.phpfreaks.com/topic/65061-case-statement-with-rand-feature/#findComment-324733 Share on other sites More sharing options...
Psycho Posted August 15, 2007 Share Posted August 15, 2007 Ok, I took Barands post as a challenge! here's a single line solution. I win! <?php $include 'You_' . (($exp <= 100) ? ((rand(1,5) <= 2) ? 'Win':'Lose') : ((rand(1,5) <= 4) ? 'Win':'Lose' )) . '.php'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/65061-case-statement-with-rand-feature/#findComment-324736 Share on other sites More sharing options...
Barand Posted August 15, 2007 Share Posted August 15, 2007 <?php include 'You_' . (($exp <= 100) ? ((rand(1,5) <= 2) ? 'Win':'Lose') : ((rand(1,5) <= 4) ? 'Win':'Lose' )) . '.php'; ?> correct version - i win Quote Link to comment https://forums.phpfreaks.com/topic/65061-case-statement-with-rand-feature/#findComment-324740 Share on other sites More sharing options...
Psycho Posted August 15, 2007 Share Posted August 15, 2007 <?php //If $exp<100 sets $chance to 2, else it is set to 4 //Chance represents the chance in 5 that the user will win $chance = ($exp<100)?2:4; //Generates a random number (1-5) and if it is equal to or less than //chance, sets the include file to the Win file, else sets it to the Lose file $inclFile = (rand(1,5)<=$chance)?'You_Win.php':'You_Lose.php'; //Includes the file include($inclFile); ?> Ok but say 2 out of 5 chances. That still means every 5 times u will win 2 correct? How would i alter the chances for different amounts like if it was out 20 attempts? How come you all have different methods surely only one method should physically work :S Simply use variables in the code in place of the chance values and the random number max limit. I'd just create a function if it will be used many times with different values: <?php function winlose($expLimit, $lowchance, $highchance, $maxchance) { $chance = ($exp<$expLimit)?$lowchance:$highchance; $inclFile = (rand(1,$maxchance)<=$chance)?'You_Win.php':'You_Lose.php'; return $inclFile; } $includeFile = winlose(100,2,4,5); include($includeFile); ?> And, there are many, many ways to accomplish the same thing in programming. The best solution will depend on a particular situation. For example, if you only needed this functionality for the values you originally specified I would not use a function. but, if you will be using different values, then a function is definitely the way to go. Quote Link to comment https://forums.phpfreaks.com/topic/65061-case-statement-with-rand-feature/#findComment-324743 Share on other sites More sharing options...
Psycho Posted August 15, 2007 Share Posted August 15, 2007 <?php include 'You_' . (($exp <= 100) ? ((rand(1,5) <= 2) ? 'Win':'Lose') : ((rand(1,5) <= 4) ? 'Win':'Lose' )) . '.php'; ?> correct version - i win Doh! I was setting the result to a string so I could echo out the value for verification. Forgot to remove the damn dollar sign! *hangs head in shame* Quote Link to comment https://forums.phpfreaks.com/topic/65061-case-statement-with-rand-feature/#findComment-324744 Share on other sites More sharing options...
SirChick Posted August 15, 2007 Author Share Posted August 15, 2007 <?php //If $exp<100 sets $chance to 2, else it is set to 4 //Chance represents the chance in 5 that the user will win $chance = ($exp<100)?2:4; //Generates a random number (1-5) and if it is equal to or less than //chance, sets the include file to the Win file, else sets it to the Lose file $inclFile = (rand(1,5)<=$chance)?'You_Win.php':'You_Lose.php'; //Includes the file include($inclFile); ?> Ok but say 2 out of 5 chances. That still means every 5 times u will win 2 correct? How would i alter the chances for different amounts like if it was out 20 attempts? How come you all have different methods surely only one method should physically work :S Simply use variables in the code in place of the chance values and the random number max limit. I'd just create a function if it will be used many times with different values: <?php function winlose($expLimit, $lowchance, $highchance, $maxchance) { $chance = ($exp<$expLimit)?$lowchance:$highchance; $inclFile = (rand(1,$maxchance)<=$chance)?'You_Win.php':'You_Lose.php'; return $inclFile; } $includeFile = winlose(100,2,4,5); include($includeFile); ?> And, there are many, many ways to accomplish the same thing in programming. The best solution will depend on a particular situation. For example, if you only needed this functionality for the values you originally specified I would not use a function. but, if you will be using different values, then a function is definitely the way to go. Could explain this bit: $expLimit, $lowchance, $highchance, $maxchance what is the "explimit" do i have to set that :S cos i dont understand that.. nor the high n low chance or max chance.. i cant work out its logic... Quote Link to comment https://forums.phpfreaks.com/topic/65061-case-statement-with-rand-feature/#findComment-324831 Share on other sites More sharing options...
Psycho Posted August 15, 2007 Share Posted August 15, 2007 Hmmm, if you were to look at the original commented code I would think it is self explanatory. Actually, the function needs to take one additional parameter: $exp <?php function winlose($expLimit, $lowchance, $highchance, $maxchance, $exp) { $chance = ($exp<$expLimit)?$lowchance:$highchance; $inclFile = (rand(1,$maxchance)<=$chance)?'You_Win.php':'You_Lose.php'; return $inclFile; } //some code to computer $exp would be needed $includeFile = winlose(100,2,4,5, $exp); include($includeFile); ?> The function will return back the file name to be included and takes the following paramters: $expLimit: the "break point" for experience points which would determine which "chance" value would be used $lowchance: the chance value to use if the exp points are below the $expLimit $highchance: the chance value to use if the exp points are equal or greater than $expLimit $maxchance: the maximum number of $exp: the eperience points of the user So the way to read this $includeFile = winlose(100,2,4,5, $exp); is as follows: If the $exp value is less than 100, then there is a 2 in 5 chance that 'You_Win.php' will be returned, else 'You_Lose.php' is returned. If the $exp value is greater than or equal to 100, then there is a 4 in 5 chance that 'You_Win.php' will be returned, else 'You_Lose.php' is returned. Another way to think of it is a die. The $maxchance value detemines the number of side on the die (in this case a five sided die). The $expLimit along with the low and high chances determine what values on the die will produce a win vs. a loss. For another example: let's suppose you want users with more than 500 points to have a 7 in 10 chance of a win and users with less than 500 points to have a 3 in 10 chance of a win. Then you would call the function like this: $includeFile = winlose(500,3,7,10, $exp); Quote Link to comment https://forums.phpfreaks.com/topic/65061-case-statement-with-rand-feature/#findComment-324930 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.