How do I imitate the game "Guess Who?

Guess the Number game PHP?

  • I have to make a "Guess te Number" game with basic PHP without the use off any programs. The only things that is allowed is Notepad++ You have to make a game where the computer chooses a number from 1 till 100 and you will have to guess that. Every time you submit a number it should say whether to go higher or lower. It should also mention the number of guesses at the end. http://roland.infsjl.nl/spel.php This is the version my teacher made and is also what it should look like. He has hid the source code somehow and this is where my questions come in. 1) Is it possible to find the source code of this site? 2) Does anyone know a simple version of this game and will you share that one with me? Thanks in advance, W

  • Answer:

    Since it's your homework, you should at least try to work out the logic yourself. Copy/pasting it from an online answer won't help you on your exams. PHP is a server-side scripting language that generates a HTML page for your browser to read, so it's not possible to read the PHP source code. Look at the structure of the page: It appears to first have a header with the game name, then a form with a label, a text input and a submit button, and under the form a hyperlink to itself. When you click the submit button with your particular value, it redirects to itself and gives a message wether your value is too low, too high or is correct (and then the amount of tries it took). When the page loads for the first time, a session variable will be made and given a random value between 0 and 100. PHP has a simple function for this. When you click the submit button, the page reloads, so that means the form action redirects to the same page and the game code is on that game.php page. Thus, a check must be done if the submit button was actually pressed and if a value was given with the form (if you've had PHP in class you should already know about $_POST and $_SESSION global arrays) before it can perform the game. Since there is a counter variable that increases each time you submit a number, there should be another session variable that increases everytime the game code is performed. After this, the game logic is incredibly simple: if the user number is lower as the random number, tell the user that, else if the number is higher as the random number tell the user that, else tell the user he won and also give the number of times he needed.

Wooly at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

<?php //Starts our php document if (!$x) //if we have already defined x and started the game, this does not run { Echo "Please Choose a Number 1-100 <p>"; //gives the user instructions $x = rand (1,100) ; //creates x } What this code does, is check to see if the game has already started. If it has, all of this is ignored. If it has not, then it randomly chooses a number to be the winning number, $x. It also prints some brief instructions for the user.

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.