Permalink
Browse files

Fix custom message conditions and order

Some conditions were 1 out (< vs <=). They're now all checked in logical
order as well, makes the code easier to read.
  • Loading branch information...
DarthJDG committed Jul 28, 2016
1 parent f8da882 commit 1b9ce74e6e174dbd8b28abc8fb7fb8639fd7d9b2
Showing with 93 additions and 93 deletions.
  1. +20 −20 NumGuess.hx
  2. +3 −3 NumGuess.j
  3. +3 −3 NumGuess.java
  4. +3 −3 num_c64.bas
  5. +2 −2 num_oop.pas
  6. +2 −2 numguess.R
  7. +3 −3 numguess.awk
  8. +3 −3 numguess.bas
  9. +4 −4 numguess.bat
  10. +3 −3 numguess.cr
  11. +2 −2 numguess.d
  12. +2 −2 numguess.dart
  13. +3 −3 numguess.go
  14. +6 −6 numguess.hs
  15. +5 −5 numguess.lua
  16. +2 −2 numguess.pas
  17. +4 −4 numguess.php
  18. +1 −1 numguess.pl
  19. +3 −3 numguess.prg
  20. +2 −2 numguess.ps1
  21. +5 −5 numguess.py
  22. +3 −3 numguess.rb
  23. +3 −3 numguess.sh
  24. +1 −1 numguess.slsh
  25. +2 −2 numguess.swift
  26. +3 −3 numguess.tcl
View
@@ -1,12 +1,12 @@
/**
* NumGuess Haxe version.
- *
- *
+ *
+ *
* It can be most easily run with the Neko runtime (ships with Haxe).
- *
+ *
* To compile:
* haxe -main NumGuess -neko NumGuess.n
- *
+ *
* To run:
* neko NumGuess.n
*/
@@ -15,19 +15,19 @@ import haxe.io.Input;
import haxe.io.Output;
class NumGuess {
-
+
private var input: Input;
-
+
private var output: Output;
-
+
/**
* Program line entry point (run in static context).
*/
public static function main(): Void {
var instance: NumGuess = new NumGuess(Sys.stdin(), Sys.stdout());
instance.playGame();
}
-
+
/**
* Constructor, instantiated an object.
* @param input The input to read from.
@@ -37,30 +37,30 @@ class NumGuess {
this.input = input;
this.output = output;
}
-
+
public function playGame(): Void {
output.writeString("Welcome to NumGuess Haxe version!\n\n");
output.writeString("Enter your name: ");
-
+
var name: String = input.readLine();
if (name == "")
name = "Player";
output.writeString("\nWelcome " + name + ", enter upper limit: ");
-
+
var limit: Int = Std.parseInt(input.readLine());
if (limit == null || limit < 10) {
limit = 10;
}
-
+
while (true) {
var tries: Int = 0;
var number: Int = 1 + Std.random(limit);
output.writeString("\nGuess my number between 1 and " + limit + "!\n\n");
while (true) {
output.writeString("Guess: ");
-
+
var guess: Int = Std.parseInt(input.readLine());
if (guess == null) {
output.writeString("That's just plain wrong.\n");
@@ -70,9 +70,9 @@ class NumGuess {
output.writeString("Out of range.\n");
continue;
}
-
+
tries++;
-
+
if (guess < number) {
output.writeString("Too low!\n");
} else if (guess > number) {
@@ -81,10 +81,10 @@ class NumGuess {
break;
}
}
-
+
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");
@@ -99,14 +99,14 @@ class NumGuess {
} else {
output.writeString("I find your lack of skill disturbing!\n");
}
-
+
output.writeString("Play again [y/N]? ");
var again: String = input.readLine();
if (again != 'y' && again != 'Y')
break;
}
-
+
output.writeString("\nOkay, bye.");
}
-
+
}
View
@@ -71,7 +71,7 @@ NameOK:
; settings summary
-
+
ldc "\nWelcome "
invokestatic NumGuess/printString(Ljava/lang/String;)V
@@ -87,7 +87,7 @@ NameOK:
if_icmpge LimitOK
; use 10 if input is smaller
-
+
pop
ldc 10
@@ -289,7 +289,7 @@ CheckIfNot:
QuitGame:
ldc "\nOkay, bye.\n"
invokestatic NumGuess/printString(Ljava/lang/String;)V
-
+
return
.end method
View
@@ -73,10 +73,10 @@ public static void main(String[] args) {
System.out.printf("You are a machine!");
} else if (tries <= max_tries * 1.1f) {
System.out.printf("Very good result!");
- } else if (tries > limit) {
- System.out.printf("I find your lack of skill disturbing!");
- } else {
+ } else if (tries <= limit) {
System.out.printf("Try harder, you can do better!");
+ } else {
+ System.out.printf("I find your lack of skill disturbing!");
}
System.out.printf("\nPlay again [y/N]? ");
View
@@ -20,9 +20,9 @@
190 IF TRIES = 1 THEN GOTO 300
200 IF TRIES < MAXTRIES THEN GOTO 310
210 IF TRIES = MAXTRIES THEN GOTO 320
-220 IF TRIES < MAXTRIES * 1.1 THEN GOTO 330
-230 IF TRIES > LIMIT THEN GOTO 350
-240 GOTO 340
+220 IF TRIES <= MAXTRIES * 1.1 THEN GOTO 330
+230 IF TRIES <= LIMIT THEN GOTO 340
+240 GOTO 350
300 PRINT "YOU'RE ONE LUCKY BASTARD!" : GOTO 400
310 PRINT "YOU ARE THE MASTER OF THIS GAME!" : GOTO 400
320 PRINT "YOU ARE A MACHINE!" : GOTO 400
View
@@ -90,8 +90,8 @@ function NumGuess.CustomMessage : String;
else if tries < max_tries then CustomMessage := 'You are the master of this game!'
else if tries = max_tries then CustomMessage := 'You are a machine!'
else if tries <= max_tries * 1.1 then CustomMessage := 'Very good result!'
- else if tries > limit then CustomMessage := 'I find your lack of skill disturbing!'
- else CustomMessage := 'Try harder, you can do better!';
+ else if tries <= limit then CustomMessage := 'Try harder, you can do better!'
+ else CustomMessage := 'I find your lack of skill disturbing!';
end;
{ Main program }
View
@@ -95,10 +95,10 @@ while (TRUE) {
tries == max_tries,
'You are a machine!',
ifelse(
- tries < max_tries * 1.1,
+ tries <= max_tries * 1.1,
'Very good result!',
ifelse(
- tries < limit,
+ tries <= limit,
'Try harder, you can do better!',
'I find your lack of skill disturbing!'
)
View
@@ -71,10 +71,10 @@ function well_done() {
print "You are a machine!"
else if(tries <= max_tries * 1.1)
print "Very good result!"
- else if(tries > limit)
- print "I find your lack of skill disturbing!"
- else
+ else if(tries <= limit)
print "Try harder, you can do better!"
+ else
+ print "I find your lack of skill disturbing!"
printf "Play again [y/N]? "
prompt = "play"
View
@@ -37,10 +37,10 @@ DO
PRINT "You are a machine!"
CASE IS <= maxtries% * 1.1
PRINT "Very good result!"
- CASE IS > limit%
- PRINT "I find your lack of skill disturbing!"
- CASE ELSE
+ CASE IS <= limit%
PRINT "Try harder, you can do better!"
+ CASE ELSE
+ PRINT "I find your lack of skill disturbing!"
END SELECT
INPUT "Play again [y/N]? ", again$
View
@@ -74,9 +74,9 @@ if %ng_tries% == 1 goto msg_lucky
if %ng_tries% lss %ng_max_tries% goto msg_master
if %ng_tries% == %ng_max_tries% goto msg_machine
if %ng_tries% leq %ng_max_tries_10% goto msg_good
-if %ng_tries% geq %ng_limit% goto msg_disturbing
+if %ng_tries% leq %ng_limit% goto msg_harder
-echo Try harder, you can do better!
+echo I find your lack of skill disturbing!
goto msg_end
:msg_lucky
@@ -95,8 +95,8 @@ goto msg_end
echo Very good result!
goto msg_end
-:msg_disturbing
-echo I find your lack of skill disturbing!
+:msg_harder
+echo Try harder, you can do better!
:msg_end
View
@@ -49,12 +49,12 @@ loop do
puts "\nWell done #{name}, you guessed my number from #{tries} tr" + (tries == 1 ? "y!" : "ies!")
- msg = "Try harder, you can do better!"
+ msg = "I find your lack of skill disturbing!"
+ msg = "Try harder, you can do better!" if tries <= limit
msg = "Very good result!" if tries <= maxTries * 1.1
+ msg = "You are a machine!" if tries == maxTries
msg = "You are the master of this game!" if tries < maxTries
msg = "You're one lucky bastard!" if tries == 1
- msg = "You are a machine!" if tries == maxTries
- msg = "I find your lack of skill disturbing!" if tries > limit
puts msg
View
@@ -50,8 +50,8 @@ void main() {
else if(tries < maxTries) writeln("You are the master of this game!");
else if(tries == maxTries) writeln("You are a machine!");
else if(tries <= maxTries * 1.1) writeln("Very good result!");
- else if(tries > limit) writeln("I find your lack of skill disturbing!");
- else writeln("Try harder, you can do better!");
+ else if(tries <= limit) writeln("Try harder, you can do better!");
+ else writeln("I find your lack of skill disturbing!");
write("Play again [y/N]? ");
View
@@ -82,8 +82,8 @@ class NumGuess {
else if(tries < max_tries) stdout.writeln('You are the master of this game!');
else if(tries == max_tries) stdout.writeln('You are a machine!');
else if(tries <= max_tries * 1.1) stdout.writeln('Very good result!');
- else if(tries > limit) stdout.writeln('I find your lack of skill disturbing!');
- else stdout.writeln('Try harder, you can do better!');
+ else if(tries <= limit) stdout.writeln('Try harder, you can do better!');
+ else stdout.writeln('I find your lack of skill disturbing!');
stdout.write('Play again [y/N]? ');
state = 'play';
View
@@ -61,10 +61,10 @@ func main() {
fmt.Printf("You are a machine!")
case tries <= int(float64(maxTries) * 1.1):
fmt.Printf("Very good result!")
- case tries > limit:
- fmt.Printf("I find your lack of skill disturbing!")
- default:
+ case tries <= limit:
fmt.Printf("Try harder, you can do better!")
+ default:
+ fmt.Printf("I find your lack of skill disturbing!")
}
fmt.Printf("\nPlay again [y/N]? ")
View
@@ -22,12 +22,12 @@ evaluate (guess, valid) num limit
customMessage :: Int -> Int -> String
customMessage tries limit
- | tries == 1 = "You're one lucky bastard!"
- | tries < maxTries = "You are the master of this game!"
- | tries == maxTries = "You are a machine!"
- | tries < maxTries10 = "Very good result!"
- | tries > limit = "I find your lack of skill disturbing!"
- | otherwise = "Try harder, you can do better!"
+ | tries == 1 = "You're one lucky bastard!"
+ | tries < maxTries = "You are the master of this game!"
+ | tries == maxTries = "You are a machine!"
+ | tries <= maxTries10 = "Very good result!"
+ | tries <= limit = "Try harder, you can do better!"
+ | otherwise = "I find your lack of skill disturbing!"
where maxTries = floor (logBase 2 (fromIntegral limit)) + 1
maxTries10 = floor (fromIntegral maxTries * 1.1)
View
@@ -68,16 +68,16 @@ while true do
custom_message = ""
if tries == 1 then
custom_message = "You're one lucky bastard!"
+ elseif tries < max_tries then
+ custom_message = "You are the master of this game!"
elseif tries == max_tries then
custom_message = "You are a machine!"
- elseif tries > max_tries and tries <= max_tries * 1.1 then
+ elseif tries <= max_tries * 1.1 then
custom_message = "Very good result!"
- elseif tries > max_tries * 1.1 and tries <= limit then
+ elseif tries <= limit then
custom_message = "Try harder, you can do better!"
- elseif tries > limit then
- custom_message = "I find your lack of skill disturbing!"
else
- custom_message = "You are the master of this game!"
+ custom_message = "I find your lack of skill disturbing!"
end
print(custom_message)
View
@@ -45,8 +45,8 @@
else if tries < max_tries then WriteLn('You are the master of this game!')
else if tries = max_tries then WriteLn('You are a machine!')
else if tries <= max_tries * 1.1 then WriteLn('Very good result!')
- else if tries > limit then WriteLn('I find your lack of skill disturbing!')
- else WriteLn('Try harder, you can do better!');
+ else if tries <= limit then WriteLn('Try harder, you can do better!')
+ else WriteLn('I find your lack of skill disturbing!');
Write('Play again [y/N]? ');
ReadLn(again);
View
@@ -59,11 +59,11 @@ function readInput() {
}
function evaluate($tries, $limit) {
- $maxJustified = floor(log($limit, 2)) + 1;
+ $maxTries = floor(log($limit, 2)) + 1;
if ($tries == 1) { $s = "You're one lucky bastard!"; }
- else if ($tries < $maxJustified) { $s = "You are the master of this game!"; }
- else if ($tries == $maxJustified) { $s = "You are a machine!"; }
- else if ($tries <= $maxJustified * 1.1) { $s = "Very good result!"; }
+ 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
@@ -63,7 +63,7 @@ ($$)
print("You are a machine!\n");
} elsif ($tries <= ($maximum * 1.1)) {
print("Very good result!\n");
- } elsif ($tries < $limit) {
+ } elsif ($tries <= $limit) {
print("Try harder, you can do better!\n");
} else {
print("I find your lack of skill disturbing!\n");
View
@@ -57,10 +57,10 @@ do while .t.
? "You are a machine!"
case tries <= maxtries * 1.1
? "Very good result!"
- case tries > limit
- ? "I find your lack of skill disturbing!"
- otherwise
+ case tries <= limit
? "Try harder, you can do better!"
+ otherwise
+ ? "I find your lack of skill disturbing!"
endcase
accept "Play again [y/N]? " to again
Oops, something went wrong.

0 comments on commit 1b9ce74

Please sign in to comment.