Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.13 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class matrice {
  4.     private int lig;
  5.     private int col;
  6.     private String [][] mat;
  7.    
  8.     Scanner clavier=new Scanner(System.in);
  9.    
  10.     public matrice (int x,int y)
  11.     {
  12.         this.lig=x-1;
  13.         this.col=y-1;
  14.         this.mat= new String [x][y];
  15.     }
  16.    
  17.     //affiche la matrice
  18.     public String affiche()
  19.     {
  20.         String s="";
  21.         for (int x=0; x <this.lig ;x++)
  22.         {
  23.             for (int y=0;y<this.col;y++)
  24.             {
  25.                 s+=(""+this.mat[x][y]+" | ");
  26.             }
  27.             s+="\n";
  28.         }
  29.         return s;
  30.     }
  31.  
  32.  
  33.     //initialise la matrice en mettant toutes les cases a vide
  34. public void initialise()
  35. {
  36.     for (int x=0; x <this.lig ;x++)
  37.     {
  38.         for (int y=0;y<this.col;y++)
  39.         {
  40.             this.mat[x][y]="";
  41.         }
  42.     }
  43. }
  44.  
  45.  
  46.  
  47.  
  48. //test pour savoir si le joueur actuel a gagne sur la ligne actuelle
  49.  
  50. public boolean gagne_ligne (String s,int lig,int col)
  51. {
  52.     boolean gagne=false;
  53.     int gauche_col = col;
  54.     int droite_col=col;
  55.     int count_gauche=0;
  56.     int count_droite=0;
  57.     int g=0;
  58.    
  59.     if (this.mat[lig][col]==s && this.mat[lig][gauche_col-1]==s
  60.         ||  this.mat[lig][col]==s && this.mat[lig][droite_col]==s)
  61.     {
  62.         while (this.mat[lig][gauche_col]==s && count_gauche<4 && gauche_col > 0)
  63.         {
  64.             count_gauche++;
  65.             gauche_col--;
  66.         }
  67.         while (this.mat[lig][droite_col]==s && count_droite<4 && droite_col<this.col )
  68.         {
  69.             count_droite++;
  70.             droite_col++;
  71.         }
  72.         g=(count_gauche+count_droite)-1;
  73.         if (g>=4)
  74.         {
  75.             gagne=true;
  76.         }
  77.     }
  78.     return gagne;
  79. }
  80.  
  81.  
  82.  
  83.  
  84. //test pour savoir si le joueur actuel a gagne sur la colonne actuelle
  85.  
  86. public boolean gagne_colonne(String s , int lig,int col )
  87. {
  88.     boolean gagne=false;
  89.     int haut_lig = lig;
  90.     int bas_lig=lig;
  91.     int count_haut=0;
  92.     int count_bas=0;
  93.     int g=0;
  94.    
  95.     if (this.mat[lig][col]==s && this.mat[haut_lig-1][col]==s
  96.         ||  this.mat[lig][col]==s && this.mat[bas_lig+1][col]==s)
  97.     {
  98.         while (this.mat[haut_lig][col]==s && count_haut<4 && haut_lig > 0)
  99.         {
  100.             count_haut++;
  101.             haut_lig--;
  102.         }
  103.        
  104.         while (this.mat [bas_lig][col]==s && count_bas<4 && bas_lig<this.lig)
  105.         {
  106.             count_bas++;
  107.             bas_lig++;
  108.         }
  109.         g=(count_bas+count_haut)-1;
  110.         if (g>=4)
  111.         {
  112.             gagne=true;
  113.         }
  114.     }
  115.     return gagne;
  116. }
  117.  
  118.  
  119.  
  120.  
  121. //test la diagonale qui part du haut droit jusqu'au bas gauche
  122.  
  123. public boolean gagne_haut_droit_to_bas_gauche(String s ,int lig,int col)
  124. {
  125.     boolean gagne=false;
  126.     int droite_col=col;
  127.     int haut_lig=lig;
  128.     int gauche_col=col;
  129.     int bas_lig=lig;
  130.     int count_droite=0;
  131.     int count_gauche=0;
  132.     int g=0;
  133.    
  134.     if (this.mat[lig][col]==s && this.mat[haut_lig-1][droite_col+1]==s
  135.             ||this.mat[lig][col]==s && this.mat[bas_lig+1][gauche_col-1]==s )
  136.     {
  137.         while (this.mat[haut_lig][droite_col]==s && count_droite<4 && haut_lig > 0 && droite_col<this.col )
  138.         {
  139.             count_droite++;
  140.             haut_lig--;
  141.             droite_col++;
  142.            
  143.         }
  144.         while (this.mat[bas_lig][gauche_col]==s && count_gauche<4 && bas_lig < this.lig && gauche_col > 0 )
  145.         {
  146.             count_gauche++;
  147.             bas_lig++;
  148.             gauche_col--;
  149.         }
  150.        
  151.     }
  152.     g=(count_gauche+count_droite)-1;
  153.     if (g>=4)
  154.     {
  155.         gagne=true;
  156.     }
  157.     return gagne;
  158. }
  159.  
  160.  
  161.  
  162.  
  163. //test sur la diago qui part du haut gauche jusqu'au bas droit
  164.  
  165. public boolean gagne_haut_gauche_bas_droit (String s, int lig, int col)
  166. {
  167.     boolean gagne=false;
  168.     int droite_col=col;
  169.     int haut_lig=lig;
  170.     int gauche_col=col;
  171.     int bas_lig=lig;
  172.     int count_droite=0;
  173.     int count_gauche=0;
  174.     int g=0;
  175.    
  176.     if (this.mat[lig][col]==s && this.mat[haut_lig-1][gauche_col-1]==s
  177.             ||this.mat[lig][col]==s && this.mat[bas_lig+1][droite_col+1]==s )
  178.     {
  179.         while (this.mat[haut_lig][gauche_col]==s && count_gauche<4 && haut_lig > 0 && gauche_col > 0)
  180.         {
  181.             count_droite++;
  182.             haut_lig--;
  183.             gauche_col--;
  184.            
  185.         }
  186.         while (this.mat[bas_lig][droite_col]==s && count_droite<4 && bas_lig < this.lig && droite_col<this.col  )
  187.         {
  188.             count_gauche++;
  189.             bas_lig++;
  190.             droite_col++;
  191.         }
  192.        
  193.     }
  194.     g=(count_gauche+count_droite)-1;
  195.     if (g>=4)
  196.     {
  197.         gagne=true;
  198.     }
  199.     return gagne;
  200. }
  201.  
  202.  
  203.  
  204.  
  205. //place le symbole dans la colonne choisi par le joueur a la suite des autres  et renvoi le numero de la ligne
  206.  
  207. public int place(String s ,int col)
  208. {
  209.     int i=this.lig;
  210.     while (this.mat[i][col] != "")
  211.     {
  212.         i--;
  213.        
  214.     }
  215.     this.mat[i][col]=s;
  216.     return i;
  217. }
  218.  
  219.  
  220.  
  221.  
  222.  
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement