From aa2dbc0c87accfee4796a3ad299d8d3e6257467e Mon Sep 17 00:00:00 2001 From: vaisse Date: Thu, 5 Feb 2026 18:03:16 +0100 Subject: [PATCH] ajout bot --- javaAPI/fr/iut_fbleau/HexGame/RandomBot.java | 40 ++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 javaAPI/fr/iut_fbleau/HexGame/RandomBot.java diff --git a/javaAPI/fr/iut_fbleau/HexGame/RandomBot.java b/javaAPI/fr/iut_fbleau/HexGame/RandomBot.java new file mode 100644 index 0000000..d75d385 --- /dev/null +++ b/javaAPI/fr/iut_fbleau/HexGame/RandomBot.java @@ -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 legal = new ArrayList<>(); + Iterator 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())); + } +} \ No newline at end of file