98 lines
1.9 KiB
Java
98 lines
1.9 KiB
Java
package model.comPlayer.generator;
|
|
|
|
import java.util.List;
|
|
|
|
import model.Board;
|
|
import model.Board.TileColor;
|
|
import model.CellPointer;
|
|
import model.Move;
|
|
import model.Referee;
|
|
import model.playerModel.Node;
|
|
import model.playerModel.PlayerModel;
|
|
|
|
public class NeuralNetworkMoveGenerator implements MoveGenerator {
|
|
|
|
public static int getSmallest(double[] list) {
|
|
int index = 0;
|
|
|
|
for (int i = 0; i < list.length; i++) {
|
|
if (list[index] < list[i]) {
|
|
index = i;
|
|
}
|
|
}
|
|
|
|
return index;
|
|
}
|
|
|
|
PlayerModel player;
|
|
|
|
public NeuralNetworkMoveGenerator(PlayerModel pm) {
|
|
player = pm;
|
|
}
|
|
|
|
@Override
|
|
public Move genMove(Board board, boolean asHuman) {
|
|
Move mv = null;
|
|
|
|
Node[] nodes = player.getOutputNodes(Referee.getBoardState(board));
|
|
|
|
TileColor color = TileColor.BLUE;
|
|
|
|
double[] colorStrengths = new double[4];
|
|
colorStrengths[0] = nodes[0].strength();
|
|
colorStrengths[1] = nodes[1].strength();
|
|
colorStrengths[2] = nodes[2].strength();
|
|
colorStrengths[3] = nodes[3].strength();
|
|
|
|
switch (getSmallest(colorStrengths)) {
|
|
case 1:
|
|
color = TileColor.GREEN;
|
|
break;
|
|
case 2:
|
|
color = TileColor.RED;
|
|
break;
|
|
case 3:
|
|
color = TileColor.YELLOW;
|
|
break;
|
|
case 0:
|
|
default:
|
|
color = TileColor.BLUE;
|
|
}
|
|
|
|
int index = 4;
|
|
for (int i = 4; i < nodes.length; i++) {
|
|
if (nodes[i].strength() > nodes[index].strength()) {
|
|
index = i;
|
|
}
|
|
}
|
|
|
|
int i = 4;
|
|
loop: for (int r = 0; r < Board.NUM_ROWS; r++) {
|
|
for (int c = 0; c < Board.NUM_COLS; c++) {
|
|
if (i == index) {
|
|
mv = new Move(color, r, c);
|
|
break loop;
|
|
}
|
|
|
|
else {
|
|
i++;
|
|
}
|
|
}
|
|
}
|
|
|
|
while (!Board.isLegal(board, mv.getCell())) {
|
|
mv = new Move(mv.getColor(), new CellPointer(
|
|
PlayerModel.rand.nextInt(Board.NUM_ROWS),
|
|
PlayerModel.rand.nextInt(Board.NUM_COLS)));
|
|
}
|
|
|
|
return mv;
|
|
}
|
|
|
|
@Override
|
|
public List<Move> genMoves(Board board, boolean asHuman, int nMoves) {
|
|
// Do nothing.
|
|
return null;
|
|
}
|
|
|
|
} |