Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /* On appelle les fonctions */
  5.  
  6. void init_carte (int tab[20][20]);
  7. void affiche_carte (int tab[20][20],int perso[2]);
  8. int deplace_perso (int a,int tab [20][20], int perso[2]);
  9.  
  10. /* Programme principal */
  11. int main()
  12. {
  13. int tab[20][20];
  14. int perso[2];
  15. int a;
  16. int b;
  17. int or=0;
  18. int l;
  19. int vie=0;
  20. int i;
  21. int j;
  22. int clef=0;
  23. int h;
  24.  
  25. printf(" Veuillez taper sur l'un des numéros suivants\n");
  26. printf(" pour effectuer un déplacement: 8=haut ,2=bas ,4=gauche ,6=droite\n");
  27.  
  28. perso[0]=0;
  29. perso[1]=0;
  30.  
  31. init_carte (tab);
  32. affiche_carte (tab,perso);
  33.  
  34. printf("Combien de pièces d'OR avez-vous trouvez :0\n");
  35. printf("Vies restantes :10\n");
  36. printf("Nombre de clef(s):0\n");
  37.  
  38. do{
  39.  
  40. a=deplace_perso (a,tab, perso);
  41. i=perso[0];
  42. j=perso[1];
  43. /* On empêche le personnage de sortir de la map sur l'axe x et on affiche une erreur si l'utilisateur tente de le faire */
  44. if (perso[0]<0)
  45. {
  46. affiche_carte (tab,perso);
  47. printf("Désolé, déplacement impossible car vous allez sortir de la map\n");
  48. perso[0]=0;
  49. }
  50. else if(perso[0]>19)
  51. {
  52. affiche_carte (tab,perso);
  53. printf("Désolé, déplacement impossible car vous allez sortir de la map\n");
  54. perso[0]=19;
  55. }
  56. /* On empêche le personnage de sortir de la map sur l'axe y et on affiche une erreur si l'utilisateur tente de le faire */
  57. else if(perso[1]<0)
  58. {
  59. affiche_carte (tab,perso);
  60. printf("Désolé, déplacement impossible car vous allez sortir de la map\n");
  61. perso[1]=0;
  62. }
  63. else if(perso[1]>19)
  64. {
  65. affiche_carte (tab,perso);
  66. printf("Désolé, déplacement impossible car vous allez sortir de la map\n");
  67. perso[1]=19;
  68. }
  69. else
  70. {
  71. affiche_carte (tab,perso);
  72. }
  73.  
  74. if(tab[perso[1]][perso[0]]==5)
  75. {
  76. tab[perso[1]][perso[0]]=0; /* Quand la position du personnage est sur 5, on ajoute une pièce d'or */
  77. b=1;
  78. or=b+or;
  79. }
  80.  
  81. if(tab[perso[1]][perso[0]]==4)
  82. {
  83. tab[perso[1]][perso[0]]=0; /* Quand la position du personnage est sur 4, on ajoute une clef */
  84. h=1;
  85. clef=h+clef;
  86. }
  87.  
  88. if((tab[perso[1]][perso[0]]==7) || (tab[perso[1]][perso[0]]==8))
  89. { /* Quand la position du personnage est sur 7 ou 8, on enleve une vie */
  90. if((tab[perso[1]][perso[0]]==7) || (tab[perso[1]][perso[0]]==8))
  91. {
  92. tab[perso[1]][perso[0]]=0;
  93. tab[i][j] = 0;
  94. }
  95. l=1;
  96. vie=vie+l;
  97. }
  98.  
  99. if((tab[perso[1]][perso[0]]==6))
  100. { /* Quand la position du personnage est sur 6, on enleve une clef */
  101. if((tab[perso[1]][perso[0]]==6))
  102. {
  103. tab[perso[1]][perso[0]]=0;
  104. tab[i][j] = 0;
  105. }
  106. h=1;
  107. clef=clef-h;
  108. }
  109.  
  110. printf("Combien de pièces d'OR avez-vous trouvez :%d\n",or);
  111. printf("Vies restantes : %d\n",(10-vie));
  112. printf("Nombre de clef(s):0%d\n",(0+clef));
  113.  
  114. }while( (a!=0) && (or!=10) && ( (10-vie)!=0) );
  115.  
  116. if(or==10)
  117. {
  118. printf("Bien jouer, vous avez gagnez\n");
  119. }
  120.  
  121. if((10-vie)==0)
  122. {
  123. printf("Perdu,retentez votre chance\n");
  124. }
  125.  
  126. if(a==0)
  127. {
  128. printf("Au revoir, merci d'avoir jouer\n");
  129. }
  130.  
  131. return 0;
  132.  
  133. }
  134. /* Fonctions qui seront utilisées dans le main */
  135.  
  136. /* Initialisation de la map */
  137.  
  138. void init_carte (int tab[20][20])
  139. {
  140. int i,j;
  141. system("clear");
  142.  
  143. //Initialisation de la map
  144. for(i=0; i<20; i++)
  145. {
  146. for(j=0; j<20; j++)
  147. {
  148. tab[i][j]=0;
  149. }
  150. }
  151.  
  152. //Rendu 'aléatoire'
  153.  
  154. for(i=0; i<20; i++)
  155. {
  156. for(j=0; j<20; j++)
  157. {
  158.  
  159. tab[i][j]=(i*j)%10;
  160.  
  161. }
  162. }
  163.  
  164. for(i=10; i<11; i++)
  165. {
  166. for(j=0; j<20; j++)
  167. {
  168.  
  169. tab[i][j]=3;
  170.  
  171. }
  172. }
  173.  
  174. for(i=0; i<20; i++)
  175. {
  176. for(j=10; j<11; j++)
  177. {
  178.  
  179. tab[i][j]=3;
  180.  
  181. }
  182. }
  183. tab[10][5]=6;
  184. tab[10][15]=6;
  185. tab[5][10]=6;
  186. tab[5][15]=6;
  187. //Zone de sécurité
  188. for(i=1; i<3; i++)
  189. {
  190. for(j=1; j<3; j++)
  191. {
  192.  
  193. tab[i][j]=0;
  194.  
  195. }
  196. }
  197. }
  198.  
  199.  
  200.  
  201. /* Affichage de la map */
  202.  
  203. void affiche_carte (int tab[20][20],int perso[2])
  204. {
  205. int i,j;
  206.  
  207. if ((perso[0]<0) || (perso[1]<0) || (perso[1]>19) || (perso[0]>19))
  208. {
  209. printf("Erreur\n");
  210. }
  211. else
  212. {
  213. system("clear");
  214. for(i=0 ; i<20 ;i++)
  215. {
  216. for(j=0 ; j<20 ; j++)
  217. {
  218. if((j==perso[0]) && (i==perso[1]))
  219. {
  220. printf("X ");
  221. }
  222. else
  223. {
  224. printf("%d ",tab[i][j]);
  225. }
  226. }
  227. printf("\n");
  228. }
  229. }
  230. }
  231.  
  232. /* Déplacement du personnage */
  233.  
  234. int deplace_perso (int a,int tab[20][20],int perso[2])
  235. {
  236. scanf("%d", &a);
  237. int clef;
  238. if(a==6)
  239. {
  240. if((tab[perso[1]][perso[0]+1]==2) || (tab[perso[1]][perso[0]+1]==3) || ((tab[perso[1]][perso[0]+1]==6) && clef>0))
  241. {
  242. perso[0]=perso[0];
  243. }
  244. else
  245. {
  246. perso[0]=perso[0]+1;
  247. perso[1]=perso[1];
  248. }
  249. }
  250.  
  251. if(a==4)
  252. {
  253. if((tab[perso[1]][perso[0]-1]==2) || (tab[perso[1]][perso[0]-1]==3) || ((tab[perso[1]][perso[0]-1]==6) && clef>0))
  254. {
  255. perso[0]=perso[0];
  256. }
  257. else
  258. {
  259. perso[0]=perso[0]-1;
  260. perso[1]=perso[1];
  261. }
  262. }
  263.  
  264. if (a==8)
  265. {
  266. if((tab[perso[1]-1][perso[0]]==2) || (tab[perso[1]-1][perso[0]]==3)|| ((tab[perso[1]][perso[0]-1]==6) && clef>0))
  267. {
  268. perso[1]=perso[1];
  269. }
  270. else
  271. {
  272. perso[0]=perso[0];
  273. perso[1]=perso[1]-1;
  274. }
  275. }
  276.  
  277. if (a==2)
  278. {
  279. if((tab[perso[1]+1][perso[0]]==2) || (tab[perso[1]+1][perso[0]]==3)|| ((tab[perso[1]][perso[0]+1]==6) && clef>0))
  280. {
  281. perso[1]=perso[1];
  282. }
  283. else
  284. {
  285. perso[0]=perso[0];
  286. perso[1]=perso[1]+1;
  287. }
  288. }
  289.  
  290. return a;
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement