arsenal-computing Posted May 1, 2009 Share Posted May 1, 2009 i have written a program to play the game hangman, but it is not working.. all of it works apart from the 'Choice 2' which i would really love to get working.. i realised it was this that didnt work when i commented that section out.. any advice and help is much appreciated, thanks xxx Quote Link to comment https://forums.phpfreaks.com/topic/156460-program-doesnt-work-please-help-hangman/ Share on other sites More sharing options...
Maq Posted May 1, 2009 Share Posted May 1, 2009 We can't really help with seeing the related code for "Choice 2". Quote Link to comment https://forums.phpfreaks.com/topic/156460-program-doesnt-work-please-help-hangman/#findComment-823820 Share on other sites More sharing options...
arsenal-computing Posted May 1, 2009 Author Share Posted May 1, 2009 Here is the Hangman program <?php // Skeleton Program code for the AQA COMP1 Summer 2009 examination // this code should be used in conjunction with the Preliminary Materials // written by the AQA COMP1 Programmer Team // developed for execution under PHP version 5.2.8 // the DisplayMenu procedure has deliberately omitted a menu choice 3 and 4 function DisplayMenu() { fwrite(STDOUT, "_________________________________\n"); fwrite(STDOUT, "\n"); fwrite(STDOUT, "1. SETTER - Makes new word/phrase\n"); fwrite(STDOUT, "\n"); fwrite(STDOUT, "2. USER - Next letter guess\n"); fwrite(STDOUT, "\n"); fwrite(STDOUT, "3. USER - Get a word from file\n"); fwrite(STDOUT, "\n"); fwrite(STDOUT, "4. SETTER - Add new phrase to file\n"); fwrite(STDOUT, "\n"); fwrite(STDOUT, "5. USER - Find out how many guesses you have made\n"); fwrite(STDOUT, "\n"); fwrite(STDOUT, "6. USER - Set the maximum number of guesses\n"); fwrite(STDOUT, "\n"); fwrite(STDOUT, "8. End\n"); fwrite(STDOUT, "\n"); } function GetNewPhrase() { $PhraseOK = false; do { fwrite(STDOUT, "Key in the new word ...(letters and any Spaces)"); $ThisNewPhrase = strtoupper(trim(fgets(STDIN))); if (strlen($ThisNewPhrase) < 10) { $PhraseOK = false; fwrite(STDOUT, "Not enough letters ...\n"); // possible further validation check(s) } else { $PhraseOK = true; } } while (!$PhraseOK); return $ThisNewPhrase; } function SetUpGuessStatusArray($NewPhrase) { global $GuessStatusArray; for ($Position = 0; $Position < strlen($NewPhrase); $Position++) { if (substr($NewPhrase, $Position, 1) == ' ') $GuessStatusArray[$Position + 1] = ' '; else $GuessStatusArray[$Position + 1] = '*'; } } function DisplayCurrentStatus($PhraseLength) { global $GuessStatusArray; for ($Position = 1; $Position <= $PhraseLength; $Position++) { fwrite(STDOUT, $GuessStatusArray[$Position]); } fwrite(STDOUT, "\n"); } function GetNextLetterGuess() { fwrite(STDOUT, "Next guess ? "); $GuessedLetter = strtoupper(trim(fgets(STDIN))); return $GuessedLetter; } function AllLettersGuessedCorrectly($NewPhrase) { global $GuessStatusArray; $Position = 1; $MissingLetter = false; do { if ($GuessStatusArray[$Position] != substr($NewPhrase, $Position - 1, 1)) $MissingLetter = true; else $Position = $Position + 1; } while (!$MissingLetter && $Position <= strlen($NewPhrase)); if (!$MissingLetter) return true; else return false; } function GetRandomPhrase($FileNameIn){ $lines = file($FileNameIn); return $lines[rand(0, count($lines)-1)]; } //we made this one. function WriteWordToFile($FileNameIn, $NewPhrase) { $MyPhrases = fopen($FileNameIn, 'a+'); fwrite($MyPhrases, "\r\n$NewPhrase"); fclose($MyPhrases); } //we made this one // Main program block starts here $PhraseHasBeenSet = false; $Choice = 0; global $NewPhrase; $GuessStatusArray = array(); $LettersGuessedArray = array(); global $NextGuessedLetter; $Position = 0; $WordGuessed = false; $NumberOfLives = 10; do { DisplayMenu(); fwrite(STDOUT, "Choice? "); $Choice = intval(trim(fgets(STDIN))); if ($Choice == 1) { $NewPhrase = strtoupper(GetNewPhrase()); SetUpGuessStatusArray($NewPhrase); $PhraseHasBeenSet = true; } /*if ($Choice == 2) { while($NumberOfLives>=1) { if ($PhraseHasBeenSet) { DisplayCurrentStatus(strlen($NewPhrase)); $NextGuessedLetter = GetNextLetterGuess(); $CorrectGuess=false; fwrite(STDOUT, "You have $NumberOfLives lives left.\n"); if(strlen($NextGuessedLetter)>1){ if($NewPhrase==$NextGuessedLetter) { $NumberOfLives=0; fwrite(STDOUT, "You have guessed the phrase correctly. You win.\n"); $CorrectGuess=true; } else { $LettersGuessedArray[count($LettersGuessedArray)] = $NextGuessedLetter; if(!$CorrectGuess){ fwrite(STDOUT, "The phrase you have guessed is incorrect.\n"); $NumberOfLives=$NumberOfLives-1; } } } else { for ($Position = 1; $Position <= strlen($NewPhrase); $Position++) { if ($NextGuessedLetter == $NewPhrase[$Position - 1]){ $GuessStatusArray[$Position] = $NextGuessedLetter; $CorrectGuess=true; } } } $PhraseGuessed = AllLettersGuessedCorrectly($NewPhrase); if ($PhraseGuessed) { $NumberOfLives=0; fwrite(STDOUT, "You have guessed correctly\n"); } elseif($NumberOfLives=1){ $NumberOfLives=0; } else { $LettersGuessedArray[count($LettersGuessedArray)]=$NextGuessedLetter; if(!$CorrectGuess){ fwrite(STDOUT, "You have guessed an incorrect letter.\n"); $NumberOfLives=$NumberOfLives-1; } } } else { fwrite(STDOUT, "The setter has not specified the word/phrase\n"); } }*/ if ($Choice == 3) { $NewPhrase = GetRandomPhrase("MyPhrases.txt"); $NewPhrase = strtoupper($NewPhrase); SetUpGuessStatusArray($NewPhrase); $PhraseHasBeenSet = true; //we added this function } if ($Choice == 4){ fwrite (STDOUT, 'Please write a new phrase:'); $InputWord = strtoupper(trim(fgets(STDIN))); WriteWordToFile("MyPhrases.txt", $InputWord); fgets(STDIN); //we added this function } /*if ($Choice == 5) { fwrite(STDOUT, "You have made COMPLETE THIS guess(es) so far.\n"); fgets(STDIN); //we added this function }*/ if($Choice==6){ fwrite(STDOUT, "Please enter the maximum number of lives/guesses(between 5 and 20.)\n"); $NumberOfLives=intval(trim(fgets(STDIN))); while($NumberOfLives<5 && $NumberOfLives>20); } if ($Choice == 8 && !$PhraseGuessed) { fwrite(STDOUT, "You have not completed this word/phrase ..Press Return to exit\n"); fgets(STDIN); } } while ($Choice != ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/156460-program-doesnt-work-please-help-hangman/#findComment-823826 Share on other sites More sharing options...
trq Posted May 1, 2009 Share Posted May 1, 2009 Your kidding aren't you? Can you at least describe your error? Were not mind readers. Quote Link to comment https://forums.phpfreaks.com/topic/156460-program-doesnt-work-please-help-hangman/#findComment-823827 Share on other sites More sharing options...
arsenal-computing Posted May 1, 2009 Author Share Posted May 1, 2009 well choice 2 is supposed to allow the user to make a guess at a letter or the entire phrase.. but the program just doesn't work unless choice 2 is commented out. it opens and closes immediately.. Quote Link to comment https://forums.phpfreaks.com/topic/156460-program-doesnt-work-please-help-hangman/#findComment-823829 Share on other sites More sharing options...
the182guy Posted May 1, 2009 Share Posted May 1, 2009 The {} for the if/else/while, at the bottom of the choice 2 code are not correct. Try this in place of the choice 2 code. I'm not sure if this is what you're aiming for with the if at the end: if ($Choice == 2) { while($NumberOfLives>=1) { if ($PhraseHasBeenSet) { DisplayCurrentStatus(strlen($NewPhrase)); $NextGuessedLetter = GetNextLetterGuess(); $CorrectGuess=false; fwrite(STDOUT, "You have $NumberOfLives lives left.\n"); if(strlen($NextGuessedLetter)>1) { if($NewPhrase==$NextGuessedLetter) { $NumberOfLives=0; fwrite(STDOUT, "You have guessed the phrase correctly. You win.\n"); $CorrectGuess=true; } else { $LettersGuessedArray[count($LettersGuessedArray)] = $NextGuessedLetter; if(!$CorrectGuess) { fwrite(STDOUT, "The phrase you have guessed is incorrect.\n"); $NumberOfLives=$NumberOfLives-1; } } } } else { for ($Position = 1; $Position <= strlen($NewPhrase); $Position++) { if ($NextGuessedLetter == $NewPhrase[$Position - 1]) { $GuessStatusArray[$Position] = $NextGuessedLetter; $CorrectGuess=true; } } } $PhraseGuessed = AllLettersGuessedCorrectly($NewPhrase); if ($PhraseGuessed) { $NumberOfLives=0; fwrite(STDOUT, "You have guessed correctly\n"); } elseif($NumberOfLives=1) { $NumberOfLives=0; } else { $LettersGuessedArray[count($LettersGuessedArray)]=$NextGuessedLetter; if(!$CorrectGuess) { fwrite(STDOUT, "You have guessed an incorrect letter.\n"); $NumberOfLives=$NumberOfLives-1; } else { fwrite(STDOUT, "The setter has not specified the word/phrase\n"); } } } } Quote Link to comment https://forums.phpfreaks.com/topic/156460-program-doesnt-work-please-help-hangman/#findComment-823833 Share on other sites More sharing options...
premiso Posted May 1, 2009 Share Posted May 1, 2009 You should really use GD Library and make it actually draw the hang man and write in the letters. That would be a hoss script. Seriously. Quote Link to comment https://forums.phpfreaks.com/topic/156460-program-doesnt-work-please-help-hangman/#findComment-823834 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.