import javax.swing.JOptionPane; public class Puissance4 { public static int pos = 0; 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 jouer(char tab[][], char j1, char caractere, int joueurActif) { //System.out.print("Joueur 1 colonne? "); //int pos = Keyboard.readInt(); if(j1 == 'h') { if (joueurActif == 1) { pos = Integer.parseInt(JOptionPane.showInputDialog(null, "Joueur 1 colonne?")); } else { pos = Integer.parseInt(JOptionPane.showInputDialog(null, "Joueur 2 colonne?")); } } else { pos = genererCoup(tab, joueurActif); } while(!(pos == 1 || pos == 2 || pos == 3 || pos == 4 || pos == 5 || pos == 6 || pos == 7)) { if(j1 == 'h') { System.out.println("Colonne hors du tableau. " + "Choisissez une autre colonne."); if (joueurActif == 1) { pos = Integer.parseInt(JOptionPane.showInputDialog(null, "Joueur 1 colonne?")); } else { pos = Integer.parseInt(JOptionPane.showInputDialog(null, "Joueur 2 colonne?")); } } else { pos = genererCoup(tab, joueurActif); } } while(tab[0][pos-1] != '.') { if(j1 == 'h') { System.out.println("Colonne pleine. " + "Choisissez une autre colonne."); if (joueurActif == 1) { pos = Integer.parseInt(JOptionPane.showInputDialog(null, "Joueur 1 colonne?")); } else { pos = Integer.parseInt(JOptionPane.showInputDialog(null, "Joueur 2 colonne?")); } } else { pos = genererCoup(tab, joueurActif); } } for(int i = tab.length - 1; i >= 0; i--) { if(tab[i][pos-1] == '.') { tab[i][pos-1] = caractere; System.out.println(">> " + caractere + " " + pos + " " + (i+1)); break; } } } public static boolean verifcolonne(char tab[][], int pos, char comparer) { int lig = 0; for(int i = 0; i < tab.length; i++) if(tab[i][pos-1] == comparer) { lig = i; break; } if (lig < (tab.length -3)) { if(tab[lig+1][pos-1] == comparer && tab[lig+2][pos-1] == comparer && tab[lig+3][pos-1] == comparer) return true; else return false; } else { return false; } } public static boolean verifligne(char tab[][], int pos, char comparer) { int lig = 0; for(int i = 0; i < tab.length; i++) if(tab[i][pos-1] == comparer) { lig = i; break; } if (pos-1 != 0) while(tab[lig][pos-2] == comparer) { pos--; if(pos-1 == 0) break; } boolean valide = false; if(pos < (tab[0].length - 2)) { if(tab[lig][pos] == comparer && tab[lig][pos+1] == comparer && tab[lig][pos+2] == comparer) valide = true; } return valide; } public static boolean verifdiago(char tab[][], int pos, char comparer) { int lig = 0; for(int i = 0; i < tab.length; i++) if(tab[i][pos-1] == comparer) { lig = i; break; } pos--; int ligDepart = lig; int posDepart = pos; boolean valide = false; while (tab[lig][pos]==comparer && pos > 0 && lig > 0) { pos--; lig--; } if(pos > 0 && lig + 1 < 3 && pos < tab[0].length - 3){ if(tab[lig+1][pos+1] == comparer && tab[lig+2][pos+2] == comparer && tab[lig+3][pos+3] == comparer){valide = true;} } else if(pos == 0 && lig < 3 && pos < tab[0].length - 3) { if(tab[lig][pos] == comparer && tab[lig+1][pos+1] == comparer && tab[lig+2][pos+2] == comparer && tab[lig+3][pos+3] == comparer){valide = true;} } pos = posDepart; lig = ligDepart; while (tab[lig][pos]==comparer && pos > 0 && lig < tab.length-1) { pos--; lig++; } if(pos > 0 && lig > 3 && pos < tab[0].length - 3){ if(tab[lig-1][pos+1] == comparer && tab[lig-2][pos+2] == comparer && tab[lig-3][pos+3] == comparer){valide = true;} } else if(pos == 0 && lig > 3 && pos < tab[0].length - 3) { if(tab[lig][pos] == comparer && tab[lig-1][pos+1] == comparer && tab[lig-2][pos+2] == comparer && tab[lig-3][pos+3] == comparer){valide = true;} } return valide; } public static int genererCoup(char[][] tableau, int joueurActif) { char[][] copie = tableau; char comparer; char comparerAutre; int retour = 0; if(joueurActif == 1){ comparer = 'X'; comparerAutre = 'O'; } else { comparer = 'O'; comparerAutre = 'X'; } for (int i = 1; i<=copie[0].length; i++) { if(verifcolonne(copie,i,comparer) || verifligne(copie,pos, comparer) || verifdiago(copie,pos, comparer)) { retour = i; i = copie[0].length; } else if(verifcolonne(copie,i,comparerAutre) || verifligne(copie,pos, comparerAutre) || verifdiago(copie,pos, comparerAutre)) { retour = i; i = copie[0].length; } else { retour = (int)(Math.random()*copie[0].length); } } return retour; } public static final int nombreDeLignes = 6; public static final int nombreDeColonnes = 7; public static void main(String [] args) { char j1 = 'h'; char j2 = 'o'; boolean joueur1Actif = true; char[][] tab = new char[nombreDeLignes][nombreDeColonnes]; initialisation(tab); afficher(tab); while(true) { jouer(tab,j1, 'X', 1); afficher(tab); if(verifcolonne(tab,pos, 'X')){break;} if(verifligne(tab,pos, 'X')){break;} if(verifdiago(tab,pos, 'X')){break;} joueur1Actif = false; jouer(tab,j2, 'O', 2); afficher(tab); if(verifcolonne(tab,pos, 'O')){break;} if(verifligne(tab,pos, 'O')){break;} if(verifdiago(tab,pos, 'O')){break;} joueur1Actif = true; } System.out.println("FINI"); if (joueur1Actif) { System.out.println("Joueur 1 a gagné"); } else { System.out.println("Joueur 2 a gagné"); } } }