Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Very Basic PHP help required
Index -> Programming, PHP -> PHP Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
websitedsc




PostPosted: Thu Mar 04, 2010 10:05 pm   Post subject: Very Basic PHP help required

Allright so I have this PHP homework, basically I have to allow a user to enter a number into a text box between 10 and 60. PHP is then required to "simulate a dice rolling" 10 times using a for loop. The 10 "rolls" are then added together and compared to the users entry in the text box. It's a basic game, if the user guesses correctly they win etc. etc. Anyway, the code should explain things better, the only problem i am having is that the maximum of 10 rolls of the dice should be 60, but occasionally the result is more than this, more than often the number 66. Have no idea why its doing this. I've tried my best to explain, please ask questions if you don't understand what the hell im rambling on about. Cheers. Ignore the lack of standards in my markup.

HTML
<html>
<head><title>question two html</title></head>
<body>
<form action="Q2.php" method="post">
<br>
<h1>Enter a number between 10 and 60 in the box below. This is your guess. The object of the game is to try and get as close to the number the program calculates. .</h1>
<br>
<input type="text" maxlength="2" name="usersguess">
<br>
<p>
<input type="submit" value="Submit">
</p>
<p>
<input type="reset" value="Reset Form">
</p>
</form>
</body>
</html>

PHP
<html>
<head><title>question two php</title></head>
<body>
<?php
$guess=$_POST["usersguess"];
$dice=rand(1,6);
$sum;
for ($i = 0; $i <= 10; $i++) {
$dice;
$sum= $sum + $dice;
}
$diff= abs($guess-$sum);
if($guess<=9 || $guess>=61){
print "Invalid entry. Please enter a number between 10 and 60.";
}
elseif($guess==$sum){
print "WINNER!.You guessed $guess and the dice rolled $sum.";
}
elseif($diff<=5){
print "You were within 5. Unlucky !.You guessed $guess and the dice rolled $sum.";
}
else{
print "Unlucky this time.You guessed $guess and the dice rolled $sum.";
}
?>
</body>
</html>
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Thu Mar 04, 2010 10:18 pm   Post subject: RE:Very Basic PHP help required

for ($i = 0; $i <= 10; $i++) {

The range of this is 0, 1, 2, ..., 10. How many numbers is that?
octopi




PostPosted: Thu Mar 04, 2010 10:51 pm   Post subject: Re: Very Basic PHP help required

Also, your only rolling the dice once.
JSBN




PostPosted: Sat Apr 03, 2010 10:15 am   Post subject: RE:Very Basic PHP help required

Yea... you are rolling 11 times you want < 10 not <= 10. Also, as octopi said above, you want to move your rand function into your for loop. Otherwise you are just adding 1 number 10 times.
Display posts from previous:   
   Index -> Programming, PHP -> PHP Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: