aboutsummaryrefslogtreecommitdiff
path: root/examples/guessing.sloth
blob: f88c35bc0542888b8f19a8b95e7c51decd4aece4 (plain)
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
foreign fn print();
foreign fn println();
foreign fn readln() String;
foreign fn random(min: Int, max: Int) Int;
foreign fn parse_int(str: String) Int;
foreign fn randGen(min: Int, max: Int) Int;
foreign fn istr(x: Int) String;

fn main() Int {
	var computer: Int = randGen(1, 10);
	var tries: Int = 0;
	var correct: Bool = false;

	while correct == false {
		print("Pick a number between 1 and 10: ");
		var human: Int = parse_int(readln());

		if human == computer {
			println("You guessed the same number as me!\n");
			correct = true;
		} else if human > computer {
			println("Your guess was too high.\n");
		} else if human < computer {
			println("Your guess was too low.\n");
		}

		tries = tries + 1;
	}

	print("It took you ");
	print(istr(tries));
	println(" to guess correctly!");

	return 0;
}