labyrinthe avec ncurses en langage C - Programmation
 

Ajouter une réponse



 Mot :   Pseudo :  
 
Bas de page
Auteur
 Sujet : labyrinthe avec ncurses en langage C
 
Profil : IDNaute
Plus d'informations

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

 


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

Message cité 1 fois
Message édité par SiM07 le 02-05-2008 à 15:09:01
Liens

Paranoid Android
Profil : IDNaute
Plus d'informations

nadou6682 a écrit :

J'aimerais savoir si vous pouviez m'aider.


Bonjour.
 
oui on peut t'aider.
il faut juste que tu exposes ton problème.


---------------
Don't panic!
mouths91 à dit : "[...]des rageux comme kelnem"
Profil : IDNaute
Plus d'informations

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

http://www.sim07.net
Profil : Modérateur
Plus d'informations

Merci d'utiliser le bbcode code lorsque tu colles du code.


---------------
Mon Blog Geek | Facebook | My Last.fm | Mon CV |
Profil : IDNaute
Plus d'informations

nadou6682 a écrit :

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é)


---------------
B800A0 8EC0 B400 B013 CD10 B91000 BA1000 B005
E81000 B407 CD21 B400 B003 CD10 B44C B000 CD21
50 89D0 BB4001 F7E3 89C7 01CF 58 26 8805 C3

Aller à :
Ajouter une réponse
  FORUM Infos-du-Net » Programmation » labyrinthe avec ncurses en langage C
 

Liens