-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram_basic.oz
297 lines (262 loc) · 8.33 KB
/
program_basic.oz
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
functor
import
ProjectLib
Browser
OS
System
Application
Open
define
CWD = {Atom.toString {OS.getCWD}}#"/"
Browse = proc {$ Buf} {Browser.browse Buf} end
Print = proc{$ S} {System.print S} end
Args = {Application.getArgs record(
'nogui'(single type:bool default:false optional:true)
'db'(single type:string default:CWD#"database/database.txt")
'ans'(single type:string default:CWD#"autoplay/test_answers.txt"))}
Output = {New Open.file init(name: stdout
flags: [write create truncate text])}
in
local
NoGUI = Args.'nogui'
DB = Args.'db'
ANS = Args.'ans'
Database = {ProjectLib.loadDatabase file DB}
ListOfAnswers = {ProjectLib.loadCharacter file ANS}
%NewCharacter = {ProjectLib.loadCharacter file CWD#"new_character/new_character.txt"}
fun {GetAllCharacters Database}
{Reverse {GetAllCharactersAux Database nil}}
end
fun {GetAllCharactersAux Database L}
case Database of nil then L
[] H|T then {GetAllCharactersAux T H.1|L}
end
end
fun {Length L}
{LengthAux L 0}
end
fun{LengthAux L N}
case L
of nil then N
[] H|T then {LengthAux T N + 1}
end
end
fun {Reverse L}
{ReverseAux L nil}
end
fun {ReverseAux L A}
case L of nil then A
[] H|T then {ReverseAux T H|A}
end
end
fun {GetQuestions Database}
{Arity Database.1}.2
end
fun {Head Xs}
Xs.1
end
fun {Tail Xs}
Xs.2
end
fun {Nth L N}
if N == 0 then {Head L}
else {Nth {Tail L} N - 1}
end
end
fun {MinPos L}
{MinPosAux L 0 9999999999 0}
end
fun {MinPosAux L N Min Pos}
case L
of nil then Pos
[] H|T then
if H < Min then {MinPosAux T N+1 H N}
else {MinPosAux T N+1 Min Pos}
end
end
end
fun {Contains L E}
case L
of nil then false
[] H|T then
if H == E then true
else {Contains T E}
end
end
end
fun {Abs I}
if I < 0 then ~I
else I
end
end
fun {DeleteInd L I}
case L
of nil then nil
[] H|T then
if I == 0 then T
else H|{DeleteInd T (I-1)}
end
end
end
fun {GetCharacterOnName Database CharacterName}
case Database of nil then nil
[] H|T then
if H.1 == CharacterName then
H
else {GetCharacterOnName T CharacterName}
end
end
end
fun {CharacterContainsQuestion Character Question}
{Contains {Arity Character} Question}
end
fun {GetCharactersOnQuestion Database Question Answer Characters}
case Database
of nil then nil
[] H|T then
if {Contains Characters H.1} then
if {CharacterContainsQuestion {GetCharacterOnName Database H.1} Question} then
if H.Question == Answer then H.1|{GetCharactersOnQuestion T Question Answer Characters}
else {GetCharactersOnQuestion T Question Answer Characters}
end
else H.1|{GetCharactersOnQuestion T Question Answer Characters}
end
else {GetCharactersOnQuestion T Question Answer Characters}
end
end
end
fun {CountQuestionCharactersFiltered Database CharactersTrue Question N}
case Database
of nil then N
[] H|T then
if {Contains CharactersTrue H.1} then
if {CharacterContainsQuestion {GetCharacterOnName Database H.1} Question} then
if H.Question == true then {CountQuestionCharactersFiltered T CharactersTrue Question N + 1}
else {CountQuestionCharactersFiltered T CharactersTrue Question N}
end
else {CountQuestionCharactersFiltered T CharactersTrue Question N}
end
else
{CountQuestionCharactersFiltered T CharactersTrue Question N}
end
end
end
fun {ComputeCountersAux Database CharactersTrue QuestionsLeft Counters N}
case QuestionsLeft
of nil then Counters
[] H|T then
local Cpt in
Cpt = {CountQuestionCharactersFiltered Database CharactersTrue H 0}
{ComputeCountersAux Database CharactersTrue T {Abs Cpt - (N-Cpt)}|Counters N}
end
end
end
fun {ComputeCounters Database CharactersTrue QuestionsLeft}
{Reverse {ComputeCountersAux Database CharactersTrue QuestionsLeft nil {Length CharactersTrue}}}
end
fun {Pop L Poped}
Poped = {Nth L 0}
{DeleteInd L 0}
end
fun {Append Xs Ys}
if {Length Xs} == 0 then Ys|nil
else
case Xs of nil then Ys
[] X|Xr then X|{Append Xr Ys}
end
end
end
fun {TreeBuilderAux Database Characters Counters Questions}
if {Or {Length Questions} < 1 {Length Characters} < 2 } then leaf(1:Characters)
else
local Min Question CharactersTrue CharactersFalse NewCountersTrue NewCountersFalse NewQuestions in
Min = {MinPos Counters}
Question = {Nth Questions Min}
if {Nth Counters Min} == {Length Characters} then leaf(1:Characters)
else
NewQuestions = {DeleteInd Questions Min}
CharactersTrue = {GetCharactersOnQuestion Database Question true Characters}
CharactersFalse = {GetCharactersOnQuestion Database Question false Characters}
NewCountersTrue = {ComputeCounters Database CharactersTrue NewQuestions}
NewCountersFalse = {ComputeCounters Database CharactersFalse NewQuestions}
question(
1:Question
true:{TreeBuilderAux Database CharactersTrue NewCountersTrue NewQuestions}
false:{TreeBuilderAux Database CharactersFalse NewCountersFalse NewQuestions}
)
end
end
end
end
fun {TreeBuilder Database}
Characters Questions Counters Tree
in
Characters = {GetAllCharacters Database}
Questions = {GetQuestions Database}
Counters = {ComputeCounters Database Characters Questions}
Tree = {TreeBuilderAux Database Characters Counters Questions}
% {Print Tree}
Tree
end
proc {PrintResults Result}
case Result of H|T then
{Print H}
if T \= nil then {Print ','}
else skip
end
{PrintResults T}
else skip
end
end
proc {WriteListToFile L F}
case L
of H|nil then
{F write(vs:H)}
[]H|T then
{F write(vs:H#",")}
{WriteListToFile T F}
else
skip
end
end
fun {GameDriver Tree}
{GameDriverAux Tree Tree nil}
end
fun {GameDriverAux Tree StaticTree Reps}
Result
Answer
OldTree
NewReps
in
if {Label Tree} == 'leaf' then
Result = {ProjectLib.found Tree.1}
else
Answer = {ProjectLib.askQuestion Tree.1}
if Answer then
Result = {GameDriverAux Tree.true StaticTree {Append Reps true}}
else
Result = {GameDriverAux Tree.false StaticTree {Append Reps false}}
end
end
if Result == false then
{Browse 'What’s going on ? I thought I could’nt make mistakes !'}
else
{Browse Result}
{WriteListToFile Result Output}
end
unit
end
in
{ProjectLib.play opts(
characters:Database
driver:GameDriver
noGUI:false
builder:TreeBuilder
autoPlay:ListOfAnswers
oopsButton:false
allowUnknown:false
%newCharacter:NewCharacter
)}
{Application.exit 0}
end
end