Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gj.kalah.player.giesel;
- import java.util.Random;
- public class Board {
- public boolean first;
- public int[] boardField = new int[] { 4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0 };
- Random rand = new Random();
- public Board(boolean isFirst) {
- first = isFirst;
- }
- public int fillStrategy() {
- int mossa = 50;
- if (first) {
- for (int i = 5; i >= 0; i--) {
- if (boardField[i] >= (boardField.length - i) - 1) {
- return i;
- }
- }
- } else {
- for (int i = 12; i >= 7; i--) {
- if (boardField[i] >= ((boardField.length - (i - 7) - 1))) {
- i = i - 7;
- return i;
- }
- }
- }
- return mossa;
- }
- public int enemyStorageStrategy() {
- int n = 50;
- int limiteInferiore = 0;
- int limiteSuperiore = 6;
- if (!first) {
- while (limiteInferiore <= 5 && limiteSuperiore >= 0) {
- if (boardField[limiteInferiore] == limiteSuperiore) {
- for (int i = 12; i <= 6; i--) {
- if (boardField[i] >= boardField[limiteInferiore]) {
- return i;
- }
- }
- return n;
- } else {
- limiteInferiore++;
- limiteSuperiore--;
- }
- }
- } else {
- limiteInferiore += 7;
- while (limiteInferiore <= 12 && limiteSuperiore >= 0) {
- if (boardField[limiteInferiore] == limiteSuperiore) {
- for (int i = 5; i <= 0; i--) {
- if (boardField[i] >= boardField[limiteInferiore]) {
- return i;
- }
- }
- return n;
- } else {
- limiteInferiore++;
- limiteSuperiore--;
- }
- }
- }
- return n;
- }
- public int storageStrategy() {
- int n = 50;
- int limiteInferiore = 0;
- int limiteSuperiore = 6;
- if (first) {
- while (limiteInferiore <= 5 && limiteSuperiore >= 0) {
- if (boardField[limiteInferiore] == limiteSuperiore) {
- n = limiteInferiore;
- return n;
- } else {
- limiteInferiore++;
- limiteSuperiore--;
- }
- }
- } else {
- limiteInferiore += 7;
- while (limiteInferiore <= 12 && limiteSuperiore >= 0) {
- if (boardField[limiteInferiore] == limiteSuperiore) {
- n = limiteInferiore - 7;
- return n;
- } else {
- limiteInferiore++;
- limiteSuperiore--;
- }
- }
- }
- return n;
- }
- public int randomMoveStrategy() {
- int x = rand.nextInt(5);
- if (first) {
- if (boardField[x] != 0) {
- return x;
- } else {
- for (int i = 0; i <= 5; i++) {
- if (boardField[i] != 0) {
- x = i;
- return x;
- }
- }
- }
- } else {
- x = x + 7;
- if (boardField[x] != 0) {
- x = x - 7;
- return x;
- } else {
- for (int i = 7; i <= 12; i++) {
- if (boardField[i] != 0) {
- x = i;
- x = x - 7;
- return x;
- }
- }
- }
- }
- return x;
- }
- public int eatEnemy() {
- int[] rubare = new int[6];
- for (int i = 0; i < 6; i++) {
- rubare[i] = 0;
- }
- int risultatoMossa = 50;
- int puntatore = 0;
- int ca = 0;
- if (first) {
- while (puntatore <= 5) {
- int somma = puntatore + boardField[puntatore];
- while (somma >= 14) {
- somma = somma - 14;
- }
- if (somma > 5 && somma < 14) {
- return risultatoMossa;
- }
- if (somma <= 5 && boardField[somma] == 0 && boardField[puntatore] > 0) {
- int inversePointer = -puntatore + 12;
- risultatoMossa = puntatore;
- rubare[ca] = inversePointer;
- }
- puntatore = puntatore + 1;
- ca = ca + 1;
- }
- } else {
- puntatore = puntatore + 7;
- while (puntatore <= 12) {
- int somma = puntatore + boardField[puntatore];
- if (somma > 12 && somma < 6) {
- return risultatoMossa;
- }
- if (somma <= 12 && boardField[somma] == 0 && boardField[puntatore] > 0) {
- int inversePointer = -puntatore + 12;
- inversePointer = inversePointer - 7;
- risultatoMossa = puntatore;
- risultatoMossa = risultatoMossa - 7;
- rubare[ca] = inversePointer;
- }
- puntatore = puntatore + 1;
- ca = ca + 1;
- }
- }
- return EatBestEnemy(rubare, risultatoMossa);
- }
- public int EatBestEnemy(int[] migliore, int risultatoMossa) {
- int contatore = 0;
- int posizione = -1;
- int max = 0;
- if (risultatoMossa == 50) {
- return risultatoMossa;
- }
- for (int e : migliore) {
- if (e > max) {
- max = e;
- posizione = contatore;
- }
- contatore = contatore + 1;
- }
- if (posizione != -1) {
- return posizione;
- }
- return risultatoMossa;
- }
- // choose strategy to apply
- public int strategyPicker() {
- int primaStrategia = eatEnemy();
- int secondaStrategia = storageStrategy();
- int terzaStrategia = fillStrategy();
- int quartaStrategia = randomMoveStrategy();
- int quintaStrategia = enemyStorageStrategy();
- if (first) {
- if (secondaStrategia != 50) {
- return secondaStrategia;
- }
- if (primaStrategia != 50) {
- return primaStrategia;
- }
- if (quintaStrategia != 50) {
- return quintaStrategia;
- }
- if (terzaStrategia != 50) {
- return terzaStrategia;
- }
- return quartaStrategia;
- } else {
- if (secondaStrategia != 50) {
- return secondaStrategia;
- }
- if (primaStrategia != 50) {
- return primaStrategia;
- }
- if (quintaStrategia != 50) {
- return quintaStrategia;
- }
- if (terzaStrategia != 50) {
- return terzaStrategia;
- }
- }
- return quartaStrategia;
- }
- public void moveMyPlayer(int conca) {
- int pointer = conca;
- int pietra = boardField[conca];
- boardField[conca] = 0;
- while (pietra > 0) {
- ++pointer;
- if (pointer == 13) {
- pointer = 0;
- }
- boardField[pointer]++;
- pietra--;
- int inversePointer = -pointer + 12;
- if (pointer >= 0 && pointer <= 5 && pietra < 1 && boardField[pointer] == 1) {
- boardField[6] += boardField[inversePointer] + 1;
- boardField[pointer] = 0;
- boardField[inversePointer] = 0;
- }
- }
- conca++;
- }
- public void moveEnemyPlayer(int conca) {
- conca = conca + 7;
- int pointer = conca;
- int pietra = boardField[conca];
- boardField[conca] = 0;
- while (pietra > 0) {
- ++pointer;
- if (pointer == 14) {
- pointer = 0;
- }
- if (pointer == 6) {
- pointer = 7;
- }
- boardField[pointer]++;
- pietra--;
- int inversePointer = -pointer + 12;
- if (pointer >= 7 && pointer <= 12 && pietra < 1 && boardField[pointer] == 1) {
- boardField[13] += boardField[inversePointer] + 1;
- boardField[pointer] = 0;
- boardField[inversePointer] = 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement