PHP - Dice Roller Help
Hey guys. I'm new around here, and I come to you to get some help on a coding. I have made a program that selects two variable via the POST function and generates a set amount of random numbers between 1 and $B. That thing works like a charm and it places them all neatly in a table, but now I want to get on with the program and make it count the amount of all the numbers specified in variable $C.
EXAMPLE: Generated 9 random numbers with a value between 1 and 10, echoing out the following: 9 8 2 6 4 7 3 10 5 Now I want it to count the numbers 8 and 9, telling me how many there are. If I can get this to work, I then want it to count the amount of 10's, add them to the amount of 8's and 9's, plus reroll them, add the following 8's and 9's, then repeat the process of counting and rerolling the 10's, then adding the 8's and 9's. Any of you have an idea of how to make this work? Similar TutorialsSo i am a noob with php, I am looking to design a ten sided dice roller that shows the results to everyone along with who rolled it. I need some help with a dice rolling project. Here is what code I've got so far I just need some help with the for and while loops. I have to simulate the the dice being rolled (between 1-6) 5000 times and track the results in a table. If anyone could help me, I'd be VERY APPRECIATIVE. <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>"Statistical analysis of rolling a six-sided die"</title> </head> <body> <h2>Statistical analysis of rolling a six-sided die</h2> <br/> <table width='600' border='1' cellspacing='0' cellpadding='5' align='center'> <?php $side1 = 1; $side2 = 2; $side3 = 3; $side4 = 4; $side5 = 5; $side6 = 6; for ($i =1; $i <= 5000; i++) { $roll = mt_rand(1, 6); } ?> </table> </body> </html> I am not a very good PHP programmer, and I got a project to "work" on. I need help writing a Dice Roll Function. This is how I'm supposed to do the two functions: New Function: dice_roll (int sides) - $ returns a random number of how many sided the dice is, between 1 and the $. New Function: dice_roll_str (string dicestr) - A string describing the dice [0-9] + d [0-9 ]+[+-][ 0-9] + size (d <number_of_dices> <number_of_sides> [+-][modifying_value]) - For example if I call the function with "3d6-3", this means: we throw 3 times with a 6-sided dice, add it up, then subtract 3-t. - The function is to return a false value on error - Possible errors: - Minimum number of of sides of the dice is 2 the maximum is 24 - A minimum number of dices 1 the maximum is 8 - The result should always be a positive number, which is not greater than twice of the result we got. I've already done the first function. This is my code: Code: [Select] function dice_roll($sides_num) { $rand_num = rand(1, $sides_num); return $rand_num; } I'm not sure it correct. It is the second function that I don't know how to do. Please, if you can help I'd be really grateful. If you can please put explanations on how the code works. Thank you so much, Evermore Im new to php and i cant get my program to display correctly. I want it to take a user's guess of a single Die roll and display whether the user guessed correctly and display a picture of the corresponding Die. i think there is something wrong with my "If" statement or logic. Please Help. Here's the Code: INPUT FORM: <html> <body> <form name = "Dice Game" action="diceroll.php" method="post"> Your Guess: <br> <input type= "text" name="" size= "1"> <input type ="submit" name = "submit"> </form> </body> </html> OUTPUT FORM: <html> <body> <?php $roll = $_POST['submit']; $dice = rand(1, 6); if ($roll == 1 && $dice == 1){ print "Great Job, You're Good!"; }else if ($roll == 1 && $dice /= 1) { print "Wrong"; } else if ($roll == 2 && $dice == 2) { print "Great Job, You're Good!";} else if ($roll == 2 && $dice /= 2) { print "Wrong"; } else if ($roll == 3 && $dice == 3) { print "Great Job, You're Good!"; } else if ($roll == 3 && $dice /= 3) { print "Wrong"; } else if ($roll == 4 && $dice == 4) { print "Great Job, You're Good!"; } else if ($roll == 4 && $dice /= 4) { print "Wrong"; } else if ($roll == 5 && $dice == 5) { print "Great Job, You're Good!"; } else if ($roll == 5 && $dice /= 5) { print "Wrong"; } else if ($roll == 6 && $dice == 6) { print "Great Job, You're Good!"; } else if ($roll == 6 && $dice /= 6) { print "Wrong"; } else { print " "; } if($dice == 1) print "<img src='dice1.png'>"."<br>"; if($dice == 2) print "<img src='dice2.png'>"."<br>"; if($dice == 3) print "<img src='dice3.png'>"."<br>"; if($dice == 4) print "<img src='dice4.png'>"."<br>"; if($dice == 5) print "<img src='dice5.png'>"."<br>"; if($dice == 6) print "<img src='dice6.png'>"."<br>"; else { print "Thanks for playing". "<br>"; } ?> <form name= "back" action= "dice.php" method= "post"> <input type="submit" name="back" value ="Play Again"> </form> </body> </html> Hello, I have a simple dice system script I am using in a chat program for a website and I was wondering if someone could simply tell me how to limit the number of dice being rolled. Here are the two chunks of script that make it work: if($irc_cmd == '/d') { if($irc_cmd == $txt) { // this can only happen with a no dice - '/d' - a help request // return an explanation of the correct format $text = "dice command is /d [[n]D]s[Xm|+a|-d|/q]*"; $this->sendToUser(null, new Message($type, $this->userid, null, $text, $this->color)); return 'ok'; } // create standard failure message $invalid_msg = new Message($type, $this->userid, null, 'ERROR: invalid dice command - '.$txt.' - enter /d for help', $this->color); // remove the original command from the dice string $dicestring = str_replace($irc_cmd,'', $txt); // use lowercase versions of D and X to make parsing simpler $dicestring = strtolower($dicestring); // remove any user entered spaces $dicestring = str_replace(' ', '', $dicestring); // note that all modifiers will follow the dice spec 'nDs' // number of dice is optional e.g. 1d4 d4 and 4 all represent a single 4-sided die // if the first token is blank, then the user intended for one die e.g. /d d20 means /d 1d20 // if the is no 'd' then the user intended one die e.g. /d 20 means /d 1d20 $parts = explode('d', $dicestring); if(count($parts)>2) { // only one 'd' is allowed $this->sendToUser(null, $invalid_msg); return 'ok'; } elseif(count($parts)==1) { // if no 'D' assume '1' die $number = 1; } else { $number = (int)$parts[0]; if ($parts[0] == "") { // if $number == 0 // if no number assume '1' die $number=1; } elseif ("$number" != "$parts[0]") { // only integers allowed $this->sendToUser(null, $invalid_msg); return 'ok'; } $dicestring = $parts[1]; } if($number < 1) { // can't allow a negative number of dice $this->sendToUser(null, $invalid_msg); return 'ok'; } // check for sides and modifiers // expand the string to put spaces around all the tokens we want $dicestring = str_replace('+', ' + ', $dicestring); $dicestring = str_replace('-', ' - ', $dicestring); $dicestring = str_replace('x', ' x ', $dicestring); $dicestring = str_replace('/', ' / ', $dicestring); // explode the whole thing to create tokens $parts = explode(' ', $dicestring); // the only other tokens should be integers // allowed formats from here are s[Xm][+a][-d][/q] // we should allow any series of these in any order, applying them left to right // the first part must be the sides $sides = (int)$parts[0]; if ("$sides" != "$parts[0]") { $this->sendToUser(null, $invalid_msg); return 'ok'; } if($sides < 1) { // can't allow a negative number of sides $this->sendToUser(null, $invalid_msg); return 'ok'; } // get the user's name //$user = ChatServer::getUser($this->userid); //$name= $user['login']; // start writing the reply string $text = '*rolls* '.$number.'d'.$sides.': '; // seed the randomizer srand(time()); // with number and sides, roll the dice, adding them up $total = 0; for($i = 0; $i < $number; $i++) { $roll = (rand()%$sides)+1; if($i != 0) $text .= '+'; $text .= $roll; $total += $roll; } // now apply all the modifiers to the roll, in the order the user specified them for ($i = 1; $i < count($parts); $i+=2) { // the value needs to be an integer $value = (int)$parts[$i+1]; $v = $parts[$i+1]; if ("$value" != "$v") { $this->sendToUser(null, $invalid_msg); return 'ok'; } // the token needs to be one of the operators - otherwise abort $token = $parts[$i]; switch ($token) { case '+': // add $total += $value; break; case '-': // subtract $total -= $value; // make minimum 1 - remove this like to allow 0 and lower if ($total<1) $total=1; break; case 'x': // multiply $total *= $value; break; case '/': // divide - round up so 1d6/3 will be the same as 1d2 $total = ceil($total/$value); break; default: $this->sendToUser(null, $invalid_msg); return 'ok'; } // add the modifier to the display string $text .= $token.$value; } // and display the final result $text .= ': '.$total; // gets sent to particular room, but with users name tacked on, so a user could spoof it // at least 'msgu' looks different $this->sendToRoom(null, new Message('rpg', $this->userid, $this->roomid, $text)); return 'ok'; } and function Message($command, $userid = null, $roomid = null, $txt = null, $color = null) { $this->command = $command; if ($command == 'rpg') $this->command = 'msgu'; $this->userid = $userid; $this->roomid = $roomid; $this->color = htmlColor($color); if($command != 'rpg') { $txt = str_replace('*rolls*', 'rolls', $txt); } if(isset($txt)) { $this->txt = $this->parse($txt); } } So again all I want it to do is limit the number of dice being rolled because right now someone can do a /d 1000000d100000 and completely crash the chat for everyone. I am thinking 100 on either variable would be plenty. Thanks for your help! |