Advertisement
Guest User

modif3

a guest
Oct 31st, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.86 KB | None | 0 0
  1.  
  2. import javax.swing. *;
  3.  
  4. /**************************************
  5.     * Déclaration des variables globales *
  6.     **************************************/
  7.  int cases[][] = new int[6][7];
  8.  
  9. String J1 = "anonyme";
  10.  
  11. String J2 = "anonyme";
  12.  
  13. /*************************
  14.     * Fonctions d’affichage *
  15.     *************************/
  16.  void initialisationGrille() {
  17.    //on initialise la grille
  18.     for (int i = 0; i < 6; i ++) for (int j = 0; j < 7; j ++) cases[i][j] =0;
  19. }
  20.  
  21. void affichageCase(int entier, int i, int j, int joueur) {
  22.    int n =0;
  23.    JLabel nouv;
  24.    if (joueur ==1)nouv = showIcon("http://semperdesign.livehost.fr/ISN/rouge.png", 30 +30 * j, 180 -30 * i, 1);
  25.    else nouv = showIcon("http://semperdesign.livehost.fr/ISN/bleu.png", 30 +30 * j, 180 -30 * i, 1);
  26. }
  27.  
  28. /*******************************
  29.     * Fonctions de gestion du jeu *
  30.     *******************************/
  31.  int choixColonne(int c) {
  32.    int i =0, j =-1,joueur = c %2 +1;
  33.    boolean pleine = true;
  34.    JLabel nouv = showIcon("http://semperdesign.livehost.fr/ISN/test.png", 30, 0, 1);
  35.    if (joueur ==1) {
  36.       showText("C'est à " + J1 + " de jouer!", 30, 10, 1);
  37.       nouv = showIcon("http://semperdesign.livehost.fr/ISN/rouge.png", 210, 0, 1);
  38.    }
  39.    else {showText("C'est à " + J2 + " de jouer!", 30, 10, 1);
  40.       nouv = showIcon("http://semperdesign.livehost.fr/ISN/bleu.png", 210, 0, 1);
  41.    }
  42.    while (j <0 || j >6 || pleine == false) {
  43.       j = readInteger("choississez la colonne dans la quelle vous mettez votre jeton") -1;
  44.       if (j >-1 && j <7)if (cases[5][j] !=0) {
  45.          println("la colonne est plein change de colonne");
  46.          pleine = false;
  47.       }
  48.       else pleine = true;
  49.    }
  50.    while (cases [i][j] > 0 && i <6) i ++;
  51.    cases[i][j] = joueur;
  52.    affichageCase(cases[i][j], i, j, joueur);
  53.    return (c +1);
  54. }
  55.  
  56. int partieTerminee(int c) {
  57.    int temp =0,gagnant =0,i =0, j =0,m,n,joueur = 2 - (c %2);
  58.    for (i = 0; i < 6; i ++) {
  59.       temp =0;
  60.       for (j = 0; j < 7; j ++) {
  61.          if (cases[i][j] == joueur)temp ++;
  62.          else temp =0;
  63.          if (temp ==4)gagnant = joueur;
  64.       }
  65.    }
  66.    for (j = 0; j < 7; j ++) {
  67.       temp =0;
  68.       for (i = 0; i < 6; i ++) {
  69.          if (cases[i][j] == joueur)temp ++;
  70.          else temp =0;
  71.          if (temp ==4)gagnant = joueur;
  72.       }
  73.    }
  74.    for (j =0; j <7; j ++) {
  75.       for (i = 0; i <6; i ++) {
  76.          temp =0;
  77.          m = j;
  78.          n = i;
  79.          if (cases[n][m] == joueur)temp ++;
  80.          while (n <5 && m <6) {
  81.             if (m <6)m ++;
  82.             if (n <5)n ++;
  83.             if (cases[n][m] == joueur)temp ++;
  84.             else temp =0;
  85.             if (temp ==4)gagnant = joueur;
  86.          }
  87.       }
  88.    }
  89.    for (j =0; j <7; j ++) {
  90.       for (i = 0; i <6; i ++) {
  91.          temp =0;
  92.          m = j;
  93.          n = i;
  94.          if (cases[n][m] == joueur)temp ++;
  95.          while (n >0 && m <6) {
  96.             if (m <6)m ++;
  97.             if (n >0)n --;
  98.             if (cases[n][m] == joueur)temp ++;
  99.             else temp =0;
  100.             if (temp ==4)gagnant = joueur;
  101.          }
  102.       }
  103.    }
  104.    return  gagnant;
  105. }
  106.  
  107. /***********************
  108.     * Programme principal *
  109.     ***********************/
  110.  void main() {
  111.    getPane().removeAll();
  112.    getPane().setOpaque(true);
  113.    getPane().setBackground(java.awt.Color.WHITE);
  114.    int partie =0,compteur =0;
  115.    while (partie ==0) {
  116.       JLabel nouv;
  117.       for (int i =5; i >=0; i --) {
  118.          for (int j = 0; j <7; j ++) {
  119.             nouv = showIcon("http://semperdesign.livehost.fr/ISN/blanc.png", 30 +30 * j, 180 -30 * i, 1);
  120.          }
  121.       }
  122.       JOptionPane.showMessageDialog(getPane(), "Bienvenue ! \nBonne partie sur\"Le Puissance 4\" !");
  123.       // On definit un pseudo pour chaque joueur.
  124.        J1 = JOptionPane.showInputDialog(getPane(), "Rentrez votre pseudo", "Joueur 1", JOptionPane.QUESTION_MESSAGE);
  125.       J2 = JOptionPane.showInputDialog(getPane(), "Rentrez votre pseudo", "Joueur 2", JOptionPane.QUESTION_MESSAGE);
  126.       initialisationGrille();
  127.       compteur =0;
  128.       JOptionPane.showMessageDialog(getPane(), J1 + " et " + J2 + ", la partie commence");
  129.       while (compteur < 42 && partieTerminee(compteur) == 0) {
  130.          //six fois sept ? ou une autre question
  131.           compteur = choixColonne(compteur);
  132.       }
  133.      nouv = showIcon("http://semperdesign.livehost.fr/ISN/test.png", 30, 0, 1);
  134.      nouv = showIcon("http://semperdesign.livehost.fr/ISN/test.png", 30, 90, 1);
  135.       if (partieTerminee(compteur) !=0) {
  136.          if (partieTerminee(compteur) ==1)showText("Bravo " + J1 + " tu à gagné!", 60, 100, 1);
  137.          else  showText("Bravo " + J2 + " tu à gagné!", 60, 100, 1);
  138.       }
  139.       else showText("Personne n'as gagné !!", 60, 100, 1);
  140.       partie =-1;
  141.       partie = JOptionPane.showConfirmDialog(null, "voulez vous rejouez", "choose one", JOptionPane.YES_NO_OPTION);
  142.    }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement