Question

How to make a hangman game web development project using php or html?

How to make a hangman game web development project using php or html?

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Hangman.php
<?php

require_once 'hangedman.php';

$words = array();
$numwords = 0;

function printPage($image, $guesstemplate, $which, $guessed, $wrong) {
  echo <<<ENDPAGE
<!DOCTYPE html>
<html>
  <head>
        <title>Hangman</title>
  </head>
</html>
<body>
  <h1>Hangman Game</h1>
  <br />
  <pre>$image</pre>
  <br />
  <p><strong>Word to guess: $guesstemplate</strong></p>
  <p>Letters used in guesses so far: $guessed</p>

  <form method="post" action="$script">
        <input type="hidden" name="wrong" value="$wrong" />
        <input type="hidden" name="lettersguessed" value="$guessed" />
        <input type="hidden" name="word" value="$which" />
        <fieldset>
          <legend>Your next guess</legend>
          <input type="text" name="letter" autofocus />
          <input type="submit" value="Guess" />
        </fieldset>
  </form>
</body>
ENDPAGE;
}

function loadWords() {
  global $words;
  global $numwords;
  $input = fopen("./words.txt", "r");

  while (true) {
          $str = fgets($input);
          if (!$str) break;
          $words[] = rtrim($str);
          $numwords++;
  }

  fclose($input);
}

function startGame() {
  global $words;
  global $numwords;
  global $hang;

  $which = rand(0, $numwords - 1);
  $word =  $words[$which];
  $len = strlen($word);
  $guesstemplate = str_repeat('_ ', $len);
  $script = $_SERVER["PHP_SELF"];

  printPage($hang[0], $guesstemplate, $which, "", 0);
}

function killPlayer($word) {
  echo <<<ENDPAGE
<!DOCTYPE html>
<html>
 <head>
        <title>Hangman</title>
  </head>
  <body>
        <h1>You lost!</h1>
        <p>The word you were trying to guess was <em>$word</em>.</p>
  </body>
</html>
ENDPAGE;
}

function congratulateWinner($word) {
  echo <<<ENDPAGE
<!DOCTYPE html>
<html>
  <head>
        <title>Hangman</title>
  </head>
  <body>
        <h1>You win!</h1>
        <p>Congratulations! You guessed that the word was <em>$word</em>.</p>
  </body>
</html>
ENDPAGE;
}

function matchLetters($word, $guessedLetters) {
  $len = strlen($word);
  $guesstemplate = str_repeat("_ ", $len);

  for ($i = 0; $i < $len; $i++) {
        $ch = $word[$i];
        if (strstr($guessedLetters, $ch)) {
          $pos = 2 * $i;
          $guesstemplate[$pos] = $ch;
        }
  }

  return $guesstemplate;
}

function handleGuess() {
  global $words;
  global $hang;

  $which = $_POST["word"];
  $word  = $words[$which];
  $wrong = $_POST["wrong"];
  $lettersguessed = $_POST["lettersguessed"];
  $guess = $_POST["letter"];
  $letter = strtoupper($guess[0]);

  if(!strstr($word, $letter)) {
        $wrong++;
  }

  $lettersguessed = $lettersguessed . $letter;
  $guesstemplate = matchLetters($word, $lettersguessed);

  if (!strstr($guesstemplate, "_")) {
        congratulateWinner($word);
  } else if ($wrong >= 6) {
        killPlayer($word);
  } else {
        printPage($hang[$wrong], $guesstemplate, $which, $lettersguessed, $wrong);
  }
}

//header("Content-type: text/plain");
loadWords();

$method = $_SERVER["REQUEST_METHOD"];

if ($method == "POST") {
  handleGuess();
} else {
  startGame();
}

?>

hangedman.php

<?php

$hang[0] =
' -------
 |/    | 
 |
 |
 |
 |
 | 
/|\
-------------';

$hang[1] =
' -------
 |/    | 
 |     o
 |
 |
 |
 | 
/|\
-------------';


