Skip to content

Commit

Permalink
Fix custom message conditions and order
Browse files Browse the repository at this point in the history
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 1b9ce74
Show file tree
Hide file tree
Showing 26 changed files with 93 additions and 93 deletions.
40 changes: 20 additions & 20 deletions NumGuess.hx
@@ -1,12 +1,12 @@
/** /**
* NumGuess Haxe version. * NumGuess Haxe version.
* *
* *
* It can be most easily run with the Neko runtime (ships with Haxe). * It can be most easily run with the Neko runtime (ships with Haxe).
* *
* To compile: * To compile:
* haxe -main NumGuess -neko NumGuess.n * haxe -main NumGuess -neko NumGuess.n
* *
* To run: * To run:
* neko NumGuess.n * neko NumGuess.n
*/ */
Expand All @@ -15,19 +15,19 @@ import haxe.io.Input;
import haxe.io.Output; import haxe.io.Output;


class NumGuess { class NumGuess {

private var input: Input; private var input: Input;

private var output: Output; private var output: Output;

/** /**
* Program line entry point (run in static context). * Program line entry point (run in static context).
*/ */
public static function main(): Void { public static function main(): Void {
var instance: NumGuess = new NumGuess(Sys.stdin(), Sys.stdout()); var instance: NumGuess = new NumGuess(Sys.stdin(), Sys.stdout());
instance.playGame(); instance.playGame();
} }

/** /**
* Constructor, instantiated an object. * Constructor, instantiated an object.
* @param input The input to read from. * @param input The input to read from.
Expand All @@ -37,30 +37,30 @@ class NumGuess {
this.input = input; this.input = input;
this.output = output; this.output = output;
} }

public function playGame(): Void { public function playGame(): Void {
output.writeString("Welcome to NumGuess Haxe version!\n\n"); output.writeString("Welcome to NumGuess Haxe version!\n\n");
output.writeString("Enter your name: "); output.writeString("Enter your name: ");

var name: String = input.readLine(); var name: String = input.readLine();
if (name == "") if (name == "")
name = "Player"; name = "Player";


output.writeString("\nWelcome " + name + ", enter upper limit: "); output.writeString("\nWelcome " + name + ", enter upper limit: ");

var limit: Int = Std.parseInt(input.readLine()); var limit: Int = Std.parseInt(input.readLine());
if (limit == null || limit < 10) { if (limit == null || limit < 10) {
limit = 10; limit = 10;
} }

while (true) { while (true) {
var tries: Int = 0; var tries: Int = 0;
var number: Int = 1 + Std.random(limit); var number: Int = 1 + Std.random(limit);
output.writeString("\nGuess my number between 1 and " + limit + "!\n\n"); output.writeString("\nGuess my number between 1 and " + limit + "!\n\n");


while (true) { while (true) {
output.writeString("Guess: "); output.writeString("Guess: ");

var guess: Int = Std.parseInt(input.readLine()); var guess: Int = Std.parseInt(input.readLine());
if (guess == null) { if (guess == null) {
output.writeString("That's just plain wrong.\n"); output.writeString("That's just plain wrong.\n");
Expand All @@ -70,9 +70,9 @@ class NumGuess {
output.writeString("Out of range.\n"); output.writeString("Out of range.\n");
continue; continue;
} }

tries++; tries++;

if (guess < number) { if (guess < number) {
output.writeString("Too low!\n"); output.writeString("Too low!\n");
} else if (guess > number) { } else if (guess > number) {
Expand All @@ -81,10 +81,10 @@ class NumGuess {
break; break;
} }
} }

output.writeString("\nWell done " + name + ", you guessed my number from " output.writeString("\nWell done " + name + ", you guessed my number from "
+ tries + (tries > 1 ? " tries" : " try") + "!\n"); + tries + (tries > 1 ? " tries" : " try") + "!\n");

var maxTries = 1 + Math.floor(Math.log(limit) / Math.log(2)); var maxTries = 1 + Math.floor(Math.log(limit) / Math.log(2));
if (tries == 1) { if (tries == 1) {
output.writeString("You're one lucky bastard!\n"); output.writeString("You're one lucky bastard!\n");
Expand All @@ -99,14 +99,14 @@ class NumGuess {
} else { } else {
output.writeString("I find your lack of skill disturbing!\n"); output.writeString("I find your lack of skill disturbing!\n");
} }

output.writeString("Play again [y/N]? "); output.writeString("Play again [y/N]? ");
var again: String = input.readLine(); var again: String = input.readLine();
if (again != 'y' && again != 'Y') if (again != 'y' && again != 'Y')
break; break;
} }

output.writeString("\nOkay, bye."); output.writeString("\nOkay, bye.");
} }

} }
6 changes: 3 additions & 3 deletions NumGuess.j
Expand Up @@ -71,7 +71,7 @@ NameOK:




; settings summary ; settings summary

ldc "\nWelcome " ldc "\nWelcome "
invokestatic NumGuess/printString(Ljava/lang/String;)V invokestatic NumGuess/printString(Ljava/lang/String;)V


Expand All @@ -87,7 +87,7 @@ NameOK:
if_icmpge LimitOK if_icmpge LimitOK


; use 10 if input is smaller ; use 10 if input is smaller

pop pop
ldc 10 ldc 10


Expand Down Expand Up @@ -289,7 +289,7 @@ CheckIfNot:
QuitGame: QuitGame:
ldc "\nOkay, bye.\n" ldc "\nOkay, bye.\n"
invokestatic NumGuess/printString(Ljava/lang/String;)V invokestatic NumGuess/printString(Ljava/lang/String;)V

return return


.end method .end method
Expand Down
6 changes: 3 additions & 3 deletions NumGuess.java
Expand Up @@ -73,10 +73,10 @@ public static void main(String[] args) {
System.out.printf("You are a machine!"); System.out.printf("You are a machine!");
} else if (tries <= max_tries * 1.1f) { } else if (tries <= max_tries * 1.1f) {
System.out.printf("Very good result!"); System.out.printf("Very good result!");
} else if (tries > limit) { } else if (tries <= limit) {
System.out.printf("I find your lack of skill disturbing!");
} else {
System.out.printf("Try harder, you can do better!"); 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]? "); System.out.printf("\nPlay again [y/N]? ");
Expand Down
6 changes: 3 additions & 3 deletions num_c64.bas
Expand Up @@ -20,9 +20,9 @@
190 IF TRIES = 1 THEN GOTO 300 190 IF TRIES = 1 THEN GOTO 300
200 IF TRIES < MAXTRIES THEN GOTO 310 200 IF TRIES < MAXTRIES THEN GOTO 310
210 IF TRIES = MAXTRIES THEN GOTO 320 210 IF TRIES = MAXTRIES THEN GOTO 320
220 IF TRIES < MAXTRIES * 1.1 THEN GOTO 330 220 IF TRIES <= MAXTRIES * 1.1 THEN GOTO 330
230 IF TRIES > LIMIT THEN GOTO 350 230 IF TRIES <= LIMIT THEN GOTO 340
240 GOTO 340 240 GOTO 350
300 PRINT "YOU'RE ONE LUCKY BASTARD!" : GOTO 400 300 PRINT "YOU'RE ONE LUCKY BASTARD!" : GOTO 400
310 PRINT "YOU ARE THE MASTER OF THIS GAME!" : GOTO 400 310 PRINT "YOU ARE THE MASTER OF THIS GAME!" : GOTO 400
320 PRINT "YOU ARE A MACHINE!" : GOTO 400 320 PRINT "YOU ARE A MACHINE!" : GOTO 400
Expand Down
4 changes: 2 additions & 2 deletions num_oop.pas
Expand Up @@ -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 the master of this game!'
else if tries = max_tries then CustomMessage := 'You are a machine!' 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 <= max_tries * 1.1 then CustomMessage := 'Very good result!'
else if tries > limit then CustomMessage := 'I find your lack of skill disturbing!' else if tries <= limit then CustomMessage := 'Try harder, you can do better!'
else CustomMessage := 'Try harder, you can do better!'; else CustomMessage := 'I find your lack of skill disturbing!';
end; end;


{ Main program } { Main program }
Expand Down
4 changes: 2 additions & 2 deletions numguess.R
Expand Up @@ -95,10 +95,10 @@ while (TRUE) {
tries == max_tries, tries == max_tries,
'You are a machine!', 'You are a machine!',
ifelse( ifelse(
tries < max_tries * 1.1, tries <= max_tries * 1.1,
'Very good result!', 'Very good result!',
ifelse( ifelse(
tries < limit, tries <= limit,
'Try harder, you can do better!', 'Try harder, you can do better!',
'I find your lack of skill disturbing!' 'I find your lack of skill disturbing!'
) )
Expand Down
6 changes: 3 additions & 3 deletions numguess.awk
Expand Up @@ -71,10 +71,10 @@ function well_done() {
print "You are a machine!" print "You are a machine!"
else if(tries <= max_tries * 1.1) else if(tries <= max_tries * 1.1)
print "Very good result!" print "Very good result!"
else if(tries > limit) else if(tries <= limit)
print "I find your lack of skill disturbing!"
else
print "Try harder, you can do better!" print "Try harder, you can do better!"
else
print "I find your lack of skill disturbing!"


printf "Play again [y/N]? " printf "Play again [y/N]? "
prompt = "play" prompt = "play"
Expand Down
6 changes: 3 additions & 3 deletions numguess.bas
Expand Up @@ -37,10 +37,10 @@ DO
PRINT "You are a machine!" PRINT "You are a machine!"
CASE IS <= maxtries% * 1.1 CASE IS <= maxtries% * 1.1
PRINT "Very good result!" PRINT "Very good result!"
CASE IS > limit% CASE IS <= limit%
PRINT "I find your lack of skill disturbing!"
CASE ELSE
PRINT "Try harder, you can do better!" PRINT "Try harder, you can do better!"
CASE ELSE
PRINT "I find your lack of skill disturbing!"
END SELECT END SELECT


INPUT "Play again [y/N]? ", again$ INPUT "Play again [y/N]? ", again$
Expand Down
8 changes: 4 additions & 4 deletions numguess.bat
Expand Up @@ -74,9 +74,9 @@ if %ng_tries% == 1 goto msg_lucky
if %ng_tries% lss %ng_max_tries% goto msg_master if %ng_tries% lss %ng_max_tries% goto msg_master
if %ng_tries% == %ng_max_tries% goto msg_machine if %ng_tries% == %ng_max_tries% goto msg_machine
if %ng_tries% leq %ng_max_tries_10% goto msg_good 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 goto msg_end


:msg_lucky :msg_lucky
Expand All @@ -95,8 +95,8 @@ goto msg_end
echo Very good result! echo Very good result!
goto msg_end goto msg_end


:msg_disturbing :msg_harder
echo I find your lack of skill disturbing! echo Try harder, you can do better!


:msg_end :msg_end


Expand Down
6 changes: 3 additions & 3 deletions numguess.cr
Expand Up @@ -49,12 +49,12 @@ loop do


puts "\nWell done #{name}, you guessed my number from #{tries} tr" + (tries == 1 ? "y!" : "ies!") 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 = "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 are the master of this game!" if tries < maxTries
msg = "You're one lucky bastard!" if tries == 1 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 puts msg


Expand Down
4 changes: 2 additions & 2 deletions numguess.d
Expand Up @@ -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 the master of this game!");
else if(tries == maxTries) writeln("You are a machine!"); else if(tries == maxTries) writeln("You are a machine!");
else if(tries <= maxTries * 1.1) writeln("Very good result!"); else if(tries <= maxTries * 1.1) writeln("Very good result!");
else if(tries > limit) writeln("I find your lack of skill disturbing!"); else if(tries <= limit) writeln("Try harder, you can do better!");
else writeln("Try harder, you can do better!"); else writeln("I find your lack of skill disturbing!");


write("Play again [y/N]? "); write("Play again [y/N]? ");


Expand Down
4 changes: 2 additions & 2 deletions numguess.dart
Expand Up @@ -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 the master of this game!');
else if(tries == max_tries) stdout.writeln('You are a machine!'); 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 <= max_tries * 1.1) stdout.writeln('Very good result!');
else if(tries > limit) stdout.writeln('I find your lack of skill disturbing!'); else if(tries <= limit) stdout.writeln('Try harder, you can do better!');
else stdout.writeln('Try harder, you can do better!'); else stdout.writeln('I find your lack of skill disturbing!');


stdout.write('Play again [y/N]? '); stdout.write('Play again [y/N]? ');
state = 'play'; state = 'play';
Expand Down
6 changes: 3 additions & 3 deletions numguess.go
Expand Up @@ -61,10 +61,10 @@ func main() {
fmt.Printf("You are a machine!") fmt.Printf("You are a machine!")
case tries <= int(float64(maxTries) * 1.1): case tries <= int(float64(maxTries) * 1.1):
fmt.Printf("Very good result!") fmt.Printf("Very good result!")
case tries > limit: case tries <= limit:
fmt.Printf("I find your lack of skill disturbing!")
default:
fmt.Printf("Try harder, you can do better!") fmt.Printf("Try harder, you can do better!")
default:
fmt.Printf("I find your lack of skill disturbing!")
} }


fmt.Printf("\nPlay again [y/N]? ") fmt.Printf("\nPlay again [y/N]? ")
Expand Down
12 changes: 6 additions & 6 deletions numguess.hs
Expand Up @@ -22,12 +22,12 @@ evaluate (guess, valid) num limit


customMessage :: Int -> Int -> String customMessage :: Int -> Int -> String
customMessage tries limit customMessage tries limit
| tries == 1 = "You're one lucky bastard!" | tries == 1 = "You're one lucky bastard!"
| tries < maxTries = "You are the master of this game!" | tries < maxTries = "You are the master of this game!"
| tries == maxTries = "You are a machine!" | tries == maxTries = "You are a machine!"
| tries < maxTries10 = "Very good result!" | tries <= maxTries10 = "Very good result!"
| tries > limit = "I find your lack of skill disturbing!" | tries <= limit = "Try harder, you can do better!"
| otherwise = "Try harder, you can do better!" | otherwise = "I find your lack of skill disturbing!"
where maxTries = floor (logBase 2 (fromIntegral limit)) + 1 where maxTries = floor (logBase 2 (fromIntegral limit)) + 1
maxTries10 = floor (fromIntegral maxTries * 1.1) maxTries10 = floor (fromIntegral maxTries * 1.1)


Expand Down
10 changes: 5 additions & 5 deletions numguess.lua
Expand Up @@ -68,16 +68,16 @@ while true do
custom_message = "" custom_message = ""
if tries == 1 then if tries == 1 then
custom_message = "You're one lucky bastard!" 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 elseif tries == max_tries then
custom_message = "You are a machine!" 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!" 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!" custom_message = "Try harder, you can do better!"
elseif tries > limit then
custom_message = "I find your lack of skill disturbing!"
else else
custom_message = "You are the master of this game!" custom_message = "I find your lack of skill disturbing!"
end end


print(custom_message) print(custom_message)
Expand Down
4 changes: 2 additions & 2 deletions numguess.pas
Expand Up @@ -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 the master of this game!')
else if tries = max_tries then WriteLn('You are a machine!') 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 <= max_tries * 1.1 then WriteLn('Very good result!')
else if tries > limit then WriteLn('I find your lack of skill disturbing!') else if tries <= limit then WriteLn('Try harder, you can do better!')
else WriteLn('Try harder, you can do better!'); else WriteLn('I find your lack of skill disturbing!');


Write('Play again [y/N]? '); Write('Play again [y/N]? ');
ReadLn(again); ReadLn(again);
Expand Down
8 changes: 4 additions & 4 deletions numguess.php
Expand Up @@ -59,11 +59,11 @@ function readInput() {
} }


function evaluate($tries, $limit) { 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!"; } if ($tries == 1) { $s = "You're one lucky bastard!"; }
else if ($tries < $maxJustified) { $s = "You are the master of this game!"; } else if ($tries < $maxTries) { $s = "You are the master of this game!"; }
else if ($tries == $maxJustified) { $s = "You are a machine!"; } else if ($tries == $maxTries) { $s = "You are a machine!"; }
else if ($tries <= $maxJustified * 1.1) { $s = "Very good result!"; } else if ($tries <= $maxTries * 1.1) { $s = "Very good result!"; }
else if ($tries <= $limit) { $s = "Try harder, you can do better!"; } else if ($tries <= $limit) { $s = "Try harder, you can do better!"; }
else { $s = "I find your lack of skill disturbing!"; } else { $s = "I find your lack of skill disturbing!"; }
return $s; return $s;
Expand Down
2 changes: 1 addition & 1 deletion numguess.pl
Expand Up @@ -63,7 +63,7 @@ ($$)
print("You are a machine!\n"); print("You are a machine!\n");
} elsif ($tries <= ($maximum * 1.1)) { } elsif ($tries <= ($maximum * 1.1)) {
print("Very good result!\n"); print("Very good result!\n");
} elsif ($tries < $limit) { } elsif ($tries <= $limit) {
print("Try harder, you can do better!\n"); print("Try harder, you can do better!\n");
} else { } else {
print("I find your lack of skill disturbing!\n"); print("I find your lack of skill disturbing!\n");
Expand Down
6 changes: 3 additions & 3 deletions numguess.prg
Expand Up @@ -57,10 +57,10 @@ do while .t.
? "You are a machine!" ? "You are a machine!"
case tries <= maxtries * 1.1 case tries <= maxtries * 1.1
? "Very good result!" ? "Very good result!"
case tries > limit case tries <= limit
? "I find your lack of skill disturbing!"
otherwise
? "Try harder, you can do better!" ? "Try harder, you can do better!"
otherwise
? "I find your lack of skill disturbing!"
endcase endcase


accept "Play again [y/N]? " to again accept "Play again [y/N]? " to again
Expand Down

0 comments on commit 1b9ce74

Please sign in to comment.