45 lines
1.1 KiB
Java
45 lines
1.1 KiB
Java
package fr.iut_fbleau.HexGame;
|
|
|
|
import fr.iut_fbleau.GameAPI.AbstractGamePlayer;
|
|
import fr.iut_fbleau.GameAPI.AbstractPly;
|
|
import fr.iut_fbleau.GameAPI.IBoard;
|
|
import fr.iut_fbleau.GameAPI.Player;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.Random;
|
|
|
|
/**
|
|
* Bot non intelligent : joue un coup légal au hasard.
|
|
*/
|
|
public class RandomBot extends AbstractGamePlayer {
|
|
|
|
private final Random rng;
|
|
|
|
public RandomBot(Player me, Random rng) {
|
|
super(me);
|
|
this.rng = rng;
|
|
}
|
|
|
|
public RandomBot(Player me, long seed) {
|
|
this(me, new Random(seed));
|
|
}
|
|
|
|
@Override
|
|
public AbstractPly giveYourMove(IBoard board) {
|
|
// On récupère tous les coups légaux via l'itérateur fourni par le plateau.
|
|
List<AbstractPly> legal = new ArrayList<>();
|
|
Iterator<AbstractPly> it = board.iterator();
|
|
while (it.hasNext()) {
|
|
legal.add(it.next());
|
|
}
|
|
|
|
if (legal.isEmpty()) {
|
|
throw new IllegalStateException("No legal move available (board is full?)");
|
|
}
|
|
|
|
return legal.get(rng.nextInt(legal.size()));
|
|
}
|
|
}
|