Skip to content

Instantly share code, notes, and snippets.

@MhdSyrwan
Forked from MhdAljuboori/Connect 4
Created May 21, 2012 18:17
Show Gist options
  • Save MhdSyrwan/2763716 to your computer and use it in GitHub Desktop.
Save MhdSyrwan/2763716 to your computer and use it in GitHub Desktop.
AI homework
initializeColumn(Column) :-
Column >0,
% create empty table
assert(top(Column, 1)),
Column1 is Column-1,
initializeColumn(Column1).
initializeColumn(0).
% Create Game with Row equal to row and column equal to column
createGame(Row, Column) :-
% size of table
assert(size(Row, Column)),
% initialize All column
initializeColumn(Column).
full(C):-size(RowCount,_),top(C,TR),TR>RowCount.
push(Column) :-
size(Row, ColumnNum),
% if column not full
(Column < ColumnNum + 1),(Column>0),
(
not(full(Column)),
% get Row number in column
top(Column, RowNum),
% add red at RowNum an column
assert(cell(red, RowNum, Column)),
% plus RowNum by one
retract(top(Column, RowNum)),
R1 is RowNum+1,
assert(top(Column, R1))
).
% win(red, RowNum, Column).
push(Player, Column) :-
size(Row, ColumnNum),
% if column not full
(Column < ColumnNum + 1),(Column>0),
(
not(full(Column)),
% get Row number in column
top(Column, RowNum),
% add red at RowNum an column
assert(cell(Player, RowNum, Column)),
% plus RowNum by one
retract(top(Column, RowNum)),
R1 is RowNum+1,
assert(top(Column, R1))
).
% ,win(red, RowNum, Column).
winVertical(Player, Row, Column) :-
cell(Player, Row, Column), R1 is Row-1,
cell(Player, R1, Column), R2 is R1-1,
cell(Player, R2, Column), R3 is R2-1,
cell(Player, R3, Column).
pop(Player,C):-top(C,TR),R is TR - 1 , retract(cell(Player, R, C)),retract(top(C,TR)), assert(top(C,R)).
winHorizontal(Player, Row, Column) :-
% X: pice already exist, Y: new pice added
% X X X Y
cell(Player, Row, Column), C1 is Column-1,
cell(Player, Row, C1), C2 is C1-1,
cell(Player, Row, C2), C3 is C2-1,
cell(Player, Row, C3), !;
% Y X X X
cell(Player, Row, Column), C1 is Column+1,
cell(Player, Row, C1), C2 is C1+1,
cell(Player, Row, C2), C3 is C2+1,
cell(Player, Row, C3), !;
% X Y X X
cell(Player, Row, Column), C1 is Column-1,
cell(Player, Row, C1), C2 is Column+1,
cell(Player, Row, C2), C3 is C2+1,
cell(Player, Row, C3), !;
% X X Y X
cell(Player, Row, Column), C1 is Column-1,
cell(Player, Row, C1), C2 is C1-1,
cell(Player, Row, C2), C3 is Column+1,
cell(Player, Row, C3), !.
winDiagonalLeftToRight(Player, Row, Column) :-
% X: pice already exist, Y: new pice added
% left side The lowest one
% Y X X X
cell(Player, Row, Column), R1 is Row+1, C1 is Column +1,
cell(Player, R1, C1), R2 is R1+1, C2 is C1 +1,
cell(Player, R2, C2), R3 is R2+1, C3 is C2 +1,
cell(Player, R3, C3), !;
% X X X Y
cell(Player, Row, Column), R1 is Row-1, C1 is Column -1,
cell(Player, R1, C1), R2 is R1-1, C2 is C1 -1,
cell(Player, R2, C2), R3 is R2-1, C3 is C2 -1,
cell(Player, R3, C3), !;
% X Y X X
cell(Player, Row, Column), R1 is Row-1, C1 is Column -1,
cell(Player, R1, C1), R2 is Row+1, C2 is Column +1,
cell(Player, R2, C2), R3 is R2+1, C3 is C2 +1,
cell(Player, R3, C3), !;
% X X Y X
cell(Player, Row, Column), R1 is Row+1, C1 is Column +1,
cell(Player, R1, C1), R2 is Row-1, C2 is Column -1,
cell(Player, R2, C2), R3 is R2-1, C3 is C2 -1,
cell(Player, R3, C3), !.
winDiagonalRightToLeft(Player, Row, Column) :-
% X: pice already exist, Y: new pice added
% left side The lowest one
% Y X X X
cell(Player, Row, Column), R1 is Row+1, C1 is Column -1,
cell(Player, R1, C1), R2 is R1+1, C2 is C1 -1,
cell(Player, R2, C2), R3 is R2+1, C3 is C2 -1,
cell(Player, R3, C3), !;
% X X X Y
cell(Player, Row, Column), R1 is Row-1, C1 is Column +1,
cell(Player, R1, C1), R2 is R1-1, C2 is C1 +1,
cell(Player, R2, C2), R3 is R2-1, C3 is C2 +1,
cell(Player, R3, C3), !;
% X Y X X
cell(Player, Row, Column), R1 is Row-1, C1 is Column +1,
cell(Player, R1, C1), R2 is Row+1, C2 is Column -1,
cell(Player, R2, C2), R3 is R2+1, C3 is C2 -1,
cell(Player, R3, C3), !;
% X X Y X
cell(Player, Row, Column), R1 is Row+1, C1 is Column -1,
cell(Player, R1, C1), R2 is Row-1, C2 is Column +1,
cell(Player, R2, C2), R3 is R2-1, C3 is C2 +1,
cell(Player, R3, C3), !.
winDiagonal(Player, Row, Column) :-
winDiagonalLeftToRight(Player, Row, Column), !;
winDiagonalRightToLeft(Player, Row, Column), !.
win(Player, Row, Column) :-
winVertical(Player, Row, Column), !;
winHorizontal(Player, Row, Column), !;
winDiagonal(Player, Row, Column), !.
win_auto(Player,Column):-top(Column,Row),Row_1 is Row - 1, win(Player, Row_1, Column).
win_count(Player,Column,N):-times_to_win(Player,Column,0,N).
times_to_win(Player, Column, N, Out):-full(Column),!,false.
times_to_win(Player, Column, N, Out):-win_auto(Player,Column),!,Out is N.
times_to_win(Player, Column, N, Out):-push(Player,Column),N_1 is N+1, times_to_win(Player,Column,N_1,Out),!,pop(Player,Column).
times_to_win(Player, Column, N, Out):-pop(Player,Column),false.
win_pred(Player,Column):-push(Player,Column), win_auto(Player,Column),!,pop(Player,Column).
win_pred(Player,Column):-pop(Player,Column),false.
ai_play(0,MinCount,MinC):-!,push(yellow,MinC).
ai_play(C,MinCount,MinC):-win_pred(yellow,C),!,push(yellow,C).
ai_play(C,MinCount,MinC):-win_pred(red,C),!,NewC is C-1,ai_play(NewC,-1,C).
ai_play(C,MinCount,MinC):-win_count(yellow,C,N), N < MinCount,!,NewC is C-1,ai_play(NewC,N,C).
ai_play(C,MinCount,MinC):-NewC is C-1, ai_play(NewC,MinCount,MinC).
ai_play:-size(R,C), ai_play(C,R,0);win_auto(yellow,C),write('i won ^_^ ').
print1(_, 0) :- !.
print1(Row, Column) :-
Column > 0,
(
(cell(Player, Row, Column), (Player = 'red', P = 'x';P = 'o'));
P = '.'
),
write(P), write(' '),
C is Column -1,
print1(Row, C).
show :-
size(Row, Column),
showTable(Row, Column).
showTable(0, _) :-!.
showTable(Row, Column) :-
Row > 0,
print1(Row, Column), nl,
R is Row-1,
showTable(R, Column).
play(C):-push(C),win_auto(red,C),!,write('you won :( ').
play(_):-ai_play.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment