Role a 6 to win! PHP Code

I'm currently learning how to use do/while loops in PHP through codeacademy. I'm working on a challenge where you create a six-sided die and continue rolling until you get a six. Here's what I've come up with so far:

<?php
$roll = 1;

do {
echo “Roll Number " . $roll . ".  Begin rolling for a 6.";
} while ($roll != 6); {
for ($rollNumber = 1; $roll != 6; $roll ++) {
$roll = rand(1, 6);
echo "On roll number " . $rollNumber .  "You got  a " $roll ".\n";
};}
?>

I though the best way to get started is by creating a roll variable that we can use once for the do loop to check if it's working:

$roll = 1;

I set the value to one instead of zero, because there's no zero number on a die, and we'll assume the player first rolls a one. Next I want to check if the do loop is working so I echo the following:

do {
echo “Roll Number " . $roll . ".  Begin rolling for a 6.";
}

After making sure the do loop worked, I create the while condition:

while ($roll != 6) {

Then I want to create a rollNumber variable to keep track of what roll I'm on and increment it:

for ($rollNumber = 1; $roll != 6; $roll ++) {

next I set the value of the roll variable so that it generates a random number between 1 and 6:

$roll = rand(1,6);

Finally I want to echo the message "On roll number you got a ." :

echo “On roll number “ . $rollNumber .  "You got  a " $roll “.\n";}

For some reason the code's not working. Any suggestions or help would be much appreciated! Thanks.

Answers


You were on the right track with a while loop and rand(). The only other thing you needed was to set a count variable to display the roll number (you can just increment this variable on every "roll").

Your code could be simplified to this:

$c = $n = 1;
while ($n != 6) {
  echo 'on roll number ' . $c++ . ' you rolled a ' . $n = rand(1, 6) . "\n";
}
Here's an example

You've made this way too complicated. There's no need for two loops.

$rolled = 0;
$numbers = range(1, 6); //generate array with 1-6
$counter = 0;
while($rolled!=6)
{
    $counter++;
    shuffle($numbers); //shuffle array
    $rolled = $numbers[0]; //grab first item
    echo '<br/>(Roll ' . $counter . ') ' . $rolled;
}

Why not have a function that returns the result of a roll, and then a loop that calls this function until it returns a 6?

<?php

function roll() {
    return mt_rand(1, 6);
}

$roll = 1;

do {
    if (roll() == 6) {
        printf('You rolled a 6 on roll %d.', $roll);
        break;
    }
    $roll++;
} while (0);

This do/while loop continues until the roll() function returns a 6.


Need Your Help

How to add a new user in drupal 7 programmatically ?My custom module defines a form with custom fields

drupal-7 drupal-modules drupal-forms

Hi being a newbie to drupal , kindly guide how to add a new user , i know user module with user_save can be used but how and where to implement it ?

What is the difference between :after and ::after?

html css html5 css3

It says the difference is that :after is CSS2, while ::after is CSS3. Are there any more important differences?