PHP - Number Guessing Game In Php
Hi!
I was assigned to create a simple php game as a part of my grade, but i got stuck. I'm not really a php expert and this isnt really working. I managed to get some of the code/game working, but it's pointless imo. I'm looking for someone who would take 3 mins and fix this code for me. The game is called "Random number guessing game." From the title you can see that the game is not really complicated.
Code:
<html> <head> <title></title> </head> <body> <h1> Ugani Random!</h1> <? php if(isset($_POST['submit'])) $x = $_POST['x']; $num = $_POST['num']; $x = rand(1, 10) if($num<$x) { echo " Your number is higher! "<br /> } if($num==$x) { echo " Correct! Press Reset to try again! "<br /> } if($num>$x) { echo " Your number is lower! "<br /> } ?> <p> <form> <input type="number" name="quantity"> <br /> <button type="submit" value="<? echo $x?>">Submit</button> <button type="reset" value="<? echo $num?>">Reset</button> </form> </p> </body> </html> Similar TutorialsI am supposed to create a guessing game in PHP that uses sessions to store a random number between 0 and 100, along with the number of guesses the user has attempted. Each time the user guesses wrong, display the number of times the user has guessed. And a give up link that displays the generated number for the current game. I have been working on this for a week and I can't figure it out. Any help would be appreciated. Here's my code thus far: game.php Code: [Select] <?php session_start(); if (!isset($_SESSION["randNum"])) { $_SESSION["randNum"]=rand(1,100); } $randNum=intval($_SESSION["randNum"]); ?> <html> <body> <center><form action="game_check.php" method="POST"> Guess a number 1-100:<input type="text" name="userGuess"/> <input type="submit" value="Guess"/> </form></center> <center><form action="game.php"> <input type="submit" value="Restart"/> </form></center> </body> </html>game_check.php Code: [Select] <?php session_start(); $_SESSION["randNum"] = $_POST["randNum"]; $userGuess=$_POST["userGuess"]; if (isset($randNum)) { if ($userGuess<$randNum) { echo "<center>You guessed too low!</center>"; } if ($userGuess>$randNum) { echo "<center>You guessed too high!</center>"; } if ($userGuess==$randNum) { echo "<center>Congratulations You're right!!!</center>"; unset($_SESSION["randNum"]); } } else { echo "Uh oh"; } ?> Im new to php and i cant get my program to display correctly. I want it to take a user's guess of a single Die roll and display whether the user guessed correctly and display a picture of the corresponding Die. i think there is something wrong with my "If" statement or logic. Please Help. Here's the Code: INPUT FORM: <html> <body> <form name = "Dice Game" action="diceroll.php" method="post"> Your Guess: <br> <input type= "text" name="" size= "1"> <input type ="submit" name = "submit"> </form> </body> </html> OUTPUT FORM: <html> <body> <?php $roll = $_POST['submit']; $dice = rand(1, 6); if ($roll == 1 && $dice == 1){ print "Great Job, You're Good!"; }else if ($roll == 1 && $dice /= 1) { print "Wrong"; } else if ($roll == 2 && $dice == 2) { print "Great Job, You're Good!";} else if ($roll == 2 && $dice /= 2) { print "Wrong"; } else if ($roll == 3 && $dice == 3) { print "Great Job, You're Good!"; } else if ($roll == 3 && $dice /= 3) { print "Wrong"; } else if ($roll == 4 && $dice == 4) { print "Great Job, You're Good!"; } else if ($roll == 4 && $dice /= 4) { print "Wrong"; } else if ($roll == 5 && $dice == 5) { print "Great Job, You're Good!"; } else if ($roll == 5 && $dice /= 5) { print "Wrong"; } else if ($roll == 6 && $dice == 6) { print "Great Job, You're Good!"; } else if ($roll == 6 && $dice /= 6) { print "Wrong"; } else { print " "; } if($dice == 1) print "<img src='dice1.png'>"."<br>"; if($dice == 2) print "<img src='dice2.png'>"."<br>"; if($dice == 3) print "<img src='dice3.png'>"."<br>"; if($dice == 4) print "<img src='dice4.png'>"."<br>"; if($dice == 5) print "<img src='dice5.png'>"."<br>"; if($dice == 6) print "<img src='dice6.png'>"."<br>"; else { print "Thanks for playing". "<br>"; } ?> <form name= "back" action= "dice.php" method= "post"> <input type="submit" name="back" value ="Play Again"> </form> </body> </html> I have just did this php code for the high-low game but the $tries variable always goes down to 0 when I refresh..Can someone help me to return the $tries to browser with the url? Thank you
<html>
<?php function High_Low($guess){
Hello, I am trying to create a simple game random number guessing game in PHP. Currently, I have an HTML page with a form on it where the user enters a number. Then when the user clicks the submit button on the form, their number is passed onto the second page. On the second page, I have some PHP code that generates a random number between 1 and 20, then it checks to see if the two numbers are the same. If they are the same, it says correct. If the guess is higher, it says so. If the guess is lower, it says so. I also made it display a form to re-guess the number (if the initial guess was incorrect). The problem I have is when the user attempts to re-guess the number. Whenever the user attempts to enter a new guess, the random number changes. I want to make it so that the random number stays the same as it is. Any help? Here is the Code for the first HTML page: Code: [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>phpNumberGame</title> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="main"></div> <div id="enterNumber"> <form action="result.php" method="post"> Enter a number: <input type="text" name="inputnumber" /><br /> <input type="submit" value="Guess" /> </form> </div> </body> </html> Here is the code for the second page: Code: [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>result</title> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="main"></div> <div id="enterNumber"> <?php $number_actual = rand (1,20); $number_guess = $_REQUEST["inputnumber"]; echo "You chose: " . $number_guess . "<br />"; echo "The random number is: " . $number_actual . "<br />"; if ($number_guess==$number_actual) echo "You guessed <b>Correctly!</b>"; elseif ($number_guess<$number_actual) { echo "You are too <b>Low</b>!"; echo "<br />"; echo "<br />"; echo "Try guessing again..."; echo "<form> <input type='text' name='inputnumber' /> <input type='submit' value='Guess' /> </form>"; } elseif ($number_guess>$number_actual) { echo "You are too <b>High</b>!"; echo "<br />"; echo "<br />"; echo "Try guessing again..."; echo "<form> <input type='text' name='inputnumber' /> <input type='submit' value='Guess' /> </form>"; } ?> </div> </body> </html> Here is what I have so far on my website, you can see it he http://joshkautz.com/phpNumberGame/phpNumberGame.html I think I included everything necessary for people to give input, if not please let me know and I will give more information! Do I need to redo things completely? Any advice would be greatly appreciated. Thanks a ton! Can someone help me create this?? Upon outside entry, a random number is chosen between 1 and 10. Use this function to generate the guess number $guessnum = rand(1,10); // you may change the variable name The user tries to guess this number by choosing from a selection list of possibilities. The user "loses" if there are three failed attempts. For reentrant activations the selection list must be sticky and the same guess number must be maintained. The guess number and the attempt number must both be propagated through hidden inputs. Here is a screen shot: Upon outside entry, or using the "Start Over" hyperlink (with the list activated): Let's suppose the number to be guessed is 6. I select 4 and guess it. Response should say "Try #1" I select 9 and guess it. Response should say "Try #2" I select 3 and guess it. Response should say "You Lose" At this point assume that we must "Start Over". This time, suppose the generated number is 8. I select 8 and guess it. Response should say "You Win" Again, assume we must "Start Over". Hello, I am building an online game(users make a character and move on a map and so on...)
All user data is stored in a mySQL database and I want the users to interact in real-time, but there can be a 1-3 second delay between the communication, but not exceed 3 seconds even if 500 players are playing at the same time.
But for the purpose of the question let's say the users can only chat between one another, if I'll have a solution for that then I can use the same method for more parts of the game.
I can't use websockets because my webhost doesn't support it( I don't want to use pusher.com).
I know I can make real-time apps with ajax long polling, but I think that with 500 players playing at the same time it's not the best solution.
So, finally:
How can I make user interaction as close as possible to a real-time game?
(Without too much load on the hosting server)
(I am sorry if some of my terms are not correct - I am just getting back to coding after a long time...)
Edited by Mythion, 17 August 2014 - 02:34 AM. I'm getting the dreaded " Invalid parameter number: number of bound variables does not match number of tokens" error and I've looked at this for days. Here is what my table looks like:
| id | int(4) | NO | PRI | NULL | auto_increment | | user_id | int(4) | NO | | NULL | | | recipient | varchar(30) | NO | | NULL | | | subject | varchar(25) | YES | | NULL | | | cc_email | varchar(30) | YES | | NULL | | | reply | varchar(20) | YES | | NULL | | | location | varchar(50) | YES | | NULL | | | stationery | varchar(40) | YES | | NULL | | | ink_color | varchar(12) | YES | | NULL | | | fontchosen | varchar(30) | YES | | NULL | | | message | varchar(500) | NO | | NULL | | | attachment | varchar(40) | YES | | NULL | | | messageDate | datetime | YES | | NULL |Here are my params: $params = array( ':user_id' => $userid, ':recipient' => $this->message_vars['recipient'], ':subject' => $this->message_vars['subject'], ':cc_email' => $this->message_vars['cc_email'], ':reply' => $this->message_vars['reply'], ':location' => $this->message_vars['location'], ':stationery' => $this->message_vars['stationery'], ':ink_color' => $this->message_vars['ink_color'], ':fontchosen' => $this->message_vars['fontchosen'], ':message' => $messageInput, ':attachment' => $this->message_vars['attachment'], ':messageDate' => $date );Here is my sql: $sql = "INSERT INTO messages (user_id,recipient, subject, cc_email, reply, location,stationery, ink_color, fontchosen, message,attachment) VALUES( $userid, :recipient, :subject, :cc_email, :reply, :location, :stationery, :ink_color, :fontchosen, $messageInput, :attachment, $date);"; And lastly, here is how I am calling it: $dbh = parent::$dbh; $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); if (empty($dbh)) return false; $stmt = $dbh->prepare($sql); $stmt->execute($params) or die(print_r($stmt->errorInfo(), true)); if (!$stmt) { print_r($dbh->errorInfo()); }I know my userid is valid and and the date is set above (I've echo'd these out to make sure). Since the id is auto_increment, I do not put that in my sql (though I've tried that too), nor in my params (tried that too). What am I missing? I feel certain it is something small, but I have spent days checking commas, semi-colons and spelling. Can anyone see what I'm doing wrong? function GetStatus(){ $fd=read_file('s_status.sta'); list($perf, $msg, $rest) = explode('\n', $fd, 3); if(isset($perf) && isset($msg)){ return '<p class="status"><img src="/images/sta/'.$perf.'.png" /> $msg</p>'; } else { return ''; } } Hi everyone, Thanks for reading. I'm having a problem with my script, I would love to know why it's just stopping parsing and no errors are getting diplayed when I call this function. I'm using E_ALL, Highly annoying when still no errors come up... When the function is called the page stops parseing, The function returns nothing. The files content does like this: Code: [Select] ERROR This is an error Som other stuff I don't need hence $rest in the explode As you can tell it's a status file I checked the contents and it's all in the right format. Thanks in advance! Hello Team, My job changed a few years back but I'm once again trying to learn to become better with php. I wanted to catch up to where I was around this time a few years back and honestly the language is coming back quickly but the quirky bottle-neck errors are still lingering. Hoping someone can take a look at the simple page I created below - post/get/session globals do not seem to be working, additionally, the semicolon after echo command does not seem to end the php command on the semicolon(displays everything). It may be a browser or something more simple but am hoping someone can at least look at my code below to see if there are any red flags.
<?php <h2>Campus Information</h2>
<form action="campus_test_form.php" method="get">
</body> Hello,
I have problem durring binding update query. I can't find what is causing problem.
public function Update(Entry $e) { try { $query = "update entry set string = $e->string,delimiter=$e->delimiter where entryid= $e->id"; $stmt = $this->db->mysqli->prepare($query); $stmt->bind_param('ssi',$e->string,$e->delimiter,$e->id); $stmt->close(); } catch(Exception $ex) { print 'Error: ' .$ex->getMessage(); } }When I run function update I'm getting next error:Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement Can you help me to solve this problem ? Edited by danchi, 17 October 2014 - 10:25 AM. I need to display a number(the number is retrieved from the db) in the form input field such that only the last 4 digits is visbile, the remaining can be masked as * or X or whatever is applicable. I know the last 4 can be obtained as follows: Code: [Select] $number=substr($number,-4,4); But when i hit the submit button the form validates the input field and checks if the number is a valid number of a specific format. Therefore when I click on the submit button then I should still be able to unmask the masked numbers or do something similar that would help me validate the whole number. Code: [Select] <input type="text" name="no" value="<?php if(!empty($number)){ echo $number;} ?>"> Hey I have a string that looks like the following: Quote top-php-tutorials-2.html I have a script that cycles through each page. The 2 in the quote above is the page number. How can I extract the number between the - and the .html and replace it with another number? I've tried Code: [Select] substr($engine->selectedcaturl, 0,-6).$v.".html"But then I realised this only works for numbers that are 1 digit long Any input would be appreciated hello, i have a start time and an end time (no dates). the following works great, except for when the start time is at night, and the end time is in the morning. im thinking if i can do an if the number is negative, add 24, that it will resolve my issue. any ideas? Code: [Select] $tbilled = $tfinish - $tstart; I have a tic tac toe game that I've been working on for a school project. When I attempt to run it a parse error comes up saying "syntax error, unexpected $end" on line 311. If anyone wants to try and run it. go here http://itstudents.ccs.uconn.edu/~iskm218st38/newnewnewtictactoe.html I'm trying to make the computer randomly select a tile after the human player selects but the first issue I need help with is why i'm getting this syntax error on a line that doesnt have code on. This is the html gameboard Code: [Select] <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Untitled</title> <meta name="generator" content="BBEdit 16.0" /> </head> <body> <center> <?php session_start() ?> <form action="tictactoe.php" method="post"> <table> <tr> <td colspan="3" align="center"> <input type="text" size="9" name="message" style="border:none; text-align:center;" /></td> </tr> <tr align="center"> <tr> <td> <input type="text" size="1" name="box1" pattern="[X|O{2}]" title="X or O" /></td> <td align="center"> <input type="text" size="1" name="box2" pattern="[X|O{2}]" title="X or O" /></td> <td align="right"> <input type="text" size="1" name="box3" pattern="[X|O{2}]" title="X or O" /></td> <td align="right"> <input type="text" size="1" name="box4" pattern="[X|O{2}]" title="X or O" /></td> </tr> <tr> <td> <input type="text" size="1" name="box5" pattern="[X|O{2}]" title="X or O" /></td> <td align="center"> <input type="text" size="1" name="box6" pattern="[X|O{2}]" title="X or O" /></td> <td align="right"> <input type="text" size="1" name="box7" pattern="[X|O{2}]" title="X or O" /></td> <td align="right"> <input type="text" size="1" name="box8" pattern="[X|O{2}]" title="X or O" /></td> </tr> <tr> <td> <input type="text" size="1" name="box9" pattern="[X|O{2}]" title="X or O" /></td> <td align="center"> <input type="text" size="1" name="box10" pattern="[X|O{2}]" title="X or O" /></td> <td align="right"> <input type="text" size="1" name="box11" pattern="[X|O{2}]" title="X or O" /></td> <td align="right"> <input type="text" size="1" name="box12" pattern="[X|O{2}]" title="X or O" /></td> </tr> <tr> <td> <input type="text" size="1" name="box13" pattern="[X|O{2}]" title="X or O" /></td> <td align="center"> <input type="text" size="1" name="box14" pattern="[X|O{2}]" title="X or O" /></td> <td align="right"> <input type="text" size="1" name="box15" pattern="[X|O{2}]" title="X or O" /></td> <td align="right"> <input type="text" size="1" name="box16" pattern="[X|O{2}]" title="X or O" /></td> </tr> <tr> <td colspan="3"><input type="button" value="new game" onclick="new_game(); play();" /></td> <td colspan="3"><input type="button" value="submit" onclick="submit();" /></td> </tr> </center> </body> </html> And this is the tictactoe.php file. Code: [Select] <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Untitled</title> <meta name="generator" content="BBEdit 16.0" /> </head> <body> <center> <?php //refer to the comment below $box1 = ''; $box2 = ''; $box3 = ''; $box4 = ''; $box5 = ''; $box6 = ''; $box7 = ''; $box8 = ''; $box9 = ''; $box10 = ''; $box11 = ''; $box12 = ''; $box13 = ''; $box14 = ''; $box15 = ''; $box16 = ''; $human = ''; $computer = ''; $players = array ($human, $computer); $gameboard = array( $box1, $box2, $box3, $box4, $box5, $box6, $box7, $box8, $box9, $box10, $box11, $box12, $box13, $box14, $box15, $box16, ); switch ($box1) { case "O": echo $_POST["box1"]; break; case "X": echo $_POST["box1"]; break; } switch ($box2) { case "O": echo $_POST["box2"]; break; case "X": echo $_POST["box2"]; break; } switch ($box3) { case "O": echo $_POST["box3"]; break; case "X": echo $_POST["box3"]; break; } switch ($box4) { case "O": echo $_POST["box4"]; break; case "X": echo $_POST["box4"]; break; } switch ($box5) { case "O": echo $_POST["box5"]; break; case "X": echo $_POST["box5"]; break; } switch ($box6) { case "O": echo $_POST["box6"]; break; case "X": echo $_POST["box6"]; break; } switch ($box7) { case "O": echo $_POST["box7"]; break; case "X": echo $_POST["box7"]; break; } switch ($box8) { case "O": echo $_POST["box8"]; break; case "X": echo $_POST["box8"]; break; } switch ($box9) { case "O": echo $_POST["box9"]; break; case "X": echo $_POST["box9"]; break; } switch ($box10) { case "O": echo $_POST["box10"]; break; case "X": echo $_POST["box10"]; break; } switch ($box11) { case "O": echo $_POST["box11"]; break; case "X": echo $_POST["box11"]; break; } switch ($box12) { case "O": echo $_POST["box12"]; break; case "X": echo $_POST["box12"]; break; } switch ($box13) { case "O": echo $_POST["box13"]; break; case "X": echo $_POST["box13"]; break; } switch ($box14) { case "O": echo $_POST["box14"]; break; case "X": echo $_POST["box14"]; break; } switch ($box15) { case "O": echo $_POST["box15"]; break; case "X": echo $_POST["box15"]; break; } switch ($box16) { case "O": echo $_POST["box16"]; break; case "X": echo $_POST["box16"]; break; } /*$gameboard = array_rand($box1, $box2, $box3, $box4, $box5, $box6, $box7, $box8, $box9, $box10, $box11, $box12, $box13, $box14, $box15, $box16 ); foreach ($gameboard as &$value) { $value = 'O'; }*/ // $gameboard is now array_rand() as &$value = O unset($value); // break the reference with the last element if ($box1=="X") echo $gameboard[$rand_keys[0]] . "\n"; if ($box2=="X") echo $gameboard[$rand_keys[0]] . "\n"; if ($box3=="X") echo $gameboard[$rand_keys[0]] . "\n"; if ($box4=="X") echo $gameboard[$rand_keys[0]] . "\n"; if ($box5=="X") echo $gameboard[$rand_keys[0]] . "\n"; if ($box6=="X") echo $gameboard[$rand_keys[0]] . "\n"; if ($box7=="X") echo $gameboard[$rand_keys[0]] . "\n"; if ($box8=="X") echo $gameboard[$rand_keys[0]] . "\n"; if ($box9=="X") echo $gameboard[$rand_keys[0]] . "\n"; if ($box10=="X") echo $gameboard[$rand_keys[0]] . "\n"; if ($box11=="X") echo $gameboard[$rand_keys[0]] . "\n"; if ($box12=="X") echo $gameboard[$rand_keys[0]] . "\n"; if ($box13=="X") echo $gameboard[$rand_keys[0]] . "\n"; if ($box14=="X") echo $gameboard[$rand_keys[0]] . "\n"; if ($box15=="X") echo $gameboard[$rand_keys[0]] . "\n"; if ($box16=="X") echo $gameboard[$rand_keys[0]] . "\n"; /* unset($b); $b[] = "box1|box2|box3|box4"; $b[] = "box5|box6|box7|box8"; $b[] = "box9|box10|box11|box12"; $b[] = "box13|box14|box15|box16"; */ /* $boardtile = (); $gameboard = array( array(box1, box2, box3), array(box4, box5, box6), array(box7, box8, box9)); public static GUI_Box */ $Human = true; function check_input( $box1 ) { if( preg_match( "X|O", $text ) ) { return false; } else { return true; // human wins if if( //horizontal ($box1 == $human && $box2 == $human && $box3 == $human && $box4 == $human) or ($box5 == $human && $box6 == $human && $box7 == $human && $box8 == $human) or ($box9 == $human && $box10 == $human && $box11 == $human && $box12 == $human) or ($box9 == $human && $box10 == $human && $box11 == $human && $box12 == $human) or //vertical ($box1 == $human && $box5 == $human && $box9 == $human && $box13 == $human) or ($box2 == $human && $box6 == $human && $box10 == $human && $box14 == $human) or ($box3 == $human && $box7 == $human && $box11 == $human && $box15 == $human) or ($box4 == $human && $box8 == $human && $box12 == $human && $box16 == $human) or //diagonal ($box4 == $human && $box7 == $human && $box10 == $human && $box13 == $human) or ($box1 == $human && $box6 == $human && $box11 == $human && $box16 == $human)) echo("human "+$players[0]+" won") ; //computer wins if if( //horizontal ($box1 == $computer && $box2 == $computer && $box3 == $computer && $box4 == $computer) or ($box5 == $computer && $box6 == $computer && $box7 == $computer && $box8 == $computer) or ($box9 == $computer && $box10 == $computer && $box11 == $computer && $box12 == $computer) or ($box9 == $computer && $box10 == $computer && $box11 == $computer && $box12 == $computer) or //vertical ($box1 == $computer && $box5 == $computer && $box9 == $computer && $box13 == $computer) or ($box2 == $computer && $box6 == $computer && $box10 == $computer && $box14 == $computer) or ($box3 == $computer && $box7 == $computer && $box11 == $computer && $box15 == $computer) or ($box4 == $computer && $box8 == $computer && $box12 == $computer && $box16 == $computer) or //diagonal ($box4 == $computer && $box7 == $computer && $box10 == $computer && $box13 == $computer) or ($box1 == $computer && $box6 == $computer && $box11 == $computer && $box16 == $computer)) echo("computer "+$players[1]+" won") ; } ?> </center> </body> </html> Can someone point me to a layman's tutorial for a simple hangman game? I downloaded one off the internet, but it doesn't do what I want. The one I use doesn't allow capitalized letters or spaces! I can live without the capitlization but MOST of the words will have spaces and therefore, this code will not work. I want to start a brand new code from scratch but I can't seem to figure it out. I am new to coding and have been coding my site using MySQL, and that's fine, but ALL of the coding for hangman is very confusing for me! Code: [Select] $letters = array('a','b','c','d','e','f','g','h','i','j','k','l','m', 'n','o','p','q','r','s','t','u','v','w','x','y','z', ' '); if(empty($_POST)) { $rightcount = '0'; $wrongcount = '0'; $words = explode("\n", file_get_contents('words.list.txt')); $right = array_fill_keys($letters, ' _ '); $wrong = array(); shuffle($words); $word = strtolower($words[0]); $rightstr = serialize($right); $wrongstr = serialize($wrong); $wordletters = str_split($word); $show = ''; foreach($wordletters as $letter) { $show .= $right[$letter]; } } else { $show = $_POST['show']; $word = $_POST['word']; $lettercount = strlen($word); echo $lettercount . "<br>"; $word = htmlspecialchars($word); $word = mysql_real_escape_string($word); $rightcount = $_POST['rightcount']; $wrongcount = $_POST['wrongcount']; $guess = strtolower($_POST['guess']); $guess = ctype_alpha($guess); if($guess == TRUE) { $guess = strtolower($_POST['guess']); } else { echo "<b>Please use only letters.</b><br>"; $guess = ''; } $letterinstance = @substr_count($word, $guess); $right = unserialize($_POST['rightstr']); $wrong = unserialize($_POST['wrongstr']); $wordletters = str_split($word); if(!(empty($guess))) { if(stristr($word, $guess)) { $show = ''; $right[$guess] = $guess; $wordletters = str_split($word); $rightcount = $rightcount + (1*$letterinstance); foreach($wordletters as $letter) { $show .= $right[$letter]; } } else { $show = ''; $wrong[$guess] = $guess; $wrongcount = $wrongcount + 1; if(count($wrong) == 6) { $show = $word; echo "You lost this round of hangman.<br>"; } else { foreach($wordletters as $letter) { $show .= $right[$letter]; } } } } $rightstr = serialize($right); $wrongstr = serialize($wrong); echo "Right: $rightcount<br>Wrong: $wrongcount<br>"; if($rightcount == $lettercount) { echo "You win!<br>"; } } echo "Incorrect Guesses: "; echo implode(', ', $wrong); echo "<br>"; echo $show; echo "<br />"; echo "<form method='post'> <input name='guess'>"; echo "<input type='hidden' name='show' value='$show'> "; echo "<input type='hidden' name='rightcount' value='$rightcount'> "; echo "<input type='hidden' name='wrongcount' value='$wrongcount'> "; echo "<input type='hidden' name='word' value='$word'> "; echo "<input type='hidden' name='rightstr' value='$rightstr'>"; echo "<input type='hidden' name='wrongstr' value='$wrongstr'>"; echo "<input type='submit' value='guess'> "; echo "</form>"; echo "<a href='hangman.php'>Reset</a>"; That is the code I am using now. The connect to the database and rewarding the user when they guess all the letters work fine, but I need for my other details to be in there and I have NO clue how to do that. I've tried a lot of things but everything just screws up. Here's the code I have now that is from scratch, and it's very basic. I really have no freakin' clue where to go from there. Code: [Select] <?php $words = explode(";", file_get_contents('words.list.txt')); shuffle($words); $word = $words[0]; $upletters = array('a','b','c','d','e','f','g','h','i','j','k','l','m', 'n','o','p','q','r','s','t','u','v','w','x','y','z'); $lowletters = array('A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'); echo $word . "<br>"; $word1 = str_replace($upletters, ' _ ', $word); $word1 = str_replace($lowletters, ' _ ', $word1); echo $word1 . "<br><br>"; echo "<form action = '1.php' method = 'POST'>"; echo "<input type = 'text' name = 'guess' size = '10'>"; echo "<input type = 'hidden' name = 'word' value = '$word'>"; echo "<input type = 'hidden' name = 'word1' value = '$word1'>"; echo "<br><input type = 'submit' value = 'Guess!'></form>"; ?> Can someone PLEASE point me in the right direction! Or give me all the functions I need to use to get through this! Thanks in advance. Hello Everyone, I am using some code for a hangman game. It is written in PHP. I am using code directly from this website http://www.daniweb.com/code/snippet267046.html and am having issues with it. I have the php file on my web server, and I also have a words.txt file on the server as well. However, I am getting errors, and the game also automatically says "you win!" even when entering just one letter. I'm not sure what is going on, as I am using the exact code from the website. Here is where the file is located: http://www.littlehousegardening.com/hangman/hangmanfinal.php Errors I am encountering: Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively in Unknown on line 0 code: Code: [Select] <?php session_start(); // The following function returns a word of dots, each dot represents a letter function word2dots($word) { $wordlength = strlen($word); $dotWord = ""; for ($i = 0; $i < $wordlength; $i++) { $dotWord = "".$dotWord."."; } return $dotWord; } // The following function opens the file, reads it, saves it into an array and returns a random word function getWord() { // Opening and reading file $filename = "words.txt"; // You can change this into whatever words file you'd like, as long as it's local. $filehandle = fopen($filename,"r"); $filecontent = fread($filehandle,filesize($filename)); fclose($filehandle); if (!$filehandle) { $_SESSION['message'] = "Could not open ".$filename; } // Splitting the content into the $words array $words = split("\|", $filecontent); $words_amount = count($words); // Calculating random number $random_number = mt_rand(0,$words_amount); // Returning random word $theWord2 = $words[$random_number]; return $theWord2; } // Resetting the message $_SESSION['message'] = ""; // Retrieving the guessWord, theWord and the amount of errors. if (isset($_POST['send_letter']) && $_SESSION['gameEnded'] != true) { if (isset($_POST['HangManLetter']) && preg_match("/[A-Z\s_]/i", $_POST['HangManLetter']) > 0) { // Getting the $letter value $letter = htmlentities(stripslashes($_POST['HangManLetter'])); // Retrieving session variables $theWord = $_SESSION['theWord']; $guessWord = $_SESSION['guessWord']; $error_amount = $_SESSION['error_amount']; // Checking wether the $letter occurs in the word // Filling the arrays: for ($d = 0; $d < strlen($theWord); $d++) { $theWordArray[$d] = substr($theWord, $d, 1); $guessWordArray[$d] = substr($guessWord, $d, 1); } // Checking occurance of the letter in theWord $letterOccured = false; for ($f = 0; $f < strlen($theWord); $f++) { if ($theWordArray[$f] == $letter) { $letterOccured = true; $guessWordArray[$f] = $theWordArray[$f]; } } // Updating the guessWord: $guessWord = ""; for ($r = 0; $r < strlen($theWord); $r++) { $guessWord = "".$guessWord."".$guessWordArray[$r].""; } $_SESSION['guessWord'] = $guessWord; if ($_SESSION['guessWord'] == $_SESSION['theWord']) { $_SESSION['message'] = "You won! <input type='submit' name='reset' value='Try again?' />"; unset($_SESSION['theWord']); unset($_SESSION['guessWord']); $_SESSION['gameEnded'] = true; $_SESSION['guessWord'] = $theWord; } if ($letterOccured == false) { $error_amount++; $_SESSION['error_amount'] = $error_amount; // If the error_amount is higher as 9, the player lost if ($error_amount > 9) { $_SESSION['message'] = "You lost! <input type='submit' name='reset' value='Try again?' />"; unset($_SESSION['theWord']); unset($_SESSION['guessWord']); $_SESSION['gameEnded'] = true; $_SESSION['guessWord'] = $theWord; } } } else { // Showing the message if (preg_match("/[A-Z\s_]/i", $_POST['letter']) < 0) { $_SESSION['message'] = "Only alphanumeric symbols are allowed!"; } else { $_SESSION['message'] = "Enter a letter!"; } // End of else isaplha ($_POST['HangManLetter']) } // End of else isset($_POST['HangManLetter']) and preg_match("/[A-Z\s_]/i", $_POST['HangManLetter']) } else { // If the game has been resetted or there has not yet been a game played $theWord = getWord(); $guessWord = word2dots($theWord); $error_amount = 0; $_SESSION['theWord'] = $theWord; $_SESSION['guessWord'] = $guessWord; $_SESSION['error_amount'] = $error_amount; $_SESSION['gameEnded'] = false; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <link rel="stylesheet" href="style1.css" type="text/css" /> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Hangman</title> </head> <body onload="document.getElementById('HangManInput').focus()"> <div style="background-color:#EFEFEF;position:relative; top:0px; left:0px; border:2px ridge #BBBBBB; width:200px; height:280px; font-family:Times New Roman; font-weight:normal; font-style:normal; text-decoration:none;"> <form action="" method="post" onsubmit="return true;"> <div id="HangManTitle" style="font-size:22px; position:absolute; top:5px; width:200px; text-align:center;"> Hangman </div> <div id="HangManConsole" style="font-size:16px; position:absolute; top:34px; text-align:center; width:200px;"> Enter a letter: <input type="text" maxlength="1" size="1" id="HangManInput" name="HangManLetter" /><input type="hidden" value="true" name="send_letter" /><input type="submit" value="Go" name="send_letter_button" /><br /> The word: <span id="HangManGuessWord"><?php echo $_SESSION['guessWord']; ?></span><br /> </div> <div id="HangManImage" style="position:absolute; top:85px; left:22px;"> <img src="http://www.hangman.symbolwebdesign.nl/hmimgs/hm<?php echo $_SESSION['error_amount']; ?>.bmp" id="HangManIMG" style="border:1px ridge #BBBBBB;" /> </div> <div id="HangManMessage" style="font-size:16px; position:absolute; top:250px; width:200px; text-align:center;"> <?php echo $_SESSION['message']; ?> </div> </form> </div> </body> </html> hi iam finished from my online strategy game but iwant add map to it players can see cities and their troops on map any 1 can give me code? Haven't done much in PHP for a while, and as a refresher and a bit of a challenge to myself I have decided to write up a simple game of sorts. Basically in the game I have townships, every play has their own town. And I want these towns to have events every 3-10 hours, and have these events have results such as births/deaths/fires/flooding/etc. At the moment I'm looking at this kind of setup: A database table with the city information.. city_id, city_owner_id, city_name, etc. I figure I can make another field such as city_nextevent and fill it with a timestamp plus a random number of seconds. then when the user views their city if city_nextevent is older than current time then run the event function and set the next event time. In my head this idea seems to work well, until I realised that if the user doesn't log on for 20+ hours the events will not stack, they will only get the one event when they next view their town when several events should have passed. I realise I could use a cronjob to check all events across all towns, but is there a better way of doing it. I had also though about adding several fields to the database containing say the next five event times, then updating each field after time had passed. Which gives more events if the user has been offline for 20+ hours. So if the play was offline for say a week, their town would pretty much dry up until they returned. Basically, I want their town to run while they're offline. But was trying to avoid using a cronjob. Anyone have any thoughts on how this could be done? Wow, I have no clue what I'm doing.. I'm trying to make a random game and if you roll a 6, you will win 500 rp and I'm trying to insert the 500 rp into the user who is logged in ($_SESSION['username'). But I... just don't know where to begin. Here is my crappy coding that I just.. am stumped on: <p><?php $dice = rand(1,6); if($dice == 1){ echo "You rolled a <br><b>1</b>"; }if($dice == 2){ echo "You rolled a <br><b>2</b>"; }if($dice == 3){ echo "You rolled a <br><b>3</b>"; }if($dice == 4){ echo "You rolled a <br><b>4</b>"; }if($dice == 5){ echo "You rolled a <br><b>5</b>"; }if($dice == 6){ echo "You rolled a <br><b>6</b>"; } $winner = "500"; if($dice == 6); { include("haha.php"); $cxn = mysqli_connect($dbhost,$dbuser,$dbpassword,$dbdatabase); $sql = "INSERT INTO Member (rp) VALUES ('$winner')"; mysqli_query($cxn,$sql); } ?> how would you code a map for a browser game like travian? i know how it works but i cant code that sort of stuff. ive got my database set up like this x y player 1 1 1 2 etc.... so i when you click an arrow it will got to the next square along. |