Advertisement
plimee

Puissance 4 FAC

Mar 28th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.84 KB | None | 0 0
  1. //package Projet_Puissance_4;
  2.  
  3. import es.*;
  4.  
  5. public class Puissance4 {
  6.         static int [][] grille = new int [6][7];
  7.        
  8.         //1.representation et initialisation de la grille
  9.         public static void initialiseGrille (){
  10.                 int i=0;
  11.                 for (i=0;i<6;i++){
  12.                         for (int j=0;j<7;j++){
  13.                                 grille[i][j]=0;
  14.                         }
  15.                 }
  16.         }
  17.  
  18.         //2. representation d'un coup d'un jouer
  19.         public static void jouer (int nj , int nc){
  20.                 int j=0;
  21.                 while (grille[j][nc] != 0){
  22.                     j++;
  23.                 }
  24.                 grille[j][nc] = nj;
  25.         }
  26.  
  27.         //3. Affichage de la grille
  28.         public static void afficheGrille(){
  29.                 for (int i=5;i>=0;i--){
  30.                         Ecriture.nouvelleLigne();
  31.                         Ecriture.uneChaine("|");
  32.                         for (int j=0;j<=6;j++){
  33.                                
  34.                                 if (grille[i][j]==1){
  35.                                         Ecriture.uneChaine("x");
  36.                                 }
  37.                                 else {
  38.                                         if (grille[i][j]==2){
  39.                                                 Ecriture.uneChaine("O");
  40.                                         }
  41.                                         else {
  42.                                                 Ecriture.uneChaine(" ");
  43.                                         }
  44.                                 }
  45.                                 Ecriture.uneChaine("|");
  46.                         }
  47.                 }
  48.                 Ecriture.nouvelleLigne();
  49.                 Ecriture.uneChaine(" 0 1 2 3 4 5 6 ");
  50.         }
  51.  
  52.         //4.1.detection d'un alignement de 4 cases Hor
  53.         public static boolean aGagneHor(int nJ, int nL, int nC) {
  54.                 boolean val = true;
  55.                 int cpt = 0;
  56.                 int j = nC;
  57.                 nL = 0;
  58.                 if (nC > 3){
  59.                     return false;
  60.                 }
  61.                 else {
  62.                     while (j < nC + 4 & val) {
  63.                         if (grille[nL][j] != nJ){
  64.                             j++;
  65.                             return val=false;
  66.                            
  67.                         }
  68.                         else{
  69.                             cpt++;
  70.                             j++;
  71.                         }
  72.                     }
  73.                         // rend le résultat
  74.                         if (cpt == 4){
  75.                             return val = true;
  76.                         }
  77.                         else{
  78.                             return val = false;
  79.                         }
  80.                 }
  81.         }
  82.  
  83.         //4.2.detection d'un alignement de 4 cases ver
  84.         public static boolean aGagneVer(int nJ, int nL, int nC) {
  85.            
  86.              boolean val = true;
  87.              int cpt = 0;
  88.              int j = nL;
  89.              if (nL > 3){
  90.                 return false;
  91.              }
  92.              else {
  93.                 while (j < nL + 4 & val) {
  94.                     if (grille[j][nC] != nJ){
  95.                         return val=false;
  96.                      }
  97.                      else{
  98.                         cpt++;
  99.                          j++;
  100.                      }
  101.                 }
  102.                      // rend le résultat
  103.                      if (cpt == 4){
  104.                         return val = true;
  105.                      }
  106.                      else{
  107.                         return val = false;
  108.                      }
  109.              }
  110.            
  111.         }
  112.          
  113.         //4.3.détection d'alignement en diag (fonction auxiliaire pour que les fonctions de test en diag ne soit pas trop longues)
  114.         public static boolean trouver(int x, int y, int nC, int nL) {
  115.             int joueur = 0;// pas de pion d'un des 2 joueurs
  116.             int compteur = 0;
  117.  
  118.             int curCol = x;
  119.             int curRow = y;
  120.  
  121.             while ((curCol >= 0) && (curCol <= 6) && (curRow >= 0) && (curRow <= 5)) {
  122.               if (grille[curRow][curCol] != joueur) {
  123.                 joueur = grille[curRow][curCol];
  124.                 compteur = 1;
  125.               } else {
  126.                 compteur++;
  127.               }
  128.               if ((joueur != 0) && (compteur == 4)) {
  129.                 return true;
  130.               }
  131.               curCol += nC;
  132.               curRow += nL;
  133.             }
  134.             return false;
  135.           }
  136.        
  137.         //4.4.detection d'un alignement de 4 cases diag mont
  138.         public static boolean aGagneDiagMont() {
  139.             // Diagonales (cherche depuis la ligne du bas)
  140.             for (int col = 0; col < 7; col++) {
  141.               // Première diagonale ( / )
  142.               if (trouver(col, 0, 1, 1)) {
  143.                 return true;
  144.               }
  145.               // Deuxième diagonale ( \ )
  146.               if (trouver(col, 0, -1, 1)) {
  147.                 return true;
  148.               }
  149.             }
  150.             return false;
  151.             }
  152.  
  153.         //4.5.detection d'un alignement de 4 cases diag desc
  154.         public static boolean aGagneDiagDesc() {
  155.           for (int ligne = 0; ligne < 6; ligne++) {
  156.               // Première diagonale ( / )
  157.               if (trouver(0, ligne, 1, 1)) {
  158.                 return true;
  159.               }
  160.               // Deuxième diagonale ( \ )
  161.               if (trouver(7 - 1, ligne, -1, 1)) {
  162.                 return true;
  163.               }
  164.             }
  165.  
  166.             // On n'a rien trouvé
  167.             return false;
  168.           }
  169.        
  170.         //5.detection de la victoire d'un joueur
  171.         public static boolean aGagne(int nJ){
  172.             for (int j = 0; j < 6; j++){
  173.                 for (int i = 0; i < 7 ; i++){
  174.                     if ( aGagneHor(nJ, j, i) == true | aGagneVer(nJ, j, i) == true | aGagneDiagMont() == true | aGagneDiagDesc() == true){
  175.                         return true ;
  176.                     }
  177.                    
  178.                 }
  179.             }
  180.             return false ;
  181.         }
  182.        
  183.         //6.detection de match nul
  184.         public static boolean matchNul(){
  185.             int i = 0;
  186.             int j = 0;
  187.             boolean val = false;
  188.            
  189.             while( i < 7 ){
  190.                 while( j <6){
  191.                     if( grille[i][j] != 0){
  192.                         i++;
  193.                         j++;
  194.                     }
  195.                     else{
  196.                         val = false;
  197.                         return val;
  198.                     }
  199.                 }
  200.             }
  201.             val = true;
  202.             return val;
  203.         }
  204.        
  205.         //7.fonction jeu qui permet de jouer une partie
  206.         public static void jeu(){
  207.             int i = 1;
  208.             boolean val = false;
  209.             initialiseGrille();
  210.            
  211.             if (matchNul()){
  212.                 Ecriture.nouvelleLigne();
  213.                 Ecriture.uneChaine("Match nul ... ");
  214.             }
  215.             else{
  216.                 while( !matchNul() && !val ){
  217.                    
  218.                     if(i == 1){
  219.                         afficheGrille();
  220.                         Ecriture.nouvelleLigne();
  221.                         Ecriture.uneChaine("Au joueur " +i);
  222.                         Ecriture.uneChaine(" de jouer : ");
  223.                         jouer(i, Lecture.unEntier());
  224.                        
  225.                         if( aGagne(i)){
  226.                             afficheGrille();
  227.                             Ecriture.nouvelleLigne();
  228.                             Ecriture.uneChaine("le joueur "+ i);
  229.                             Ecriture.uneChaine(" à gagné");
  230.                             val = true;
  231.                         }
  232.                         else{
  233.                             i++;
  234.                         }
  235.                     }
  236.                     else if(i == 2){
  237.                         afficheGrille();
  238.                         Ecriture.nouvelleLigne();
  239.                         Ecriture.uneChaine("Au joueur " +i);
  240.                         Ecriture.uneChaine(" de jouer : ");
  241.                         jouer(i, Lecture.unEntier());
  242.                         if( aGagne(i)){
  243.                             afficheGrille();
  244.                             Ecriture.nouvelleLigne();
  245.                             Ecriture.uneChaine("le joueur "+ i);
  246.                             Ecriture.uneChaine(" à gagné");
  247.                             val = true;
  248.                         }
  249.                         else{
  250.                             i--;
  251.                         }
  252.                     }
  253.                    
  254.                 Ecriture.nouvelleLigne();
  255.                 }
  256.                
  257.             }
  258.         }
  259.        
  260.         public static void main(String[] args) {
  261.                 jeu();
  262.                
  263.  
  264.  
  265.  
  266.         }
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement