Welcome! Log In Create A New Profile

Advanced

smileys with beer Numbers Game!!!

Posted by 72903694_Sipho 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
avatar smileys with beer Numbers Game!!!
July 24, 2013 10:01AM


Good morning my fellow scholars, last night after work I was thinking of some interesting ways of practicing what we've learned so far and walaah!!! it hit me.. eye popping smiley

Why not create a numbers game where you only need a name to play, to play one simply enters their name, they get welcomed into the game and roll the dice by pushing the play button. When the button is pushed it spins some numbers between 1 and 6 (2 virtual dice). basically there are 2 dice, each time you roll them random numbers are picked from each dice and multiplied by each other, if this results in a score greater than 15 (Because I like number 15) you win the game.. grinning smiley

Anyways here's what I coded, I invited everybody to join in and improve the game, its unfortunate that I don't play blackjack, if new the rules imagine what this game would be like? Anyways guys, on the following code I make use of everything we've learned to date (strings, variables, string manipulation, arrays, function, forms e.t.c) eye rolling smiley Someone perhaps can help with the fronted or if someone knows blackjack, they could give us a rundown of how the game works.eye rolling smiley I thought of using the include() function to split the login page from the game but never got around to doing it..eye rolling smiley OH!! tja one more thing i created a favicon for the site so you will need a
Language: HTML
<LINK REL="icon" TYPE="images/ico" HREF="/images/diceGame.ico">
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..spinning smiley sticking its tongue out

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>

LETS GET CRACKING GUYS...cool smiley
smileys with beer Re: Numbers Game!!!
July 25, 2013 03:48PM
Nice.
I'm quite happy since I do understand some of what's going on here.smile
I haven't yet tried to create my own php app.Working through this code,
lets me se how to practically implement the concepts we learned
Thanks.
smileys with beer Re: Numbers Game!!!
July 26, 2013 10:08AM
Cool idea..will be going through the code today will post an updated version.
avatar smileys with beer Re: Numbers Game!!!
July 26, 2013 11:10AM
Ok that's awesome..thumbs up
smileys with beer Re: Numbers Game!!!
July 30, 2013 08:03AM
Hi Sipho,

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

<?php

$luckyNumbers = array(1,2,3,4,5,6);

if($yourName){

<form name .......>
<input type ......></form>
}

?>

you will need to close the php tag at the beginning of the form
and re-open the php tag at the end of the form.
therefore, the code above will become

<?php

$luckyNumbers = array(1,2,3,4,5,6);

if($yourName){ ?>

<form name .......>
<input type ......></form>
<?php }

?>

Is that correct ?
avatar smileys with beer Re: Numbers Game!!!
July 30, 2013 09:43AM
@78147646 Papias

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
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..sad smiley
Interesting observation and you are infect right. However there are other ways I could've done it i.e observe the following code:

METHOD 1 -> Commenting out HTML tags with the "\" so they dont get confused with PHP code:
Language: PHP
if($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.. }

Ok method 1 requires a lot of typing and attention to detail as your saw, now lets look at the following METHOD 2:

Language: PHP
if($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; }

The aim here is to make the php parser ignore the HTML code, when the browser gets it, it presents it as HTML. So tja, the story goes... If this () condition is met, then proceed wth what ever is inside the {} parenthesis ..thumbs up I hope this helps you..
smileys with beer Re: Re: Numbers Game!!!
July 30, 2013 12:02PM
Hi all
Added a few comments to the logic of the code hope this willhelp others understand php formatting

Language: PHP
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; // 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
smileys with beer Re: Re: Numbers Game!!!
August 05, 2013 09:44AM
Howzit Guys

I found this link on youtube about making a numbers game, which i think will be useful for people that a new to php and wanna understand the code that is similar to sipho's one.
http://www.youtube.com/watch?v=0oSFr6UoRao

Regards
Sorry, only registered users may post in this forum.

Click here to login