@Test public void testThinkWithL4() { pieces.add( L4); List<Move> moves = ai.thinkUpToNMoves(0,0); assertTrue(moves.size()== 6); }
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 { 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 { 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); }
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); }
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; }