Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 KB | None | 0 0
  1. package modele;
  2.  
  3. public class TableauTicTacToe {
  4.  
  5.     // ordre : [rangée][colonne]
  6.     public static final byte NBR_COLONNES = 3;
  7.     public static final byte NBR_RANGEE = 3;
  8.     public static final byte CASE_X = 1;
  9.     public static final byte CASE_O = 2;
  10.     public static final byte CASE_VIDE = 0;
  11.  
  12.     private byte[][] tableauTicTacToeInt;
  13.  
  14.     public TableauTicTacToe() {
  15.         super();
  16.     }
  17.  
  18.     public byte[][] getTableauTicTacToeInt() {
  19.         return tableauTicTacToeInt;
  20.     }
  21.  
  22.     public void setTableauTicTacToeInt(byte[][] tableauTicTacToeInt) {
  23.         if (validerTicTacToeInt(tableauTicTacToeInt)) {
  24.             this.tableauTicTacToeInt = tableauTicTacToeInt;
  25.         } else {
  26.             System.out.println("Tableau invalide.");
  27.         }
  28.     }
  29.  
  30.     public void initialiserTableau() {
  31.         for (int i = 0; i < NBR_RANGEE; i++) {
  32.             for (int j = 0; j < NBR_COLONNES; j++) {
  33.                 tableauTicTacToeInt[i][j] = CASE_VIDE;
  34.             }
  35.         }
  36.     }
  37.  
  38.     public boolean validerPositionCoup(byte rangee, byte colonne) {
  39.  
  40.         return rangee >= 0 && colonne >= 0 && rangee < NBR_RANGEE && colonne < NBR_COLONNES
  41.                 && tableauTicTacToeInt[rangee][colonne] == CASE_VIDE;
  42.     }
  43.  
  44.     public boolean jouerX(byte rangee, byte colonne) {
  45.         return jouer(CASE_X, rangee, colonne);
  46.     }
  47.  
  48.     public boolean jouerO(byte rangee, byte colonne) {
  49.         return jouer(CASE_O, rangee, colonne);
  50.     }
  51.  
  52.     private boolean jouer(byte joueur, byte rangee, byte colonne) {
  53.         boolean coupValide = true;
  54.         if (!validerCase(joueur))
  55.             coupValide = false;
  56.         if (tableauTicTacToeInt[rangee][colonne] == CASE_VIDE) {
  57.             tableauTicTacToeInt[rangee][colonne] = joueur;
  58.         } else {
  59.             coupValide = false;
  60.         }
  61.         return coupValide;
  62.     }
  63.  
  64.     public byte getValeur(byte rangee, byte colonne) {
  65.         return tableauTicTacToeInt[rangee][colonne];
  66.     }
  67.  
  68.     public boolean verifierXGagnant() {
  69.         return verifierGagnant(CASE_X);
  70.     }
  71.  
  72.     public boolean verifierOGagnant() {
  73.         return verifierGagnant(CASE_O);
  74.     }
  75.  
  76.     private byte[][] getLigneGagnant(byte joueur) {
  77.         byte[][] ligneGagnante = new byte[2][3];
  78.  
  79.         for (byte i = 0; i < NBR_RANGEE; i++) {
  80.             if (tableauTicTacToeInt[i][0] == joueur && tableauTicTacToeInt[i][1] == joueur
  81.                     && tableauTicTacToeInt[i][2] == joueur) {
  82.                 ligneGagnante[0]=new byte[] {i,i,i};
  83.                 ligneGagnante[1]=new byte[] {0,1,2};
  84.             }
  85.  
  86.         }
  87.         for (byte i = 0; i < NBR_COLONNES; i++) {
  88.             if (tableauTicTacToeInt[0][i] == joueur && tableauTicTacToeInt[1][i] == joueur
  89.                     && tableauTicTacToeInt[2][i] == joueur) {
  90.  
  91.                 ligneGagnante[0]=new byte[] {0,1,2};
  92.                 ligneGagnante[1]=new byte[] {i,i,i};
  93.             }
  94.  
  95.         }
  96.         if (tableauTicTacToeInt[0][0] == joueur && tableauTicTacToeInt[1][1] == joueur
  97.                 && tableauTicTacToeInt[2][2] == joueur) {
  98.  
  99.             ligneGagnante[0]=new byte[] {0,1,2};
  100.             ligneGagnante[1]=new byte[] {0,1,2};
  101.  
  102.         }
  103.         if (tableauTicTacToeInt[2][0] == joueur && tableauTicTacToeInt[1][1] == joueur
  104.                 && tableauTicTacToeInt[0][2] == joueur) {
  105.             ligneGagnante[0]=new byte[] {0,1,2};
  106.             ligneGagnante[1]=new byte[] {2,1,0};
  107.         }
  108.  
  109.         return ligneGagnante;
  110.     }
  111.  
  112.     private boolean verifierGagnant(byte joueur) {
  113.         boolean gagneWow = false;
  114.         for (int i = 0; i < NBR_RANGEE; i++) {
  115.             if (tableauTicTacToeInt[i][0] == joueur && tableauTicTacToeInt[i][1] == joueur
  116.                     && tableauTicTacToeInt[i][2] == joueur)
  117.                 gagneWow = true;
  118.         }
  119.         for (int i = 0; i < NBR_COLONNES; i++) {
  120.             if (tableauTicTacToeInt[0][i] == joueur && tableauTicTacToeInt[1][i] == joueur
  121.                     && tableauTicTacToeInt[2][i] == joueur)
  122.                 gagneWow = true;
  123.         }
  124.         if (tableauTicTacToeInt[0][0] == joueur && tableauTicTacToeInt[1][1] == joueur
  125.                 && tableauTicTacToeInt[2][2] == joueur)
  126.             gagneWow = true;
  127.         if (tableauTicTacToeInt[2][0] == joueur && tableauTicTacToeInt[1][1] == joueur
  128.                 && tableauTicTacToeInt[0][2] == joueur)
  129.             gagneWow = true;
  130.  
  131.         return gagneWow;
  132.     }
  133.  
  134.     private boolean validerTicTacToeInt(byte[][] tableauTicTacToeInt) {
  135.         boolean valide = true;
  136.         if (tableauTicTacToeInt[0].length != NBR_COLONNES || tableauTicTacToeInt.length != NBR_RANGEE) {
  137.             valide = false;
  138.         }
  139.         for (int i = 0; i < NBR_RANGEE; i++) {
  140.             for (int j = 0; j < NBR_COLONNES; j++) {
  141.                 if (tableauTicTacToeInt[i][j] == CASE_X || tableauTicTacToeInt[i][j] == CASE_O
  142.                         || tableauTicTacToeInt[i][j] == CASE_VIDE) {
  143.                     valide = false;
  144.                 }
  145.             }
  146.         }
  147.  
  148.         return valide;
  149.     }
  150.  
  151.     private boolean validerCase(byte uneCase) {
  152.         return (uneCase == CASE_X || uneCase == CASE_O || uneCase == CASE_VIDE);
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement