Skip to content

Instantly share code, notes, and snippets.

@isstaif
Created May 19, 2012 08:46
Show Gist options
  • Save isstaif/2730113 to your computer and use it in GitHub Desktop.
Save isstaif/2730113 to your computer and use it in GitHub Desktop.
Prolog Four Winns game
:- dynamic state/3.
:- dynamic top/2.
top(0,0).
play(Color, Col) :-
top(Col, Row), assert(state(Col, Row, Color)),
Next is Row + 1, retract(top(Col, Row)), assert(top(Col, Next)).
win(X,Y, Winner) :-
state(X,Y, Color), N1 is Y - 1, state(X, N1, Color), N2 is N1 - 1, state(X,N2, Color), N3 is N2 - 1, state(X, N3, Color), Winner = Color.
win(X,Y, Winner) :-
state(X,Y, Color), N1 is X - 1, state(N1, Y, Color), N2 is N1 - 1, state(N2, Y, Color), N3 is N2 - 1, state(N3, Y, Color), Winner = Color.
win(X,Y, Winner) :-
state(X,Y, Color),
N1 is X + 1, M1 is Y + 1, state(N1, M1, Color),
N2 is N1 + 1, M2 is M1 + 1, state(N2, M2, Color),
N3 is N2 + 1, M3 is M2 + 1, state(N3, M3, Color),
Winner = Color.
win(X,Y, Winner) :-
state(X,Y, Color),
N1 is X + 1, M1 is Y - 1, state(N1, M1, Color),
N2 is N1 + 1, M2 is M1 - 1, state(N2, M2, Color),
N3 is N2 + 1, M3 is M2 - 1, state(N3, M3, Color),
Winner = Color.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment