Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.30 KB | None | 0 0
  1. :- consult('grille.pl').
  2.  
  3. /* Spécifications de notre jeu
  4. - : case vide
  5. x : coup joué par l'utilisateur
  6. o : coup joué par l'IA
  7. */
  8.  
  9. /*Point d'entrée du programme*/
  10. commencer :- write('Bonjour, veuillez chosir votre prochain coup par le biais du clavier (touches 1 a 7) \n\r'), nl,
  11. get_grille(X),
  12. afficher_grille(X), nl, nl,
  13. mouvement_joueur(X, _).
  14.  
  15.  
  16.  
  17.  
  18.  
  19. /* UTILS */
  20. /* Incrementing a number X */
  21. incr(X, X1) :- X1 is X + 1.
  22.  
  23. /* Does nothing. Is use to copy the value A the second parameter.*/
  24. copy(A, A).
  25.  
  26. /* finds the column number associated with the specific list */
  27. find_column_number([], _, 8, _).
  28. find_column_number([H|_], C, N, M):- H = C, M is N.
  29. find_column_number([_|T], C, N, M):- N1 is N+1, find_column_number(T, C, N1, M).
  30.  
  31. /* finds the first free column of the grille */
  32. find_a_free_column([], 8, _).
  33. find_a_free_column([H|T], N, M):- contient_pas('-', H), N1 is N+1, find_a_free_column(T, N1, M).
  34. find_a_free_column([H|_], N, M):-M is N, not(contient_pas('-', H)).
  35.  
  36. /* find 3 characters in 3 lists are at the same indices, or increasing decreasing order */
  37. row3(X,Y,Z):- length(X,N),length(Y,N),length(Z,N).
  38. rightD3(X,Y,Z):- length(X,N1),length(Y,N2),length(Z,N3), N2 is N1+1, N3 is N2+1.
  39. leftD3(X,Y,Z):- length(X,N1),length(Y,N2),length(Z,N3), N2 is N1-1, N3 is N2-1.
  40.  
  41. /* checks if a list does not contain a particular member */
  42. contient_pas(_, []).
  43. contient_pas(X, [H|T]):- X\=H, contient_pas(X, T).
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53. /* CONDITIONS de VICTOIRE */
  54. /* colonne de quatre */
  55. etat_final(grille(X), R):- append(_,[C|_], X) ,
  56. append(_,[R,R,R,R|_],C).
  57.  
  58.  
  59. /* rangee de quatre, ou diagonale de chaque direction */
  60. etat_final(grille(X), R):-
  61. append(_,[Col1, Col2, Col3, Col4|_], X),
  62. append(P1, [R|_], Col1),
  63. append(P2, [R|_], Col2),
  64. append(P3, [R|_], Col3),
  65. append(P4, [R|_], Col4),
  66. (row(P1,P2,P3,P4) ; rightD(P1,P2,P3,P4) ; leftD(P1,P2,P3,P4)).
  67.  
  68.  
  69. /* partie nulle*/
  70. etat_final(grille([])).
  71. etat_final(grille([H|T])):- contient_pas('-', H), etat_final(grille(T)).
  72.  
  73.  
  74. /* fonctions pour determiner si c'est une rang�e, ou diagonale. */
  75. row(W,X,Y,Z):- length(W,N),length(X,N),length(Y,N),length(Z,N).
  76. rightD(W,X,Y,Z):- length(W,N1),length(X,N2),length(Y,N3),length(Z,N4), N2 is N1+1, N3 is N2+1, N4 is N3+1.
  77. leftD(W,X,Y,Z):- length(W,N1),length(X,N2),length(Y,N3),length(Z,N4), N2 is N1-1, N3 is N2-1, N4 is N3-1.
  78.  
  79.  
  80. /*Les états conclusifs. Dependant du gagnant, un message different s'affiche. */
  81. etat_final_humain(X, R):- etat_final(X, R), R = 'x', write_gagnant(), nl, nl.
  82. etat_final_ordi(X, R):- etat_final(X, R), R = 'o', write_perdant(), nl, nl.
  83. etat_final_egal(X):- etat_final(X), write('Partie Nul!').
  84.  
  85. write_gagnant() :- write_haut(),
  86. write("|--+ Vous avez gagne ! +--|"), nl,
  87. write_bas().
  88.  
  89. write_perdant() :- write_haut(),
  90. write("|--+ Vous avez perdu ! +--|"), nl,
  91. write_bas().
  92.  
  93. write_haut() :- write("+--------+----*----+--------+"), nl,
  94. write("|------+ +------|"), nl,
  95. write("|----+ +----|"), nl.
  96.  
  97. write_bas() :- write("|----+ +----|"), nl,
  98. write("|------+ +------|"), nl,
  99. write("+--------+----*----+--------+").
  100.  
  101.  
  102.  
  103. /* OPERATIONS */
  104. /* Validate player entry */
  105. column(1).
  106. column(2).
  107. column(3).
  108. column(4).
  109. column(5).
  110. column(6).
  111. column(7).
  112. column(_) :- write('Invalid column number.'), nl, nl, enter_column_number(_).
  113. enter_column_number(X) :- nl,
  114. write("*---------------------------*"), nl, write("| |"), nl,
  115. write('| Enter a column number : |'), nl,
  116. read(N),
  117. write("| |"), nl,
  118. write("*---------------------------*"), nl,
  119. nl, column(N), X is N.
  120.  
  121. /* Place a coin in a column */
  122. add_coin_in_column(X, ['-'], [X]).
  123. add_coin_in_column(X, ['-', A|B], [X, A|B]) :- A \== '-'.
  124. add_coin_in_column(X, ['-', A|B], ['-', A1|B1]) :- (A == '-'),
  125. add_coin_in_column(X, [A|B], [A1|B1]).
  126. add_coin_in_column(_, [A|_], _) :- A \== '-',write('Wrong move : the column is full!'), nl, write('Game stopped.'), nl, nl.
  127.  
  128. /* Find the desired column and move a coin in the column */
  129. find_column(X, [A|B], [A|B2], N, M) :- N \== M,
  130. incr(M, M2),
  131. copy(A, A),
  132. find_column(X, B, B2, N, M2).
  133. find_column(X, [A|B], [A1|B], N, M) :- N == M,
  134. copy(B, B),
  135. add_coin_in_column(X, A, A1).
  136.  
  137. /* Move a coin in the specified column */
  138. move_coin(X, grille(Y), grille(Y2), N) :- find_column(X, Y, Y2, N, 1).
  139.  
  140. /* Player move */
  141. mouvement_joueur(X, X2) :- enter_column_number(N),
  142. move_coin('x', X, X2, N),
  143. afficher_grille(X2), nl, nl,
  144. next_player('o', X2, _).
  145.  
  146. /* Decide a winner */
  147. /* Decide who's turn to play */
  148. next_player('o', X, _) :- etat_final_humain(X, 'x').
  149. next_player('x', X, _) :- etat_final_ordi(X, 'o').
  150. next_player(_, X, _):- etat_final_egal(X).
  151. next_player('o', X, X2) :- machine(X, X2).
  152. next_player('x', X, X2) :- mouvement_joueur(X, X2).
  153.  
  154.  
  155.  
  156.  
  157. /* JOUEUR IA */
  158. /* Ici, l'ordi a plusieurs actions qu'il peut completer. En ordre de priorite:
  159. * 1. Bloque une colonne, rangee, ou diagonale gagnante du joueur
  160. * humain.
  161. * 2. Jouer un jeton gagnant.
  162. * 3. Bloque l'avance d'un joueur qui pourrait l'amener a gagner.
  163. * 4. Joueur un jeton adjacent a ses autres, pour s'avancer vers une
  164. * victoire.
  165. * 5. Dans le cas ou aucune de ses actions demeurent possible,
  166. * l'ordinateur joue dans une colonne vide. */
  167. /* L'ordinateur cherche une colonne ou les conditions s'appliquent, et il joue dans la colonne. */
  168.  
  169. /* colonne de 3 */
  170. machine(grille(X), X2):- (append(_,[C|_], X),
  171. append(_,['-','x','x','x'|_],C))
  172. ->(find_column_number(X, C, 1, M),
  173. not(contient_pas('-', C)),
  174. machine_play(X, X2, M)).
  175.  
  176. machine(grille(X), X2):- (append(_,[C|_], X),
  177. append(_,['-','o','o','o'|_],C))
  178. ->(find_column_number(X, C, 1, M),
  179. not(contient_pas('-', C)),
  180. machine_play(X, X2, M)).
  181. /* rangee, ou diagonale de deux ou plus */
  182. machine(grille(X), X2):-
  183. ( append(_,[Col1, Col2, Col3|_], X),
  184. append(P1, ['-'|_], Col1),
  185. append(P2, ['o'|_], Col2),
  186. append(P3, ['o'|_], Col3), (row3(P1,P2,P3); rightD3(P1,P2,P3); leftD3(P1,P2,P3)))
  187. -> (find_column_number(X, Col3, 1, M),
  188. not(contient_pas('-', Col3)),
  189. machine_play(X, X2, M)).
  190.  
  191. machine(grille(X), X2):-
  192. ( append(_,[Col1, Col2, Col3|_], X),
  193. append(P1, ['o'|_], Col1),
  194. append(P2, ['o'|_], Col2),
  195. append(P3, ['-'|_], Col3), (row3(P1,P2,P3); rightD3(P1,P2,P3); leftD3(P1,P2,P3)))
  196. -> (find_column_number(X, Col1, 1, M),
  197. not(contient_pas('-', Col1)),
  198. machine_play(X, X2, M)).
  199.  
  200. machine(grille(X), X2):-
  201. ( append(_,[Col1, Col2, Col3|_], X),
  202. append(P1, ['x'|_], Col1),
  203. append(P2, ['x'|_], Col2),
  204. append(P3, ['-'|_], Col3), (row3(P1,P2,P3); rightD3(P1,P2,P3); leftD3(P1,P2,P3)))
  205. -> (find_column_number(X, Col3, 1, M),
  206. not(contient_pas('-', Col3)),
  207. machine_play(X, X2, M)).
  208.  
  209. machine(grille(X), X2):-
  210. ( append(_,[Col1, Col2, Col3|_], X),
  211. append(P1, ['-'|_], Col1),
  212. append(P2, ['x'|_], Col2),
  213. append(P3, ['x'|_], Col3), (row3(P1,P2,P3); rightD3(P1,P2,P3); leftD3(P1,P2,P3)))
  214. -> (find_column_number(X, Col1, 1, M),
  215. not(contient_pas('-', Col1)),
  216. machine_play(X, X2, M)).
  217.  
  218. /* colonne de deux */
  219. machine(grille(X), X2):- (append(_,[C|_], X),
  220. append(_,['-','-','x','x'|_],C))
  221. ->(find_column_number(X, C, 1, M),
  222. not(contient_pas('-', C)),
  223. machine_play(X, X2, M)).
  224. machine(grille(X), X2):- (append(_,[C|_], X),
  225. append(_,['-','-','o','o'|_],C))
  226. ->(find_column_number(X, C, 1, M),
  227. not(contient_pas('-', C)),
  228. machine_play(X, X2, M)).
  229. /* colonne de un 'o' */
  230. machine(grille(X), X2):- (append(_,[C|_], X),
  231. append(_,['-','-','-','o'|_],C))
  232. ->(find_column_number(X, C, 1, M),
  233. not(contient_pas('-', C)),
  234. machine_play(X, X2, M)).
  235.  
  236. /* dans le cas exception ou aucune des conditions s'appliquent */
  237. machine(grille(X), X2):- find_a_free_column(X, 1, M),machine_play(X, X2, M).
  238.  
  239. /* l'action de jouer un jeton */
  240. machine_play(X, X2, C):- move_coin('o', grille(X), X2, C),
  241. afficher_grille(X2), nl, nl,
  242. next_player('x', X2, _).
  243.  
  244. /********************************************************************************/
  245. /* --------------------------------------------------------------------------------------------
  246. Fichier relatif à la gestion de la grille et l'affichage de celle-ci
  247. --------------------------------------------------------------------------------------------*/
  248. get_grille(X):- X= grille([[-, -, -, -, -, -],
  249. [-, -, -, -, -, -],
  250. [-, -, -, -, -, -],
  251. [-, -, -, -, -, -],
  252. [-, -, -, -, -, -],
  253. [-, -, -, -, -, -],
  254. [-, -, -, -, -, -]]).
  255.  
  256.  
  257.  
  258.  
  259. /*Permet d'afficher les bordures horizontales de la grille*/
  260. afficher_grille(grille(X)):- write("+---+---+---+---+---+---+---+"), nl,
  261. show(X,0),
  262. afficher_numeros.
  263.  
  264. show(_,6).
  265. /*Permet d'afficher les bordures verticales de la grille*/
  266. show(A,N):- write("| "), afficher_ligne(A,B),
  267. nl,
  268. M is N + 1,
  269. show(B, M).
  270.  
  271.  
  272. afficher_ligne([],_) :- nl, write("+---+---+---+---+---+---+---+").
  273. afficher_ligne([[A|B]|C],[B|D]):- afficher_carreau(A), write(' | '), afficher_ligne(C,D).
  274.  
  275. afficher_carreau(A) :- (A == '-') -> write(" "); write(A).
  276.  
  277. /* Affiche les numéros de colonne */
  278. afficher_numeros :- write('| 1 | 2 | 3 | 4 | 5 | 6 | 7 |').
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement