Question
1) DiceRoll.php Program: You will need to complete, save and submit all versions of the program in php created by following the instructions on pages
1) DiceRoll.php Program: You will need to complete, save and submit all versions of the program in php created by following the instructions on pages 85, 89,90,94,99,102,105,107 of your Textbook. You need to save all the 8 versions of the program as separate files and name them as different versions. For each version of the file you will execute it in the browser and take a screenshot of the output. You can submit each screen shot individually or you can paste them all in one word document and label them appropriately.
Could you please help me with these php codes, thank you! It is so confusing to me.
In the next steps, you will create a script to roll a pair of dice and
evaluate the outcome. For this exercise, you will use the function
rand(1,6), which generates a random integer from 1 to 6.
To create the dice script:
1. Create a new document in your text editor. Type the
declaration, element, header information,
and
element. Use the strict DTD and Dice Roll asthe content of the
2. Add the following script section to the document body:
?>
3. Add the following code to the beginning of the script
section. Th is will create the $FaceNamesSingular and
$FaceNamesPlural arrays and populate them with text.
$FaceNamesSingular = array("one", "two", "three",
"four", "fi ve", "six");
$FaceNamesPlural = array("ones", "twos", "threes",
"fours", "fi ves", "sixes");
4. Now create the CheckForDoubles function. It takes two
parameters, $Die1 and $Die2, and uses echo statements and
the global $FaceNamesSingular and $FaceNamesPlural
arrays to display one of two diff erent messages, depending on
whether $Die1 equals $Die2 (doubles were rolled).
To simplify the DiceRoll.php script by replacing two if statements
with one if . . . else statement:
1. Return to the DiceRoll.php document in your text editor.
2. Because you only need the if statement to test for doubles,
you can display the message for rolls that are not doubles
in the else clause. Modify the CheckForDoubles() function
so that the two if statements are replaced with a single
if . . . else statement. Th e following code shows how the
statements for the CheckForDoubles() function should look:
if ($Die1 == $Die2) // Doubles
echo "The roll was double ",
$FaceNamesPlural[$Die1-1], ".
";
else // Not Doubles
echo "The roll was a ",
$FaceNamesSingular[$Die1-1],
" and a ", $FaceNamesSingular[$Die2-1],
".
";
3. Save and upload the DiceRoll.php document.
4. Open the DiceRoll.php fi le in your Web browser by entering
the following URL: http://
Chapter.02/Chapter/DiceRoll.php. You should still see a
Web page similar to the one shown in Figure 2-4. Use the
refresh button to verify that both doubles and nondoubles are
displayed correctly.
5. Close your Web browser window.
To modify the DiceRoll.php program so it uses nested if . . . else
statements to display the score text:
1. Return to the DiceRoll.php document in your text editor.
2. Modify the CheckForDoubles() function to return a Boolean
value indicating whether doubles were rolled by adding the
text shown in bold.
function CheckForDoubles($Die1, $Die2) {
global $FaceNamesSingular;
global $FaceNamesPlural;
$ReturnValue = false;
if ($Die1 == $Die2) { // Doubles
echo "The roll was double ",
$FaceNamesPlural[$Die1-1], ".
";
$ReturnValue = true;
}
else { // Not Doubles
echo "The roll was a ",
$FaceNamesSingular[$Die1-1],
" and a ",
$FaceNamesSingular[$Die2-1], ".
";
$ReturnValue = false;
}
return $ReturnValue;
}
To modify the DiceRoll.php script to use a switch statement for the
score text:
1. Return to the DiceRoll.php document in your text editor.
2. Replace the nested if . . . else statements with the following
switch statement in the DisplayScoreText() function. Note
the use of the nested if . . . else statement in the default
case that allows the DisplayScoreText() function to display
a message for all of the possible rolls:
switch ($Score) {
case 2:
echo "You rolled snake eyes!
";
break;
case 3:
echo "You rolled a loose deuce!
";
break;
case 5:
echo "You rolled a fever fi ve!
";
break;
case 7:
echo "You rolled a natural!
";
break;
case 9:
echo "You rolled a nina!
";
break;
To modify the DiceRoll.php script to evaluate fi ve rolls using a while
statement:
1. Return to the DiceRoll.php document in your text editor.
2. Immediately after the declaration of the $Dice array, declare
and initialize two new variables: $DoublesCount and
$RollNumber.
$DoublesCount = 0;
$RollNumber = 1;
3. After the new variable declarations, create a while loop by
adding the code shown in bold. Also, revise the echo statement
by making the change shown in bold.
while ($RollNumber <= 5) {
$Dice[0] = rand(1,6);
$Dice[1] = rand(1,6);
$Score = $Dice[0] + $Dice[1];
echo "
";
echo "The total score for roll $RollNumber was
$Score.
";
$Doubles = CheckForDoubles($Dice[0],$Dice[1]);
DisplayScoreText($Score, $Doubles);
echo "
";if ($Doubles)
++$DoublesCount;
++$RollNumber;
} // End of the while loop
To use a do . . . while statement:
1. Return to the DiceRoll.php document in your text editor.
2. Change the while statement to a do . . . while statement, as
follows:
do {
$Dice[0] = rand(1,6);
$Dice[1] = rand(1,6);
$Score = $Dice[0] + $Dice[1];
echo "
";
echo "The total score for roll $RollNumber was
$Score.
";
$Doubles = CheckForDoubles($Dice[0],$Dice[1]);
DisplayScoreText($Score, $Doubles);
echo "
";if ($Doubles)
++$DoublesCount;
++$RollNumber;
} while ($RollNumber <= 5); /* End of the do . . .
while loop */
To replace the do . . . while statement in DiceRoll.php with a for
statement:
1. Return to the DiceRoll.php document in your text editor.
2. Change the do . . . while statement to a for statement, as
follows:
for ($RollNumber = 1; $RollNumber <= 5;
++$RollNumber) {
$Dice[0] = rand(1,6);
$Dice[1] = rand(1,6);
$Score = $Dice[0] + $Dice[1];
echo "
";
echo "The total score for roll $RollNumber was
$Score.
";
$Doubles = CheckForDoubles($Dice[0],$Dice[1]);
DisplayScoreText($Score, $Doubles);
echo "
";if ($Doubles)
++$DoublesCount;
} // End of the for loop
3. Save and upload the DiceRoll.php document.
4. Open the DiceRoll.php fi le in your Web browser by entering
the following URL: http://
Chapter.02/Chapter/DiceRoll.php. Th e output should still
appear as shown in Figure 2-8.
5. Close your Web browser window.
To create a fi nal version of DiceRoll.php that displays all possible outcomes
of rolling two dice:
1. Return to the DiceRoll.php document in your text editor.
2. Immediately after the declaration of the $FaceNamesSingular
and $FaceNamesPlural arrays, declare a new array named
$FaceValues, as follows:
$FaceValues = array( 1, 2, 3, 4, 5, 6);
3. Delete the declaration of the $Dice array and add a new declaration
for a variable named $RollCount, as follows:
$RollCount = 0;
4. Create a new array called $ScoreCount and initialize it using
the following for loop:
$ScoreCount = array();
for ($PossibleRolls = 2; $PossibleRolls <= 12;
++$PossibleRolls) {
$ScoreCount[$PossibleRolls] = 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started