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" />
</component>
<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$/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/PusherBoard.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/PusherBoard.java" afterDir="false" />
</list>
<list id="98b8a79f-2f53-42bf-96da-7af322697a0d" name="Changes by acastonguay" comment="" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
@ -23,13 +23,6 @@
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_BRANCH_BY_REPOSITORY">
<map>
<entry key="$PROJECT_DIR$" value="alexis" />
</map>
</option>
</component>
<component name="MacroExpansionManager">
<option name="directoryName" value="x0367gi2" />
</component>
@ -56,17 +49,15 @@
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
"codeWithMe.voiceChat.enabledByDefault": "false",
"last_opened_file_path": "/home/william/Dev/Projects",
"last_opened_file_path": "/home/william/Dev/Projects/Uni/Log320/LOG320_Lab4",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
"node.js.selected.package.tslint": "(autodetect)",
"nodejs_package_manager_path": "npm",
"vue.rearranger.settings.migration": "true"
}
}]]></component>
<component name="RunManager" selected="Application.Main">
<component name="RunManager">
<configuration name="Client" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="laboratoire4.Client" />
<module name="Lab4" />
@ -80,36 +71,8 @@
<option name="Make" enabled="true" />
</method>
</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>
<list>
<item itemvalue="Application.Main" />
<item itemvalue="Application.Test" />
<item itemvalue="Application.Client" />
</list>
</recent_temporary>
@ -123,54 +86,11 @@
<option name="presentableId" value="Default" />
<updated>1679255648701</updated>
<workItem from="1679255649975" duration="1223000" />
<workItem from="1679261446095" duration="1927000" />
<workItem from="1679281763874" duration="46000" />
<workItem from="1679344371350" duration="4138000" />
<workItem from="1679365557789" duration="21000" />
<workItem from="1679424430928" duration="11764000" />
<workItem from="1679261446095" duration="1908000" />
</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 />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</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>

View File

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

View File

@ -1,99 +1,124 @@
package laboratoire4;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.io.*;
import java.net.*;
public class Client {
private final Socket socket;
private final BufferedInputStream input;
private final BufferedOutputStream output;
private PusherBoard board;
class Client {
public static void main(String[] args) {
public Client(String host, int port) throws IOException {
socket = new Socket(host, port);
input = new BufferedInputStream(socket.getInputStream());
output = new BufferedOutputStream(socket.getOutputStream());
}
Socket MyClient;
BufferedInputStream input;
BufferedOutputStream output;
int[][] board = new int[8][8];
public void listen() {
try {
while (true) {
char cmd;
cmd = (char) input.read();
MyClient = new Socket("localhost", 8888);
if (board == null) {
boolean play = initializeBoard(cmd);
if (play) {
play();
}
}
if (cmd == '3') {
play();
}
if (cmd == '4') {
handleInvalidMove();
}
if (cmd == '5') {
stopGame();
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
private boolean initializeBoard(char cmd) throws IOException {
Player player = cmd == '1' ? Player.RED : Player.BLACK;
input = new BufferedInputStream(MyClient.getInputStream());
output = new BufferedOutputStream(MyClient.getOutputStream());
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
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();
input.read(aBuffer, 0, size);
int size = input.available();
//System.out.println("size " + size);
input.read(aBuffer,0,size);
String s = new String(aBuffer).trim();
String[] boardValues = s.split(" ");
board = new PusherBoard(player, boardValues);
return player == Player.RED;
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++;
}
}
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());
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];
private void handleInvalidMove() throws InterruptedException {
System.err.println("Mouvement invalide!");
Thread.sleep(500);
board.printBoard();
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++;
}
}
}
// Le serveur demande le prochain coup
// 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();
}
}
}
catch (IOException e) {
System.out.println(e);
}
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 {
private Node root;
public Node buildTree(PusherBoard board){
public Node buildTree(){
return null;
}
@ -15,26 +16,14 @@ public class GameTree {
static class Node {
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.pawn = pawn;
this.movement = movement;
}
public Collection<Node> getChilds() {
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;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class MiniMax {
private static final int MAX_DEPTH = 2;
private static Random random = new Random();
private static final int MAX_DEPTH = 4;
public static MiniMaxResult miniMax(PusherBoard board) {
return miniMax(board, true, 0, 0);
public static int miniMax(GameTree tree) {
return miniMax(tree.getRoot(), Player.MAX, 0);
}
private static MiniMaxResult miniMax(PusherBoard board, boolean max, int depth, int alphaBeta) {
int limScore = max ? Integer.MIN_VALUE : Integer.MAX_VALUE;
List<Pawn> pawns = max ?
board.getMaxPawns() :
board.getMinPawns();
// Collections.shuffle(pawns);
List<MiniMaxResult> results = new ArrayList<>();
for (Pawn pawn : pawns) {
int originalRow = pawn.getRow();
int originalCol = pawn.getCol();
for (Pawn.PawnMovement movement : Pawn.PawnMovement.values()) {
if (pawn.isMoveValid(board, movement)) {
pawn.move(movement);
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();
private static int miniMax(GameTree.Node node, Player player, int depth) {
if (depth == MAX_DEPTH) {
return evaluate(node);
}
if ((max && score > limScore) ||
(!max && score < limScore)) {
limScore = score;
return player == Player.MAX ?
max(node, depth) :
min(node, depth);
}
MiniMaxResult result = new MiniMaxResult(limScore, pawn, movement);
private static int max(GameTree.Node node, int depth) {
int maxScore = Integer.MIN_VALUE;
//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);
for (GameTree.Node child : node.getChilds()) {
int score = miniMax(child, Player.MIN, depth + 1);
if (score > maxScore) {
maxScore = score;
}
}
// Choisir aléatoirement
int index = random.nextInt(results.size());
return results.get(index);
return maxScore;
}
private static int evaluate(Pawn pawn, PusherBoard board, boolean max) {
int score = didWin(pawn, max) + didCapture(pawn, board, max) + isPushed(pawn);
private static int min(GameTree.Node node, int depth) {
int minScore = Integer.MAX_VALUE;
int homeRow = pawn.getPlayer() == Player.RED ? 0 : 7;
if (pawn.getRow() != homeRow) {
score += 50;
for (GameTree.Node child : node.getChilds()) {
int score = miniMax(child, Player.MIN, depth + 1);
if (score < minScore) {
minScore = score;
}
}
int direction = max ? 1 : -1;
return score * direction;
return minScore;
}
private static int isPushed(Pawn pawn) {
if (pawn instanceof Pushed) {
return 5;
}
private static int evaluate(GameTree.Node node) {
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;
}
static class MiniMaxResult {
private final int score;
private final Pawn pawn;
private final Pawn.PawnMovement movement;
public MiniMaxResult(int score, Pawn pawn, Pawn.PawnMovement movement) {
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;
public abstract class Pawn {
protected final Player player;
protected int row;
protected int col;
protected int direction;
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;
}
}
public enum Pawn {
R_PUSHER,
R_PUSHED,
B_PUSHER,
B_PUSHED
}

View File

@ -1,6 +1,6 @@
package laboratoire4;
public enum Player {
BLACK,
RED
MIN,
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,139 +1,129 @@
package laboratoire4;
import java.util.ArrayList;
import java.util.List;
public class PusherBoard {
private final Player player;
private Pawn[][] board;
private final List<Pawn> maxPawns = new ArrayList<>();
private final List<Pawn> minPawns = new ArrayList<>();
public PusherBoard(Player player, String[] boardValues) {
this.player = player;
newGame(boardValues);
public PusherBoard() {
this.newGame();
}
public void newGame(String[] boardValues) {
public void newGame(){
this.board = new Pawn[8][8];
int col = 0, row = 0;
for (String boardValue : boardValues) {
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);
for(int i = 0 ;i<board.length;i++){
board[7][i] = Pawn.B_PUSHER;
}
if (pawnPlayer == player) {
maxPawns.add(pawn);
} else {
minPawns.add(pawn);
for(int i = 0 ;i<board.length;i++){
board[6][i] = Pawn.B_PUSHED;
}
board[row][col] = pawn;
for(int i = 0 ;i<board.length;i++){
board[1][i] = Pawn.R_PUSHED;
}
col++;
if (col == board.length) {
col = 0;
row++;
}
for(int i = 0 ;i<board.length;i++){
board[0][i] = Pawn.R_PUSHER;
}
}
public String runNextMove() {
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) {
public int move (String move){
//FORMAT ex : D2-D3
String[] split = move.trim().split(" - ");
move(split[0], split[1]);
String[] split = move.split("-");
return move(split[0],split[1]);
}
private void move(Pawn pawn, Pawn.PawnMovement movement) {
move(pawn.getCol(), pawn.getRow(), pawn.getCol() + movement.getMove(), pawn.getRow() + pawn.getDirection());
}
public void move(String from, String to) {
public int move (String from, String to){
//FORMAT ex : from {D2}, to {D3}
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_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) {
if (!isValid(from_col, from_row, to_col)) {
return;
public int move(int from_col, int from_row, int to_col, int to_row){
//FORMAT ex : from_col {3}, from_row {2}, to_col {3}, to_row {3}
//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];
Pawn destPawn = board[to_row][to_col];
if (destPawn != null) {
if (destPawn.getPlayer() == player) {
maxPawns.remove(destPawn);
} else {
minPawns.remove(destPawn);
}
}
board[from_row][from_col] = null;
board[to_row][to_col] = pawn;
pawn.setRow(to_row);
pawn.setCol(to_col);
return -1;
}
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);
private boolean isValid(int from_col, int from_row, int to_col, int to_row){
if (pawn == null) {
return false;
Pawn[][] board = this.getBoard();
//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;
}
return pawn.isMoveValid(this, move);
//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;
}
}
return true;
}
public Pawn[][] getBoard() {
return board;
}
public List<Pawn> getMaxPawns() {
return maxPawns;
}
public void printBoard(){
public List<Pawn> getMinPawns() {
return minPawns;
}
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) {
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] + " | ");
} else {
}else{
System.out.print(" | ");
}
}

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"));
}
}