needhelp :( Posted April 8, 2009 Share Posted April 8, 2009 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] Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 8, 2009 Share Posted April 8, 2009 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 Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 8, 2009 Share Posted April 8, 2009 Just notied you're using STDIN and STDOUT for in/output streams - where exactly are you developing this? Are you compiling it to run directly under Windows? Quote Link to comment Share on other sites More sharing options...
needhelp :( Posted April 8, 2009 Author Share Posted April 8, 2009 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 Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 8, 2009 Share Posted April 8, 2009 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. Quote Link to comment Share on other sites More sharing options...
needhelp :( Posted April 8, 2009 Author Share Posted April 8, 2009 i'm using a program called eclipse which php can be written on..ok wel thanks anyway Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 8, 2009 Share Posted April 8, 2009 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. Quote Link to comment Share on other sites More sharing options...
needhelp :( Posted April 8, 2009 Author Share Posted April 8, 2009 ok..wel thanks anyway.. if anyone does know how to do any of those in the way my php is written then please help..i reali need it thanks.. Quote Link to comment Share on other sites More sharing options...
trq Posted April 8, 2009 Share Posted April 8, 2009 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. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 8, 2009 Share Posted April 8, 2009 !!! I've never seen PHP using STDIN and STDOUT except for when being compiled into an executable for Windows directly! Quote Link to comment Share on other sites More sharing options...
trq Posted April 8, 2009 Share Posted April 8, 2009 !!! I've never seen PHP using STDIN and STDOUT except for when being compiled into an executable for Windows directly! PHP is not compiled, so your still making little sense. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 8, 2009 Share Posted April 8, 2009 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 Quote Link to comment Share on other sites More sharing options...
trq Posted April 8, 2009 Share Posted April 8, 2009 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. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 8, 2009 Share Posted April 8, 2009 Not using the native php binaries. That's just what I was getting at. Quote Link to comment 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.