-
Notifications
You must be signed in to change notification settings - Fork 0
/
quiz.pl
135 lines (121 loc) · 3.5 KB
/
quiz.pl
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
:- use_module(library(pce)).
:- dynamic quiz/1, title/1, question/4.
:- dynamic range/3, myscore/2.
setup_run(File) :-
assertz(quiz(File)),
see(File),
readin,
seen,
runquiz,
!.
readin :-
read(Title),
assertz(title(Title)),
readqs.
readqs :-
repeat,
read(Qnumber),
read(Qtext),
process(Qnumber, Qtext),
Qtext = endquestions,
readranges.
process(0, endquestions) :- !.
process(Qnumber, Qtext) :-
proc2([], Anslist, -9999, Maxscore),
assertz(question(Qnumber, Qtext, Anslist, Maxscore)).
proc2(Anscurrent, Anslist, Maxsofar, Maxscore) :-
read(Ans),
( (Ans = end,
Anslist = Anscurrent,
Maxscore = Maxsofar,
!)
; (read(Score),
append(Anscurrent, [ans(Ans, Score)], Ansnew),
Maxnew is max(Maxsofar, Score),
proc2(Ansnew, Anslist, Maxnew, Maxscore))
).
readranges :-
repeat,
read(First),
proc(First),
First = endmarkscheme.
proc(endmarkscheme) :- !.
proc(First):-
read(Last),
read(Feedback),
assertz(range(First, Last, Feedback)).
runquiz :-
retractall(myscore(_, _)),
assertz(myscore(0, 0)),
title(T),
new(@dialog, dialog('Quiz')),
new(@intro, text(T)),
new(@button, button('Avanti', and(message(@dialog, destroy),
message(@intro, destroy),
message(@button, destroy),
message(@prolog, askq)))),
send_list(@dialog, append, [@intro, @button]),
send(@button, show_focus_border(false)),
send(@button, radius(30)),
send(@dialog, open(point(100, 100))),
nl.
askq :-
(
question(Qnumber, Qtext, Anslist, _)
->
extract(Anslist, Answers),
new(D, dialog('Quiz')),
swritef(String, 'Domanda %w', [Qnumber]),
new(N, text(String)),
new(Q, text(Qtext)),
new(A, menu(risposte, choice)),
send_list(A, append, Answers),
new(B1, button('Invio', message(@prolog, request, A?selection))),
send(B1, show_focus_border(false)),
send(B1, radius(30)),
new(B2, button('Avanti', and(message(D, destroy), message(@prolog, askq)))),
send(B2, radius(30)),
send_list(D, append, [N, Q, A, B1, B2]),
send(D, open(point(100, 100))),
send(A, clear_selection)
;
result
).
result :-
myscore(My, Max),
retractall(title(_)),
quiz(File),
in_range(My, Feedback),
swritef(String, 'Hai ottenuto %w punti su %w', [My, Max]),
new(D, dialog('Quiz')),
new(S, text(String)),
new(F, text(Feedback)),
new(B1, button('Rifai', and(message(D, destroy),
message(@prolog, setup_run, File)))),
new(B2, button('Esci', message(D, destroy))),
send(B1, show_focus_border(false)),
send(B1, radius(30)),
send(B2, radius(30)),
send_list(D, append, [S, F, B1, B2]),
send(D, open(point(100, 100))).
request(Answer) :-
question(Qnumber, Qtext, Anslist, Maxscore),
retract(question(Qnumber, Qtext, Anslist, Maxscore)),
member(ans(Answer, Score), Anslist),
usescore(Score, Maxscore),
!.
usescore(Score, Maxscore) :-
retract(myscore(Old, Oldtotal)),
New is Old + Score,
Newtotal is Oldtotal + Maxscore,
assertz(myscore(New, Newtotal)).
extract([], []).
extract([ans(Answer, _)|T1], [Answer|T2]) :-
extract(T1, T2).
in_range(My, Feedback) :-
range(Min, Max, Feedback),
(
(My >= Min , My =< Max)
-> true
; fail
).