forked from DarthJDG/NumGuess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
numguess.dart
91 lines (77 loc) · 2.22 KB
/
numguess.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math';
void main() {
new NumGuess();
}
class NumGuess {
String state;
String name;
int limit;
int num;
int tries;
int max_tries;
var random;
NumGuess() {
random = new Random();
stdout.write('Welcome to NumGuess Dart version!\n\nEnter your name: ');
state = 'name';
var sub;
sub = UTF8.decoder.bind(stdin).listen((str) {
str = str.trim();
switch(state) {
case 'name':
name = str == '' ? 'Player' : str;
stdout.write('\nWelcome ${name}, enter upper limit: ');
state = 'limit';
break;
case 'limit':
limit = max(10, int.parse(str, radix:10, onError:(str) => 10));
max_tries = (log(limit) / log(2)).floor() + 1;
startGame();
break;
case 'guess':
if(evaluateGuess(str)) wellDone(); else stdout.write('Guess: ');
break;
case 'play':
if(str.toUpperCase() == 'Y') startGame(); else {
sub.cancel();
stdout.writeln('\nOkay, bye.');
exit;
}
break;
}
});
}
void startGame() {
tries = 0;
num = random.nextInt(limit) + 1;
stdout.write('\nGuess my number between 1 and ${limit}!\n\nGuess: ');
state = 'guess';
}
bool evaluateGuess(String str) {
int guess = int.parse(str, radix:10, onError:(str) => null);
if(guess == null) stdout.writeln('That\'s just plain wrong.');
else if(guess < 1 || guess > limit) stdout.writeln('Out of range.');
else {
tries++;
if(guess < num) stdout.writeln('Too low!');
else if(guess > num) stdout.writeln('Too high!');
else return true;
}
return false;
}
void wellDone() {
String tries_word = tries == 1 ? 'try' : 'tries';
stdout.writeln('\nWell done ${name}, you guessed my number from ${tries} ${tries_word}!');
if(tries == 1) stdout.writeln('You\'re one lucky bastard!');
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!');
stdout.write('Play again [y/N]? ');
state = 'play';
}
}