Permalink
Browse files

Minor optimisations and variable name changes

Fixes #34, fixes #35, fixes #36, fixes #37, fixes #38, fixes #39, fixes
#40
  • Loading branch information...
DarthJDG committed Aug 29, 2016
1 parent 4c7a3aa commit 4d7fe064af0a96a2d2f330aace9cbce512b674d7
Showing with 72 additions and 88 deletions.
  1. +2 −1 NumGuess.hx
  2. +3 −3 numguess.lua
  3. +12 −15 numguess.php
  4. +40 −48 numguess.pl
  5. +3 −3 numguess.py
  6. +11 −17 numguess.swift
  7. +1 −1 numguess.uvb
View
@@ -53,6 +53,8 @@ class NumGuess {
limit = 10;
}
+ var maxTries = 1 + Math.floor(Math.log(limit) / Math.log(2));
+
while (true) {
var tries: Int = 0;
var number: Int = 1 + Std.random(limit);
@@ -85,7 +87,6 @@ class NumGuess {
output.writeString("\nWell done " + name + ", you guessed my number from "
+ tries + (tries > 1 ? " tries" : " try") + "!\n");
- var maxTries = 1 + Math.floor(Math.log(limit) / Math.log(2));
if (tries == 1) {
output.writeString("You're one lucky bastard!\n");
} else if (tries < maxTries) {
View
@@ -58,12 +58,12 @@ while true do
end
end
- tries_declination = "try"
+ tries_word = "try"
if tries > 1 then
- tries_declination = "tries"
+ tries_word = "tries"
end
- print("\nWell done " .. name .. ", you guessed my number from " .. tries .. " " .. tries_declination .. "!")
+ print("\nWell done " .. name .. ", you guessed my number from " .. tries .. " " .. tries_word .. "!")
custom_message = ""
if tries == 1 then
View
@@ -15,6 +15,8 @@
$limit = 10;
}
+$maxTries = floor(log($limit, 2)) + 1;
+
do {
$number = rand(1, $limit);
echo "\nGuess my number between 1 and $limit!\n\n";
@@ -45,11 +47,17 @@
}
} while (!$success);
- $tryNoun = $tries > 1 ? 'tries' : 'try';
- echo "\nWell done $name, you guessed my number from $tries $tryNoun!\n";
- echo evaluate($tries, $limit) . "\n";
+ $triesWord = $tries > 1 ? 'tries' : 'try';
+ echo "\nWell done $name, you guessed my number from $tries $triesWord!\n";
+
+ if ($tries == 1) { echo "You're one lucky bastard!"; }
+ else if ($tries < $maxTries) { echo "You are the master of this game!"; }
+ else if ($tries == $maxTries) { echo "You are a machine!"; }
+ else if ($tries <= $maxTries * 1.1) { echo "Very good result!"; }
+ else if ($tries <= $limit) { echo "Try harder, you can do better!"; }
+ else { echo "I find your lack of skill disturbing!"; }
- echo 'Play again [y/N]? ';
+ echo "\nPlay again [y/N]? ";
} while (in_array(readInput(), array('Y', 'y')));
echo "\nOkay, bye.\n";
@@ -58,15 +66,4 @@ function readInput() {
return trim(fgets(STDIN));
}
-function evaluate($tries, $limit) {
- $maxTries = floor(log($limit, 2)) + 1;
- if ($tries == 1) { $s = "You're one lucky bastard!"; }
- else if ($tries < $maxTries) { $s = "You are the master of this game!"; }
- else if ($tries == $maxTries) { $s = "You are a machine!"; }
- else if ($tries <= $maxTries * 1.1) { $s = "Very good result!"; }
- else if ($tries <= $limit) { $s = "Try harder, you can do better!"; }
- else { $s = "I find your lack of skill disturbing!"; }
- return $s;
-}
-
?>
View
@@ -6,10 +6,41 @@
use Scalar::Util qw(looks_like_number);
-# a single game
-sub play($$) {
- my($name, $limit) = @_;
+# welcome message
+print("Welcome to NumGuess Perl version!\n\n");
+# input name
+my $name;
+print("Enter your name: ");
+$name = <STDIN>;
+chomp($name);
+
+# use default player name if none given
+if ($name eq "") {
+ $name = "Player";
+}
+
+# input upper limit (until suitable)
+my $limit = 0;
+print("\nWelcome $name, enter upper limit: ");
+$limit = <STDIN>;
+chomp($limit);
+if (not looks_like_number($limit)) {
+ $limit = 0;
+}
+
+# use default limit if input not suitable
+if ($limit < 10) {
+ $limit = 10;
+}
+
+# maximum number of guesses
+my $maxTries = floor(log($limit) / log(2)) + 1;
+
+# play a game (while other than N is given as answer)
+my $again;
+do {
+ # play the game
# generate the random number
my $number = int(rand($limit)) + 1;
@@ -51,65 +82,26 @@ ($$)
my $tries_word = ($tries == 1) ? "try" : "tries";
print("\nWell done $name, you guessed my number from $tries $tries_word!\n");
- # maximum number of guesses
- my $maximum = floor(log($limit) / log(2)) + 1;
-
# custom message
if ($tries == 1) {
print("You're one lucky bastard!\n");
- } elsif ($tries < $maximum) {
+ } elsif ($tries < $maxTries) {
print("You are the master of this game!\n");
- } elsif ($tries == $maximum) {
+ } elsif ($tries == $maxTries) {
print("You are a machine!\n");
- } elsif ($tries <= ($maximum * 1.1)) {
+ } elsif ($tries <= ($maxTries * 1.1)) {
print("Very good result!\n");
} elsif ($tries <= $limit) {
print("Try harder, you can do better!\n");
} else {
print("I find your lack of skill disturbing!\n");
}
-}
-
-
-# welcome message
-print("Welcome to NumGuess Perl version!\n\n");
-
-# input name
-my $name;
-print("Enter your name: ");
-$name = <STDIN>;
-chomp($name);
-
-# use default player name if none given
-if ($name eq "") {
- $name = "Player";
-}
-
-# input upper limit (until suitable)
-my $limit = 0;
-print("\nWelcome $name, enter upper limit: ");
-$limit = <STDIN>;
-chomp($limit);
-if (not looks_like_number($limit)) {
- $limit = 0;
-}
-
-# use default limit if input not suitable
-if ($limit < 10) {
- $limit = 10;
-}
-
-# play a game (while other than N is given as answer)
-my $play;
-do {
- # play the game
- play($name, $limit);
# ask whether to play again
print("Play again [y/N]? ");
- $play = <STDIN>;
- chomp($play);
-} while (lc($play) eq "y");
+ $again = <STDIN>;
+ chomp($again);
+} while (lc($again) eq "y");
# say goodbye
print("\nOkay, bye.\n");
View
@@ -50,10 +50,10 @@
except:
print("\bThat's just plain wrong.")
- tries_declination = "try"
+ tries_word = "try"
if tries > 1:
- tries_declination = "tries"
- print("\nWell done " + name + ", you guessed my number from " + str(tries) + " " + tries_declination + "!")
+ tries_word = "tries"
+ print("\nWell done " + name + ", you guessed my number from " + str(tries) + " " + tries_word + "!")
custom_message = ""
View
@@ -19,27 +19,14 @@ func inputNumber(prompt: String, def: Int? = nil, min: Int? = nil) -> Int? {
return value
}
-// Returns a message to evaluate player performance
-func getCustomMessage(num: Int, _ limit: Int, _ tries: Int) -> String {
- let maxTries = Int(log2(Double(limit))) + 1
- let maxTries10 = Int(Double(maxTries) * 1.1)
-
- switch tries {
- case 1: return "You're one lucky bastard!"
- case 2..<maxTries: return "You are the master of this game!"
- case maxTries: return "You are a machine!"
- case maxTries...maxTries10: return "Very good result!"
- case maxTries10...limit: return "Try harder, you can do better!"
- default: return "I find your lack of skill disturbing!"
- }
-}
-
// Seed the random generator
srandom(UInt32(time(nil)))
print("Welcome to NumGuess Swift version!\n")
var name = inputString("Enter your name:", def: "Player")
var limit = inputNumber("\nWelcome \(name), enter upper limit:", def: 10, min: 10)!
+let maxTries = Int(log2(Double(limit))) + 1
+let maxTries10 = Int(Double(maxTries) * 1.1)
repeat {
print("\nGuess my number between 1 and \(limit)!\n")
@@ -64,9 +51,16 @@ repeat {
let triesWord = tries == 1 ? "try" : "tries"
print("\nWell done \(name), you guessed my number from \(tries) \(triesWord)!")
- print(getCustomMessage(num, limit, tries))
+
+ switch tries {
+ case 1: print("You're one lucky bastard!")
+ case 2..<maxTries: print("You are the master of this game!")
+ case maxTries: print("You are a machine!")
+ case maxTries...maxTries10: print("Very good result!")
+ case maxTries10...limit: print("Try harder, you can do better!")
+ default: print("I find your lack of skill disturbing!")
+ }
} while "Y" == inputString("Play again [y/N]?").uppercaseString
print("\nOkay, bye.")
-
View
@@ -16,6 +16,7 @@ CRT "Welcome ":NAME:", enter upper limit: ":
INPUT LIMIT
IF NOT(NUM(LIMIT)) OR LIMIT < 10 THEN LIMIT = 10
+MAX.TRIES = INT(LN(LIMIT) / LN(2)) + 1
LOOP
NUM = RND(LIMIT) + 1
@@ -52,7 +53,6 @@ LOOP
CRT
CRT "Well done ":NAME:", you guessed my number from ":TRIES:" ":TRIES.WORD:"!"
- MAX.TRIES = INT(LN(LIMIT) / LN(2)) + 1
BEGIN CASE
CASE TRIES = 1
CRT "You're one lucky bastard!"

0 comments on commit 4d7fe06

Please sign in to comment.