Compare commits
5 Commits
Bots-aleat
...
kara-mosr-
| Author | SHA1 | Date | |
|---|---|---|---|
| db6db8e6c2 | |||
| 2dfc6014e0 | |||
| 3aec1d3f6e | |||
| a7d3e9d138 | |||
| f207da0e2b |
@@ -105,6 +105,7 @@ Voici un récapitulatif des commandes Git que vous utiliserez fréquemment :
|
||||
git commit -m "Ajout de [...] #numeroticket"
|
||||
|
||||
|
||||
|
||||
## 4. Pousser la branche
|
||||
|
||||
git push -set-upstream origin <nom-de-la-branche-#numeroticket>
|
||||
@@ -112,4 +113,9 @@ Voici un récapitulatif des commandes Git que vous utiliserez fréquemment :
|
||||
|
||||
## 5. Supprimer une branche
|
||||
|
||||
git branch -d <nom_de_la_branche>
|
||||
git branch -d <nom_de_la_branche>
|
||||
|
||||
## 6. Lancer des parties avec bots aleatoires
|
||||
|
||||
javac -d build $(find . -name "*.java")
|
||||
java -cp build fr.iut_fbleau.HexGame.HexSimMain --games 10000 --size 11 --seed 123 --csv results.csv
|
||||
@@ -34,8 +34,8 @@ public abstract class AbstractGame {
|
||||
|
||||
// constructeur à appeler dans le constructeur d'un fils concret avec super.
|
||||
public AbstractGame(IBoard b, EnumMap<Player,AbstractGamePlayer> m){
|
||||
this.currentBoard=b;
|
||||
this.mapPlayers=m;
|
||||
this.currentBoard=b;
|
||||
this.mapPlayers=m;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package fr.iut_fbleau.HexGame;
|
||||
|
||||
import fr.iut_fbleau.GameAPI.*;
|
||||
|
||||
import java.util.EnumMap;
|
||||
import java.util.Scanner;
|
||||
|
||||
@@ -19,12 +18,19 @@ public class HexMain {
|
||||
HexBoard board = new HexBoard(size);
|
||||
|
||||
Scanner sc = new Scanner(System.in);
|
||||
Result res;
|
||||
EnumMap<Player, AbstractGamePlayer> players = new EnumMap<>(Player.class);
|
||||
players.put(Player.PLAYER1, new HumanConsolePlayer(Player.PLAYER1, sc));
|
||||
players.put(Player.PLAYER2, new HumanConsolePlayer(Player.PLAYER2, sc));
|
||||
|
||||
AbstractGame game = new AbstractGame(board, players) {};
|
||||
Result res = game.run();
|
||||
|
||||
if (args.length>=2 && args[1].equals("autoplay")) {
|
||||
Simulation sim = new Simulation(board, players);
|
||||
res = sim.run();
|
||||
} else {
|
||||
AbstractGame game = new AbstractGame(board, players) {};
|
||||
res = game.run();
|
||||
}
|
||||
|
||||
System.out.println(board);
|
||||
System.out.println("Résultat (du point de vue de PLAYER1) : " + res);
|
||||
|
||||
145
javaAPI/fr/iut_fbleau/HexGame/Simulation.java
Normal file
145
javaAPI/fr/iut_fbleau/HexGame/Simulation.java
Normal file
@@ -0,0 +1,145 @@
|
||||
package fr.iut_fbleau.HexGame;
|
||||
|
||||
import fr.iut_fbleau.GameAPI.*;
|
||||
import java.util.EnumMap;
|
||||
import java.util.LinkedList;
|
||||
|
||||
|
||||
public class Simulation extends AbstractGame {
|
||||
|
||||
//ATTRIBUTS
|
||||
private HexPly bestmove;
|
||||
private float bestoutcome;
|
||||
private int MAXDEPTH = 6;
|
||||
private LinkedList<Integer[]> taken = new LinkedList<Integer[]>();
|
||||
|
||||
//ATTRIBUTS QUE JE NE VOUDRAIS PAS CRÉER IDÉALEMENT
|
||||
private IBoard simCurrentBoard;
|
||||
private EnumMap<Player, AbstractGamePlayer> simmapPlayers;
|
||||
|
||||
//CONSTRUCTEUR
|
||||
public Simulation(IBoard b, EnumMap<Player,AbstractGamePlayer> m){
|
||||
super(b, m);
|
||||
simCurrentBoard = b;
|
||||
simmapPlayers = m;
|
||||
}
|
||||
|
||||
//METHODES
|
||||
private float explMAX(HexBoard position, int depth){
|
||||
if (position.getResult()==Result.LOSS) {
|
||||
return -1.0f;
|
||||
} else if (position.getResult()==Result.WIN){
|
||||
return 1.0f;
|
||||
} else if (depth==MAXDEPTH) {
|
||||
return 0f;
|
||||
} else {
|
||||
float bestcase = -1.0f;
|
||||
HexPly bestcasemove;
|
||||
HexPly testmove;
|
||||
for (int i=0; i<position.getSize(); i++) {
|
||||
for (int j=0; j<position.getSize(); j++) {
|
||||
if(depth==0){
|
||||
//System.out.println("MAX New Line :");
|
||||
}
|
||||
Integer[] t = new Integer[]{i, j};
|
||||
testmove = new HexPly(Player.PLAYER1, i, j);
|
||||
if(!taken.contains(t) && position.isLegal(testmove)){
|
||||
//System.out.println(" MAX test move : "+Integer.toString(i)+","+Integer.toString(j));
|
||||
taken.add(t);
|
||||
position.doPly(testmove);
|
||||
float val = explMIN(position, depth+1);
|
||||
if (val >= bestcase) {
|
||||
//System.out.println(" MAX new best case");
|
||||
bestcase = val;
|
||||
bestcasemove = testmove;
|
||||
if (depth==0) {
|
||||
this.bestoutcome = bestcase;
|
||||
this.bestmove = bestcasemove;
|
||||
}
|
||||
}
|
||||
position.undoPly();
|
||||
taken.remove(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
return bestcase;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private float explMIN(HexBoard position, int depth){
|
||||
if (position.getResult()==Result.LOSS) {
|
||||
return -1.0f;
|
||||
} else if (position.getResult()==Result.WIN){
|
||||
return 1.0f;
|
||||
} else if (depth==MAXDEPTH) {
|
||||
return 0f;
|
||||
} else {
|
||||
float bestcase = 1.0f;
|
||||
HexPly bestcasemove;
|
||||
HexPly testmove;
|
||||
for (int i=0; i<position.getSize(); i++) {
|
||||
for (int j=0; j<position.getSize(); j++) {
|
||||
if(depth==0){
|
||||
//System.out.println("MIN New Line :");
|
||||
}
|
||||
Integer[] t = new Integer[]{i, j};
|
||||
testmove = new HexPly(Player.PLAYER2, i, j);
|
||||
if(!taken.contains(t) && position.isLegal(testmove)){
|
||||
//System.out.println(" MIN test move : "+Integer.toString(i)+","+Integer.toString(j));
|
||||
taken.add(t);
|
||||
position.doPly(testmove);
|
||||
float val = explMAX(position, depth+1);
|
||||
if (val <= bestcase) {
|
||||
//System.out.println(" MIN new best case");
|
||||
bestcase = val;
|
||||
bestcasemove = testmove;
|
||||
if (depth==0) {
|
||||
this.bestoutcome = bestcase;
|
||||
this.bestmove = bestcasemove;
|
||||
}
|
||||
}
|
||||
|
||||
position.undoPly();
|
||||
taken.remove(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
return bestcase;
|
||||
}
|
||||
}
|
||||
|
||||
private AbstractPly GiveBestMove(IBoard board) {
|
||||
if (!(board instanceof HexBoard)) {
|
||||
throw new IllegalArgumentException("Ce joueur attend un HexBoard.");
|
||||
}
|
||||
HexBoard hb = (HexBoard) board;
|
||||
float bestcase;
|
||||
if(hb.getCurrentPlayer()==Player.PLAYER1){
|
||||
bestcase = explMAX(hb, 0);
|
||||
} else {
|
||||
bestcase = explMIN(hb, 0);
|
||||
}
|
||||
return this.bestmove;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result run(){
|
||||
while(!simCurrentBoard.isGameOver()) {
|
||||
AbstractGamePlayer player = simmapPlayers.get(simCurrentBoard.getCurrentPlayer());
|
||||
IBoard board = simCurrentBoard.safeCopy();
|
||||
AbstractPly ply = GiveBestMove(board);
|
||||
HexPly concretePly = (HexPly) ply;
|
||||
|
||||
if (simCurrentBoard.isLegal(ply)) {
|
||||
simCurrentBoard.doPly(ply);
|
||||
taken.add(new Integer[]{concretePly.getRow(), concretePly.getCol()});
|
||||
System.out.println("Player "+player+" goes ("+concretePly.getRow()+","+concretePly.getCol()+")");
|
||||
}
|
||||
else throw new IllegalStateException("Player "+ player + " is a bloody cheat. He tried playing : "+concretePly.getRow()+","+concretePly.getCol()+" I give up.");
|
||||
}
|
||||
return simCurrentBoard.getResult();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user