Advertisement
Guest User

jeu fini

a guest
Dec 13th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.85 KB | None | 0 0
  1. #include "projet_simulation_partie2.h"
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <stdio.h>
  5. #define NBPERS 2
  6.  
  7. // FONCTION GENERATION ALEATOIRE
  8.  
  9. int aleatoire(int limite){ //fonction qu'on utilise dans init_personnage pour générer des valeurs aléatoirement dans un intervalle défini
  10. return rand() % limite;
  11. }
  12.  
  13. // FONCTION INITIALISATION PERSONNAGE
  14.  
  15. struct personnage init_personnage (char clan) {
  16.  
  17.  
  18. struct personnage p;
  19. p.Point_De_Vie=aleatoire(101); //génération aléatoire des points de vie, pos, fdf, ... via la fonction aléatoire ci-dessus
  20. p.Position_X=aleatoire(50);
  21. p.Position_Y=aleatoire(50);
  22. p.Portee=aleatoire(16);
  23. p.Puissance_de_Frappe=aleatoire(50);
  24. p.Clan=clan;
  25.  
  26. return p;
  27. }
  28.  
  29.  
  30. // FONCTION DECLARATION PERSONNAGES DANS CHAQUE CLAN
  31.  
  32. struct simulation init_simulation() {
  33.  
  34. int NbPersonnageRouge=0;
  35. int NbPersonnageNoir=0;
  36. int x=0;
  37. struct simulation simu;
  38. while(x<NBPERS){
  39. simu.Rouge[NbPersonnageRouge]=init_personnage('R');
  40. simu.Noir[NbPersonnageNoir]=init_personnage('N');
  41. NbPersonnageRouge=NbPersonnageRouge+1;
  42. NbPersonnageNoir=NbPersonnageNoir+1;
  43. x=x+1;
  44. }
  45.  
  46. return simu;
  47. }
  48. // FONCTION AFFICHER TABLEAU PERSONNAGE
  49.  
  50. void afficher_tableau_personnage(struct simulation simu) {
  51.  
  52. int x=0;
  53. while (x<NBPERS) {
  54. printf("\nPersonnage %i du clan Rouge : " ,x);
  55. afficher_personnage(simu.Rouge[x]);
  56. printf("\nPersonnage %i du clan Noir : " ,x);
  57. afficher_personnage(simu.Noir[x]);
  58. x=x+1;
  59. }
  60. }
  61. // FONCTION AFFICHER PERSONNAGE
  62.  
  63. void afficher_personnage (struct personnage p) {
  64. if (p.Point_De_Vie!=0) {
  65. printf("\nPoints de vie : %d", p.Point_De_Vie); //affichage en décimal (%d) des variables via printf
  66. printf("\nPosition X: %d" , p.Position_X);
  67. printf("\nPosition Y: %d" , p.Position_Y);
  68. printf("\nPortee : %d", p.Portee);
  69. printf("\nPuissance de frappe : %d", p.Puissance_de_Frappe);
  70. printf("\nClan : %c", p.Clan);
  71. printf("\n");
  72. }
  73. else if (p.Point_De_Vie==0) {
  74. printf(" R.I.P");
  75. printf("\nLocalisation Cadavre :");
  76. printf("\nPosition X: %d" , p.Position_X);
  77. printf("\nPosition Y: %d" , p.Position_Y);
  78. }
  79. }
  80.  
  81. // FONCTION DEPLACEMENT
  82.  
  83. struct personnage Deplacement (struct personnage p) {
  84.  
  85. int deplacement;
  86. deplacement=aleatoire(4);
  87.  
  88. if ((deplacement==0)&&(p.Position_Y!=0)&&(p.Point_De_Vie!=0)) {
  89. p.Position_Y=p.Position_Y-1;}
  90. else if ((deplacement==1)&&(p.Position_Y!=49)&&(p.Point_De_Vie!=0)) {
  91. p.Position_Y=p.Position_Y+1;}
  92. else if ((deplacement==2)&&(p.Position_X!=0)&&(p.Point_De_Vie!=0)) {
  93. p.Position_X=p.Position_X-1;}
  94. else if ((deplacement==3)&&(p.Position_X!=49)&&(p.Point_De_Vie!=0)) {
  95. p.Position_X=p.Position_X+1;}
  96.  
  97. return p;
  98. }
  99.  
  100. // FONCTION MODIFIANT LA STRUCTURE DE SIMULATION (Déplacement)
  101.  
  102. struct simulation ModificationPosition (struct simulation simu) {
  103.  
  104.  
  105. int x=0;
  106. int NbPersonnageRouge=0;
  107. int NbPersonnageNoir=0;
  108. while(x<NBPERS){
  109. simu.Rouge[NbPersonnageRouge]=Deplacement(simu.Rouge[NbPersonnageRouge]);
  110. simu.Noir[NbPersonnageNoir]=Deplacement(simu.Noir[NbPersonnageNoir]);
  111. NbPersonnageRouge=NbPersonnageRouge+1;
  112. NbPersonnageNoir=NbPersonnageNoir+1;
  113. x=x+1;
  114. }
  115. return simu;
  116. }
  117.  
  118.  
  119.  
  120. // FONCTION POUR DETERMINER SI UN PERSONNAGE 1 PEUT ATTAQUER UN PERSONNAGE 2 (QUESTION 7)
  121.  
  122. int A_Portee_De_Tir(struct personnage a, struct personnage b) {
  123.  
  124. int PossibiliteDattaque;
  125. if ((abs(a.Position_X-b.Position_X)<=a.Portee)&&(abs(a.Position_Y-b.Position_Y)<=a.Portee)) {
  126. PossibiliteDattaque=1;
  127. }
  128. else {
  129. PossibiliteDattaque=0;
  130. }
  131. return PossibiliteDattaque;
  132. }
  133.  
  134.  
  135.  
  136.  
  137.  
  138. // FONCTION POUR DECREMENTER LES POINTS DE VIE
  139.  
  140. struct personnage Blessure(struct personnage a, struct personnage b) {
  141.  
  142.  
  143. if ((A_Portee_De_Tir(a,b)==1)&&(a.Point_De_Vie!=0)) {
  144. b.Point_De_Vie=b.Point_De_Vie-a.Puissance_de_Frappe;
  145. }
  146. if (b.Point_De_Vie<0) {
  147. b.Point_De_Vie=0;
  148. }
  149.  
  150. return b;
  151.  
  152. }
  153.  
  154. // FONCTION MODIFIANT LA STRUCTURE DE SIMULATION (Attaque)
  155.  
  156. struct simulation Tir(struct simulation simu) {
  157.  
  158.  
  159. int y=0;
  160. while (y<NBPERS) {
  161. int x=0;
  162. while (x<NBPERS){
  163. simu.Noir[y]=Blessure(simu.Rouge[x] , simu.Noir[y]);
  164. simu.Rouge[y]=Blessure(simu.Noir[x] , simu.Rouge[y]);
  165. x=x+1;
  166. }
  167. y=y+1;
  168. }
  169.  
  170. return simu;
  171.  
  172. }
  173.  
  174. /////////////////////////////////////// QUESTION 8 //////////////////////////////////
  175.  
  176. int TestFin(struct personnage p) { // on teste si les points de vie du personnage sont à zéro
  177.  
  178. int mort=0;
  179. if (p.Point_De_Vie==0) {
  180. mort=1;
  181. }
  182. return mort;
  183. }
  184.  
  185.  
  186. int mort (struct simulation simu) {
  187.  
  188. int Fin=0;
  189. int CompteurMortRouge=0;
  190. int CompteurMortNoir=0;
  191. int x=0;
  192.  
  193. while (x<NBPERS) {
  194. if (TestFin(simu.Rouge[x])==1) {
  195. CompteurMortRouge=CompteurMortRouge+1;
  196. }
  197. x=x+1;
  198. }
  199. x=0;
  200.  
  201. while (x<NBPERS) {
  202. if (TestFin(simu.Noir[x])==1) {
  203. CompteurMortNoir=CompteurMortNoir+1;
  204. }
  205. x=x+1;
  206. }
  207.  
  208. if ((CompteurMortRouge==NBPERS)||(CompteurMortNoir==NBPERS)) {
  209. printf ("\nsimulation finie\n");
  210. Fin=1;
  211. }
  212. else {
  213. printf("\nle combat continue\n");
  214.  
  215. }
  216. return Fin;
  217. }
  218. ===============================================================================
  219. //déclaration des fonctions
  220. #define NBPERS 2
  221.  
  222. int aleatoire(int limite); // Fonction génération aléatoire (Partie 1)
  223.  
  224. typedef struct personnage personnage; //definition du type struct personnage
  225.  
  226. struct personnage {
  227.  
  228. int Point_De_Vie; // déclaration de la structure et ses variables
  229. int Position_X;
  230. int Position_Y;
  231. int Portee;
  232. int Puissance_de_Frappe;
  233. char Clan;
  234. };
  235.  
  236. typedef personnage clan[NBPERS];
  237.  
  238.  
  239. struct simulation {
  240. clan Rouge;
  241. clan Noir;
  242. int NbTourSimulation;
  243. };
  244.  
  245. // fonction initialisation personnage (Partie 1)
  246.  
  247. struct personnage init_personnage (char clan);
  248.  
  249. // fonction affichage personnage (partie 1)
  250.  
  251. void afficher_personnage (struct personnage p);
  252.  
  253. // fonction pour afficher un tableau de personnage
  254.  
  255. void afficher_tableau_personnage (struct simulation simu);
  256.  
  257. //initialisation simulation
  258.  
  259. struct simulation init_simulation();
  260.  
  261. // fonction déplacement
  262.  
  263. struct personnage Deplacement(struct personnage p);
  264.  
  265. //modification deplacement
  266.  
  267. struct simulation ModificationPosition(struct simulation simu);
  268.  
  269.  
  270. // FONCTION POUR DETERMINER SI UN PERSONNAGE A PEUT ATTAQUER UN PERSONNAGE B (QUESTION 7)
  271.  
  272. int A_Portee_De_Tir(struct personnage a, struct personnage b);
  273.  
  274. struct personnage Blessure(struct personnage a, struct personnage b);
  275.  
  276. struct simulation Tir(struct simulation simu);
  277.  
  278. // TEST SI UN CLAN EST MORT (QUESTION 8)
  279.  
  280. int TestFin(struct personnage p);
  281.  
  282. int mort (struct simulation simu);
  283. ==============================================================
  284. #include "projet_simulation_partie2.h"
  285. #include <stdlib.h>
  286. #include <time.h>
  287. #include <stdio.h>
  288.  
  289. int main() {
  290.  
  291. srand(time(NULL));
  292. typedef struct simulation simulation;
  293. struct simulation simu;
  294.  
  295. printf("\nInitialisation de la simulation\n");
  296.  
  297. simu = init_simulation();
  298. afficher_tableau_personnage(simu);
  299. int Fin=0;
  300. int NbTour=0;
  301. while (Fin!=1) {
  302.  
  303. printf("\nTour %d \n", NbTour);
  304. NbTour = NbTour+1;
  305. simu = ModificationPosition(simu);
  306. simu = Tir(simu);
  307. afficher_tableau_personnage(simu);
  308. Fin = mort(simu);
  309.  
  310. }
  311.  
  312.  
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement