Movement logic

This commit is contained in:
FyloZ 2023-03-20 17:41:13 -04:00
parent 86e3b5acc2
commit ec7f3568e5
Signed by: william
GPG Key ID: 835378AE9AF4AE97
7 changed files with 312 additions and 79 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,12 +4,13 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<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" />
<list default="true" id="41395b4b-3252-492c-a869-5f4dab107186" name="Changes" comment="MiniMax">
<change afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/Pushed.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/Pusher.java" 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/MiniMax.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/MiniMax.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/Pawn.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/Pawn.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>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -23,6 +24,13 @@
</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>
@ -44,19 +52,19 @@
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
"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)",
"vue.rearranger.settings.migration": "true"
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;SHARE_PROJECT_CONFIGURATION_FILES&quot;: &quot;true&quot;,
&quot;last_opened_file_path&quot;: &quot;/home/william/Dev/Projects&quot;,
&quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
&quot;node.js.detected.package.tslint&quot;: &quot;true&quot;,
&quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
&quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;,
&quot;vue.rearranger.settings.migration&quot;: &quot;true&quot;
}
}]]></component>
}</component>
<component name="RunManager">
<configuration name="Client" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="laboratoire4.Client" />
@ -86,11 +94,25 @@
<option name="presentableId" value="Default" />
<updated>1679255648701</updated>
<workItem from="1679255649975" duration="1223000" />
<workItem from="1679261446095" duration="1908000" />
<workItem from="1679261446095" duration="1927000" />
<workItem from="1679281763874" duration="46000" />
<workItem from="1679344371350" duration="3857000" />
</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>
<option name="localTasksCounter" value="2" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="MiniMax" />
<option name="LAST_COMMIT_MESSAGE" value="MiniMax" />
</component>
</project>

View File

@ -5,8 +5,7 @@ import java.util.Collection;
public class GameTree {
private Node root;
public Node buildTree(){
public Node buildTree(PusherBoard board){
return null;
}

View File

@ -1,8 +1,56 @@
package laboratoire4;
public enum Pawn {
R_PUSHER,
R_PUSHED,
B_PUSHER,
B_PUSHED
public abstract class Pawn {
protected final PawnColor color;
protected int col;
protected int row;
public Pawn(PawnColor color, int col, int row) {
this.color = color;
this.col = col;
this.row = row;
}
public PawnColor getColor() {
return color;
}
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 abstract boolean isMoveValid(PusherBoard board, PawnMovement movement);
enum PawnColor {
RED,
BLACK
}
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;
}
}
}

View File

@ -0,0 +1,26 @@
package laboratoire4;
public class Pushed extends Pawn {
public Pushed(PawnColor color, int col, int row) {
super(color, col, row);
}
@Override
public boolean isMoveValid(PusherBoard game, PawnMovement movement) {
Pawn[][] board = game.getBoard();
Pawn pusher = null;
Pawn to = board[row + 1][col + movement.getMove()];
if (col > 0 && movement == PawnMovement.RIGHT_DIAGONAL) {
pusher = board[row - 1][col - 1];
} else if (col < board.length - 1 && movement == PawnMovement.LEFT_DIAGONAL) {
pusher = board[row - 1][col + 1];
} else if (movement == PawnMovement.STRAIGHT) {
pusher = board[row - 1][col];
}
boolean pusherValid = pusher != null;
boolean destinationValid = to == null || to.color != this.color;
return pusherValid && destinationValid;
}
}

View File

@ -0,0 +1,14 @@
package laboratoire4;
public class Pusher extends Pawn {
public Pusher(PawnColor color, int col, int row) {
super(color, col, row);
}
@Override
public boolean isMoveValid(PusherBoard game, PawnMovement movement) {
Pawn to = game.getBoard()[row + 1][col + movement.getMove()];
return to == null || to.color != this.color;
}
}

View File

@ -1,7 +1,6 @@
package laboratoire4;
public class PusherBoard {
private Pawn[][] board;
public PusherBoard() {
@ -11,20 +10,22 @@ public class PusherBoard {
public void newGame() {
this.board = new Pawn[8][8];
// TODO: Construire depuis le serveur
for (int i = 0; i < board.length; i++) {
board[7][i] = Pawn.B_PUSHER;
for (int j = 0; j < board.length; j++) {
Pawn pawn = null;
Pawn.PawnColor color = i < board.length / 2 ?
Pawn.PawnColor.RED :
Pawn.PawnColor.BLACK;
if (i == 0 || i == board.length - 1) {
pawn = new Pusher(color, i, j);
} else if (i == 1 || i == board.length - 2) {
pawn = new Pushed(color, i, j);
}
for(int i = 0 ;i<board.length;i++){
board[6][i] = Pawn.B_PUSHED;
board[i][j] = pawn;
}
for(int i = 0 ;i<board.length;i++){
board[1][i] = Pawn.R_PUSHED;
}
for(int i = 0 ;i<board.length;i++){
board[0][i] = Pawn.R_PUSHER;
}
}
@ -63,11 +64,11 @@ public class PusherBoard {
private boolean isValid(int from_col, int from_row, int to_col, int to_row) {
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;
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];
@ -94,19 +95,19 @@ public class PusherBoard {
//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;
return board[from_row + 1][from_col + 1] == Pawn.B_PUSHER;
} else if (from_col - 1 == to_col) {
if(board[from_row-1][from_col+1] != Pawn.B_PUSHER) return false;
return board[from_row - 1][from_col + 1] == Pawn.B_PUSHER;
} else if (from_col == to_col) {
if(board[from_row+1][from_col] != Pawn.B_PUSHER) return false;
return board[from_row + 1][from_col] == Pawn.B_PUSHER;
}
} 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;
return board[from_row + 1][from_col - 1] == Pawn.R_PUSHER;
} else if (from_col - 1 == to_col) {
if(board[from_row-1][from_col-1] != Pawn.R_PUSHER) return false;
return board[from_row - 1][from_col - 1] == Pawn.R_PUSHER;
} else if (from_col == to_col) {
if(board[from_row-1][from_col] != Pawn.R_PUSHER) return false;
return board[from_row - 1][from_col] == Pawn.R_PUSHER;
}
}
@ -118,7 +119,6 @@ public class PusherBoard {
}
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) {