Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing. *;
- /**************************************
- * Déclaration des variables globales *
- **************************************/
- int cases[][] = new int[6][7];
- String J1 = "anonyme";
- String J2 = "anonyme";
- /*************************
- * Fonctions d’affichage *
- *************************/
- void initialisationGrille() {
- //on initialise la grille
- for (int i = 0; i < 6; i ++) for (int j = 0; j < 7; j ++) cases[i][j] =0;
- }
- void affichageCase(int entier, int i, int j, int joueur) {
- int n =0;
- JLabel nouv;
- if (joueur ==1)nouv = showIcon("http://semperdesign.livehost.fr/ISN/rouge.png", 30 +30 * j, 180 -30 * i, 1);
- else nouv = showIcon("http://semperdesign.livehost.fr/ISN/bleu.png", 30 +30 * j, 180 -30 * i, 1);
- }
- /*******************************
- * Fonctions de gestion du jeu *
- *******************************/
- int choixColonne(int c) {
- int i =0, j =-1,joueur = c %2 +1;
- boolean pleine = true;
- JLabel nouv = showIcon("http://semperdesign.livehost.fr/ISN/test.png", 30, 0, 1);
- if (joueur ==1) {
- showText("C'est à " + J1 + " de jouer!", 30, 10, 1);
- nouv = showIcon("http://semperdesign.livehost.fr/ISN/rouge.png", 210, 0, 1);
- }
- else {showText("C'est à " + J2 + " de jouer!", 30, 10, 1);
- nouv = showIcon("http://semperdesign.livehost.fr/ISN/bleu.png", 210, 0, 1);
- }
- while (j <0 || j >6 || pleine == false) {
- j = readInteger("choississez la colonne dans la quelle vous mettez votre jeton") -1;
- if (j >-1 && j <7)if (cases[5][j] !=0) {
- println("la colonne est plein change de colonne");
- pleine = false;
- }
- else pleine = true;
- }
- while (cases [i][j] > 0 && i <6) i ++;
- cases[i][j] = joueur;
- affichageCase(cases[i][j], i, j, joueur);
- return (c +1);
- }
- int partieTerminee(int c) {
- int temp =0,gagnant =0,i =0, j =0,m,n,joueur = 2 - (c %2);
- for (i = 0; i < 6; i ++) {
- temp =0;
- for (j = 0; j < 7; j ++) {
- if (cases[i][j] == joueur)temp ++;
- else temp =0;
- if (temp ==4)gagnant = joueur;
- }
- }
- for (j = 0; j < 7; j ++) {
- temp =0;
- for (i = 0; i < 6; i ++) {
- if (cases[i][j] == joueur)temp ++;
- else temp =0;
- if (temp ==4)gagnant = joueur;
- }
- }
- for (j =0; j <7; j ++) {
- for (i = 0; i <6; i ++) {
- temp =0;
- m = j;
- n = i;
- if (cases[n][m] == joueur)temp ++;
- while (n <5 && m <6) {
- if (m <6)m ++;
- if (n <5)n ++;
- if (cases[n][m] == joueur)temp ++;
- else temp =0;
- if (temp ==4)gagnant = joueur;
- }
- }
- }
- for (j =0; j <7; j ++) {
- for (i = 0; i <6; i ++) {
- temp =0;
- m = j;
- n = i;
- if (cases[n][m] == joueur)temp ++;
- while (n >0 && m <6) {
- if (m <6)m ++;
- if (n >0)n --;
- if (cases[n][m] == joueur)temp ++;
- else temp =0;
- if (temp ==4)gagnant = joueur;
- }
- }
- }
- return gagnant;
- }
- /***********************
- * Programme principal *
- ***********************/
- void main() {
- getPane().removeAll();
- getPane().setOpaque(true);
- getPane().setBackground(java.awt.Color.WHITE);
- int partie =0,compteur =0;
- while (partie ==0) {
- JLabel nouv;
- for (int i =5; i >=0; i --) {
- for (int j = 0; j <7; j ++) {
- nouv = showIcon("http://semperdesign.livehost.fr/ISN/blanc.png", 30 +30 * j, 180 -30 * i, 1);
- }
- }
- JOptionPane.showMessageDialog(getPane(), "Bienvenue ! \nBonne partie sur\"Le Puissance 4\" !");
- // On definit un pseudo pour chaque joueur.
- J1 = JOptionPane.showInputDialog(getPane(), "Rentrez votre pseudo", "Joueur 1", JOptionPane.QUESTION_MESSAGE);
- J2 = JOptionPane.showInputDialog(getPane(), "Rentrez votre pseudo", "Joueur 2", JOptionPane.QUESTION_MESSAGE);
- initialisationGrille();
- compteur =0;
- JOptionPane.showMessageDialog(getPane(), J1 + " et " + J2 + ", la partie commence");
- while (compteur < 42 && partieTerminee(compteur) == 0) {
- //six fois sept ? ou une autre question
- compteur = choixColonne(compteur);
- }
- nouv = showIcon("http://semperdesign.livehost.fr/ISN/test.png", 30, 0, 1);
- nouv = showIcon("http://semperdesign.livehost.fr/ISN/test.png", 30, 90, 1);
- if (partieTerminee(compteur) !=0) {
- if (partieTerminee(compteur) ==1)showText("Bravo " + J1 + " tu à gagné!", 60, 100, 1);
- else showText("Bravo " + J2 + " tu à gagné!", 60, 100, 1);
- }
- else showText("Personne n'as gagné !!", 60, 100, 1);
- partie =-1;
- partie = JOptionPane.showConfirmDialog(null, "voulez vous rejouez", "choose one", JOptionPane.YES_NO_OPTION);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement