barnabe0057 icon

TP_TableauxMultidimensionnels

barnabe0057 | PRO | 02/20/22 05:43:09 PM UTC | 0 ⭐ | 733 👁️ | Never ⏰ | []
C# |

3.11 KB

|

None

|

0 👍

/

0 👎

using System;
 
namespace MNS // Note: actual namespace depends on the project name.
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string[,] grille = new string[8, 8];
            string[] lettres = { "A", "B", "C", "D", "E", "F", "G", "H" };
 
            for (int i = 0; i < grille.GetLength(0); i++)
            {
                for (int j = 0; j < grille.GetLength(1); j++)
                {
                    int aAfficher = i + 1;
                    grille[i, j] = lettres[j] + aAfficher.ToString();
                }
            }
 
 
            grille[0, 6] = "x";
            AfficherTableauADeuxDimensions(grille);
 
            //DeplacementRoi(grille, 7, 7);
 
            DeplacementTour(grille, 0, 2);
        }
 
        public static void AfficherTableauADeuxDimensions(string[,] tableau)
        {
            //on parcourt les lignes
            for (int i = 0; i < tableau.GetLength(0); i++)
            {
                //on parcourt les colonnes
                for (int j = 0; j < tableau.GetLength(1); j++)
                {
                    Console.Write(tableau[i,j] + " ");
                }
 
                Console.Write("\n");
            }
        }
 
        public static void DeplacementTour(string[,] grille, int indexX, int indexY)
        {
            //affichage de la colonne
            for (int i = 0; i < grille.GetLength(0); i++)
            {
                if(i != indexX)
                    Console.WriteLine(grille[i, indexY]); 
            }
 
            //affichage de la ligne
            for (int i = 0; i < grille.GetLength(1); i++)
            {
                if(i != indexY)
                    Console.WriteLine(grille[indexX, i]);
            }
 
            //affichage dans une direction bloqué par les autres pièces (à faire pour les trois autres directions)
            /*for (int i = indexY + 1; i < grille.GetLength(1); i++)
            {
                if (grille[indexX, i] == "x")
                    break;
 
                Console.WriteLine(grille[indexX, i]);
            }*/
        }
 
        public static void DeplacementRoi(string[,] grille, int indexX, int indexY)
        {
            // X X X
            // X R X
            // X X X
            for (int i = -1; i <= 1; i++)
            {
                for (int j = -1; j <= 1; j++)
                {
                    bool enDehorsDuTableauAGauche = indexX == 0 && j == -1;
                    bool enDehorsDuTableauADroite = indexX == grille.GetLength(0) - 1 && j == 1;
                    bool enDehorsDuTableauEnHaut = indexY == 0 && i == -1;
                    bool enDehorsDuTableauEnBas = indexY == grille.GetLength(1) - 1 && i == 1;
                    bool caseDuRoi = i == 0 && j == 0;
 
                    if(!(caseDuRoi || enDehorsDuTableauAGauche || enDehorsDuTableauADroite 
                        || enDehorsDuTableauEnBas || enDehorsDuTableauEnHaut))
                        Console.WriteLine(grille[indexX + j, indexY + i]);
                }
            }
 
        }
 
    }
}

Comments