Question

Create a script that presents a word guessing game. Allow users to guess the word letter-by-letter by entering a character in a form. Start by assigning a secret word to a variable named $secret. Afte...

Create a script that presents a word guessing game. Allow users to guess the word letter-by-letter by entering a character in a form. Start by assigning a secret word to a variable named $secret. After each guess, print the word using asterisks for each remaining letter but fill in the letters that the user guessed correctly. You need to store the user’s guess in a hidden text field name $hidden_guess. For example, if the word you want users to guess the word programming, and the user has successfully guessed the letters g and m, then store ***g**mm**g in the hidden form field $hidden_guess. Use a single script named GuessingGame.php to display and process the form. Please help me out !

0 0
Add a comment Improve this question Transcribed image text
Answer #1

<!--front end html file-->

<!DOCTYPE html>
<html>
<head>
   <title>Guess the word</title>
</head>
<body>
<form id="formSubmit">
   <h4>Guess the word</h4>
   <!--character read-->
   <input type="text" id="word">
   <!--submit button-->
   <input type="submit" id="submit" value="submit">
   <!--hidden gussed word-->
   <input type="hidden" name="hidden_guess" value="*********" id="opt">
</form>
<!--the required functonally can only be accomplished by using ajax-->
<p id="opt1"></p>
<!--to dispaly gussed word along with the *-->
<script type="text/javascript">
       var i;
       //taking the vlaues
       document.getElementById('formSubmit').addEventListener('submit',function (e) {
       //to prevent the defalut action
       e.preventDefault();
       //xmlhttprequest object
       var xhr = new XMLHttpRequest();
       //making post request
       xhr.open("POST",'guessinggame.php',true);
       xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
       //getting the entered value
       var word = document.getElementById('word').value;
       var sendWord = "word="+word;
       //function called after the response received
       xhr.onload = function(){
if (this.status==200) {
var pos = xhr.responseText;
var str = document.getElementById("opt").value;
//cretaing the hidden field value
var newString ="";
for(i=0;i<str.length;i++){
    if(pos.includes(i)){
        newString+=word;
    }else if(str.charAt(i)=="*"){
        newString+="*";
    }else{
        newString+=str.charAt(i);
    }
}
//ensmebling in the html
document.getElementById("opt").value=newString;
document.getElementById("opt1").innerHTML=newString;
}
       }
       xhr.send(sendWord);
   });
</script>
</body>
</html>

<!--back end php file -->

<?php
   $secret = "prashanth";
   $word = strtolower($_POST["word"]);
   $last = 0;
   $i=0;
   if(!empty($word)){
       while (($last=strpos($secret,$word,$last))!=false) {
           $allpos[$i]=$last;
           $i++;
           $last=$last+strlen($word);
       }if(empty($allpos) && strpos($secret, $word)==0){
           $allpos[0]=0;
       }
       $allpos=array_values($allpos);
       echo json_encode($allpos);
   }else{
       $message="Please enter the letter";
       echo $message;
   }
  
?>

11 Elements Console Sources Network Performance Memory Applicati doctype html chead</head> Guess the word <html> submit ▼ <bo

Add a comment
Know the answer?
Add Answer to:
Create a script that presents a word guessing game. Allow users to guess the word letter-by-letter by entering a character in a form. Start by assigning a secret word to a variable named $secret. Afte...
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
  • Create a script that presents a word-guessing game. Allow users to guess the word one letter at a...

    Create a script that presents a word-guessing game. Allow users to guess the word one letter at a time by entering a character in a form. Start by assigning a secret word to a variable. After each guess, print the word using asterisks for each remaining letter, but fill in the letters that the user guessed correctly. Store the user’s guess in a form field. For example, if the word you want users to guess is “suspicious” and the user...

  • Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program...

    Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program 3 - Hangman Game Assignment purpose: User defined functions, character arrays, c style string member functions Write an interactive program that will allow a user to play the game of Hangman. You will need to: e You will use four character arrays: o one for the word to be guessed (solution) o one for the word in progress (starword) o one for all of...

  • Word Guessing Game Assignment Help Needed. This C++ (I'm using Visual Studio 2015) assignment is actually...

    Word Guessing Game Assignment Help Needed. This C++ (I'm using Visual Studio 2015) assignment is actually kind of confusing to me. I keep getting lost in all of the words even though these are supposed to instruct me as to how to do this. Can I please get some help as to where to start? Word guessing game: Overview: Create a game in which the user has a set number of tries to correctly guess a word. I highly recommend...

  • Using C programming REQUIREMENTS: This program is a letter guessing game where a simple Al opponent...

    Using C programming REQUIREMENTS: This program is a letter guessing game where a simple Al opponent generates a secret letter for the user to guess. The user must be able to take turns guessing the secret letter, with the Al responding to the user's guesses. After successfully guessing, the user must be allowed to play again. The Al must count how many turns the user has taken. Here is an example of a game in progress where the human user...

  • Do in Python please provide screenshots aswell. Here are the requirements for your game: 1. The...

    Do in Python please provide screenshots aswell. Here are the requirements for your game: 1. The computer must select a word at random from the list of avail- able words that was provided in words.txt. The functions for loading the word list and selecting a random word have already been provided for you in ps3 hangman.py. 2. The game must be interactive; the flow of the game should go as follows: • At the start of the game, let the...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

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
ADVERTISEMENT