Jump to content

PHP Hangman


needhelp :(

Recommended Posts

I have a piece of PHP which i have to edit and i am really stuck :(

 

(file is attatched)

 

I need to be able to:

 

GET RANDOM PHRASE FROM FILE

WRITE A RANDOM PHRASE TO A FILE

CHECK THE FILE EXISTS

CHECK THE FILE HAS WORDS IN

COUNT HOW MANY GUESSES AND STOP WHEN MAX HAS REACHED

ALLOW THE USER TO SET MAXIMUM GUESSES

GUESS A WHOLE WORD

SHOW WHAT LETTERS HAVE BEEN TRIED

CHECK IF A GUESS HAS ALREADY BEEN TRIED

DRAW A LITTLE HANGMAN!

 

(I have done the first three..i think)..but i need help on doing the rest..

 

if someone could help me it would be very much appreaciated :)

 

thankyou x

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

Welcome!!!

 

We can't write code for you - some of us (including me) are actually sat at work behind a desk and don't have a great deal of time on each post.

 

Instead of adding your code as an attachment you can post it directly but if you do, please remember to surround it with [code] and [/code] tags so it appears like this:

<?php
echo 'doing it this way preserves formatting';
?>
and HTML stands out as it isn't colored

Link to comment
Share on other sites

Hi..im sorry i didn't realise but i am actually really stuck :|

 

erm..yh its being run on windows and heres a copy of the code..

 

<?php

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. SETTER - Pick a random word\n");
fwrite(STDOUT, "\n");
fwrite(STDOUT, "4. SETTER - Add random phrase to file\n");
fwrite(STDOUT, "\n");
fwrite(STDOUT, "5. End\n");
fwrite(STDOUT, "\n");
}

function GetNewPhrase() {
$PhraseOK = false;
do {
	fwrite(STDOUT, "Key in the new word ...(letters and any Spaces)");
	$ThisNewPhrase = 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 PickWordFromFile ($FileNameIn){
$lines = file ($FileNameIn);
return $lines [rand (0, count($lines)-1)];

}

function writewordtofile ($FileNameIn,$newphrases){
	$myphrases = fopen($FileNameIn, 'a+');
	fwrite($myphrases, "\r\n$newphrases");
	fclose ($myphrases);

}

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


// Main program block starts here
$PhraseHasBeenSet = false;
$Choice = 0;
global $NewPhrase;
$GuessStatusArray = array();
$LettersGuessedArray = array();
global $NextGuessedLetter;
$Position = 0;
$WordGuessed = false;

do {
DisplayMenu();
fwrite(STDOUT, "Choice? ");
$Choice = intval(trim(fgets(STDIN)));
if ($Choice == 1) {
	$NewPhrase = GetNewPhrase();
	SetUpGuessStatusArray($NewPhrase);
	$PhraseHasBeenSet = true;
}
if ($Choice == 2) {
	if ($PhraseHasBeenSet) {
		DisplayCurrentStatus(strlen($NewPhrase));
		$NextGuessedLetter = GetNextLetterGuess();
		for ($Position = 1; $Position <= strlen($NewPhrase); $Position++) 
		{
			if ($NextGuessedLetter == $NewPhrase[$Position - 1])
				$GuessStatusArray[$Position] = $NextGuessedLetter;
		}
		DisplayCurrentStatus(strlen($NewPhrase));
		$PhraseGuessed = AllLettersGuessedCorrectly($NewPhrase);
		if ($PhraseGuessed) 
			fwrite(STDOUT, "You have guessed correctly\n");
	}
	else
		fwrite(STDOUT, "The setter has not specified the word/phrase\n");

}

	if ($Choice == 3){
	$NewPhrase = PickWordFromFile ('myphrases.txt');
	SetUpGuessStatusArray($NewPhrase);
	$PhraseHasBeenSet = true;
}

if ($Choice == 4)
	fwrite (STDOUT, 'Please write a new phrase: ');
	$inputword = trim(fgets(STDIN));
	writewordtofile ('myphrases.txt',$inputword);
	fgets(STDIN);


if ($Choice == 5 && !$PhraseGuessed) {
	fwrite(STDOUT, "You have not completed this word/phrase ..Press Return to exit\n");
	fgets(STDIN);
}
} while ($Choice != 5);

?>


 

sorry if im being really awkward :(

Link to comment
Share on other sites

I don't think anyone actually develops PHP directly for Windows here - I may be wrong but I've not seen anyone using it that way before.

 

The best thing I suggest is take a look at developing it for the internet instead or take a look at using a different language. Compiling PHP for Windows requires a vast change in code which takes it away from standard PHP and almost into a new language.

 

If you're just playing with this for fun then there are many variations of BASIC out there that would be much better suited for this.

 

If PHP is something you want to continue with then you'll be better off installing a development centre like XAMP, WAMP or LAMP (preferably one of the first two) or getting some web space and taking it from there.

Link to comment
Share on other sites

I'm using Eclipse as well but everything I (and I believe I can say safely here - everyone else) writes for the web.

 

I hope you continue to persue PHP for the web - it's greatly satisfying writing apps that anyone can use almost instantly and this is a great community for help 24/7.

Link to comment
Share on other sites

The best thing I suggest is take a look at developing it for the internet instead or take a look at using a different language. Compiling PHP for Windows requires a vast change in code which takes it away from standard PHP and almost into a new language.

 

Writting to STDOUT is exactly the same as using echo so I'm not exactly sure what your talking about.

 

To the op. You need to be allot more specific with your actual problem. Like Yesideez said, were not here to write code for people so providing us a feature list and simply stating you've implimented a few but need help with the rest doesn't help. Narrow your code and problems down into small parts and post them.

Link to comment
Share on other sites

You CAN compile PHP - I've had a compiler installed on my machine but removed it due to lack of compatibility with web PHP - I'll dig the link out and PM you the compiler if you want.

 

Sincere apologies to needhelp - I showed your source to my boss and he informed me that you're likely to be running it from a console so please disregard most of what I have said :(

Link to comment
Share on other sites

You CAN compile PHP

 

Not using the native php binaries. I am aware there are custom compilers out there, but they have nothing to do with STDOUT or the op's requests.

 

Still, writting to STDOUT is no different then using echo, I don't see how it was relevent at all to the op's request.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.