Created
March 6, 2013 02:10
-
-
Save seyDoggy/5096180 to your computer and use it in GitHub Desktop.
Guess the Number game.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php -q | |
<?php | |
/* | |
Problem Solving and Programming Concepts | |
Adam Merrifield (2697795). | |
Guess the Numbers | |
NOTE: I'm not much for this pseudo code so I chose to write it as a | |
real program first and then convert it to pseudo code. | |
PSEUDO CODE | |
start | |
DECLARATIONS | |
randomNumber = rand(1, 1000) | |
msgGuess = "Guess a number between 1 and 1000: " | |
msgPlay = "Would you like to play again? (Y or N): " | |
userInput | |
userChoice | |
succeed = 0 | |
playAgain = "Y" | |
while (playAgain == "Y") | |
while (succeed == 0) { | |
userInput = prompt(msgGuess) | |
succeed = compareNums(userInput, randomNumber) | |
endwhile | |
userChoice = prompt(msgPlay) | |
playAgain = newGame(userChoice) | |
endwhile | |
stop | |
prompt(message = null) { | |
output message | |
input = STDIN | |
return | |
compareNums(input, answer) | |
if input is a number | |
if input == answer | |
output "You guessed the number!" | |
succeed = 1 | |
else | |
if input > answer | |
output "Too high. Try again." | |
else | |
output "Too low. Try again." | |
endif | |
succeed = 0 | |
endif | |
else | |
output "==> input ...is not a number. Try again." | |
succeed = 0 | |
endif | |
return | |
newGame(input) | |
if input == "Y" | |
playAgain == "Y" | |
else | |
output "Thanks for playing!" | |
playAgain == "N" | |
endif | |
return | |
*/ | |
/** | |
* Actual running program | |
*/ | |
$randomNumber = rand(1, 1000); | |
$msgGuess = "Guess a number between 1 and 1000: "; | |
$msgPlay = "Would you like to play again? (Y or N): "; | |
$userInput; | |
$userChoice; | |
$succeed = 0; | |
$playAgain = "Y"; | |
while ($playAgain == "Y") { | |
while ($succeed == 0) { | |
$userInput = prompt($msgGuess); | |
$succeed = compareNums($userInput, $randomNumber); | |
} | |
$userChoice = prompt($msgPlay); | |
$playAgain = newGame($userChoice); | |
} | |
function prompt($message = null) { | |
fwrite(STDOUT, $message); | |
$input = trim(fgets(STDIN)); | |
return $input; | |
} | |
function compareNums($input, $answer) | |
{ | |
if (ctype_digit($input)) { | |
if ($input == $answer) { | |
echo "You guessed the number!\n"; | |
$succeed = 1; | |
} else { | |
if ($input > $answer) { | |
echo "Too high. Try again.\n"; | |
} else { | |
echo "Too low. Try again.\n"; | |
} | |
$succeed = 0; | |
} | |
} else { | |
echo "\n==> $input ...is not a number. Try again.\n"; | |
$succeed = 0; | |
} | |
return $succeed; | |
} | |
function newGame($input) | |
{ | |
if ($input == "Y" || $input == "y") { | |
$playAgain = "Y"; | |
} else { | |
$playAgain = "N"; | |
echo "Thanks for playing!"; | |
} | |
return $playAgain; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment