Created
December 17, 2014 00:58
-
-
Save zyzo/1ede6992af6bdbb03507 to your computer and use it in GitHub Desktop.
Programme solveur du jeu Le Compte Est Bon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Programme solveur du jeu Le Compte Est Bon. | |
* Donnees : - Ensemble P de six nombres P = [P1, P2, P3, P4, P5, P6], | |
* chaque nombre tire aleatoirement parmi 1,2,3,4,5,7,8,9,10,25,50,75,100] | |
* - Nombre cible N | |
* Objectif : Trouver un ensemble d'oprations (+,-,*,// division entiere) utilisant | |
les nombres de P qui conduisent a N. Le tirage des nombres est sans remis | |
*/ | |
% Modelisation de l'etat initial et final | |
% Arguments : liste de nombre + liste d'actions + nombre cible N | |
etat_initial([[5, 100, 2, 6, 2, 1], 994]). | |
etat_final([[994|_R], 994]). | |
% Operations | |
operation(A, B, [A+B, Res]) :- Res is A+B, Res =< 999. | |
operation(A, B, [A-B, Res]) :- A > B, Res is A-B. | |
operation(A, B, [A*B, Res]) :- Res is A*B, Res =< 999. | |
operation(A, B, [A//B, Res]) :- 0 is A mod B, Res is A//B. | |
% Choix de deux elements de P1 et stocke dans P2 | |
choix_plaques(P1, A, B, P2) :- | |
select(A, P1, P3), | |
select(B, P3, P2). | |
% Associe un etat suivant Ej a un etat courant Ei apres realisation de l'action Aij | |
arc(Ei, Ej, [Aij, Res]) :- | |
Ei = [Pi | Nb], | |
Ej = [Pj | Nb], | |
choix_plaques(Pi, A, B, R), | |
operation(A, B, [Aij, Res]), | |
Pj = [Res|R]. | |
% Chercher un chemin entre 2 etats quelconques | |
chemin(Ei, Ei, []). | |
chemin(Ei, Ej, [Aik|Ckj]) :- arc(Ei, Ek, Aik), chemin(Ek, Ej, Ckj). | |
% Chemin depuis l'etat initial vers l'etat final | |
main(Solution) :- | |
etat_initial(I), | |
etat_final(F), | |
chemin(I, F, Solution). | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment