-
Notifications
You must be signed in to change notification settings - Fork 30
/
guess.nim
57 lines (48 loc) · 1.14 KB
/
guess.nim
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
#
# guess.nim - guessing game in nim
#
# This is written to demonstrate this language versus the same program
# written in other languages.
#
# COMPILE:
# nim compile guess.nim
#
# 31-Aug-2018 Scott Emmons Created.
#
import random, strutils
randomize()
let
answer = rand(99)+1 # For nim 0.17 or older, use random(100)+1 instead
const
scorefile = "highscores_nim"
var
guess = -1
num = 0
echo("guess.nim - Guess a number between 1 and 100\n")
# Play game
while guess != answer:
num+=1
stdout.write("Enter guess ", num, ": ")
guess = parseInt(readLine(stdin))
if guess < answer:
echo("Higher...")
elif guess > answer:
echo("Lower...")
echo("Correct! That took ", num, " guesses.\n")
# Save high score
stdout.write("Please enter your name: ")
let name = readLine(stdin)
try:
let outfile = open(scorefile, fmAppend)
write(outfile, name & " " & $(num) & "\n")
close(outfile)
except IOError:
echo("ERROR: Can't write to ", scorefile)
# Print high scores
echo("\nPrevious high scores:")
try:
let infile = open(scorefile)
echo readAll(infile)
close(infile)
except IOError:
echo("ERROR: Can't read from ", scorefile)