Advertisement
barnabe0057

TP_TableauxMultidimensionnels

Feb 20th, 2022
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.11 KB | None | 0 0
  1. using System;
  2.  
  3. namespace MNS // Note: actual namespace depends on the project name.
  4. {
  5.     internal class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             string[,] grille = new string[8, 8];
  10.             string[] lettres = { "A", "B", "C", "D", "E", "F", "G", "H" };
  11.  
  12.             for (int i = 0; i < grille.GetLength(0); i++)
  13.             {
  14.                 for (int j = 0; j < grille.GetLength(1); j++)
  15.                 {
  16.                     int aAfficher = i + 1;
  17.                     grille[i, j] = lettres[j] + aAfficher.ToString();
  18.                 }
  19.             }
  20.  
  21.  
  22.             grille[0, 6] = "x";
  23.             AfficherTableauADeuxDimensions(grille);
  24.  
  25.             //DeplacementRoi(grille, 7, 7);
  26.  
  27.             DeplacementTour(grille, 0, 2);
  28.         }
  29.  
  30.         public static void AfficherTableauADeuxDimensions(string[,] tableau)
  31.         {
  32.             //on parcourt les lignes
  33.             for (int i = 0; i < tableau.GetLength(0); i++)
  34.             {
  35.                 //on parcourt les colonnes
  36.                 for (int j = 0; j < tableau.GetLength(1); j++)
  37.                 {
  38.                     Console.Write(tableau[i,j] + " ");
  39.                 }
  40.  
  41.                 Console.Write("\n");
  42.             }
  43.         }
  44.  
  45.         public static void DeplacementTour(string[,] grille, int indexX, int indexY)
  46.         {
  47.             //affichage de la colonne
  48.             for (int i = 0; i < grille.GetLength(0); i++)
  49.             {
  50.                 if(i != indexX)
  51.                     Console.WriteLine(grille[i, indexY]);
  52.             }
  53.  
  54.             //affichage de la ligne
  55.             for (int i = 0; i < grille.GetLength(1); i++)
  56.             {
  57.                 if(i != indexY)
  58.                     Console.WriteLine(grille[indexX, i]);
  59.             }
  60.  
  61.             //affichage dans une direction bloqué par les autres pièces (à faire pour les trois autres directions)
  62.             /*for (int i = indexY + 1; i < grille.GetLength(1); i++)
  63.             {
  64.                 if (grille[indexX, i] == "x")
  65.                     break;
  66.  
  67.                 Console.WriteLine(grille[indexX, i]);
  68.             }*/
  69.         }
  70.  
  71.         public static void DeplacementRoi(string[,] grille, int indexX, int indexY)
  72.         {
  73.             // X X X
  74.             // X R X
  75.             // X X X
  76.             for (int i = -1; i <= 1; i++)
  77.             {
  78.                 for (int j = -1; j <= 1; j++)
  79.                 {
  80.                     bool enDehorsDuTableauAGauche = indexX == 0 && j == -1;
  81.                     bool enDehorsDuTableauADroite = indexX == grille.GetLength(0) - 1 && j == 1;
  82.                     bool enDehorsDuTableauEnHaut = indexY == 0 && i == -1;
  83.                     bool enDehorsDuTableauEnBas = indexY == grille.GetLength(1) - 1 && i == 1;
  84.                     bool caseDuRoi = i == 0 && j == 0;
  85.  
  86.                     if(!(caseDuRoi || enDehorsDuTableauAGauche || enDehorsDuTableauADroite
  87.                         || enDehorsDuTableauEnBas || enDehorsDuTableauEnHaut))
  88.                         Console.WriteLine(grille[indexX + j, indexY + i]);
  89.                 }
  90.             }
  91.  
  92.         }
  93.  
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement