-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23-matches.pl
31 lines (27 loc) · 1.01 KB
/
23-matches.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
play :-
writeln("Try yourself with 23 matches!"), nl,
writeln("Wanna start first? (1 - yes, 0 - no)"),
read(First),
game(23, First), !.
game(1, 0) :-
nl, writeln("Congratulations! :)"), !.
game(1, 1) :-
nl, writeln("Game over! You took the last match! :("), !.
game(M, 0) :-
step(M, T),
nl, write("Computer took "), write(T), (T == 1 -> write(" match..."); write(" matches...")), nl,
M1 is M - T, write(M1), (M1 == 1 -> write(" match"); write(" matches")), write(" left"), nl,
game(M1, 1), !.
game(M, 1) :-
nl, writeln("Take 1, 2 or 3 matches..."), read(T),
write("You took "), write(T), (T == 1 -> write(" match..."); write(" matches...")), nl,
M1 is M - T, write(M1), (M1 == 1 -> write(" match"); write(" matches")), write(" left"), nl,
game(M1, 0), !.
step(M, T) :-
Mod is M mod 4, Mod = 0, T is 3, !.
step(M, T) :-
Mod is M mod 4, Mod = 3, T is 2, !.
step(M, T) :-
Mod is M mod 4, Mod = 2, T is 1, !.
step(M, T) :-
Mod is M mod 4, Mod = 1, T is 1, !.