Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
30
Task/24-game/Prolog/24-game.pro
Normal file
30
Task/24-game/Prolog/24-game.pro
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
:- initialization(main).
|
||||
|
||||
|
||||
answer(24).
|
||||
play :- round, play ; true.
|
||||
|
||||
round :-
|
||||
prompt(Ns), get_line(Input), Input \= "stop"
|
||||
, ( phrase(parse(Ns,[]), Input) -> Result = 'correct'
|
||||
; Result = 'wrong'
|
||||
), write(Result), nl, nl
|
||||
. % where
|
||||
prompt(Ns) :- length(Ns,4), maplist(random(1,10), Ns)
|
||||
, write('Digits: '), write(Ns), nl
|
||||
.
|
||||
|
||||
parse([],[X]) --> { answer(X) }.
|
||||
parse(Ns,[Y,X|S]) --> "+", { Z is X + Y }, parse(Ns,[Z|S]).
|
||||
parse(Ns,[Y,X|S]) --> "-", { Z is X - Y }, parse(Ns,[Z|S]).
|
||||
parse(Ns,[Y,X|S]) --> "*", { Z is X * Y }, parse(Ns,[Z|S]).
|
||||
parse(Ns,[Y,X|S]) --> "/", { Z is X div Y }, parse(Ns,[Z|S]).
|
||||
parse(Ns,Stack) --> " ", parse(Ns,Stack).
|
||||
parse(Ns,Stack) --> { select(N,Ns,Ns1), number_codes(N,[Code]) }
|
||||
, [Code], parse(Ns1,[N|Stack])
|
||||
.
|
||||
|
||||
get_line(Xs) :- get_code(X)
|
||||
, ( X == 10 -> Xs = [] ; Xs = [X|Ys], get_line(Ys) )
|
||||
.
|
||||
main :- randomize, play, halt.
|
||||
Loading…
Add table
Add a link
Reference in a new issue