Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package modele;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Random;
- public class AdversaireFacile extends Adversaire {
- TableauTicTacToe tableauTicTacToe;
- private static final byte ORDI_CASE = TableauTicTacToe.CASE_O;
- private static final byte HUMAIN_CASE = TableauTicTacToe.CASE_X;
- /**
- * Marche juste sur les lignes / colonnes, pas les diago
- */
- @Override
- public byte[] calculerProchainCoup(TableauTicTacToe tableauTicTacToe) {
- byte rangee;
- byte colonne;
- boolean gagne = false;
- byte[] positionGagnante = new byte[] { -1, -1 };
- List<byte[]> positionsPossibles = new ArrayList<byte[]>();
- for (byte i = 0; i < TableauTicTacToe.NBR_RANGEE; i++) {
- if (bloquerLigne(new byte[] { tableauTicTacToe.getValeur(i, (byte) 0),
- tableauTicTacToe.getValeur(i, (byte) 1), tableauTicTacToe.getValeur(i, (byte) 2) })) {
- for (byte j = 0; j < TableauTicTacToe.NBR_COLONNES; j++) {
- if (tableauTicTacToe.getValeur(i, j) == TableauTicTacToe.CASE_VIDE) {
- positionsPossibles.add(new byte[] { i, j });
- }
- }
- } else if (gagnerLigne(new byte[] { tableauTicTacToe.getValeur(i, (byte) 0),
- tableauTicTacToe.getValeur(i, (byte) 1), tableauTicTacToe.getValeur(i, (byte) 2) })) {
- for (byte j = 0; j < TableauTicTacToe.NBR_COLONNES; j++) {
- if (tableauTicTacToe.getValeur(i, j) == TableauTicTacToe.CASE_VIDE) {
- positionGagnante = (new byte[] { i, j });
- }
- }
- }
- }
- for (byte j = 0; j < TableauTicTacToe.NBR_COLONNES; j++) {
- if (bloquerLigne(new byte[] { tableauTicTacToe.getValeur((byte) 0, j),
- tableauTicTacToe.getValeur((byte) 1, j), tableauTicTacToe.getValeur((byte) 2, j) })) {
- for (byte i = 0; i < TableauTicTacToe.NBR_RANGEE; i++) {
- if (tableauTicTacToe.getValeur(i, j) == TableauTicTacToe.CASE_VIDE) {
- positionsPossibles.add(new byte[] { i, j });
- }
- }
- } else if (gagnerLigne(new byte[] { tableauTicTacToe.getValeur((byte) 0, j),
- tableauTicTacToe.getValeur((byte) 1, j), tableauTicTacToe.getValeur((byte) 2, j) })) {
- for (byte i = 0; i < TableauTicTacToe.NBR_RANGEE; i++) {
- if (tableauTicTacToe.getValeur(i, j) == TableauTicTacToe.CASE_VIDE) {
- positionGagnante = (new byte[] { i, j });
- }
- }
- }
- }
- byte[] posCoup;
- if (gagne) {
- // L'ordi peut gagner, c'est cool
- posCoup = positionGagnante;
- } else if (positionsPossibles.size() == 0) {
- // Aucune possibilité ne permet de ganger
- boolean caseValide = false;
- do {
- rangee = super.getCaseAleatoire();
- colonne = super.getCaseAleatoire();
- caseValide = tableauTicTacToe.validerPositionCoup(rangee, colonne);
- } while (!caseValide);
- posCoup = new byte[] { rangee, colonne };
- } else {
- posCoup = positionsPossibles.get(getRandomInt(positionsPossibles.size()));
- }
- return posCoup;
- }
- private int getRandomInt(int max) {
- Random rand = new Random();
- return (byte) rand.nextInt(max);
- }
- private boolean bloquerLigne(byte[] ligne) {
- return (ligne[0] == HUMAIN_CASE && (ligne[0] == ligne[1] || ligne[0] == ligne[2]))
- || (ligne[2] == HUMAIN_CASE && (ligne[2] == ligne[1]));
- }
- /**
- * Vérifie si l'ordinateur peut gagner en c
- *
- * @param ligne
- * @return
- */
- private boolean gagnerLigne(byte[] ligne) {
- return (ligne[0] == ORDI_CASE && (ligne[0] == ligne[1] || ligne[0] == ligne[2]))
- || (ligne[2] == ORDI_CASE && (ligne[2] == ligne[1]));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement