Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.45 KB | None | 0 0
  1. package modele;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.Random;
  7.  
  8. public class AdversaireFacile extends Adversaire {
  9.  
  10.     TableauTicTacToe tableauTicTacToe;
  11.     private static final byte ORDI_CASE = TableauTicTacToe.CASE_O;
  12.     private static final byte HUMAIN_CASE = TableauTicTacToe.CASE_X;
  13.  
  14.     /**
  15.      * Marche juste sur les lignes / colonnes, pas les diago
  16.      */
  17.     @Override
  18.     public byte[] calculerProchainCoup(TableauTicTacToe tableauTicTacToe) {
  19.  
  20.         byte rangee;
  21.         byte colonne;
  22.  
  23.         boolean gagne = false;
  24.         byte[] positionGagnante = new byte[] { -1, -1 };
  25.  
  26.         List<byte[]> positionsPossibles = new ArrayList<byte[]>();
  27.         for (byte i = 0; i < TableauTicTacToe.NBR_RANGEE; i++) {
  28.             if (bloquerLigne(new byte[] { tableauTicTacToe.getValeur(i, (byte) 0),
  29.                     tableauTicTacToe.getValeur(i, (byte) 1), tableauTicTacToe.getValeur(i, (byte) 2) })) {
  30.                 for (byte j = 0; j < TableauTicTacToe.NBR_COLONNES; j++) {
  31.                     if (tableauTicTacToe.getValeur(i, j) == TableauTicTacToe.CASE_VIDE) {
  32.                         positionsPossibles.add(new byte[] { i, j });
  33.                     }
  34.                 }
  35.             } else if (gagnerLigne(new byte[] { tableauTicTacToe.getValeur(i, (byte) 0),
  36.                     tableauTicTacToe.getValeur(i, (byte) 1), tableauTicTacToe.getValeur(i, (byte) 2) })) {
  37.                 for (byte j = 0; j < TableauTicTacToe.NBR_COLONNES; j++) {
  38.                     if (tableauTicTacToe.getValeur(i, j) == TableauTicTacToe.CASE_VIDE) {
  39.                         positionGagnante = (new byte[] { i, j });
  40.                     }
  41.                 }
  42.             }
  43.         }
  44.         for (byte j = 0; j < TableauTicTacToe.NBR_COLONNES; j++) {
  45.             if (bloquerLigne(new byte[] { tableauTicTacToe.getValeur((byte) 0, j),
  46.                     tableauTicTacToe.getValeur((byte) 1, j), tableauTicTacToe.getValeur((byte) 2, j) })) {
  47.                 for (byte i = 0; i < TableauTicTacToe.NBR_RANGEE; i++) {
  48.                     if (tableauTicTacToe.getValeur(i, j) == TableauTicTacToe.CASE_VIDE) {
  49.                         positionsPossibles.add(new byte[] { i, j });
  50.                     }
  51.                 }
  52.             } else if (gagnerLigne(new byte[] { tableauTicTacToe.getValeur((byte) 0, j),
  53.                     tableauTicTacToe.getValeur((byte) 1, j), tableauTicTacToe.getValeur((byte) 2, j) })) {
  54.                 for (byte i = 0; i < TableauTicTacToe.NBR_RANGEE; i++) {
  55.                     if (tableauTicTacToe.getValeur(i, j) == TableauTicTacToe.CASE_VIDE) {
  56.                         positionGagnante = (new byte[] { i, j });
  57.                     }
  58.                 }
  59.             }
  60.         }
  61.         byte[] posCoup;
  62.         if (gagne) {
  63.             // L'ordi peut gagner, c'est cool
  64.             posCoup = positionGagnante;
  65.  
  66.         } else if (positionsPossibles.size() == 0) {
  67.             // Aucune possibilité ne permet de ganger
  68.             boolean caseValide = false;
  69.             do {
  70.                 rangee = super.getCaseAleatoire();
  71.                 colonne = super.getCaseAleatoire();
  72.                 caseValide = tableauTicTacToe.validerPositionCoup(rangee, colonne);
  73.             } while (!caseValide);
  74.             posCoup = new byte[] { rangee, colonne };
  75.         } else {
  76.             posCoup = positionsPossibles.get(getRandomInt(positionsPossibles.size()));
  77.         }
  78.         return posCoup;
  79.     }
  80.  
  81.     private int getRandomInt(int max) {
  82.         Random rand = new Random();
  83.  
  84.         return (byte) rand.nextInt(max);
  85.     }
  86.  
  87.     private boolean bloquerLigne(byte[] ligne) {
  88.         return (ligne[0] == HUMAIN_CASE && (ligne[0] == ligne[1] || ligne[0] == ligne[2]))
  89.                 || (ligne[2] == HUMAIN_CASE && (ligne[2] == ligne[1]));
  90.     }
  91.  
  92.     /**
  93.      * Vérifie si l'ordinateur peut gagner en c
  94.      *
  95.      * @param ligne
  96.      * @return
  97.      */
  98.     private boolean gagnerLigne(byte[] ligne) {
  99.         return (ligne[0] == ORDI_CASE && (ligne[0] == ligne[1] || ligne[0] == ligne[2]))
  100.                 || (ligne[2] == ORDI_CASE && (ligne[2] == ligne[1]));
  101.     }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement