Announcements | Last Post | |
---|---|---|
SoC Curricula | 09/30/2017 01:08PM | |
Demarcation or scoping of examinations and assessment | 02/13/2017 07:59AM | |
School of Computing Short Learning Programmes | 11/24/2014 08:37AM | |
Unisa contact information | 07/28/2011 01:28PM |
Numbers Game!!! July 24, 2013 10:01AM |
Registered: 11 years ago Posts: 73 Rating: 0 |
This part is optional guys you can even comment it out if you like, but if you need the image I can email you the favicon..Language: HTML<LINK REL="icon" TYPE="images/ico" HREF="/images/diceGame.ico">
Language: PHP<!DOCTYPE> <HTML> <HEAD> <TITLE>numbers_Game</TITLE> <META NAME="Description" CONTENT="Numbers Game"> <META NAME="Keywords" CONTENT="Dice, Cards, Casino"> <META NAME="Author" CONTENT="Sipho Mkhwanazi"> <link rel="icon" TYPE="images/ico" HREF="images/diceGame.ico"> <?PHP #lets write a dice game ##these values here need to be initiated, the default score is ';0';(no roll) $yourName = ""; $myNumber = ""; $score = 0; if(isset($_POST[';enter';])){ #lets check if the enter button was clicked, if so then lets trim the value from $POST and innitiate the variable $yourName $yourName=trim($_POST[';username';]); if($yourName){ $yourName=$yourName; echo "<h1><span style=\"color: white;\">Welcome to the GAME: <span style=\"color:yellow;\">".$yourName."</span></span></h1>"; }else{ echo "<h2><span style=\"color:red;\">Please enter a valid name!!</h2>"; $youName = ""; #If the above code evaluates to false then $yourName is empty, therefore no game will be played } } if(isset($_POST[';clear';])){ $yourName = " "; } ?> </HEAD> <BODY bgcolor="blue"> <?PHP #The following function is the actual engine that will give us a score by multiplying 2 random numbers function playTime($randomNumber, $anyNumber){ return $randomNumber*$anyNumber; } ?> <?PHP $luckyNumbers = array(1,2,3,4,5,6); #this array represents all the 6 sides of a dice and every side has a value i.e side 1 = 1, 2 = 2 e.t.c if($yourName){ ?> <form Name="MyForm>" Method="POST" Action="numbersGame.php"> <p><span style="color:white;">Press Play to roll the dice!! lets see how lucky you are:</span></p> <input type="submit" Value="play" Name="play"> </form> <?PHP } ##Completes our if statement.. if(isset($_POST[';play';])){ #lets check if the play button was pressed $roll = rand(1,6); #If button was pressed then innitiate $roll with random numbers between 1 and 6 representing the 6 sides of a dice $random = array_rand($luckyNumbers,1); #lets pick a random number from the array $luckyNumbers, again any number here represents one of the 6 sides of a dice $score = playTime($random, $roll); #score gets a value from our engine ->the function playTime() if($score > 15){ echo "<h1><span style=\"color:yellow;\">WOW you';ve wone!!! You rolled: ".$roll." and got a score of : ".$score."</span></h1>"; }else{ echo "<h2><em><span style=\"color:red;\">SORRY Please try again!!</span><span style=\"color:white;\"> you rolled: ".$roll." and your overall score was: ".$score."</span></em></h2><h2><em> <span style=\"color:white;\">To win this game you need: ".$lost=15-$score." more points"."</span></em></h2>"; $yourName = "TryAgain"; ##In order for the player to be able to retry/play again by just hitting enter innitiating $youName with "TryAgain"/any value that will evaluate to true is neccessary } } ?> <h1><span style="color:white";>Lets play some dice</span></h1> <p><em><span style="color:white;">To win this game you must score a number greater than 15, Goodluck!!!</span></em></p> <p><em><span style="color:white;">Please NOTE!!! to play your name is required each time...</span></em></p> <form Method="POST" Name="myForm2" Action="numbersGame.php"> <p><span style="color:white;">Whats your name stranger:</span></p><input type="text" Value="<?php echo $yourName;?>" Name="username"><br> <input type="submit" Value="Enter" Name="enter"> <input type="reset" Value="Clear" Name="clear"> </form> </BODY> </HTML>
Re: Numbers Game!!! July 25, 2013 03:48PM |
Registered: 10 years ago Posts: 38 Rating: 0 |
Re: Numbers Game!!! July 26, 2013 10:08AM |
Registered: 10 years ago Posts: 8 Rating: 1 |
Re: Numbers Game!!! July 26, 2013 11:10AM |
Registered: 11 years ago Posts: 73 Rating: 0 |
Re: Numbers Game!!! July 30, 2013 08:03AM |
Registered: 10 years ago Posts: 17 Rating: 0 |
Re: Numbers Game!!! July 30, 2013 09:43AM |
Registered: 11 years ago Posts: 73 Rating: 0 |
Am sorry for the late reply. I was waist deep attending a server SOS, troubleshooting for a better part of yestarday and was socialy absent as a result..Quote
Cool stuff! there is a lot to learn from your code, I noticed that once you have opened your php tags and then you want to insert a form inside the php tags it won't gonna work
Language: PHPif($yourName){ echo "<form NAME=\"form1\" Method=\"POST\".......>"; #Here I commented out the double quotes inside my HTML so the PHP parser sees this -> "<form NAME.......>" echo "<input TYPE=\"submit\" ......></form>"; #Same here.. }
Language: PHPif($yourName){ echo <<<myHTML #echo/print will print all of our HTML code contained inside our <<<EOF EOF; magic tags.. <form NAME="form1" Method="POST".......> #pure HTML code here <input TYPE="submit" ......></form> #Same here.. myHTML; } #Lets close the condition here.. #OR another way would be to do this: $form1 = <<<myHTML #initiate a variable with a form <form NAME="form1" Method="POST".......> <input TYPE="submit" ......></form> myHTML; if($yourName){ echo $form1; }
Re: Re: Numbers Game!!! July 30, 2013 12:02PM |
Registered: 10 years ago Posts: 8 Rating: 1 |
Language: PHPif(isset($_POST[';enter';])){ #lets check if the enter button was clicked, if so then lets trim the value from $POST and innitiate the variable $yourName $yourName = trim($_POST[';username';]); if($yourName){ $yourName = $yourName; // think we can get rid of this as we already defined the var with the $_POST[';username';] echo "<h1><span style=\"color: white;\">Welcome to the GAME: <span style=\"color:yellow;\">".$yourName."</span></span></h1>"; }else{ echo "<h2><span style=\"color:red;\">Please enter a valid name!!</span></h2>"; $youName = ""; #If the above code evaluates to false then $yourName is empty, therefore no game will be played }
Language: PHP<?PHP // keep tags lowercase - good practice
Re: Re: Numbers Game!!! August 05, 2013 09:44AM |
Registered: 10 years ago Posts: 8 Rating: 1 |