$hang[2] =
' -------
 |/    | 
 |     o
 |     |
 |     |
 |
 | 
/|\
-------------';

$hang[3] =
' -------
 |/    | 
 |     o
 |     |
 |     |
 |    /
 | 
/|\
-------------';

$hang[4] =
' -------
 |/    | 
 |     o
 |     |
 |     |
 |    / \
 | 
/|\
-------------';

$hang[5] =
' -------
 |/    | 
 |     o
 |   --|
 |     |
 |    / \
 | 
/|\
-------------';

$hang[6] =
' -------
 |/    | 
 |     o
 |   --|--
 |     |
 |    / \
 | 
/|\
-------------';

?>
Add a comment
Know the answer?
Add Answer to:
How to make a hangman game web development project using php or html?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How to make a hangman game web development project using php or html?

    How to make a hangman game web development project using php or html?

  • PHP, HTML I have this code in the picture below for a guess game.

    PHP, HTML I have this code in the picture below for a guess game.

  • A PHP based web page is NOT a client/server architecture type application because PHP code is...

    A PHP based web page is NOT a client/server architecture type application because PHP code is not executed on the client. True False Which of the following application is least likely to be created using PHP? Process user inputs from an HTML form. Include a common copyright notice on every page. Create a dynamic HTML table to show search results. Display a dynamic live clock showing hour, minute, and second. Which of the following is not a server side processing...

  • I have a web development project. First, it requires to have a website with a simple...

    I have a web development project. First, it requires to have a website with a simple design like the picture. Second, after fill in the information in the text box, click submit, the information will be save in the database using MySQL. Must have a captcha (like Google Captcha) to reduce spam. Also, must have a database password encryption. You can code this using php, html for coding, and css for designing. Third, code a php file to show all...

  • C++ Hangman (game) In this project, you are required to implement a program that can play...

    C++ Hangman (game) In this project, you are required to implement a program that can play the hangman game with the user. The hangman game is described in https://en.wikipedia.org/wiki/Hangman_(game) . Your program should support the following functionality: - Randomly select a word from a dictionary -- this dictionary should be stored in an ASCII text file (the format is up to you). The program then provides the information about the number of letters in this word for the user to...

  • Write a Java program implementing a hangman game. This should be a multi file project, managed...

    Write a Java program implementing a hangman game. This should be a multi file project, managed with the directory structure discussed in class, compiled with the ant tool (using a build.xml file), and packaged as a JAR file, ready for distribution. Instructions on how to generate these files are included in our lectures. You can practice with the files ready to compile attached to Lecture 24 (the TicTacToe class example). Your project should include: 1. a text file for the...

  • Create a Hangman Game in C++ using Classes. The program doesn't need to have the hangman...

    Create a Hangman Game in C++ using Classes. The program doesn't need to have the hangman drawing, it just needs the following: - Ten 3-letter words - Keep track of everytime the player misses a letter - Simple and commented code

  • PHP & HTML please Design a web form that contains an input field. The input field...

    PHP & HTML please Design a web form that contains an input field. The input field will accept a list of words that are separated by comma "," . When user hits the submit button, show the results: 1. The count of words. Also show if the count is an even or odd number. 2. The count of words that contains letter 'a' or 'A'.

  • Create a simple commented C++ HANGMAN game using CLASSES using the following specifications: - The game...

    Create a simple commented C++ HANGMAN game using CLASSES using the following specifications: - The game must keep track of the player misses - It must ONLY use 10 three letter words - It does NOT need a graphical implementation

  • PHP Program Complete PHP code (and HTML Code) Using functions to abstract the logic away from...

    PHP Program Complete PHP code (and HTML Code) Using functions to abstract the logic away from the rest of your code makes it easier to read and maintain. PHP provides a number of useful functions as standard, and this week you will use some of these functions to obtain information. Create a program that compares two strings provided on an HTML form, and determine if the two strings are anagrams. The HTML page asks the user for the input strings,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT