Advertisement
Guest User

Untitled

a guest
May 31st, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. % Tic Tac Toe
  2. % <01-06-2016>
  3. % <Mehdi Gunes>, <11035021>, <amehdigunes@gmail.com>
  4. % <Mathijs Maijer, <11043024>, <matevy1@gmail.com>
  5.  
  6. % Algorithms:
  7. :- include(minimax).
  8. %:- include(alphabeta).
  9. %:- include(followstrategy).
  10.  
  11. grid([ [i, i, i],
  12. [i, i, i],
  13. [i, i, i] ]).
  14.  
  15.  
  16.  
  17.  
  18. % Legal moves:
  19. legalmove(Length/Height, Grid):-
  20. heightlist(Grid, Height, HeightList),
  21. lengthlist(HeightList, Length, i).
  22.  
  23. heightlist([List | _], 1, List).
  24.  
  25. heightlist([_ | T], Length, List):-
  26. Length2 is Length - 1,
  27. heightlist(T, Length2, List).
  28.  
  29. lengthlist([State | _], 1, State).
  30.  
  31. lengthlist([_ | T], Length, State):-
  32. Length2 is Length - 1,
  33. lengthlist(T, Length2, State).
  34.  
  35. % Winning:
  36. win(Grid, Winner):-
  37. (Y = 1; Y = 2; Y = 3),
  38. heightlist(Grid, Y, HorizontalLine),
  39. HorizontalLine = [X, X, X],
  40. X \= i,
  41. ((X = x, Winner = x); (X = o, Winner = o)).
  42.  
  43. win(Grid, Winner):-
  44. heightlist(Grid, 1, HorizontalLine1),
  45. heightlist(Grid, 2, HorizontalLine2),
  46. heightlist(Grid, 3, HorizontalLine3),
  47. lengthlist(HorizontalLine1, 1, X),
  48. lengthlist(HorizontalLine2, 2, X),
  49. lengthlist(HorizontalLine3, 3, X),
  50. X \= i,
  51. ((X = x, Winner = x); (X = o, Winner = o)).
  52.  
  53. win(Grid, Winner):-
  54. heightlist(Grid, 1, HorizontalLine1),
  55. heightlist(Grid, 2, HorizontalLine2),
  56. heightlist(Grid, 3, HorizontalLine3),
  57. lengthlist(HorizontalLine1, 3, X),
  58. lengthlist(HorizontalLine2, 2, X),
  59. lengthlist(HorizontalLine3, 1, X),
  60. X \= i,
  61. ((X = x, Winner = x); (X = o, Winner = o)).
  62.  
  63. win(Grid, Winner):-
  64. heightlist(Grid, 1, HorizontalLine1),
  65. heightlist(Grid, 2, HorizontalLine2),
  66. heightlist(Grid, 3, HorizontalLine3),
  67. (Y = 1; Y = 2; Y = 3),
  68. lengthlist(HorizontalLine1, Y, X),
  69. lengthlist(HorizontalLine2, Y, X),
  70. lengthlist(HorizontalLine3, Y, X),
  71. X \= i,
  72. ((X = x, Winner = x); (X = o, Winner = o)).
  73.  
  74. % Draw
  75. draw([]).
  76.  
  77. draw([H | T]):-
  78. checkdraw(H),
  79. draw(T).
  80.  
  81. checkdraw([]).
  82.  
  83. checkdraw([H | T]):-
  84. H \= i,
  85. checkdraw(T).
  86.  
  87. % Find all possible moves:
  88.  
  89. moves(State, PossibleStates):-
  90. move(State, PossibleMoves),
  91. movesToGrid(State, PossibleMoves, PossibleStates), !.
  92.  
  93. move(State, PossibleMoves):-
  94. move1(State, 1/1, PossibleMoves).
  95.  
  96. move1(_, 4/1, []).
  97.  
  98. move1(State, L/H, PossibleMoves):-
  99. L < 4,
  100. move2(State, L/H, PossibleMoves2),
  101. L2 is L+1,
  102. move1(State, L2/H, PossibleMoves3),
  103. append(PossibleMoves2, PossibleMoves3, PossibleMoves).
  104.  
  105. move1(_,_,[]).
  106.  
  107. move2(_, _/4, []).
  108.  
  109. move2(State, L/H, PossibleMoves):-
  110. H < 4,
  111. legalmove(L/H, State),
  112. H2 is H+1,
  113. move2(State, L/H2, PossibleMoves2),
  114. PossibleMoves = [L/H | PossibleMoves2].
  115.  
  116. move2(State, L/H, PossibleMoves):-
  117. H < 4,
  118. H2 is H+1,
  119. move2(State, L/H2, PossibleMoves).
  120.  
  121. move2(_, _, []).
  122.  
  123.  
  124. movesToGrid(_, [], []).
  125.  
  126. movesToGrid(State, [H | T], Grids1):-
  127. applyMove(x, State, H, Grid),
  128. movesToGrid(State, T, Grids2),
  129. Grids1 = [Grid | Grids2].
  130.  
  131. % Apply moves
  132. applyMove(Player, Grid, Length/Height, NextState):-
  133. heightlist(Grid, Height, HeightList),
  134. replace(HeightList, Length, _, ChangedLengthList, Player),
  135. replace2(Grid, Height, _, ChangedLengthList, NextState).
  136.  
  137. replace2([_ | T], 1, List, ChangedLengthList, Result):-
  138. append(List, [ChangedLengthList | T], Result).
  139.  
  140. replace2([H | T], Length, List, ChangedLengthList, Result):-
  141. Length2 is Length - 1,
  142. append(List, [H], List2),
  143. replace2(T, Length2, List2, ChangedLengthList, Result).
  144.  
  145. replace([_ | T], 1, List, Result, Player):-
  146. append(List, [Player | T], Result).
  147.  
  148. replace([H | T], Length, List, Result, Player):-
  149. Length2 is Length - 1,
  150. append(List, [H], List2),
  151. replace(T, Length2, List2, Result, Player).
  152.  
  153. staticval(State, 1):-
  154. win(State, x).
  155.  
  156. staticval(State, -1):-
  157. win(State, o).
  158.  
  159. staticval(State, 0):-
  160. draw(State).
  161.  
  162. % Animate
  163.  
  164. animate(Grid):-
  165. jump(30),
  166. write(' -------------'), nl,
  167. animate2(Grid),
  168. jump(10), !.
  169.  
  170. animate2([]).
  171.  
  172. animate2([H | T]):-
  173. animate_symbols(H), nl,
  174. write(' -------------'), nl,
  175. animate2(T).
  176.  
  177. animate_symbols([]).
  178.  
  179. animate_symbols([H | T]):-
  180. write('| '),
  181. write(H),
  182. write(' |'),
  183. animate_symbols(T).
  184.  
  185. jump(0).
  186.  
  187. jump(Counter):-
  188. Counter2 is Counter - 1,
  189. nl,
  190. jump(Counter2).
  191.  
  192. % User input
  193. min_to_move(Grid):-
  194. count(Grid, x, N1),
  195. count(Grid, o, N2),
  196. N1 > N2.
  197.  
  198. max_to_move(Grid):-
  199. count(Grid, x, N1),
  200. count(Grid, o, N2),
  201. N1 = N2.
  202.  
  203. count([], _, 0).
  204.  
  205. count([H | T], S, N):-
  206. count2(H, S, N2),
  207. count(T, S, N3),
  208. N is N2 + N3.
  209.  
  210. count2([], _, 0).
  211.  
  212. count2([S | T], S, N):-
  213. count2(T, S, N2),
  214. N is N2 +1.
  215.  
  216. count2([H | T], S, N):-
  217. H \= S,
  218. count2(T, S, N).
  219.  
  220.  
  221. % Run
  222. /*
  223. :- grid(Grid),
  224. animate(Grid),
  225. write('Please make your step: length/height.'), nl,
  226. read(Input),
  227. legalmove(Input, Grid),
  228. applyMove(x, Grid, Input, NextState),
  229. animate(NextState),
  230. sleep(3),
  231. minimax(Grid, BestSucc, _),
  232. animate(BestSucc).
  233. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement