Compare commits

..

No commits in common. "master" and "alexis" have entirely different histories.

14 changed files with 298 additions and 692 deletions

View File

@ -1,124 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

View File

@ -4,13 +4,13 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="41395b4b-3252-492c-a869-5f4dab107186" name="Changes" comment="Small fixes"> <list default="true" id="41395b4b-3252-492c-a869-5f4dab107186" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/GameTree.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/Player.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/laboratoire4/Client.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/Client.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/laboratoire4/MiniMax.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/MiniMax.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/laboratoire4/MiniMax.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/MiniMax.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/laboratoire4/PusherBoard.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/PusherBoard.java" afterDir="false" />
</list> </list>
<list id="98b8a79f-2f53-42bf-96da-7af322697a0d" name="Changes by acastonguay" comment="" />
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
@ -23,13 +23,6 @@
</list> </list>
</option> </option>
</component> </component>
<component name="Git.Settings">
<option name="RECENT_BRANCH_BY_REPOSITORY">
<map>
<entry key="$PROJECT_DIR$" value="alexis" />
</map>
</option>
</component>
<component name="MacroExpansionManager"> <component name="MacroExpansionManager">
<option name="directoryName" value="x0367gi2" /> <option name="directoryName" value="x0367gi2" />
</component> </component>
@ -56,17 +49,15 @@
"RunOnceActivity.OpenProjectViewOnStart": "true", "RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true", "RunOnceActivity.ShowReadmeOnStart": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true", "SHARE_PROJECT_CONFIGURATION_FILES": "true",
"codeWithMe.voiceChat.enabledByDefault": "false", "last_opened_file_path": "/home/william/Dev/Projects/Uni/Log320/LOG320_Lab4",
"last_opened_file_path": "/home/william/Dev/Projects",
"node.js.detected.package.eslint": "true", "node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true", "node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)", "node.js.selected.package.eslint": "(autodetect)",
"node.js.selected.package.tslint": "(autodetect)", "node.js.selected.package.tslint": "(autodetect)",
"nodejs_package_manager_path": "npm",
"vue.rearranger.settings.migration": "true" "vue.rearranger.settings.migration": "true"
} }
}]]></component> }]]></component>
<component name="RunManager" selected="Application.Main"> <component name="RunManager">
<configuration name="Client" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true"> <configuration name="Client" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="laboratoire4.Client" /> <option name="MAIN_CLASS_NAME" value="laboratoire4.Client" />
<module name="Lab4" /> <module name="Lab4" />
@ -80,36 +71,8 @@
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
</method> </method>
</configuration> </configuration>
<configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="laboratoire4.Main" />
<module name="Lab4" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="laboratoire4.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Test" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="laboratoire4.Test" />
<module name="Lab4" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="laboratoire4.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue="Application.Main" />
<item itemvalue="Application.Test" />
<item itemvalue="Application.Client" /> <item itemvalue="Application.Client" />
</list> </list>
</recent_temporary> </recent_temporary>
@ -123,54 +86,11 @@
<option name="presentableId" value="Default" /> <option name="presentableId" value="Default" />
<updated>1679255648701</updated> <updated>1679255648701</updated>
<workItem from="1679255649975" duration="1223000" /> <workItem from="1679255649975" duration="1223000" />
<workItem from="1679261446095" duration="1927000" /> <workItem from="1679261446095" duration="1908000" />
<workItem from="1679281763874" duration="46000" />
<workItem from="1679344371350" duration="4138000" />
<workItem from="1679365557789" duration="21000" />
<workItem from="1679424430928" duration="11764000" />
</task> </task>
<task id="LOCAL-00001" summary="MiniMax">
<created>1679263366439</created>
<option name="number" value="00001" />
<option name="presentableId" value="LOCAL-00001" />
<option name="project" value="LOCAL" />
<updated>1679263366439</updated>
</task>
<task id="LOCAL-00002" summary="Movement logic">
<created>1679348475071</created>
<option name="number" value="00002" />
<option name="presentableId" value="LOCAL-00002" />
<option name="project" value="LOCAL" />
<updated>1679348475071</updated>
</task>
<task id="LOCAL-00003" summary="Small fixes">
<created>1679490100944</created>
<option name="number" value="00003" />
<option name="presentableId" value="LOCAL-00003" />
<option name="project" value="LOCAL" />
<updated>1679490100944</updated>
</task>
<option name="localTasksCounter" value="4" />
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" /> <option name="version" value="3" />
</component> </component>
<component name="VcsManagerConfiguration">
<MESSAGE value="MiniMax" />
<MESSAGE value="Movement logic" />
<MESSAGE value="Small fixes" />
<option name="LAST_COMMIT_MESSAGE" value="Small fixes" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/java/laboratoire4/PusherBoard.java</url>
<line>60</line>
<option name="timeStamp" value="7" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
</component>
</project> </project>

View File

@ -9,8 +9,8 @@
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<properties> <properties>
<maven.compiler.source>11</maven.compiler.source> <maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target> <maven.compiler.target>18</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>

View File

@ -1,99 +1,124 @@
package laboratoire4; package laboratoire4;
import java.io.BufferedInputStream; import java.io.*;
import java.io.BufferedOutputStream; import java.net.*;
import java.io.IOException;
import java.net.Socket;
public class Client { class Client {
private final Socket socket; public static void main(String[] args) {
private final BufferedInputStream input;
private final BufferedOutputStream output;
private PusherBoard board;
public Client(String host, int port) throws IOException { Socket MyClient;
socket = new Socket(host, port); BufferedInputStream input;
input = new BufferedInputStream(socket.getInputStream()); BufferedOutputStream output;
output = new BufferedOutputStream(socket.getOutputStream()); int[][] board = new int[8][8];
}
public void listen() {
try { try {
while (true) { MyClient = new Socket("localhost", 8888);
char cmd;
cmd = (char) input.read();
if (board == null) { input = new BufferedInputStream(MyClient.getInputStream());
boolean play = initializeBoard(cmd); output = new BufferedOutputStream(MyClient.getOutputStream());
if (play) { BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
play(); while(1 == 1){
char cmd = 0;
cmd = (char)input.read();
System.out.println(cmd);
// Debut de la partie en joueur blanc
if(cmd == '1'){
byte[] aBuffer = new byte[1024];
int size = input.available();
//System.out.println("size " + size);
input.read(aBuffer,0,size);
String s = new String(aBuffer).trim();
System.out.println(s);
String[] boardValues;
boardValues = s.split(" ");
int x=0,y=0;
for(int i=0; i<boardValues.length;i++){
board[x][y] = Integer.parseInt(boardValues[i]);
x++;
if(x == 8){
x = 0;
y++;
}
}
System.out.println("Nouvelle partie! Vous jouer blanc, entrez votre premier coup : ");
String move = null;
move = console.readLine();
output.write(move.getBytes(),0,move.length());
output.flush();
}
// Debut de la partie en joueur Noir
if(cmd == '2'){
System.out.println("Nouvelle partie! Vous jouer noir, attendez le coup des blancs");
byte[] aBuffer = new byte[1024];
int size = input.available();
//System.out.println("size " + size);
input.read(aBuffer,0,size);
String s = new String(aBuffer).trim();
System.out.println(s);
String[] boardValues;
boardValues = s.split(" ");
int x=0,y=0;
for(int i=0; i<boardValues.length;i++){
board[x][y] = Integer.parseInt(boardValues[i]);
x++;
if(x == 8){
x = 0;
y++;
}
} }
} }
if (cmd == '3') {
play();
}
if (cmd == '4') { // Le serveur demande le prochain coup
handleInvalidMove(); // Le message contient aussi le dernier coup joue.
} if(cmd == '3'){
byte[] aBuffer = new byte[16];
int size = input.available();
System.out.println("size :" + size);
input.read(aBuffer,0,size);
String s = new String(aBuffer);
System.out.println("Dernier coup :"+ s);
System.out.println("Entrez votre coup : ");
String move = null;
move = console.readLine();
output.write(move.getBytes(),0,move.length());
output.flush();
}
// Le dernier coup est invalide
if(cmd == '4'){
System.out.println("Coup invalide, entrez un nouveau coup : ");
String move = null;
move = console.readLine();
output.write(move.getBytes(),0,move.length());
output.flush();
}
// La partie est terminée
if(cmd == '5'){
byte[] aBuffer = new byte[16];
int size = input.available();
input.read(aBuffer,0,size);
String s = new String(aBuffer);
System.out.println("Partie Terminé. Le dernier coup joué est: "+s);
String move = null;
move = console.readLine();
output.write(move.getBytes(),0,move.length());
output.flush();
if (cmd == '5') {
stopGame();
break;
} }
} }
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} }
} catch (IOException e) {
System.out.println(e);
private boolean initializeBoard(char cmd) throws IOException {
Player player = cmd == '1' ? Player.RED : Player.BLACK;
byte[] aBuffer = new byte[1024];
int size = input.available();
input.read(aBuffer, 0, size);
String s = new String(aBuffer).trim();
String[] boardValues = s.split(" ");
board = new PusherBoard(player, boardValues);
return player == Player.RED;
}
private void play() throws IOException, InterruptedException {
byte[] aBuffer = new byte[16];
int size = input.available();
if (size > 0) {
input.read(aBuffer, 0, size);
String previousMove = new String(aBuffer);
System.out.println("Mouvement reçu: " + previousMove);
board.move(previousMove);
} }
// Thread.sleep(1000);
String nextMove = board.runNextMove();
System.out.println("Prochain mouvement: " + nextMove);
output.write(nextMove.getBytes(), 0, nextMove.length());
output.flush();
}
private void handleInvalidMove() throws InterruptedException {
System.err.println("Mouvement invalide!");
Thread.sleep(500);
board.printBoard();
}
private void stopGame() throws IOException {
System.out.println("GG well played!");
socket.close();
} }
} }

View File

@ -5,7 +5,8 @@ import java.util.Collection;
public class GameTree { public class GameTree {
private Node root; private Node root;
public Node buildTree(PusherBoard board){
public Node buildTree(){
return null; return null;
} }
@ -15,26 +16,14 @@ public class GameTree {
static class Node { static class Node {
private final Collection<Node> childs; private final Collection<Node> childs;
private final Pawn pawn;
private final Pawn.PawnMovement movement;
Node(Collection<Node> childs, Pawn pawn, Pawn.PawnMovement movement) { Node(Collection<Node> childs) {
this.childs = childs; this.childs = childs;
this.pawn = pawn;
this.movement = movement;
} }
public Collection<Node> getChilds() { public Collection<Node> getChilds() {
return childs; return childs;
} }
public Pawn getPawn() {
return pawn;
}
public Pawn.PawnMovement getMovement() {
return movement;
}
} }
} }

View File

@ -1,9 +0,0 @@
package laboratoire4;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
new Client("localhost", 8888).listen();
}
}

View File

@ -1,147 +1,50 @@
package laboratoire4; package laboratoire4;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class MiniMax { public class MiniMax {
private static final int MAX_DEPTH = 2; private static final int MAX_DEPTH = 4;
private static Random random = new Random();
public static MiniMaxResult miniMax(PusherBoard board) { public static int miniMax(GameTree tree) {
return miniMax(board, true, 0, 0); return miniMax(tree.getRoot(), Player.MAX, 0);
} }
private static MiniMaxResult miniMax(PusherBoard board, boolean max, int depth, int alphaBeta) { private static int miniMax(GameTree.Node node, Player player, int depth) {
int limScore = max ? Integer.MIN_VALUE : Integer.MAX_VALUE; if (depth == MAX_DEPTH) {
List<Pawn> pawns = max ? return evaluate(node);
board.getMaxPawns() : }
board.getMinPawns();
// Collections.shuffle(pawns);
List<MiniMaxResult> results = new ArrayList<>(); return player == Player.MAX ?
max(node, depth) :
min(node, depth);
}
for (Pawn pawn : pawns) { private static int max(GameTree.Node node, int depth) {
int originalRow = pawn.getRow(); int maxScore = Integer.MIN_VALUE;
int originalCol = pawn.getCol();
for (Pawn.PawnMovement movement : Pawn.PawnMovement.values()) { for (GameTree.Node child : node.getChilds()) {
if (pawn.isMoveValid(board, movement)) { int score = miniMax(child, Player.MIN, depth + 1);
pawn.move(movement); if (score > maxScore) {
maxScore = score;
int nextAlphaBeta = max ? Math.max(limScore, alphaBeta) : Math.min(limScore, alphaBeta);
int score = evaluate(pawn, board, max);
if (depth < MAX_DEPTH) {
score = miniMax(board, !max, depth + 1, nextAlphaBeta).getScore();
}
if ((max && score > limScore) ||
(!max && score < limScore)) {
limScore = score;
}
MiniMaxResult result = new MiniMaxResult(limScore, pawn, movement);
//elagage alphaBeta
// if ((max && limScore > alphaBeta) ||
// (!max && limScore < alphaBeta)) {
// pawn.setRow(originalRow);
// pawn.setCol(originalCol);
//
// return result;
// }
results.add(result);
}
pawn.setRow(originalRow);
pawn.setCol(originalCol);
} }
} }
// Choisir aléatoirement return maxScore;
int index = random.nextInt(results.size());
return results.get(index);
} }
private static int evaluate(Pawn pawn, PusherBoard board, boolean max) { private static int min(GameTree.Node node, int depth) {
int score = didWin(pawn, max) + didCapture(pawn, board, max) + isPushed(pawn); int minScore = Integer.MAX_VALUE;
int homeRow = pawn.getPlayer() == Player.RED ? 0 : 7; for (GameTree.Node child : node.getChilds()) {
if (pawn.getRow() != homeRow) { int score = miniMax(child, Player.MIN, depth + 1);
score += 50; if (score < minScore) {
} minScore = score;
int direction = max ? 1 : -1;
return score * direction;
}
private static int isPushed(Pawn pawn) {
if (pawn instanceof Pushed) {
return 5;
}
return 0;
}
private static int didWin(Pawn pawn, boolean max) {
int goal = pawn.getPlayer() == Player.RED ? 7 : 0;
if (pawn.getRow() == goal) {
return max ? 1000 : 100000;
}
return 0;
}
private static int didCapture(Pawn pawn, PusherBoard board, boolean max) {
Pawn capturedPawn = board.getBoard()[pawn.getRow()][pawn.getCol()];
if (capturedPawn != null) {
if (capturedPawn.getPlayer() != pawn.getPlayer()) {
if (max) {
return capturedPawn instanceof Pusher ? 100 : 50;
} else {
return capturedPawn instanceof Pusher ? 10000 : 5000;
}
} }
} }
return 0; return minScore;
} }
static class MiniMaxResult { private static int evaluate(GameTree.Node node) {
private final int score;
private final Pawn pawn;
private final Pawn.PawnMovement movement;
public MiniMaxResult(int score, Pawn pawn, Pawn.PawnMovement movement) { return 0;
this.score = score;
this.pawn = pawn;
this.movement = movement;
}
public int getScore() {
return score;
}
public Pawn getPawn() {
return pawn;
}
public Pawn.PawnMovement getMovement() {
return movement;
}
@Override
public String toString() {
return "MiniMaxResult{" +
"score=" + score +
", pawn=" + pawn +
", movement=" + movement +
'}';
}
} }
} }

View File

@ -1,93 +1,8 @@
package laboratoire4; package laboratoire4;
public abstract class Pawn { public enum Pawn {
protected final Player player; R_PUSHER,
protected int row; R_PUSHED,
protected int col; B_PUSHER,
protected int direction; B_PUSHED
public Pawn(Player player, int row, int col) {
this.player = player;
this.row = row;
this.col = col;
this.direction = player == Player.RED ? 1 : -1;
}
public void move(PawnMovement movement) {
setRow(row + direction);
setCol(col + movement.move);
}
public String getPosition() {
char colStr = (char)(col + 65);
return String.format("%s%d", colStr, row + 1);
}
public Player getPlayer() {
return player;
}
public int getCol() {
return col;
}
public int getRow() {
return row;
}
public void setCol(int col) {
this.col = col;
}
public void setRow(int row) {
this.row = row;
}
public int getDirection() {
return direction;
}
public boolean isMoveValid(PusherBoard game, PawnMovement movement) {
Pawn[][] board = game.getBoard();
int nextRow = row + getDirection();
if (nextRow < 0 || nextRow >= board.length) {
return false;
}
int nextCol = col + movement.move;
if (nextCol < 0 || nextCol >= board.length) {
return false;
}
return isMoveValid(board, movement);
}
protected abstract boolean isMoveValid(Pawn[][] board, PawnMovement movement);
enum PawnMovement {
STRAIGHT(0),
LEFT_DIAGONAL(-1),
RIGHT_DIAGONAL(1);
private final int move;
PawnMovement(int move) {
this.move = move;
}
public int getMove() {
return move;
}
public static PawnMovement from(int move) {
if (move == 0) {
return STRAIGHT;
}
if (move == 1) {
return RIGHT_DIAGONAL;
}
return LEFT_DIAGONAL;
}
}
} }

View File

@ -1,6 +1,6 @@
package laboratoire4; package laboratoire4;
public enum Player { public enum Player {
BLACK, MIN,
RED MAX
} }

View File

@ -1,34 +0,0 @@
package laboratoire4;
public class Pushed extends Pawn {
public Pushed(Player player, int row, int col) {
super(player, row, col);
}
@Override
public boolean isMoveValid(Pawn[][] board, PawnMovement movement) {
Pawn pusher = null;
Pawn to = board[row + direction][col + movement.getMove()];
if (col > 0 && movement == PawnMovement.RIGHT_DIAGONAL) {
pusher = board[row - direction][col - 1];
} else if (col < board.length - 1 && movement == PawnMovement.LEFT_DIAGONAL) {
pusher = board[row - direction][col + 1];
} else if (movement == PawnMovement.STRAIGHT) {
pusher = board[row - direction][col];
}
boolean pusherValid = pusher instanceof Pusher && pusher.player == player;
boolean destinationValid = to == null || (movement != PawnMovement.STRAIGHT && to.player != this.player);
return pusherValid && destinationValid;
}
@Override
public String toString() {
return "Pushed{" +
player +
", " + col +
", " + row +
"} ";
}
}

View File

@ -1,27 +0,0 @@
package laboratoire4;
public class Pusher extends Pawn {
public Pusher(Player player, int row, int col) {
super(player, row, col);
}
@Override
public boolean isMoveValid(Pawn[][] board, PawnMovement movement) {
Pawn to = board[row + direction][col + movement.getMove()];
if (to == null) {
return true;
}
return to.player != this.player && movement != PawnMovement.STRAIGHT;
}
@Override
public String toString() {
return "Pusher{" +
player +
", " + col +
", " + row +
"} ";
}
}

View File

