Jump to content

Case statement with rand feature


SirChick

Recommended Posts

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

Link to comment
Share on other sites

... 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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

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);

?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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);

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

<?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*

Link to comment
Share on other sites

<?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...

Link to comment
Share on other sites

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);

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.