// public bool EstUnMur(CPosition pos) { if (m_matriceInt[pos.PositionLigne, pos.PositionColonne] == MUR) { return(true); } else { return(false); } }
public bool EstEgale(CPosition pos) { if (pos.PositionLigne == PositionLigne && pos.PositionColonne == PositionColonne) { return(true); } else { return(false); } }
public bool EstAdjacent(CPosition pos) { if ((pos.PositionLigne == PositionLigne - 1 && pos.PositionColonne == PositionColonne) || (pos.PositionLigne == PositionLigne + 1 && pos.PositionColonne == PositionColonne) || (pos.PositionLigne == PositionLigne && pos.PositionColonne == PositionColonne + 1) || (pos.PositionLigne == PositionLigne && pos.PositionColonne == PositionColonne - 1)) { return(true); // la position est adjacente } else { return(false); } }
// return true lorsque l'on a pu inscrire le passage du personnage par un . public bool MarquerPassage(CPosition pos) { if (m_matriceInt[pos.PositionLigne, pos.PositionColonne] != PERSONNAGE && m_matriceInt[pos.PositionLigne, pos.PositionColonne] != MUR ) { m_matriceInt[pos.PositionLigne, pos.PositionColonne] = AETEOCCUPEE; return(true); } else { return(false); } }
public bool EstOccupee(CPosition pos) { if (m_matriceInt[pos.PositionLigne, pos.PositionColonne] == ARRIVEE) { return(false); } else if (m_matriceInt[pos.PositionLigne, pos.PositionColonne] != ESPACE) { return(true); // la position est occupée } else { return(false); // libre } }
// constructeur, vérification, initialisation matrice public CLabyrinthe(string[] schemaString, int nbLignesInt, int nbColonnesInt) { // controle de coherence : le nombre de lignes doit correspondre à la longeur du tableau schemaString passé // et pour chaque string passée, il faut que le nombre de caractere de la string corresponde au nombre de colonnes bool yaDesErreursBool = false; // controle du nombre d elignes if (schemaString.Length != nbLignesInt) { Console.WriteLine("LAB003 : erreur sur nombre de lignes (" + nbLignesInt + " attendue, " + schemaString.Length + " passée)"); yaDesErreursBool = true; } // controle que chaque ligne comporte le nombre de colonnes declaré for (int i = 0; i < schemaString.Length; i++) { if (schemaString[i].Length != nbColonnesInt) { Console.WriteLine("LAB004 : erreur sur nombre de colonnes pour <" + schemaString[i] + "> (" + nbColonnesInt + " attendue, " + schemaString[i].Length + " passée)"); yaDesErreursBool = true; } } // vérification existence d'un départ ET UN SEUL int compteurDepartInt = 0; for (int i = 0; i < schemaString.Length; i++) { for (int j = 0; j < schemaString[i].Length; j++) { if (schemaString[i][j] == 'd') { compteurDepartInt++; } } } if (compteurDepartInt == 0) { Console.WriteLine("LAB005 : Attention, il n'y a pas de départ d!"); yaDesErreursBool = true; } else if (compteurDepartInt == 1) { } else { Console.WriteLine("LAB006 : Attention, il y a TROP de départ d!"); yaDesErreursBool = true; } // vérification existence d'un arrivée ET UNE SEULE int compteurarriveeInt = 0; for (int i = 0; i < schemaString.Length; i++) { for (int j = 0; j < schemaString[i].Length; j++) { if (schemaString[i][j] == 'a') { compteurarriveeInt++; } } } if (compteurarriveeInt == 0) { Console.WriteLine("LAB007 : Attention, il n'y a pas de arrivée a!"); yaDesErreursBool = true; } else if (compteurarriveeInt == 1) { } else { Console.WriteLine("LAB008 : Attention, il y a TROP de arrivée a!"); yaDesErreursBool = true; } // vérification que le labyrinthg est entouré de murs // la première ligne doit etre un mur complet // la dernière ligne doit etre un mur complet // les autres doivent être un mur en premier caractère et un mur en dernier caractere for (int i = 0; i < schemaString.Length; i++) { // si je suis sur la premier ou la dernier ligne if (i == 0 || i == schemaString.Length - 1) { for (int j = 0; j < schemaString[i].Length; j++) { if (schemaString[i][j] != '*') { Console.WriteLine("LAB009 : Attention, en ligne " + (i + 1) + ", mur non complet!"); yaDesErreursBool = true; break; //pas la peine de tester tous les cas, il y a un TROU! } } } else // je suis sur une ligne qcq autre que premier ou dernier { if (schemaString[i][0] != '*') { Console.WriteLine("LAB010 : Attention, en ligne " + (i + 1) + ", trou mur gauche !"); yaDesErreursBool = true; } if (schemaString[i][schemaString[i].Length - 1] != '*') { Console.WriteLine("LAB011 : Attention, en ligne " + (i + 1) + ", trou mur droit!"); yaDesErreursBool = true; } } // } // vérification que tout le reste est en tirets for (int i = 0; i < schemaString.Length; i++) { for (int j = 0; j < schemaString[i].Length; j++) { if (schemaString[i][j] != '-' && schemaString[i][j] != 'd' && schemaString[i][j] != 'a' && schemaString[i][j] != '*' ) { Console.WriteLine("LAB012 : Attention, en ligne " + (i + 1) + ", caractere tiret obligatoire autre que mur arrivee depart!"); yaDesErreursBool = true; } } } // on sort immédiatement si pb de valeurs, car sinon on ne peut pastransformer le schema en matrice if (yaDesErreursBool) { throw new Exception("LAB005 : erreur(s) sur controle objet labyrinthe "); } // si on en est là, c'est que on a un depart, une arrivée, des espaces et des murs //on peut donc traduire betement en matrice m_nbLignesInt = nbLignesInt; m_nbColonnesInt = nbColonnesInt; m_matriceInt = new int[m_nbLignesInt, m_nbColonnesInt]; // assignation des valeurs dans la matrice for (int i = 0; i < schemaString.Length; i++) { for (int j = 0; j < schemaString[i].Length; j++) { if (schemaString[i][j] == '-') { m_matriceInt[i, j] = ESPACE; } else if (schemaString[i][j] == 'd') { m_matriceInt[i, j] = DEPART; } else if (schemaString[i][j] == 'a') { m_matriceInt[i, j] = ARRIVEE; } else if (schemaString[i][j] == '*') { m_matriceInt[i, j] = MUR; } } } // initialisation des positions for (int i = 0; i < schemaString.Length; i++) { for (int j = 0; j < schemaString[i].Length; j++) { if (m_matriceInt[i, j] == DEPART) { m_departPosition = new CPosition(i, j); } if (m_matriceInt[i, j] == ARRIVEE) { m_arriveePosition = new CPosition(i, j); } } } }
public void DeplacementSuivant() { int reponseLigneInt = Int32.MinValue; int reponseColonneInt = Int32.MinValue; bool coordonneesValidesBool = false; // CONTROLE SUR LES VALEURS ENTREES PAR L'UTILISATEUR // Premier question et l'objectif est de revenir ici pour analyser tous les nouveaux essais #region Controle que les coordonnées soient bien dans le tableau while (!coordonneesValidesBool) { while (!coordonneesValidesBool) { Console.WriteLine("Quelle position souhaitez-vous atteindre ?"); Console.Write("Indice Ligne : "); reponseLigneInt = int.Parse(Console.ReadLine()) - 1; // car utilisateur ne prend pas en compte le 0 Console.Write("Indice Colonne : "); reponseColonneInt = int.Parse(Console.ReadLine()) - 1; if (((reponseLigneInt < 0 || reponseLigneInt > m_labyrintheLabyrinthe.NombreLignesInt - 1) || (reponseColonneInt < 0 || reponseColonneInt > m_labyrintheLabyrinthe.NombresColonnesInt - 1))) // si les coordonnées ne sont pas valides { Console.WriteLine("LAB019 : Les coordonnées <(" + (reponseLigneInt + 1) + ";" + (reponseColonneInt + 1) + ")> ne peuvent pas être appliquées ici"); } else { coordonneesValidesBool = true; } } #endregion //Les coordonnées sont donc dans le tableau //IL FAUT VERIFIER QUE LA DEPLACEMENT EST POSSIBLE #region Adjacente à la case demandée ? CPosition reponsePosition = new CPosition(reponseLigneInt, reponseColonneInt); // Controle sur le fait que la case demandée est adjacente à la case actuelle du personnage if (reponsePosition.EstEgale(m_personnePosition)) { coordonneesValidesBool = false; Console.WriteLine("LAB020 : tu n'as pas fait de déplacement"); } //METHODE ESTADJACENT DANS LA CLASSE CPOSITION else if (!reponsePosition.EstAdjacent(m_personnePosition) || m_labyrintheLabyrinthe.EstOccupee(reponsePosition)) //|| !m_personnePosition.EstEgale(m_labyrintheLabyrinthe.PositionArrivee) { Console.WriteLine("LAB022 : La position n'est pas adjacente ou la case est occupée !"); coordonneesValidesBool = false; } else { //la case est libre et adjacente // on effectue le déplacement m_labyrintheLabyrinthe.MarquerPassage(m_personnePosition); m_personnePosition = reponsePosition; Console.WriteLine("La nouvelle position du personnage est : " + m_personnePosition.ToString()); m_labyrintheLabyrinthe.MarquerPassage(m_personnePosition); } } #endregion }
public CPersonnage(CLabyrinthe laby) { m_personnePosition = laby.PositionDepart; m_labyrintheLabyrinthe = laby; }