aboutsummaryrefslogtreecommitdiff
path: root/examples/guessing.sloth
diff options
context:
space:
mode:
authorCody <cody@codyq.dev>2023-06-24 21:11:44 -0500
committerCody <cody@codyq.dev>2023-06-24 21:11:44 -0500
commitdef8adfcb9a9572f4e030990626a0d08377118bd (patch)
treef230fdf3d281f3f9a91f0510d954344f2bdb48ce /examples/guessing.sloth
parent0ee365c80667a3a5f43a86ead75fd0da5be23282 (diff)
downloadsloth-def8adfcb9a9572f4e030990626a0d08377118bd.tar.gz
hehehaha
Diffstat (limited to 'examples/guessing.sloth')
-rw-r--r--examples/guessing.sloth47
1 files changed, 25 insertions, 22 deletions
diff --git a/examples/guessing.sloth b/examples/guessing.sloth
index 05c25db..5a759e5 100644
--- a/examples/guessing.sloth
+++ b/examples/guessing.sloth
@@ -1,26 +1,29 @@
-val computer: int = random(1, 10);
+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;
-var tries: int = 0;
-var correct: bool = false;
+fn main() {
+ var computer: Int = random(1, 10);
+ var tries: Int = 0;
+ var correct: Bool = false;
-while !correct {
- print("\nPick a number between 1 and 10: ");
- val human: int = parse_int(readln());
+ while !correct {
+ 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!");
- correct = true;
- }
-
- if human > computer {
- println("Your guess was too high.");
- }
-
- if human < computer {
- println("Your guess was too low.");
- }
-
- tries = tries + 1;
-}
+ 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;
+ }
-println("\nIt took you ", tries, " tries to guess correctly!");
+ println("It took you ", tries, " tries to guess correctly!");
+}