william #1

Merged
william merged 6 commits from william into master 2023-03-21 18:08:15 -04:00
12 changed files with 734 additions and 139 deletions

124
.idea/uiDesigner.xml Normal file
View File

@ -0,0 +1,124 @@
<?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,11 +4,23 @@
<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=""> <list default="true" id="41395b4b-3252-492c-a869-5f4dab107186" name="Changes" comment="Movement logic">
<change afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/GameTree.java" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/Main.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$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.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/GameTree.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/GameTree.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/Pawn.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/Pawn.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/laboratoire4/Player.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/Player.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/laboratoire4/Pushed.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/Pushed.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/laboratoire4/Pusher.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/Pusher.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" />
<change beforePath="$PROJECT_DIR$/src/main/java/laboratoire4/Test.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/laboratoire4/TestPusherBoard.java" beforeDir="false" />
</list>
<list id="98b8a79f-2f53-42bf-96da-7af322697a0d" name="Changes by acastonguay" comment="">
<change beforePath="$PROJECT_DIR$/src/main/java/laboratoire4/GameTree.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/GameTree.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" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
@ -23,6 +35,41 @@
</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="LineStatusTrackerManager">
<file path="$PROJECT_DIR$/src/main/java/laboratoire4/MiniMax.java">
<ranges>
<range start1="2" end1="2" start2="2" end2="6" changelist="41395b4b-3252-492c-a869-5f4dab107186" />
<range start1="4" end1="4" start2="8" end2="9" changelist="41395b4b-3252-492c-a869-5f4dab107186" />
<range start1="5" end1="7" start2="10" end2="12" changelist="41395b4b-3252-492c-a869-5f4dab107186" />
<range start1="9" end1="13" start2="14" end2="19" changelist="41395b4b-3252-492c-a869-5f4dab107186" />
<range start1="14" end1="18" start2="20" end2="21" changelist="41395b4b-3252-492c-a869-5f4dab107186" />
<range start1="19" end1="21" start2="22" end2="25" changelist="41395b4b-3252-492c-a869-5f4dab107186" />
<range start1="22" end1="28" start2="26" end2="39" changelist="41395b4b-3252-492c-a869-5f4dab107186" />
<range start1="29" end1="31" start2="40" end2="50" changelist="41395b4b-3252-492c-a869-5f4dab107186" />
<range start1="32" end1="34" start2="51" end2="53" changelist="41395b4b-3252-492c-a869-5f4dab107186" />
<range start1="35" end1="39" start2="54" end2="56" changelist="41395b4b-3252-492c-a869-5f4dab107186" />
<range start1="42" end1="43" start2="59" end2="62" changelist="41395b4b-3252-492c-a869-5f4dab107186" />
<range start1="45" end1="46" start2="64" end2="67" changelist="41395b4b-3252-492c-a869-5f4dab107186" />
<range start1="47" end1="48" start2="68" end2="69" changelist="41395b4b-3252-492c-a869-5f4dab107186" />
<range start1="49" end1="49" start2="70" end2="125" changelist="98b8a79f-2f53-42bf-96da-7af322697a0d" />
</ranges>
</file>
<file path="$PROJECT_DIR$/src/main/java/laboratoire4/GameTree.java">
<ranges>
<range start1="17" end1="17" start2="17" end2="19" changelist="98b8a79f-2f53-42bf-96da-7af322697a0d" />
<range start1="18" end1="19" start2="20" end2="21" changelist="98b8a79f-2f53-42bf-96da-7af322697a0d" />
<range start1="20" end1="20" start2="22" end2="24" changelist="98b8a79f-2f53-42bf-96da-7af322697a0d" />
<range start1="25" end1="25" start2="29" end2="37" changelist="41395b4b-3252-492c-a869-5f4dab107186" />
</ranges>
</file>
</component>
<component name="MacroExpansionManager"> <component name="MacroExpansionManager">
<option name="directoryName" value="x0367gi2" /> <option name="directoryName" value="x0367gi2" />
</component> </component>
@ -49,7 +96,8 @@
"RunOnceActivity.OpenProjectViewOnStart": "true", "RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true", "RunOnceActivity.ShowReadmeOnStart": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true", "SHARE_PROJECT_CONFIGURATION_FILES": "true",
"last_opened_file_path": "/home/william/Dev/Projects/Uni/Log320/LOG320_Lab4", "codeWithMe.voiceChat.enabledByDefault": "false",
"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)",
@ -57,7 +105,7 @@
"vue.rearranger.settings.migration": "true" "vue.rearranger.settings.migration": "true"
} }
}]]></component> }]]></component>
<component name="RunManager"> <component name="RunManager" selected="Application.Main">
<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" />
@ -71,8 +119,36 @@
<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>
@ -86,11 +162,35 @@
<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="1908000" /> <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" />
</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>
<option name="localTasksCounter" value="3" />
<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" />
<option name="LAST_COMMIT_MESSAGE" value="Movement logic" />
</component>
</project> </project>

View File

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

View File

@ -1,124 +1,97 @@
package laboratoire4; package laboratoire4;
import java.io.*; import java.io.BufferedInputStream;
import java.net.*; import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.Socket;
class Client { public class Client {
public static void main(String[] args) { private final Socket socket;
private final BufferedInputStream input;
private final BufferedOutputStream output;
private PusherBoard board;
Socket MyClient; public Client(String host, int port) throws IOException {
BufferedInputStream input; socket = new Socket(host, port);
BufferedOutputStream output; input = new BufferedInputStream(socket.getInputStream());
int[][] board = new int[8][8]; output = new BufferedOutputStream(socket.getOutputStream());
}
public void listen() {
try { try {
MyClient = new Socket("localhost", 8888); while (true) {
char cmd;
cmd = (char) input.read();
input = new BufferedInputStream(MyClient.getInputStream()); if (board == null) {
output = new BufferedOutputStream(MyClient.getOutputStream()); boolean play = initializeBoard(cmd);
BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); if (play) {
while(1 == 1){ play();
char cmd = 0; }
}
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;
cmd = (char)input.read();
System.out.println(cmd);
// Debut de la partie en joueur blanc
if(cmd == '1'){
byte[] aBuffer = new byte[1024]; byte[] aBuffer = new byte[1024];
int size = input.available(); int size = input.available();
//System.out.println("size " + size);
input.read(aBuffer,0,size); input.read(aBuffer, 0, size);
String s = new String(aBuffer).trim(); String s = new String(aBuffer).trim();
System.out.println(s); String[] boardValues = s.split(" ");
String[] boardValues;
boardValues = s.split(" "); board = new PusherBoard(player, boardValues);
int x=0,y=0;
for(int i=0; i<boardValues.length;i++){ return player == Player.RED;
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 : "); private void play() throws IOException {
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++;
}
}
}
// 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]; byte[] aBuffer = new byte[16];
int size = input.available(); int size = input.available();
input.read(aBuffer,0,size); if (size > 0) {
String s = new String(aBuffer); input.read(aBuffer, 0, size);
System.out.println("Partie Terminé. Le dernier coup joué est: "+s); String previousMove = new String(aBuffer);
String move = null; System.out.println("Mouvement reçu: " + previousMove);
move = console.readLine();
output.write(move.getBytes(),0,move.length()); board.move(previousMove);
}
String nextMove = board.runNextMove();
System.out.println("Prochain mouvement: " + nextMove);
output.write(nextMove.getBytes(), 0, nextMove.length());
output.flush(); output.flush();
}
}
}
catch (IOException e) {
System.out.println(e);
} }
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,19 +5,36 @@ import java.util.Collection;
public class GameTree { public class GameTree {
private Node root; private Node root;
public Node buildTree(PusherBoard board){
return null;
}
public Node getRoot() { public Node getRoot() {
return root; return root;
} }
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) { Node(Collection<Node> childs, Pawn pawn, Pawn.PawnMovement movement) {
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

@ -0,0 +1,9 @@
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,49 +1,126 @@
package laboratoire4; package laboratoire4;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class MiniMax { public class MiniMax {
private static final int MAX_DEPTH = 4; private static final int MAX_DEPTH = 4;
private static Random random = new Random();
public static int miniMax(GameTree tree) { public static MiniMaxResult miniMax(PusherBoard board) {
return miniMax(tree.getRoot(), Player.MAX, 0); return miniMax(board, true, 0, Integer.MIN_VALUE);
} }
private static int miniMax(GameTree.Node node, Player player, int depth) { private static MiniMaxResult miniMax(PusherBoard board, boolean max, int depth, int alphaBeta) {
if (depth == MAX_DEPTH) { int limScore = max ? Integer.MIN_VALUE : Integer.MAX_VALUE;
return evaluate(node); List<Pawn> pawns = max ?
board.getMaxPawns() :
board.getMinPawns();
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 score = depth < MAX_DEPTH ?
miniMax(board, !max, depth + 1, limScore).getScore() :
evaluate(pawn, board);
score *= pawn.getDirection();
if ((max && score > limScore) ||
(!max && score < limScore)) {
limScore = score;
} }
return player == Player.MAX ? MiniMaxResult result = new MiniMaxResult(limScore, pawn, movement);
max(node, depth) :
min(node, depth); //elagage alphaBeta
if ((max && limScore >= alphaBeta) ||
(!max && limScore <= alphaBeta)) {
pawn.setRow(originalRow);
pawn.setCol(originalCol);
return result;
} }
private static int max(GameTree.Node node, int depth) { results.add(result);
int maxScore = Integer.MIN_VALUE; }
for (GameTree.Node child : node.getChilds()) { pawn.setRow(originalRow);
int score = miniMax(child, Player.MIN, depth + 1); pawn.setCol(originalCol);
if (score > maxScore) {
maxScore = score;
} }
} }
return maxScore; // Choisir aléatoirement
int index = random.nextInt(results.size());
return results.get(index);
} }
private static int min(GameTree.Node node, int depth) { private static int evaluate(Pawn pawn, PusherBoard board) {
int minScore = Integer.MAX_VALUE; int score = didWin(pawn);
score = Math.max(score, didCapture(pawn, board));
for (GameTree.Node child : node.getChilds()) { return score;
int score = miniMax(child, Player.MIN, depth + 1); }
if (score < minScore) {
minScore = score; private static int didWin(Pawn pawn) {
int goal = pawn.getPlayer() == Player.RED ? 7 : 0;
if (pawn.getRow() == goal) {
return Integer.MAX_VALUE;
}
return 0;
}
private static int didCapture(Pawn pawn, PusherBoard board) {
Pawn capturedPawn = board.getBoard()[pawn.getRow()][pawn.getCol()];
if (capturedPawn != null) {
if (capturedPawn.getPlayer() != pawn.getPlayer()) {
return 50;
} }
} }
return minScore; return 0;
} }
private static int evaluate(GameTree.Node node) { 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

@ -0,0 +1,93 @@
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;
}
}
}

View File

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

View File

@ -0,0 +1,34 @@
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;
boolean destinationValid = to == null || to.player != this.player;
return pusherValid && destinationValid;
}
@Override
public String toString() {
return "Pushed{" +
player +
", " + col +
", " + row +
"} ";
}
}

View File

@ -0,0 +1,27 @@
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

@ -0,0 +1,141 @@
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 void newGame(String[] boardValues) {
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);
}
if (pawnPlayer == player) {
maxPawns.add(pawn);
} else {
minPawns.add(pawn);
}
board[row][col] = pawn;
}
col++;
if (col == board.length) {
col = 0;
row++;
}
}
}
public String runNextMove() {
MiniMax.MiniMaxResult result = MiniMax.miniMax(this);
Pawn pawn = result.getPawn();
String initialPosition = pawn.getPosition();
move(pawn, result.getMovement());
return initialPosition + "-" + pawn.getPosition();
}
public void move(String move) {
//FORMAT ex : D2-D3
String[] split = move.trim().split(" - ");
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) {
//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 to_col = (int) to.charAt(0) - 65;
int to_row = Integer.parseInt(String.valueOf(to.charAt(1))) - 1;
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;
}
Pawn pawn = board[from_row][from_col];
Pawn destPawn = board[to_row][to_col];
if (destPawn != null) {
if (destPawn.getPlayer() == Player.RED) {
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);
}
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() {
return board;
}
public List<Pawn> getMaxPawns() {
return maxPawns;
}
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) {
System.out.print(this.board[i][j] + " | ");
} else {
System.out.print(" | ");
}
}
System.out.println();
System.out.println("----------------------------------------------------------------------------------------");
}
}
}