Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class matrice {
- private int lig;
- private int col;
- private String [][] mat;
- Scanner clavier=new Scanner(System.in);
- public matrice (int x,int y)
- {
- this.lig=x-1;
- this.col=y-1;
- this.mat= new String [x][y];
- }
- //affiche la matrice
- public String affiche()
- {
- String s="";
- for (int x=0; x <this.lig ;x++)
- {
- for (int y=0;y<this.col;y++)
- {
- s+=(""+this.mat[x][y]+" | ");
- }
- s+="\n";
- }
- return s;
- }
- //initialise la matrice en mettant toutes les cases a vide
- public void initialise()
- {
- for (int x=0; x <this.lig ;x++)
- {
- for (int y=0;y<this.col;y++)
- {
- this.mat[x][y]="";
- }
- }
- }
- //test pour savoir si le joueur actuel a gagne sur la ligne actuelle
- public boolean gagne_ligne (String s,int lig,int col)
- {
- boolean gagne=false;
- int gauche_col = col;
- int droite_col=col;
- int count_gauche=0;
- int count_droite=0;
- int g=0;
- if (this.mat[lig][col]==s && this.mat[lig][gauche_col-1]==s
- || this.mat[lig][col]==s && this.mat[lig][droite_col]==s)
- {
- while (this.mat[lig][gauche_col]==s && count_gauche<4 && gauche_col > 0)
- {
- count_gauche++;
- gauche_col--;
- }
- while (this.mat[lig][droite_col]==s && count_droite<4 && droite_col<this.col )
- {
- count_droite++;
- droite_col++;
- }
- g=(count_gauche+count_droite)-1;
- if (g>=4)
- {
- gagne=true;
- }
- }
- return gagne;
- }
- //test pour savoir si le joueur actuel a gagne sur la colonne actuelle
- public boolean gagne_colonne(String s , int lig,int col )
- {
- boolean gagne=false;
- int haut_lig = lig;
- int bas_lig=lig;
- int count_haut=0;
- int count_bas=0;
- int g=0;
- if (this.mat[lig][col]==s && this.mat[haut_lig-1][col]==s
- || this.mat[lig][col]==s && this.mat[bas_lig+1][col]==s)
- {
- while (this.mat[haut_lig][col]==s && count_haut<4 && haut_lig > 0)
- {
- count_haut++;
- haut_lig--;
- }
- while (this.mat [bas_lig][col]==s && count_bas<4 && bas_lig<this.lig)
- {
- count_bas++;
- bas_lig++;
- }
- g=(count_bas+count_haut)-1;
- if (g>=4)
- {
- gagne=true;
- }
- }
- return gagne;
- }
- //test la diagonale qui part du haut droit jusqu'au bas gauche
- public boolean gagne_haut_droit_to_bas_gauche(String s ,int lig,int col)
- {
- boolean gagne=false;
- int droite_col=col;
- int haut_lig=lig;
- int gauche_col=col;
- int bas_lig=lig;
- int count_droite=0;
- int count_gauche=0;
- int g=0;
- if (this.mat[lig][col]==s && this.mat[haut_lig-1][droite_col+1]==s
- ||this.mat[lig][col]==s && this.mat[bas_lig+1][gauche_col-1]==s )
- {
- while (this.mat[haut_lig][droite_col]==s && count_droite<4 && haut_lig > 0 && droite_col<this.col )
- {
- count_droite++;
- haut_lig--;
- droite_col++;
- }
- while (this.mat[bas_lig][gauche_col]==s && count_gauche<4 && bas_lig < this.lig && gauche_col > 0 )
- {
- count_gauche++;
- bas_lig++;
- gauche_col--;
- }
- }
- g=(count_gauche+count_droite)-1;
- if (g>=4)
- {
- gagne=true;
- }
- return gagne;
- }
- //test sur la diago qui part du haut gauche jusqu'au bas droit
- public boolean gagne_haut_gauche_bas_droit (String s, int lig, int col)
- {
- boolean gagne=false;
- int droite_col=col;
- int haut_lig=lig;
- int gauche_col=col;
- int bas_lig=lig;
- int count_droite=0;
- int count_gauche=0;
- int g=0;
- if (this.mat[lig][col]==s && this.mat[haut_lig-1][gauche_col-1]==s
- ||this.mat[lig][col]==s && this.mat[bas_lig+1][droite_col+1]==s )
- {
- while (this.mat[haut_lig][gauche_col]==s && count_gauche<4 && haut_lig > 0 && gauche_col > 0)
- {
- count_droite++;
- haut_lig--;
- gauche_col--;
- }
- while (this.mat[bas_lig][droite_col]==s && count_droite<4 && bas_lig < this.lig && droite_col<this.col )
- {
- count_gauche++;
- bas_lig++;
- droite_col++;
- }
- }
- g=(count_gauche+count_droite)-1;
- if (g>=4)
- {
- gagne=true;
- }
- return gagne;
- }
- //place le symbole dans la colonne choisi par le joueur a la suite des autres et renvoi le numero de la ligne
- public int place(String s ,int col)
- {
- int i=this.lig;
- while (this.mat[i][col] != "")
- {
- i--;
- }
- this.mat[i][col]=s;
- return i;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement