Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- :- consult('grille.pl').
- /* Spécifications de notre jeu
- - : case vide
- x : coup joué par l'utilisateur
- o : coup joué par l'IA
- */
- /*Point d'entrée du programme*/
- commencer :- write('Bonjour, veuillez chosir votre prochain coup par le biais du clavier (touches 1 a 7) \n\r'), nl,
- get_grille(X),
- afficher_grille(X), nl, nl,
- mouvement_joueur(X, _).
- /* UTILS */
- /* Incrementing a number X */
- incr(X, X1) :- X1 is X + 1.
- /* Does nothing. Is use to copy the value A the second parameter.*/
- copy(A, A).
- /* finds the column number associated with the specific list */
- find_column_number([], _, 8, _).
- find_column_number([H|_], C, N, M):- H = C, M is N.
- find_column_number([_|T], C, N, M):- N1 is N+1, find_column_number(T, C, N1, M).
- /* finds the first free column of the grille */
- find_a_free_column([], 8, _).
- find_a_free_column([H|T], N, M):- contient_pas('-', H), N1 is N+1, find_a_free_column(T, N1, M).
- find_a_free_column([H|_], N, M):-M is N, not(contient_pas('-', H)).
- /* find 3 characters in 3 lists are at the same indices, or increasing decreasing order */
- row3(X,Y,Z):- length(X,N),length(Y,N),length(Z,N).
- rightD3(X,Y,Z):- length(X,N1),length(Y,N2),length(Z,N3), N2 is N1+1, N3 is N2+1.
- leftD3(X,Y,Z):- length(X,N1),length(Y,N2),length(Z,N3), N2 is N1-1, N3 is N2-1.
- /* checks if a list does not contain a particular member */
- contient_pas(_, []).
- contient_pas(X, [H|T]):- X\=H, contient_pas(X, T).
- /* CONDITIONS de VICTOIRE */
- /* colonne de quatre */
- etat_final(grille(X), R):- append(_,[C|_], X) ,
- append(_,[R,R,R,R|_],C).
- /* rangee de quatre, ou diagonale de chaque direction */
- etat_final(grille(X), R):-
- append(_,[Col1, Col2, Col3, Col4|_], X),
- append(P1, [R|_], Col1),
- append(P2, [R|_], Col2),
- append(P3, [R|_], Col3),
- append(P4, [R|_], Col4),
- (row(P1,P2,P3,P4) ; rightD(P1,P2,P3,P4) ; leftD(P1,P2,P3,P4)).
- /* partie nulle*/
- etat_final(grille([])).
- etat_final(grille([H|T])):- contient_pas('-', H), etat_final(grille(T)).
- /* fonctions pour determiner si c'est une rang�e, ou diagonale. */
- row(W,X,Y,Z):- length(W,N),length(X,N),length(Y,N),length(Z,N).
- 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.
- 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.
- /*Les états conclusifs. Dependant du gagnant, un message different s'affiche. */
- etat_final_humain(X, R):- etat_final(X, R), R = 'x', write_gagnant(), nl, nl.
- etat_final_ordi(X, R):- etat_final(X, R), R = 'o', write_perdant(), nl, nl.
- etat_final_egal(X):- etat_final(X), write('Partie Nul!').
- write_gagnant() :- write_haut(),
- write("|--+ Vous avez gagne ! +--|"), nl,
- write_bas().
- write_perdant() :- write_haut(),
- write("|--+ Vous avez perdu ! +--|"), nl,
- write_bas().
- write_haut() :- write("+--------+----*----+--------+"), nl,
- write("|------+ +------|"), nl,
- write("|----+ +----|"), nl.
- write_bas() :- write("|----+ +----|"), nl,
- write("|------+ +------|"), nl,
- write("+--------+----*----+--------+").
- /* OPERATIONS */
- /* Validate player entry */
- column(1).
- column(2).
- column(3).
- column(4).
- column(5).
- column(6).
- column(7).
- column(_) :- write('Invalid column number.'), nl, nl, enter_column_number(_).
- enter_column_number(X) :- nl,
- write("*---------------------------*"), nl, write("| |"), nl,
- write('| Enter a column number : |'), nl,
- read(N),
- write("| |"), nl,
- write("*---------------------------*"), nl,
- nl, column(N), X is N.
- /* Place a coin in a column */
- add_coin_in_column(X, ['-'], [X]).
- add_coin_in_column(X, ['-', A|B], [X, A|B]) :- A \== '-'.
- add_coin_in_column(X, ['-', A|B], ['-', A1|B1]) :- (A == '-'),
- add_coin_in_column(X, [A|B], [A1|B1]).
- add_coin_in_column(_, [A|_], _) :- A \== '-',write('Wrong move : the column is full!'), nl, write('Game stopped.'), nl, nl.
- /* Find the desired column and move a coin in the column */
- find_column(X, [A|B], [A|B2], N, M) :- N \== M,
- incr(M, M2),
- copy(A, A),
- find_column(X, B, B2, N, M2).
- find_column(X, [A|B], [A1|B], N, M) :- N == M,
- copy(B, B),
- add_coin_in_column(X, A, A1).
- /* Move a coin in the specified column */
- move_coin(X, grille(Y), grille(Y2), N) :- find_column(X, Y, Y2, N, 1).
- /* Player move */
- mouvement_joueur(X, X2) :- enter_column_number(N),
- move_coin('x', X, X2, N),
- afficher_grille(X2), nl, nl,
- next_player('o', X2, _).
- /* Decide a winner */
- /* Decide who's turn to play */
- next_player('o', X, _) :- etat_final_humain(X, 'x').
- next_player('x', X, _) :- etat_final_ordi(X, 'o').
- next_player(_, X, _):- etat_final_egal(X).
- next_player('o', X, X2) :- machine(X, X2).
- next_player('x', X, X2) :- mouvement_joueur(X, X2).
- /* JOUEUR IA */
- /* Ici, l'ordi a plusieurs actions qu'il peut completer. En ordre de priorite:
- * 1. Bloque une colonne, rangee, ou diagonale gagnante du joueur
- * humain.
- * 2. Jouer un jeton gagnant.
- * 3. Bloque l'avance d'un joueur qui pourrait l'amener a gagner.
- * 4. Joueur un jeton adjacent a ses autres, pour s'avancer vers une
- * victoire.
- * 5. Dans le cas ou aucune de ses actions demeurent possible,
- * l'ordinateur joue dans une colonne vide. */
- /* L'ordinateur cherche une colonne ou les conditions s'appliquent, et il joue dans la colonne. */
- /* colonne de 3 */
- machine(grille(X), X2):- (append(_,[C|_], X),
- append(_,['-','x','x','x'|_],C))
- ->(find_column_number(X, C, 1, M),
- not(contient_pas('-', C)),
- machine_play(X, X2, M)).
- machine(grille(X), X2):- (append(_,[C|_], X),
- append(_,['-','o','o','o'|_],C))
- ->(find_column_number(X, C, 1, M),
- not(contient_pas('-', C)),
- machine_play(X, X2, M)).
- /* rangee, ou diagonale de deux ou plus */
- machine(grille(X), X2):-
- ( append(_,[Col1, Col2, Col3|_], X),
- append(P1, ['-'|_], Col1),
- append(P2, ['o'|_], Col2),
- append(P3, ['o'|_], Col3), (row3(P1,P2,P3); rightD3(P1,P2,P3); leftD3(P1,P2,P3)))
- -> (find_column_number(X, Col3, 1, M),
- not(contient_pas('-', Col3)),
- machine_play(X, X2, M)).
- machine(grille(X), X2):-
- ( append(_,[Col1, Col2, Col3|_], X),
- append(P1, ['o'|_], Col1),
- append(P2, ['o'|_], Col2),
- append(P3, ['-'|_], Col3), (row3(P1,P2,P3); rightD3(P1,P2,P3); leftD3(P1,P2,P3)))
- -> (find_column_number(X, Col1, 1, M),
- not(contient_pas('-', Col1)),
- machine_play(X, X2, M)).
- machine(grille(X), X2):-
- ( append(_,[Col1, Col2, Col3|_], X),
- append(P1, ['x'|_], Col1),
- append(P2, ['x'|_], Col2),
- append(P3, ['-'|_], Col3), (row3(P1,P2,P3); rightD3(P1,P2,P3); leftD3(P1,P2,P3)))
- -> (find_column_number(X, Col3, 1, M),
- not(contient_pas('-', Col3)),
- machine_play(X, X2, M)).
- machine(grille(X), X2):-
- ( append(_,[Col1, Col2, Col3|_], X),
- append(P1, ['-'|_], Col1),
- append(P2, ['x'|_], Col2),
- append(P3, ['x'|_], Col3), (row3(P1,P2,P3); rightD3(P1,P2,P3); leftD3(P1,P2,P3)))
- -> (find_column_number(X, Col1, 1, M),
- not(contient_pas('-', Col1)),
- machine_play(X, X2, M)).
- /* colonne de deux */
- machine(grille(X), X2):- (append(_,[C|_], X),
- append(_,['-','-','x','x'|_],C))
- ->(find_column_number(X, C, 1, M),
- not(contient_pas('-', C)),
- machine_play(X, X2, M)).
- machine(grille(X), X2):- (append(_,[C|_], X),
- append(_,['-','-','o','o'|_],C))
- ->(find_column_number(X, C, 1, M),
- not(contient_pas('-', C)),
- machine_play(X, X2, M)).
- /* colonne de un 'o' */
- machine(grille(X), X2):- (append(_,[C|_], X),
- append(_,['-','-','-','o'|_],C))
- ->(find_column_number(X, C, 1, M),
- not(contient_pas('-', C)),
- machine_play(X, X2, M)).
- /* dans le cas exception ou aucune des conditions s'appliquent */
- machine(grille(X), X2):- find_a_free_column(X, 1, M),machine_play(X, X2, M).
- /* l'action de jouer un jeton */
- machine_play(X, X2, C):- move_coin('o', grille(X), X2, C),
- afficher_grille(X2), nl, nl,
- next_player('x', X2, _).
- /********************************************************************************/
- /* --------------------------------------------------------------------------------------------
- Fichier relatif à la gestion de la grille et l'affichage de celle-ci
- --------------------------------------------------------------------------------------------*/
- get_grille(X):- X= grille([[-, -, -, -, -, -],
- [-, -, -, -, -, -],
- [-, -, -, -, -, -],
- [-, -, -, -, -, -],
- [-, -, -, -, -, -],
- [-, -, -, -, -, -],
- [-, -, -, -, -, -]]).
- /*Permet d'afficher les bordures horizontales de la grille*/
- afficher_grille(grille(X)):- write("+---+---+---+---+---+---+---+"), nl,
- show(X,0),
- afficher_numeros.
- show(_,6).
- /*Permet d'afficher les bordures verticales de la grille*/
- show(A,N):- write("| "), afficher_ligne(A,B),
- nl,
- M is N + 1,
- show(B, M).
- afficher_ligne([],_) :- nl, write("+---+---+---+---+---+---+---+").
- afficher_ligne([[A|B]|C],[B|D]):- afficher_carreau(A), write(' | '), afficher_ligne(C,D).
- afficher_carreau(A) :- (A == '-') -> write(" "); write(A).
- /* Affiche les numéros de colonne */
- afficher_numeros :- write('| 1 | 2 | 3 | 4 | 5 | 6 | 7 |').
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement