AI { protected List<Move> thinkUpToNMoves(int color, int level) { List<Move> moves = new ArrayList<Move>(); Board board = game.boards.get(color); int nbSeeds = board.seeds().size(); Log.d(tag, "# of seeds : " + nbSeeds); if (nbSeeds==0) return moves; long startedAt = new Date().getTime(); List<Square> seeds = board.seeds(); Collections.sort(seeds); if (board.pieces.size()> board.nbPieces - 4) { seeds = seeds.subList(0, Math.min(seeds.size(), 2)); } for (Square seed : seeds) { int movesAgainstSeed=0; Log.d(tag, "---- seed : " + seed); int maxMovesAgainstSeed = maxMoves[level] / nbSeeds; for (int p=0; p<board.pieces.size() && movesAgainstSeed<maxMovesAgainstSeed; p++) { Piece piece = board.pieces.get(p); for (int r=0; r<piece.rotations; r++, piece.rotate(1)) { for( int f=0; f<piece.flips; f++, piece.flip()) { for (Square s : piece.squares()) { int i = seed.i - s.i; int j = seed.j - s.j; if ( !board.outside(s, i, j) && game.fits(piece, i, j)) { Move move = new Move(piece, i, j); if (!game.valid(move)) { Log.e(tag, "Inconsistant ! "+move); } int score = SIZE_WEIGHT * piece.count; if (board.pieces.size()> board.nbPieces-5) { int io = game.size/2 - i; int jo = game.size/2 - j; score -= CENTER_WEIGHT * (io*io + jo*jo); } int seedsIfAdding = board.scoreSeedsIfAdding(piece, i, j); score += SEEDS_WEIGHT * seedsIfAdding ; int enemyscore = game.scoreEnemySeedsIfAdding(board.color, piece, i, j); score -= ENEMY_SEEDS_WEIGHT * enemyscore; if (board.pieces.size() < 9) { score += CHAINING_WEIGHT*chainingScore(color, move); } move.score = score; if (board.pieces.size()<= board.nbPieces-4 || piece.count>=5) { moves.add(move); } movesAgainstSeed++; if (moves.size()>= maxMoves[level]) { autoAdaptLevel(startedAt); return moves; } } } } } } } autoAdaptLevel(startedAt); return moves; } AI(Game game); boolean hasMove(int color); Move think(int color, int level); boolean overlaps( int color, Piece piece, int i, int j); static final String tag; public Game game; public int adaptedLevel; }
@Test public void testThinkWithL4() { pieces.add( L4); List<Move> moves = ai.thinkUpToNMoves(0,0); assertTrue(moves.size()== 6); }
@Test public void testThinkWithP5() { pieces.add( P5); List<Move> moves = ai.thinkUpToNMoves(0,3); assertTrue(moves.size()== 6); }
@Test public void testThinkWithP5AndL4() { Move move = new Move( P5, 0, 1); game.play(move); assertTrue (board.seeds().size() == 2); pieces.add( L4 ); List<Move> moves = ai.thinkUpToNMoves(0,3); assertTrue(moves.size() ==17); game.play(new Move(L4, 1,4)); Log.d(tag, "" + board); assertTrue (board.seeds().size() == 4); }
@Test public void testThinkWithP5L4I3() { game.play(new Move(P5, 0, 1)); game.play(new Move(L4, 1,4)); pieces.add( I3 ); List<Move> moves = ai.thinkUpToNMoves(0, 3); assertTrue(board.seeds().size() == 4); Log.d(tag, "#moves : " + moves.size()); assertTrue(moves.size()== 6); I3.rotate(1); valid = game.play( new Move(I3, 4, 6)); assertTrue(valid); Log.d(tag, "" + board); assertTrue (board.seeds().size() == 6); }
@Test public void testThinkWithP5L4I3P5() { game.play( new Move( P5, 0, 1)); game.play( new Move(L4, 1,4)); pieces.add( I3 ); I3.rotate(1); valid = game.play( new Move(I3, 4,6)); assertTrue(valid); assertTrue (board.seeds().size() == 6); pieces.add( P5 ); List<Move> moves = ai.thinkUpToNMoves(0, 3); System.out.println("move : " + moves.size()); assertTrue(moves.size()== 37); }