Skip to content

Instantly share code, notes, and snippets.

@DawidSzcz
Created April 13, 2015 21:21
Show Gist options
  • Save DawidSzcz/24d3938759e73fdc0d01 to your computer and use it in GitHub Desktop.
Save DawidSzcz/24d3938759e73fdc0d01 to your computer and use it in GitHub Desktop.
war(L1, L2, K):-
asserta(first(L1)),
asserta(second(L2)),
openList(L1, Lo1-D1),
openList(L2, Lo2-D2),
war(Lo1-D1, Lo2-D2, []-_, 0, K).
war(X-X, _, _, K, K):-
var(X), !. % X-X identyfikuje sie z kazda lista roznicowa 0_o
war(_, X-X, _, K, K):-
var(X), !.
war([H1|T1]-D1,[H2|T2]-D2, BCards-D3, S, K):-
S1 is S +1,
(H1 > H2, !,
open_append(BCards-D3, [H1,H2|DN]-DN, R),
open_append(T1-D1, R, R1),
(isInfinite(R1, T2-D2, S1, K),!;
war(R1, T2-D2, []-_, S1, K));
(H1 < H2, !,
open_append(BCards-D3, [H1,H2|DN]-DN, R),
open_append(T2-D2, R, R2),
(isInfinite(T1-D1, R2, S1, K),!;
war(T1-D1, R2, []-_, S1, K));
S2 is S1 + 1,
(battle(T1, T2, H1b-T1b, H2b-T2b), !,
open_append(BCards-D3, [H1, H2, H1b, H2b|DN]-DN, R),
war(T1b-D1, T2b-D2, R, S2, K);
K = S1))).
battle([], _, _, _):-!, fail.
battle(_,[], _, _):-!, fail.
battle([H1|T1], [H2|T2], H1-T1, H2-T2).
isInfinite(L1-D1, L2-D2, S,inf):-
D1 = [],
D2 = [],
(first(L1);
asserta(first(L1)),
asserta(second(L2)),
fail).
openList([H], [H|D]-D).
openList([H|T], [H|R]-D):-
openList(T, R-D).
open_append(L1, []-_, L1).
open_append([]-_, X, X).
open_append(L1-L2, L2-D2, L1-D2).
retrF:-
retract(first(_)),
retrF.
retrS:-
retract(second(_)),
retrS.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment