Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package modele;
- public class TableauTicTacToe {
- // ordre : [rangée][colonne]
- public static final byte NBR_COLONNES = 3;
- public static final byte NBR_RANGEE = 3;
- public static final byte CASE_X = 1;
- public static final byte CASE_O = 2;
- public static final byte CASE_VIDE = 0;
- private byte[][] tableauTicTacToeInt;
- public TableauTicTacToe() {
- super();
- }
- public byte[][] getTableauTicTacToeInt() {
- return tableauTicTacToeInt;
- }
- public void setTableauTicTacToeInt(byte[][] tableauTicTacToeInt) {
- if (validerTicTacToeInt(tableauTicTacToeInt)) {
- this.tableauTicTacToeInt = tableauTicTacToeInt;
- } else {
- System.out.println("Tableau invalide.");
- }
- }
- public void initialiserTableau() {
- for (int i = 0; i < NBR_RANGEE; i++) {
- for (int j = 0; j < NBR_COLONNES; j++) {
- tableauTicTacToeInt[i][j] = CASE_VIDE;
- }
- }
- }
- public boolean validerPositionCoup(byte rangee, byte colonne) {
- return rangee >= 0 && colonne >= 0 && rangee < NBR_RANGEE && colonne < NBR_COLONNES
- && tableauTicTacToeInt[rangee][colonne] == CASE_VIDE;
- }
- public boolean jouerX(byte rangee, byte colonne) {
- return jouer(CASE_X, rangee, colonne);
- }
- public boolean jouerO(byte rangee, byte colonne) {
- return jouer(CASE_O, rangee, colonne);
- }
- private boolean jouer(byte joueur, byte rangee, byte colonne) {
- boolean coupValide = true;
- if (!validerCase(joueur))
- coupValide = false;
- if (tableauTicTacToeInt[rangee][colonne] == CASE_VIDE) {
- tableauTicTacToeInt[rangee][colonne] = joueur;
- } else {
- coupValide = false;
- }
- return coupValide;
- }
- public byte getValeur(byte rangee, byte colonne) {
- return tableauTicTacToeInt[rangee][colonne];
- }
- public boolean verifierXGagnant() {
- return verifierGagnant(CASE_X);
- }
- public boolean verifierOGagnant() {
- return verifierGagnant(CASE_O);
- }
- private byte[][] getLigneGagnant(byte joueur) {
- byte[][] ligneGagnante = new byte[2][3];
- for (byte i = 0; i < NBR_RANGEE; i++) {
- if (tableauTicTacToeInt[i][0] == joueur && tableauTicTacToeInt[i][1] == joueur
- && tableauTicTacToeInt[i][2] == joueur) {
- ligneGagnante[0]=new byte[] {i,i,i};
- ligneGagnante[1]=new byte[] {0,1,2};
- }
- }
- for (byte i = 0; i < NBR_COLONNES; i++) {
- if (tableauTicTacToeInt[0][i] == joueur && tableauTicTacToeInt[1][i] == joueur
- && tableauTicTacToeInt[2][i] == joueur) {
- ligneGagnante[0]=new byte[] {0,1,2};
- ligneGagnante[1]=new byte[] {i,i,i};
- }
- }
- if (tableauTicTacToeInt[0][0] == joueur && tableauTicTacToeInt[1][1] == joueur
- && tableauTicTacToeInt[2][2] == joueur) {
- ligneGagnante[0]=new byte[] {0,1,2};
- ligneGagnante[1]=new byte[] {0,1,2};
- }
- if (tableauTicTacToeInt[2][0] == joueur && tableauTicTacToeInt[1][1] == joueur
- && tableauTicTacToeInt[0][2] == joueur) {
- ligneGagnante[0]=new byte[] {0,1,2};
- ligneGagnante[1]=new byte[] {2,1,0};
- }
- return ligneGagnante;
- }
- private boolean verifierGagnant(byte joueur) {
- boolean gagneWow = false;
- for (int i = 0; i < NBR_RANGEE; i++) {
- if (tableauTicTacToeInt[i][0] == joueur && tableauTicTacToeInt[i][1] == joueur
- && tableauTicTacToeInt[i][2] == joueur)
- gagneWow = true;
- }
- for (int i = 0; i < NBR_COLONNES; i++) {
- if (tableauTicTacToeInt[0][i] == joueur && tableauTicTacToeInt[1][i] == joueur
- && tableauTicTacToeInt[2][i] == joueur)
- gagneWow = true;
- }
- if (tableauTicTacToeInt[0][0] == joueur && tableauTicTacToeInt[1][1] == joueur
- && tableauTicTacToeInt[2][2] == joueur)
- gagneWow = true;
- if (tableauTicTacToeInt[2][0] == joueur && tableauTicTacToeInt[1][1] == joueur
- && tableauTicTacToeInt[0][2] == joueur)
- gagneWow = true;
- return gagneWow;
- }
- private boolean validerTicTacToeInt(byte[][] tableauTicTacToeInt) {
- boolean valide = true;
- if (tableauTicTacToeInt[0].length != NBR_COLONNES || tableauTicTacToeInt.length != NBR_RANGEE) {
- valide = false;
- }
- for (int i = 0; i < NBR_RANGEE; i++) {
- for (int j = 0; j < NBR_COLONNES; j++) {
- if (tableauTicTacToeInt[i][j] == CASE_X || tableauTicTacToeInt[i][j] == CASE_O
- || tableauTicTacToeInt[i][j] == CASE_VIDE) {
- valide = false;
- }
- }
- }
- return valide;
- }
- private boolean validerCase(byte uneCase) {
- return (uneCase == CASE_X || uneCase == CASE_O || uneCase == CASE_VIDE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement