Advertisement
Guest User

CarreMagique

a guest
Nov 21st, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | None | 0 0
  1. import iut.algo.Clavier;
  2.  
  3. /** Class Le carre magique
  4.   * @author Monster
  5.   * date : 18/11/2019
  6.   */
  7.  
  8. public class TP6_CarreMagique
  9. {
  10.     public static boolean verifNbChiffre(int[][] tab)
  11.     {
  12.         boolean verif_Nombre = false;
  13.         int a=1;
  14.  
  15.         while (a <= 9)
  16.         {
  17.             int cpt_Nombre=0;
  18.             for (int i=0; i<tab.length; i++)
  19.             {
  20.                 for (int j=0; j<tab.length; j++)
  21.                 {
  22.                     if (tab[i][j] == a)// Compte la présence du nombre
  23.                     {
  24.                         cpt_Nombre++;
  25.                     }      
  26.                 }              
  27.             }
  28.             a++;           
  29.             if (cpt_Nombre != 1){verif_Nombre = false;}
  30.             else{verif_Nombre = true;}
  31.         }    
  32.         return verif_Nombre;         
  33.     }
  34.     public static boolean verifSomme(int[][] tab)
  35.     {
  36.         final int TAILLE = 3;
  37.  
  38.         int somme=0, sommeRef=0;
  39.         boolean bOk;
  40.         boolean[] nombre;
  41.         nombre = new boolean [(TAILLE*TAILLE)+1];
  42.  
  43.         for(int cpt=0; cpt<(TAILLE*TAILLE)+1; cpt++)
  44.         {
  45.             nombre[cpt]=false;
  46.         }
  47.        
  48.         bOk=true; // On initialise le bOk à vrai
  49.  
  50.         for(int numlign=0;numlign<TAILLE;numlign++)// On met tout à vrai notre tableau de boolean
  51.         {
  52.             for(int numcol=0;numcol<TAILLE;numcol++)
  53.             {
  54.                 if(nombre[tab[numlign][numcol]])
  55.                 {
  56.                     bOk=false;
  57.                 }
  58.                 else
  59.                 {
  60.                     nombre[tab[numlign][numcol]]=true ;
  61.                 }
  62.         }
  63.      
  64.         // Compte si la somme de la ligne est différent de celle de la colonne
  65.         sommeRef=0;
  66.         for(int numlign2=0; numlign2<TAILLE;numlign2++)
  67.         {
  68.             sommeRef=sommeRef+tab[numlign2][numlign2];
  69.         }
  70.         for(int numlign3=0; numlign3<TAILLE;numlign3++)
  71.         {
  72.             somme=0;
  73.             for(int numcol2=0;numcol2<TAILLE;numcol2++)
  74.             {
  75.                 somme=somme+tab[numlign3][numcol2];
  76.             }
  77.         }
  78.  
  79.         if(somme != sommeRef) // Si la somme de la ligne est différent de celle de la colonne
  80.             {
  81.                 bOk=false;
  82.             }
  83.         }
  84.  
  85.         // Compte la somme des lignes
  86.         somme=0;
  87.         for(int numlign4=0; numlign4<TAILLE;numlign4++)
  88.             {
  89.                 somme=somme+tab[numlign4][TAILLE-1-numlign4];
  90.             }
  91.         if(somme != sommeRef) // Si la somme de la ligne est différent de celle de première ligne
  92.             {
  93.                 bOk=false;
  94.             }
  95.  
  96.         return bOk;
  97.     }
  98.     public static String Affichage(int[][] tab)
  99.     {
  100.         String retour = "   ";
  101.         for(int i=0; i<tab[0].length; i++)
  102.         {
  103.             retour = retour + "  " + i + " ";
  104.         }
  105.         retour = retour + "\n   +";
  106.        
  107.         for(int i=0; i<tab[0].length; i++)
  108.         {
  109.             retour = retour + "---+";
  110.         }
  111.         retour = retour + "\n";
  112.         for(int i=0; i<tab.length; i++)
  113.         {
  114.             retour = retour + i + " ";
  115.             if(i<10) retour = retour + " ";
  116.             retour = retour + "| ";
  117.             for(int j=0; j<tab[i].length; j++)
  118.             {
  119.                 retour = retour + tab[i][j] + " | ";
  120.             }
  121.             retour = retour + "\n   +";
  122.             for(int j=0; j<tab[i].length; j++)
  123.             {
  124.                 retour = retour + "---+";
  125.             }
  126.             retour = retour + "\n";
  127.         }
  128.  
  129.         return retour;
  130.     }
  131.     public static void main(String[] a)
  132.     {
  133.         /*----------------------*/
  134.         /*  Données             */
  135.         /*----------------------*/
  136.  
  137.         int[][]  tabCarreMagique;
  138.         tabCarreMagique = new int[3][3];
  139.        
  140.         int nb_Joueur=0;
  141.         int nb_Joueur_Centre=0;
  142.  
  143.         boolean carre;
  144.            
  145.         /*----------------------*/
  146.         /*  Instructions        */
  147.         /*----------------------*/
  148.         System.out.print("Entrer un nombre qui se placera au centre : " + " ");
  149.         nb_Joueur_Centre = Clavier.lire_int();
  150.         tabCarreMagique[1][1] = nb_Joueur_Centre;  
  151.            
  152.            
  153.         /* Remplit le tableau de nombre saisit par l'utilisateur*/
  154.         for (int i=0; i<tabCarreMagique.length; i++)
  155.         {
  156.             for (int j=0; j<tabCarreMagique.length; j++)
  157.             {
  158.                
  159.                     if (tabCarreMagique[i][j] == tabCarreMagique[1][1]){tabCarreMagique[i][j] = tabCarreMagique[1][1];}
  160.                     else
  161.                     {
  162.                         System.out.println(TP6_CarreMagique.Affichage(tabCarreMagique));
  163.                         System.out.print("Entrer votre chiffre : (case " + i + ";" + j + ")" + " ");
  164.                         nb_Joueur = Clavier.lire_int();
  165.                         tabCarreMagique[i][j] = nb_Joueur;
  166.                     }
  167.             }
  168.         }
  169.  
  170.        
  171.  
  172.         System.out.println(TP6_CarreMagique.Affichage(tabCarreMagique));
  173.  
  174.         carre = verifSomme(tabCarreMagique);
  175.  
  176.         if (carre) { System.out.println("Bravo ! C'est un carre magique");}
  177.         else{System.out.println("Dommage ! Ce n'est pas un carre magique");}
  178.  
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement