-
Notifications
You must be signed in to change notification settings - Fork 0
/
guessTheNumber.fg
148 lines (128 loc) · 3.49 KB
/
guessTheNumber.fg
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
137
138
139
140
141
142
143
144
145
146
147
148
#!syntax:lispish
#====================================================
#
# utils
#
#====================================================
#====================================================
{deffunc blink (p_cssSelector)
{repeat 4
:addCssClassTo 'highlight' $p_cssSelector
.waitSeconds 0.3
:DelCssClassFrom 'highlight' $p_cssSelector
.waitSeconds 0.3
}
}
#====================================================
#
# gameCourse
#
#====================================================
#====================================================
{deffunc gameCourse (p_minNumber p_maxNumber)
.var numberToGuess <-- :randomIntBetween $p_minNumber $p_maxNumber
.var tryNumber <-- 'not_defined'
.var numberOfSteps <-- 0
:displayNewMessageIn (+ 'Guess my number (between ' $p_minNumber ' and ' $p_maxNumber ')!') '#gameBoard'
{while [ $tryNumber /= $numberToGuess ]
# Try
#=====
tryNumber <-- .awaitNewHumanNumberIn '#gameBoard'
numberOfSteps <-- [+ 1]
# Answer
#========
{ if [$tryNumber < $numberToGuess] :displayNewMessageIn 'Too low! Try again!' '#gameBoard' }
{ if [$tryNumber > $numberToGuess] :displayNewMessageIn 'Too high! Try again!' '#gameBoard' }
{ if [$tryNumber = $numberToGuess] :displayNewMessageIn ['Well Done! Number of steps: ' + $numberOfSteps] '#gameBoard' }
}
~{
# What to do if interrupted
#===========================
displayNewMessageIn (+ 'The number to guess was ' $numberToGuess '.') '#gameBoard'
}
# Return
#=======
$numberOfSteps
}
#====================================================
#
# main program
#
#====================================================
#====================================================
.var main
.var numGameCourse <-- 0
.var score <-- 0
.var level <-- 1
.var limitNumber <-- 8
.var theGameCourse
#======================
# main
#======================
# This is a "par" bloc: all direct child blocks of it are executed in parallel
{par @main
# Game
#=======
{whileTrue @theGameCourse
.var gameCourseNumberOfSteps
# Game course
#------------
numGameCourse <-- [+ 1]
.waitSeconds 1
# -----> This is the point of the program: calling "gameCourse" function !!!! <-----
gameCourseNumberOfSteps <-- :gameCourse 1 [$limitNumber - 1]
# Now, a game course is finished
# update score
#-------------
.var diffScore <-- [[3 * $level] - $gameCourseNumberOfSteps]
diffScore <-- [+ 6]
score <-- [+ $diffScore]
# update level and limit
#-----------------------
{if [$gameCourseNumberOfSteps <= [$level + 2]]
level <-- [+ 1]
limitNumber <-- [* 2]
}
.waitSeconds 3
}
# Interrupt
#============
{whileTrue
.awaitClickBeep '#giveUp'
{if [$level > 1]
level <-- [- 1]
limitNumber <-- [/ 2]
}
.restart theGameCourse
}
# quit
#========
# I have to say it's a sequence ("seq") because I'm a direct child of a "par" block
{seq
.awaitClickBeep '#quit'
.break main
}
# Display of all pieces of information
#-------------------------------------
{whileTrue
:await numGameCourse beep
{if [$numGameCourse > 0]
:displayNewMessageIn (+ 'Game Course #' $numGameCourse) '#gameBoard/title'
}
}
{whileTrue
:await score beep
:displayMessageIn $score '#score'
.blink '#scoreHead'
}
{whileTrue
:await level beep
:displayMessageIn $level '#level'
.blink '#levelHead'
}
}
#======================
# Good bye
#======================
:displayNewMessageIn (+ 'Good job! You get ' $score ' in ' $numGameCourse ' courses.') '#gameBoard'
:displayNewMessageIn 'Good bye! See you soon!' '#gameBoard'