Created
September 2, 2013 20:20
-
-
Save sleepyfox/6416925 to your computer and use it in GitHub Desktop.
A board analyser for our 7 Languages in 7 Weeks Prolog session
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% A board is a list of states, each of which can be 'O', '' or 'X' | |
%% e.g. board('', '', '', '', '', '', '', '', ''). | |
board('X', 'O', '', 'X', '', '', 'X', 'O', ''). | |
row_one_win(X) :- board(X, X, X, _, _, _, _, _, _). | |
row_two_win(X) :- board(_, _, _, X, X, X, _, _, _). | |
row_three_win(X) :- board(_, _, _, _, _, _, X, X, X). | |
row_win(X) :- row_one_win(X) ; row_two_win(X) ; row_three_win(X). | |
column_one_win(X) :- board(X, _, _, X,_, _, X, _, _). | |
column_two_win(X) :- board(_, X, _, _, X, _, _, X, _). | |
column_three_win(X) :- board(_, _, X, _, _, X, _, _, X). | |
column_win(X) :- column_one_win(X) ; column_two_win(X) ; column_three_win(X). | |
leading_diagonal_win(X) :- board(X, _, _, _, X, _, _, _, X). | |
trailing_diagonal_win(X) :- board(_, _, X, _, X, _, X, _, _). | |
diagonal_win(X) :- leading_diagonal_win(X) ; trailing_diagonal_win(X). | |
win(X) :- (row_win(X) ; column_win(X) ; diagonal_win(X)), \=(X, ''). | |
draw :- \+ win(X). | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 20 is a win by (row OR column OR diagonal) AND X NOT EQUAL TO the empty string