rendered paste bodyimport javax.swing.JOptionPane;
public class Puissance4
{
public static void initialisation(char tab[][])
{
//remplissage
//la premiere dimension du tableau est le nombre de rangees,
//en faisant tab.length, on obtient le nombre de rangees.
//on boucle sur chacune des rangees.
for( int i = 0 ; i< tab.length; i++)
{
//on boucle sur chacun des elements de la rangees i.
//en faisant tab[i].length, on obtient le nombre de colonnes
for(int j = 0; j< tab[i].length; j++)
{
//on met un caractere '.' a la ieme rangee,
//jieme colonne du tableau tab
tab[i][j] = '.';
}
}
}
public static void afficher(char tab[][])
{
System.out.println(" 1 2 3 4 5 6 7");
for(int i = 0; i< tab.length; i++)
{
System.out.print(i+1 + " ");
for(int j = 0; j< tab[i].length; j++)
System.out.print( tab[i][j]+ " " );
System.out.println();
}
System.out.println();
}
public static void joueur1(char tab[][],char j1, int pos)
{
//System.out.print("Joueur 1 colonne? ");
//int pos = Keyboard.readInt();
pos = Integer.parseInt(JOptionPane.showInputDialog(null, "Joueur 1 colonne?"));
if(!(pos == 1 || pos == 2 || pos == 3 || pos == 4
|| pos == 5 || pos == 6 || pos == 7))
{
System.out.println("Colonne hors du tableau. " +
"Choisissez une autre colonne.");
joueur1(tab,j1,pos);
return;
}
if(tab[0][pos-1] != '.')
{
System.out.println("Colonne pleine. " +
"Choisissez une autre colonne.");
joueur1(tab,j1,pos);
return;
}
for(int i = tab.length - 1; i >= 0; i--)
{
if(tab[i][pos-1] == '.')
{
tab[i][pos-1] = 'X';
System.out.println(">> X " + pos + " " + (i+1));
break;
}
}
}
public static void joueur2(char tab[][],char j2, int pos)
{
//System.out.print("Joueur 2 colonne? ");
//int pos = Keyboard.readInt();
pos = Integer.parseInt(JOptionPane.showInputDialog(null, "Joueur 2 colonne?"));
if(!(pos == 1 || pos == 2 || pos == 3 || pos == 4
|| pos == 5 || pos == 6 || pos == 7))
{
System.out.println("Colonne hors du tableau. " +
"Choisissez une autre colonne.");
joueur2(tab,j2,pos);
return;
}
if(tab[0][pos-1] != '.')
{
System.out.println("Colonne pleine. " +
"Choisissez une autre colonne.");
joueur2(tab,j2,pos);
return;
}
for(int i = tab.length - 1; i >= 0; i--)
{
if(tab[i][pos-1] == '.')
{
tab[i][pos-1] = 'O';
System.out.println(">> O " + pos + " " + (i+1));
break;
}
}
}
public static boolean veriflignejou1(char tab[][], int pos)
{
int lig = 0;
for(int i = 0; i < tab.length; i++)
if(tab[i][pos-1] == 'X')
{
lig = i;
break;
}
if(tab[lig-1][pos-1] == 'X' && tab[lig-2][pos-1] == 'X'
&& tab[lig-3][pos-1] == 'X')
return true;
else if(tab[lig+1][pos-1] == 'X' && tab[lig+2][pos-1] == 'X'
&& tab[lig+3][pos-1] == 'X')
return true;
else return false;
}
public static final int nombreDeLignes = 6;
public static final int nombreDeColonnes = 7;
public static void main(String [] args)
{
char j1 = 'h';
char j2 = 'h';
int pos = 0;
char[][] tab = new char[nombreDeLignes][nombreDeColonnes];
initialisation(tab);
afficher(tab);
while(true)
{
joueur1(tab,j1,pos);
afficher(tab);
if(veriflignejou1(tab,pos))
break;
joueur2(tab,j2,pos);
afficher(tab);
}
System.out.println("FINI");
}
}