Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Tic Tac Toe
- % <01-06-2016>
- % <Mehdi Gunes>, <11035021>, <amehdigunes@gmail.com>
- % <Mathijs Maijer, <11043024>, <matevy1@gmail.com>
- % Algorithms:
- :- include(minimax).
- %:- include(alphabeta).
- %:- include(followstrategy).
- grid([ [i, i, i],
- [i, i, i],
- [i, i, i] ]).
- % Legal moves:
- legalmove(Length/Height, Grid):-
- heightlist(Grid, Height, HeightList),
- lengthlist(HeightList, Length, i).
- heightlist([List | _], 1, List).
- heightlist([_ | T], Length, List):-
- Length2 is Length - 1,
- heightlist(T, Length2, List).
- lengthlist([State | _], 1, State).
- lengthlist([_ | T], Length, State):-
- Length2 is Length - 1,
- lengthlist(T, Length2, State).
- % Winning:
- win(Grid, Winner):-
- (Y = 1; Y = 2; Y = 3),
- heightlist(Grid, Y, HorizontalLine),
- HorizontalLine = [X, X, X],
- X \= i,
- ((X = x, Winner = x); (X = o, Winner = o)).
- win(Grid, Winner):-
- heightlist(Grid, 1, HorizontalLine1),
- heightlist(Grid, 2, HorizontalLine2),
- heightlist(Grid, 3, HorizontalLine3),
- lengthlist(HorizontalLine1, 1, X),
- lengthlist(HorizontalLine2, 2, X),
- lengthlist(HorizontalLine3, 3, X),
- X \= i,
- ((X = x, Winner = x); (X = o, Winner = o)).
- win(Grid, Winner):-
- heightlist(Grid, 1, HorizontalLine1),
- heightlist(Grid, 2, HorizontalLine2),
- heightlist(Grid, 3, HorizontalLine3),
- lengthlist(HorizontalLine1, 3, X),
- lengthlist(HorizontalLine2, 2, X),
- lengthlist(HorizontalLine3, 1, X),
- X \= i,
- ((X = x, Winner = x); (X = o, Winner = o)).
- win(Grid, Winner):-
- heightlist(Grid, 1, HorizontalLine1),
- heightlist(Grid, 2, HorizontalLine2),
- heightlist(Grid, 3, HorizontalLine3),
- (Y = 1; Y = 2; Y = 3),
- lengthlist(HorizontalLine1, Y, X),
- lengthlist(HorizontalLine2, Y, X),
- lengthlist(HorizontalLine3, Y, X),
- X \= i,
- ((X = x, Winner = x); (X = o, Winner = o)).
- % Draw
- draw([]).
- draw([H | T]):-
- checkdraw(H),
- draw(T).
- checkdraw([]).
- checkdraw([H | T]):-
- H \= i,
- checkdraw(T).
- % Find all possible moves:
- moves(State, PossibleStates):-
- move(State, PossibleMoves),
- movesToGrid(State, PossibleMoves, PossibleStates), !.
- move(State, PossibleMoves):-
- move1(State, 1/1, PossibleMoves).
- move1(_, 4/1, []).
- move1(State, L/H, PossibleMoves):-
- L < 4,
- move2(State, L/H, PossibleMoves2),
- L2 is L+1,
- move1(State, L2/H, PossibleMoves3),
- append(PossibleMoves2, PossibleMoves3, PossibleMoves).
- move1(_,_,[]).
- move2(_, _/4, []).
- move2(State, L/H, PossibleMoves):-
- H < 4,
- legalmove(L/H, State),
- H2 is H+1,
- move2(State, L/H2, PossibleMoves2),
- PossibleMoves = [L/H | PossibleMoves2].
- move2(State, L/H, PossibleMoves):-
- H < 4,
- H2 is H+1,
- move2(State, L/H2, PossibleMoves).
- move2(_, _, []).
- movesToGrid(_, [], []).
- movesToGrid(State, [H | T], Grids1):-
- applyMove(x, State, H, Grid),
- movesToGrid(State, T, Grids2),
- Grids1 = [Grid | Grids2].
- % Apply moves
- applyMove(Player, Grid, Length/Height, NextState):-
- heightlist(Grid, Height, HeightList),
- replace(HeightList, Length, _, ChangedLengthList, Player),
- replace2(Grid, Height, _, ChangedLengthList, NextState).
- replace2([_ | T], 1, List, ChangedLengthList, Result):-
- append(List, [ChangedLengthList | T], Result).
- replace2([H | T], Length, List, ChangedLengthList, Result):-
- Length2 is Length - 1,
- append(List, [H], List2),
- replace2(T, Length2, List2, ChangedLengthList, Result).
- replace([_ | T], 1, List, Result, Player):-
- append(List, [Player | T], Result).
- replace([H | T], Length, List, Result, Player):-
- Length2 is Length - 1,
- append(List, [H], List2),
- replace(T, Length2, List2, Result, Player).
- staticval(State, 1):-
- win(State, x).
- staticval(State, -1):-
- win(State, o).
- staticval(State, 0):-
- draw(State).
- % Animate
- animate(Grid):-
- jump(30),
- write(' -------------'), nl,
- animate2(Grid),
- jump(10), !.
- animate2([]).
- animate2([H | T]):-
- animate_symbols(H), nl,
- write(' -------------'), nl,
- animate2(T).
- animate_symbols([]).
- animate_symbols([H | T]):-
- write('| '),
- write(H),
- write(' |'),
- animate_symbols(T).
- jump(0).
- jump(Counter):-
- Counter2 is Counter - 1,
- nl,
- jump(Counter2).
- % User input
- min_to_move(Grid):-
- count(Grid, x, N1),
- count(Grid, o, N2),
- N1 > N2.
- max_to_move(Grid):-
- count(Grid, x, N1),
- count(Grid, o, N2),
- N1 = N2.
- count([], _, 0).
- count([H | T], S, N):-
- count2(H, S, N2),
- count(T, S, N3),
- N is N2 + N3.
- count2([], _, 0).
- count2([S | T], S, N):-
- count2(T, S, N2),
- N is N2 +1.
- count2([H | T], S, N):-
- H \= S,
- count2(T, S, N).
- % Run
- /*
- :- grid(Grid),
- animate(Grid),
- write('Please make your step: length/height.'), nl,
- read(Input),
- legalmove(Input, Grid),
- applyMove(x, Grid, Input, NextState),
- animate(NextState),
- sleep(3),
- minimax(Grid, BestSucc, _),
- animate(BestSucc).
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement