Se connecter avec
S'enregistrer | Connectez-vous

labyrinthe avec ncurses en langage C

Dernière réponse : dans Programmation

Bonjour
alors j'ai comme devoir de coder en langage c, en utilisant ncurses, un labyrinthe dans lequel se deplace un curseur, ce labyrinthe ayan des portes et des cles, une porte ne pouvant s'ouvrir et laisser passer le curseur que si ce dernier a deja la clé de cette porte.
Alors j'ai réussi à avoir le code suivant, qui permet au curseur de se deplacer dans le labyrinthe.
J'aimerais savoir si vous pouviez m'aider.
Merci d'avance


  1. #include <ncurses.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5.  
  6. void ncurses();
  7. void mat();
  8. void deplacer();
  9.  
  10. void ncurses(){
  11.  
  12. initscr();
  13. noecho();
  14. start_color();
  15. init_pair(1, COLOR_RED, COLOR_RED);
  16. init_pair(2, COLOR_RED, COLOR_BLUE);
  17. init_pair(3, COLOR_RED, COLOR_WHITE);
  18. init_pair(4, COLOR_RED, COLOR_GREEN);
  19.  
  20. }
  21.  
  22. void mat(){
  23.  
  24. int largeur=30,hauteur=30;
  25. char lab[largeur][hauteur];
  26. int nb_ligne,nb_col;
  27.  
  28.  
  29.  
  30. for(nb_ligne=0;nb_ligne<=hauteur;nb_ligne++)
  31. {
  32. for(nb_col=0;nb_col<=largeur;nb_col++)
  33. {
  34. lab[nb_ligne][nb_col]='0';
  35. }
  36. }
  37. }
  38.  
  39. void deplacer(int nb_ligne, int nb_col){
  40.  
  41.  
  42. move(nb_ligne,nb_col);
  43. wattrset (stdscr, COLOR_PAIR(2));
  44. addch('>');
  45.  
  46. }
  47.  
  48. int main (int argc, char **argv)
  49. {
  50. int largeur=30,hauteur=30;
  51. char lab[largeur][hauteur];
  52. int nb_ligne,nb_col;
  53.  
  54. FILE *fichier;
  55.  
  56. char chaine;
  57. int key=0;
  58.  
  59.  
  60. fichier=fopen("test.txt","r");
  61. /****************init lab******************/
  62. mat();
  63. /****************initlab******************/
  64.  
  65. /***********ncureses*************/
  66. ncurses();
  67. /************ncursess*******/
  68.  
  69.  
  70. while (!feof(fichier))
  71. {
  72. for(nb_ligne=0;nb_ligne<=hauteur;nb_ligne++)
  73. {
  74.  
  75. for(nb_col=1;nb_col<=largeur;nb_col++)
  76. {
  77. fscanf(fichier,"%c",&lab[nb_ligne][nb_col]);
  78. if(lab[nb_ligne][nb_col]=='0')
  79. {
  80.  
  81. attron(COLOR_PAIR(1));
  82. mvprintw(nb_ligne,nb_col," ");
  83. attroff(COLOR_PAIR(1));
  84.  
  85. }
  86.  
  87. else if(lab[nb_ligne][nb_col]=='1')
  88. {
  89. attron(COLOR_PAIR(3));
  90. mvprintw(nb_ligne,nb_col," ");
  91. attroff(COLOR_PAIR(3));
  92.  
  93. }
  94. else if(lab[nb_ligne][nb_col]=='M')
  95. {
  96. attron(COLOR_PAIR(2));
  97. mvprintw(nb_ligne,nb_col," ");
  98. attroff(COLOR_PAIR(2));
  99.  
  100. }
  101. else if(lab[nb_ligne][nb_col]=='K')
  102. {
  103. attron(COLOR_PAIR(4));
  104. mvprintw(nb_ligne,nb_col,"&");
  105. attroff(COLOR_PAIR(4));
  106.  
  107. }
  108.  
  109. refresh();
  110.  
  111. }
  112. fscanf(fichier,"%c",&chaine);
  113.  
  114. }
  115. }
  116.  
  117. keypad(stdscr,TRUE);
  118.  
  119. curs_set(0);
  120.  
  121.  
  122. nb_ligne= 1 ;
  123. nb_col= 5 ;
  124. move(nb_ligne,nb_col);
  125.  
  126.  
  127.  
  128. while (key != KEY_F(12))
  129. {
  130. wattrset (stdscr, COLOR_PAIR(3));
  131.  
  132. switch (key)
  133. {
  134.  
  135. case KEY_RIGHT:
  136. if (largeur-1 > nb_col)
  137. {
  138.  
  139. if (lab[nb_ligne][nb_col+1] != '0' && lab[nb_ligne+1][nb_col] != 'V')
  140. {
  141. addch (' ');
  142. nb_col++;
  143. deplacer(nb_ligne,nb_col);
  144. }
  145. }
  146. break;
  147.  
  148. case KEY_LEFT:
  149. if (0 < nb_col)
  150. {
  151. if (lab[nb_ligne][nb_col-1] != '0' && lab[nb_ligne+1][nb_col] != 'V')
  152. {
  153. addch (' ');
  154. nb_col--;
  155. deplacer(nb_ligne,nb_col);
  156. }
  157. }
  158. break;
  159.  
  160. case KEY_DOWN:
  161. if (hauteur-1 > nb_ligne)
  162. {
  163. if (lab[nb_ligne+1][nb_col] != '0' && lab[nb_ligne+1][nb_col] != 'V')
  164. {
  165. addch (' ');
  166. nb_ligne++;
  167. /******deplacer*********/
  168. deplacer(nb_ligne,nb_col);
  169. /*********deplacer***********/
  170. }
  171. }
  172. break;
  173.  
  174.  
  175.  
  176.  
  177. case KEY_UP:
  178. if (0< nb_ligne)
  179. {
  180. if (lab[nb_ligne-1][nb_col] != '0' && lab[nb_ligne+1][nb_col] != 'm')
  181. {
  182. addch (' ');
  183. nb_ligne--;
  184. deplacer(nb_ligne,nb_col);
  185. }
  186. }
  187. break;
  188.  
  189. }
  190.  
  191.  
  192.  
  193. key=getch();
  194. move(nb_ligne,nb_col);
  195. refresh();
  196.  
  197. }
  198.  
  199. echo();
  200. endwin();
  201. exit(0);
  202.  
  203. return 0;
  204.  
  205. }

Autres pages sur : labyrinthe ncurses langage

Lassé par la pub ? Créez un compte
Expert Programmation

nadou6682 a dit :
merci bcp
en fait je ne sais pas comment ajouter la gestion des portes et des cles
merci encore

porte = mur qui n'est plus un mur si tu as la clé. Voilà, utilise des variables pour mémoriser l'état dans le quel tu trouves (avec ou sans clé)
Lassé par la pub ? Créez un compte
Tom's guide dans le monde