Jump to content

Program doesn't work.. please help ; "Hangman"


Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/156460-program-doesnt-work-please-help-hangman/
Share on other sites

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 != ;

?>

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");
		}
	}
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.