ajout bot
This commit is contained in:
40
javaAPI/fr/iut_fbleau/HexGame/RandomBot.java
Normal file
40
javaAPI/fr/iut_fbleau/HexGame/RandomBot.java
Normal file
@@ -0,0 +1,40 @@
|
||||
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;
|
||||
|
||||
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) {
|
||||
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()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user