-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguessingGame.nim
More file actions
136 lines (114 loc) · 3.89 KB
/
guessingGame.nim
File metadata and controls
136 lines (114 loc) · 3.89 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import strutils
import std/random
import nigui
# Variable Setup
let randomNum: int = rand(100)
doAssert randomNum in 0..100
var guessCount: int = 0
# Not a great way to do it, but I don't know how to otherwise.
var numbers: array = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
var inputIsNum: bool = false
var hasInput: bool = false
# GUI Setup
app.init()
var mainWindow = newWindow("Simple Guessing Game")
mainWindow.height = 300.scaleToDpi
mainWindow.width = 400.scaleToDpi
var rootContainer = newLayoutContainer(Layout_Vertical)
mainWindow.add(rootContainer)
rootContainer.scrollableHeight = 400
var inputContainer = newLayoutContainer(Layout_Horizontal)
rootContainer.add(inputContainer)
var countContainer = newLayoutContainer(Layout_Vertical)
rootContainer.add(countContainer)
var guessTextArea = newTextBox()
inputContainer.add(guessTextArea)
let applyGuessButton = newButton("Guess")
inputContainer.add(applyGuessButton)
var infoLabel = newLabel()
rootContainer.add(infoLabel)
proc gameWon() =
guessTextArea.hide()
applyGuessButton.hide()
var restartButton = newButton("Quit")
inputContainer.add(restartButton)
restartButton.focus()
restartButton.onClick = proc(event: ClickEvent) =
app.quit()
# Made by ChatGPT
proc isNumeric(s: string): bool =
for c in s:
if not c.isDigit:
return false
return s.len > 0
proc applyTurn() =
# Main problem with this is it doesn't know when a letter is with a number. It sees the string has a number and accepts it, causing an error.
for letter in numbers:
if (guessTextArea.text != ""):
hasInput = true
echo "has input"
if (guessTextArea.text.contains(letter)):
inputIsNum = true
echo "true"
break
elif (guessTextArea.text.contains(letter) == false):
inputIsNum = false
echo letter
guessTextArea.focus()
if (isNumeric(guessTextArea.text) and hasInput):
guessCount += 1
var countText: string
if (guessTextArea.text.parseInt() > randomNum):
countText = ", too large!"
elif (guessTextArea.text.parseInt() < randomNum):
countText = ", too small!"
else:
countText = ", correct!"
rootContainer.add(newLabel(guessTextArea.text & countText))
if (guessTextArea.text.parseInt() == randomNum):
var pluralText: string
if (guessCount > 1):
pluralText = " times!"
else:
pluralText = " time!"
infoLabel.text = "Well done! You guessed " & $guessCount & pluralText
gameWon()
elif (hasInput == false):
mainWindow.alert("Enter a number")
elif (isNumeric(guessTextArea.text) == false):
mainWindow.alert("Only numbers are allowed")
guessTextArea.text = ""
hasInput = false
applyGuessButton.onClick = proc(event: ClickEvent) = applyTurn()
guessTextArea.onKeyDown = proc(event: KeyboardEvent) =
if event.key == Key_Return:
applyTurn()
mainWindow.show()
guessTextArea.focus()
app.run()
# Old CLI game
#[
while (true):
echo "Guess the number:"
let guessedNum = readLine(stdin).parseInt()
if (guessedNum > randomNum):
echo "Too large"
guessCount += 1
elif (guessedNum < randomNum):
echo "Too small"
guessCount += 1
else:
guessCount += 1
echo "Well done! You guessed ", guessCount,
if (guessCount>1):
" times!"
else:
" time!"
echo "Would you like to play again? y/n"
var playAgain = readLine(stdin)
if playAgain == "y":
randomNum = rand(1..100)
guessCount = 0
else:
break
]#