69 lines
2.1 KiB
Java
69 lines
2.1 KiB
Java
|
|
package fr.iut_fbleau.HexGame;
|
||
|
|
|
||
|
|
import fr.iut_fbleau.GameAPI.*;
|
||
|
|
|
||
|
|
import java.util.Scanner;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Joueur humain en console.
|
||
|
|
*
|
||
|
|
* Format attendu : "row col" (indices à partir de 0).
|
||
|
|
*/
|
||
|
|
public class HumanConsolePlayer extends AbstractGamePlayer {
|
||
|
|
|
||
|
|
private final Scanner in;
|
||
|
|
|
||
|
|
public HumanConsolePlayer(Player me, Scanner in) {
|
||
|
|
super(me);
|
||
|
|
this.in = in;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public AbstractPly giveYourMove(IBoard board) {
|
||
|
|
if (!(board instanceof HexBoard)) {
|
||
|
|
throw new IllegalArgumentException("Ce joueur attend un HexBoard.");
|
||
|
|
}
|
||
|
|
HexBoard hb = (HexBoard) board;
|
||
|
|
|
||
|
|
while (true) {
|
||
|
|
System.out.println(hb);
|
||
|
|
System.out.print("Joueur " + board.getCurrentPlayer() + " - entrez un coup (row col) : ");
|
||
|
|
|
||
|
|
String line = in.nextLine().trim();
|
||
|
|
if (line.equalsIgnoreCase("quit") || line.equalsIgnoreCase("exit")) {
|
||
|
|
throw new IllegalStateException("Partie interrompue par l'utilisateur.");
|
||
|
|
}
|
||
|
|
if (line.equalsIgnoreCase("help")) {
|
||
|
|
System.out.println("Entrez deux entiers : row col (0 <= row,col < " + hb.getSize() + ")");
|
||
|
|
System.out.println("Commandes: help, quit");
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
String[] parts = line.split("\\s+");
|
||
|
|
if (parts.length != 2) {
|
||
|
|
System.out.println("Format invalide. Exemple: 3 4");
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
int r = Integer.parseInt(parts[0]);
|
||
|
|
int c = Integer.parseInt(parts[1]);
|
||
|
|
HexPly ply = new HexPly(board.getCurrentPlayer(), r, c);
|
||
|
|
|
||
|
|
if (!hb.isLegal(ply)) {
|
||
|
|
System.out.println("Coup illégal (case occupée / hors plateau / mauvais joueur). Réessayez.");
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (hb.isWinningMove(ply)) {
|
||
|
|
System.out.println("Coup gagnant !");
|
||
|
|
}
|
||
|
|
|
||
|
|
return ply;
|
||
|
|
} catch (NumberFormatException e) {
|
||
|
|
System.out.println("Veuillez entrer deux entiers.");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|