@ -1,140 +1,130 @@
package laboratoire4; package laboratoire4;
import java.util.ArrayList;
import java.util.List;
public class PusherBoard { public class PusherBoard {
private final Player player;
private Pawn[][] board; private Pawn[][] board;
private final List<Pawn> maxPawns = new ArrayList<>(); public PusherBoard() {
private final List<Pawn> minPawns = new ArrayList<>(); this.newGame();
public PusherBoard(Player player, String[] boardValues) {
this.player = player;
newGame(boardValues);
} }
public void newGame(String[] boardValues) { public void newGame(){
this.board = new Pawn[8][8]; this.board = new Pawn[8][8];
int col = 0, row = 0; for(int i = 0 ;i<board.length;i++){
for (String boardValue : boardValues) { board[7][i] = Pawn.B_PUSHER;
int v = Integer.parseInt(boardValue); }
if (v != 0) {
Player pawnPlayer = (v == 1 || v == 2) ? Player.RED : Player.BLACK;
Pawn pawn;
if (v % 2 == 0) { // 2 et 4 sont les pushers
pawn = new Pusher(pawnPlayer, row, col);
} else {
pawn = new Pushed(pawnPlayer, row, col);
}
if (pawnPlayer == player) { for(int i = 0 ;i<board.length;i++){
maxPawns.add(pawn); board[6][i] = Pawn.B_PUSHED;
} else { }
minPawns.add(pawn);
}
board[row][col] = pawn; for(int i = 0 ;i<board.length;i++){
} board[1][i] = Pawn.R_PUSHED;
}
col++; for(int i = 0 ;i<board.length;i++){
if (col == board.length) { board[0][i] = Pawn.R_PUSHER;
col = 0;
row++;
}
} }
} }
public String runNextMove() { public int move (String move){
MiniMax.MiniMaxResult result = MiniMax.miniMax(this);
Pawn pawn = result.getPawn();
String initialPosition = pawn.getPosition();
System.out.println(result.getScore());
move(pawn, result.getMovement());
return initialPosition + "-" + pawn.getPosition();
}
public void move(String move) {
//FORMAT ex : D2-D3 //FORMAT ex : D2-D3
String[] split = move.trim().split(" - "); String[] split = move.split("-");
move(split[0], split[1]);
return move(split[0],split[1]);
} }
private void move(Pawn pawn, Pawn.PawnMovement movement) { public int move (String from, String to){
move(pawn.getCol(), pawn.getRow(), pawn.getCol() + movement.getMove(), pawn.getRow() + pawn.getDirection());
}
public void move(String from, String to) {
//FORMAT ex : from {D2}, to {D3} //FORMAT ex : from {D2}, to {D3}
int from_col = (int) from.charAt(0) - 65; int from_col = (int) from.charAt(0) - 65;
int from_row = Integer.parseInt(String.valueOf(from.charAt(1))) - 1; int from_row = Integer.parseInt(String.valueOf(from.charAt(1)))-1;
int to_col = (int) to.charAt(0) - 65; int to_col = (int) to.charAt(0) - 65;
int to_row = Integer.parseInt(String.valueOf(to.charAt(1))) - 1; int to_row = Integer.parseInt(String.valueOf(to.charAt(1)))-1;
move(from_col, from_row, to_col, to_row); return move(from_col, from_row, to_col, to_row);
} }
public void move(int from_col, int from_row, int to_col, int to_row) { public int move(int from_col, int from_row, int to_col, int to_row){
if (!isValid(from_col, from_row, to_col)) { //FORMAT ex : from_col {3}, from_row {2}, to_col {3}, to_row {3}
return;
//System.out.println("Move :" + from_col+""+from_row + "-"+ to_col+""+to_row);
//System.out.println("Move is valid : " + isValid(from_col,from_row,to_col,to_row));
if(isValid(from_col,from_row,to_col,to_row)){
Pawn pawn = this.getBoard()[from_row][from_col];
//System.out.println("Pawn to move : " + pawn);
this.getBoard()[from_row][from_col] = null;
this.getBoard()[to_row][to_col] = pawn;
} }
Pawn pawn = board[from_row][from_col]; return -1;
Pawn destPawn = board[to_row][to_col]; }
if (destPawn != null) {
if (destPawn.getPlayer() == player) { private boolean isValid(int from_col, int from_row, int to_col, int to_row){
maxPawns.remove(destPawn);
} else { Pawn[][] board = this.getBoard();
minPawns.remove(destPawn);
//out of bound?
if((from_col >7 || from_col<0)||((from_row >7 || from_row<0))||((to_col >7 || to_col<0))||((to_row >7 || to_row<0))) return false;
//no pawn to move?
Pawn pawnToMove = board[from_row][from_col];
if(pawnToMove == null) return false;
//Pawn at destination is our own pawn?
Pawn destination = board[to_row][to_col];
char source_color = pawnToMove.name().charAt(0);
if(destination != null){
char destination_color = destination.name().charAt(0);
if(source_color == destination_color) return false;
}
//Pawn goes back? or move is in valid range?
if(source_color == 'R'){
if(from_row>to_row) return false;
if(from_row+1 != to_row) return false;
}else if(source_color == 'B'){
if(from_row<to_row) return false;
if(from_row-1 != to_row) return false;
}
//PUSHED is in front a PUSHER?
if(pawnToMove.equals(Pawn.B_PUSHED)){
if(from_col+1==to_col){
if(board[from_row+1][from_col+1] != Pawn.B_PUSHER) return false;
}else if(from_col-1==to_col){
if(board[from_row-1][from_col+1] != Pawn.B_PUSHER) return false;
}else if(from_col==to_col){
if(board[from_row+1][from_col] != Pawn.B_PUSHER) return false;
}
}else if(pawnToMove.equals(Pawn.R_PUSHED)){
if(from_col+1 == to_col){
if(board[from_row+1][from_col-1] != Pawn.R_PUSHER) return false;
}else if(from_col-1 == to_col){
if(board[from_row-1][from_col-1] != Pawn.R_PUSHER) return false;
}else if(from_col == to_col){
if(board[from_row-1][from_col] != Pawn.R_PUSHER) return false;
} }
} }
board[from_row][from_col] = null; return true;
board[to_row][to_col] = pawn;
pawn.setRow(to_row);
pawn.setCol(to_col);
}
private boolean isValid(int from_col, int from_row, int to_col) {
Pawn pawn = getBoard()[from_row][from_col];
Pawn.PawnMovement move = Pawn.PawnMovement.from(to_col - from_col);
if (pawn == null) {
return false;
}
return pawn.isMoveValid(this, move);
} }
public Pawn[][] getBoard() { public Pawn[][] getBoard() {
return board; return board;
} }
public List<Pawn> getMaxPawns() { public void printBoard(){
return maxPawns;
}
public List<Pawn> getMinPawns() { for(int i=7; i>=0; i--){
return minPawns; for (int j = 0 ; j < this.board.length; j++){
} if(this.board[i][j] != null){
public void printBoard() {
for (int i = 7; i >= 0; i--) {
for (int j = 0; j < this.board.length; j++) {
if (this.board[i][j] != null) {
System.out.print(this.board[i][j] + " | "); System.out.print(this.board[i][j] + " | ");
} else { }else{
System.out.print(" | "); System.out.print(" | ");
} }
} }
System.out.println(); System.out.println();

View File

@ -0,0 +1,29 @@
package laboratoire4;
public class Test {
public static void main(String[] args) {
PusherBoard pusherBoard = new PusherBoard();
testMoves(pusherBoard);
pusherBoard.printBoard();
}
private static void testMoves(PusherBoard pusherBoard){
//Move examples
pusherBoard.move("D2","D3");
pusherBoard.move("C2","C3");
pusherBoard.move("D1","D2");
pusherBoard.move("D2","E3");
pusherBoard.move("C1","C2");
pusherBoard.move("E3","D4");
pusherBoard.move("A5","A6");
pusherBoard.move("D2","D1");
pusherBoard.move("H8","H9");
pusherBoard.move("A1","A3");
pusherBoard.move("A1","A2");
pusherBoard.move("A8","A6");
pusherBoard.move("A8","A7");
pusherBoard.move("D3","D4");
pusherBoard.move("C3","D4");
}
}

View File

@ -0,0 +1,29 @@
package laboratoire4;
import org.junit.Test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import static org.junit.jupiter.api.Assertions.*;
class TestPusherBoard {
private Method getIsValidMethod() throws NoSuchMethodException {
Method method = PusherBoard.class.getDeclaredMethod("isValid", int.class, int.class, int.class, int.class);
method.setAccessible(true);
return method;
}
@Test
public void moveValidation() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
PusherBoard pusherBoard = new PusherBoard();
//Move examples
assertEquals(true, getIsValidMethod().invoke(null, null, new Integer[] { 3,2,3,3 }));
// assertEquals(true,pusherBoard.move("C2","C3"));
// assertEquals(true,pusherBoard.move("D1","D3"));
// assertEquals(true,pusherBoard.move("D2","E3"));
}
}