Ai advances
This commit is contained in:
parent
cbd763ac47
commit
19bcbec099
|
@ -66,14 +66,18 @@ public class MalteAI implements Player{
|
|||
}
|
||||
System.out.println(Arrays.toString(weights));
|
||||
for (int i = 0; i < weights.length; i++) {
|
||||
if (Math.abs(max - weights[i]) < 0.01) {
|
||||
if (Math.abs(max - weights[i]) < 0.000001) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public double calculateWeight(int[][] board, int row) {
|
||||
public double calculateWeight(int[][] board, int column) {
|
||||
double W_START = 1.0;
|
||||
double W_ILLEGAL = -1.0;
|
||||
double W_INSTANT_WIN = 2;
|
||||
double W_PREVENT_INSTANT_WIN = 1.9;
|
||||
/* The amount of equal ideas, stretching out in star-like form.
|
||||
* ^ ^ ^
|
||||
* \ | /
|
||||
|
@ -86,42 +90,74 @@ public class MalteAI implements Player{
|
|||
*/
|
||||
int[] nrs = new int[8];
|
||||
int[] ids = new int[8];
|
||||
double weight = W_START;
|
||||
// Prever a place in the center;
|
||||
weight *= 1.0 - Math.abs(3.0 - column) / (3.0 * 100.0);
|
||||
// Relative positions like (-1, -1), (0, -1), etc.
|
||||
Position[] relAround = Position.getRelCirclePositions();
|
||||
Position thisPos = getLastEmpty(board, row);
|
||||
// The position we're looking at
|
||||
Position thisPos = getLastEmpty(board, column);
|
||||
// There is no space left in this row.
|
||||
if (thisPos == null) {
|
||||
return -1.0;
|
||||
weight *= W_ILLEGAL;
|
||||
return weight;
|
||||
}
|
||||
// Collect information about our surroundings.
|
||||
for (int i = 0; i < nrs.length; i++) {
|
||||
// The postion around us we're collecting information from.
|
||||
Position curPos = thisPos.add(relAround[i]);
|
||||
// Check whether this position is on the board at all.
|
||||
if (isPosOnBoard(curPos)) {
|
||||
// Get the ID in this position.
|
||||
ids[i] = getIDFromBoard(board, curPos);
|
||||
// Get the number of IDs in this direction.
|
||||
nrs[i] = countIDsInDir(board, ids[i], curPos, relAround[i]);
|
||||
} else {
|
||||
// If not on board, -1 is the way to go.
|
||||
ids[i] = -1;
|
||||
nrs[i] = -1;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 8; i++) {
|
||||
if (ids[i] == this.id &&
|
||||
nrs[i] == 3) {
|
||||
return 1.0;
|
||||
} else if (ids[i] == this.enemyID &&
|
||||
nrs[i] == 3) {
|
||||
return 0.9;
|
||||
// Look for problems/options in one direction at a time.
|
||||
for (int i = 0; i < nrs.length; i++) {
|
||||
if (nrs[i] == 3) {
|
||||
if (ids[i] == this.id) {
|
||||
// We can win like this!
|
||||
weight *= W_INSTANT_WIN;
|
||||
} else if (ids[i] == this.enemyID) {
|
||||
// We can at least prevent him from winning.
|
||||
weight *= W_PREVENT_INSTANT_WIN;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 4; i++) {
|
||||
// Look for problems/options in two directions at a time.
|
||||
for (int i = 0; i < nrs.length / 2; i++) {
|
||||
// Same ID on opposite sides.
|
||||
if (ids[i] == ids[i + 4]) {
|
||||
// Almost a finished row.
|
||||
if (nrs[i] + nrs[i + 4] > 3) {
|
||||
if (ids[i] == this.id) {
|
||||
return 1.0;
|
||||
// Its our ID.
|
||||
weight *= W_INSTANT_WIN;
|
||||
} else if (ids[i] == this.enemyID) {
|
||||
return 0.9;
|
||||
// Its the enemy's ID.
|
||||
weight *= W_PREVENT_INSTANT_WIN;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0.0;
|
||||
// Maybe we get lucky TODO: Expand this.
|
||||
int nrOfMyIDAround = 0;
|
||||
int nrOfEnemyIDAround = 0;
|
||||
for (int i = 0; i < ids.length; i++) {
|
||||
if (ids[i] == this.id) {
|
||||
nrOfMyIDAround++;
|
||||
} else if (ids[i] == this.enemyID) {
|
||||
nrOfEnemyIDAround++;
|
||||
}
|
||||
}
|
||||
weight *= 1.0 + (nrOfEnemyIDAround + nrOfMyIDAround) / (8.0 * 10.0);
|
||||
return weight;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue