Example #1
0
piece_siam piece_correspondre_nom_cours(const char* nom_cours)
{
  assert(nom_cours!=NULL);
  assert(strlen(nom_cours)==3);

  //Possibilites:
  //
  // - nom vaut "***" -> case vide
  // - nom vaut "RRR" -> rocher
  // - nom commence par "e-" ou "r-"
  //     Alors il s'agit d'un animal.
  //     -> Recuperer le 3eme caractere
  //     -> Le convertir dans l'orientation de l'animal.
  //  - Si aucun des cas precedent, alors affiche erreur.

  piece_siam piece;
  piece_initialiser(&piece);


  if(strncmp(nom_cours,"***",3)==0)
    {
      piece_definir_case_vide(&piece);
    }
  else if(strncmp(nom_cours,"RRR",3)==0)
    {
      piece_definir_rocher(&piece);
    }
  else if((nom_cours[0]=='e' || nom_cours[0]=='r') && nom_cours[1]=='-')
    {
      const type_piece type=type_correspondre_caractere_animal(nom_cours[0]);
      const orientation_deplacement orientation=orientation_correspondre_caractere(nom_cours[2]);

      piece_definir(&piece,type,orientation);
    }
  else
    {
      printf("Erreur fonction %s\n",__FUNCTION__);
      abort();
    }

  return piece;

}
void ligne_de_commande_parser(const char* ligne_commande,action_a_realiser* action)
{
    assert(action!=NULL);


    action_initialiser(action);

    if(ligne_commande==NULL)
        return ;

    if(strlen(ligne_commande)>=1 && ligne_commande[0]=='#')
        return ;



    else if(strlen(ligne_commande)>=3 &&
            (strncmp(ligne_commande,"FIN",3)==0 ||
             strncmp(ligne_commande,"fin",3)==0 ||
             strncmp(ligne_commande,"Fin",3)==0) )
    {
        action->type_action=fin;
        return ;
    }


    else if(strlen(ligne_commande)>=2 && ligne_commande[0]=='n' && ligne_commande[1]==' ')
    {
        int x=0;int y=0;char dir='\0';
        if(sscanf(ligne_commande,"n %d %d %c",&x,&y,&dir)==3)
        {
            if(orientation_caractere_etre_integre(dir))
            {
                action->type_action=introduction;
                action->x_depart=x;
                action->y_depart=y;
                action->orientation=orientation_correspondre_caractere(dir);

                return ;
            }
        }
    }


    else if(strlen(ligne_commande)>=2 && ligne_commande[0]=='d' && ligne_commande[1]==' ')
    {
        int x=0;int y=0;char dir='\0';char ori='\0';
        if(sscanf(ligne_commande,"d %d %d %c %c",&x,&y,&dir,&ori)==4)
        {
            if(orientation_caractere_etre_integre(dir))
            {
                action->type_action=deplacement;
                action->x_depart=x;
                action->y_depart=y;
                action->deplacement=orientation_correspondre_caractere(dir);
                action->orientation=orientation_correspondre_caractere(ori);

                return ;
            }
        }
    }



    else if(strlen(ligne_commande)>=2 && ligne_commande[0]=='o' && ligne_commande[1]==' ')
    {
        int x=0;int y=0;char dir=0;
        if(sscanf(ligne_commande,"o %d %d %c",&x,&y,&dir)==3)
        {
            if(orientation_caractere_etre_integre(dir))
            {
                action->type_action=changement_orientation;
                action->x_depart=x;
                action->y_depart=y;
                action->orientation=orientation_correspondre_caractere(dir);

                return ;
            }
        }
    }


    else if(strlen(ligne_commande)>=1 && ligne_commande[0]=='i')
    {
        action->type_action=initialisation;
        return;
    }


    else if(strlen(ligne_commande)>=4 && strncmp(ligne_commande,"lit ",4)==0)
    {
        char buffer_filename[MAX_NOM_FICHIER_TAILLE];
        if(sscanf(ligne_commande,"lit %s",buffer_filename)==1)
        {
            action->type_action=lecture_fichier;
            strncpy(action->filename,buffer_filename,128);
            return ;
        }
    }



}