From 456ed3f15ba0a61bbcfe66d69541b71fd87c2072 Mon Sep 17 00:00:00 2001 From: Storm-GH Date: Mon, 20 Mar 2023 13:39:19 -0400 Subject: [PATCH 1/6] board + move --- src/main/java/laboratoire4/GameTree.java | 6 ++ src/main/java/laboratoire4/MiniMax.java | 1 + src/main/java/laboratoire4/Pawn.java | 8 ++ src/main/java/laboratoire4/PusherBoard.java | 87 +++++++++++++++++++++ src/main/java/laboratoire4/Test.java | 14 ++++ 5 files changed, 116 insertions(+) create mode 100644 src/main/java/laboratoire4/Pawn.java create mode 100644 src/main/java/laboratoire4/PusherBoard.java create mode 100644 src/main/java/laboratoire4/Test.java diff --git a/src/main/java/laboratoire4/GameTree.java b/src/main/java/laboratoire4/GameTree.java index 0502068..68e88b2 100644 --- a/src/main/java/laboratoire4/GameTree.java +++ b/src/main/java/laboratoire4/GameTree.java @@ -5,6 +5,11 @@ import java.util.Collection; public class GameTree { private Node root; + + public Node buildTree(){ + return null; + } + public Node getRoot() { return root; } @@ -20,4 +25,5 @@ public class GameTree { return childs; } } + } diff --git a/src/main/java/laboratoire4/MiniMax.java b/src/main/java/laboratoire4/MiniMax.java index 91c7028..e3cab45 100644 --- a/src/main/java/laboratoire4/MiniMax.java +++ b/src/main/java/laboratoire4/MiniMax.java @@ -45,5 +45,6 @@ public class MiniMax { private static int evaluate(GameTree.Node node) { + return 0; } } diff --git a/src/main/java/laboratoire4/Pawn.java b/src/main/java/laboratoire4/Pawn.java new file mode 100644 index 0000000..10bb29e --- /dev/null +++ b/src/main/java/laboratoire4/Pawn.java @@ -0,0 +1,8 @@ +package laboratoire4; + +public enum Pawn { + R_PUSHER, + R_PUSHED, + B_PUSHER, + B_PUSHED +} diff --git a/src/main/java/laboratoire4/PusherBoard.java b/src/main/java/laboratoire4/PusherBoard.java new file mode 100644 index 0000000..369dfd6 --- /dev/null +++ b/src/main/java/laboratoire4/PusherBoard.java @@ -0,0 +1,87 @@ +package laboratoire4; + +public class PusherBoard { + + private Pawn[][] board; + + public PusherBoard() { + this.newGame(); + } + + public void newGame(){ + this.board = new Pawn[8][8]; + + for(int i = 0 ;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("----------------------------------------------------------------------------------------"); + } + } +} diff --git a/src/main/java/laboratoire4/Test.java b/src/main/java/laboratoire4/Test.java new file mode 100644 index 0000000..9bf5a91 --- /dev/null +++ b/src/main/java/laboratoire4/Test.java @@ -0,0 +1,14 @@ +package laboratoire4; + +public class Test { + public static void main(String[] args) { + PusherBoard pusherBoard = new PusherBoard(); + //Move examples + pusherBoard.move("D2","D3"); + pusherBoard.move("C2","C3"); + pusherBoard.move("D1","D2"); + pusherBoard.move("D2","E3"); + + pusherBoard.printBoard(); + } +} From 550fc9835fb67d76952c7e62d1516d7d2e43d432 Mon Sep 17 00:00:00 2001 From: Storm-GH Date: Mon, 20 Mar 2023 15:56:34 -0400 Subject: [PATCH 2/6] isValid TODO done --- src/main/java/laboratoire4/PusherBoard.java | 79 +++++++++++++++---- src/main/java/laboratoire4/Test.java | 13 +++ .../java/laboratoire4/TestPusherBoard.java | 29 +++++++ 3 files changed, 105 insertions(+), 16 deletions(-) create mode 100644 src/main/java/laboratoire4/TestPusherBoard.java diff --git a/src/main/java/laboratoire4/PusherBoard.java b/src/main/java/laboratoire4/PusherBoard.java index 369dfd6..043ed28 100644 --- a/src/main/java/laboratoire4/PusherBoard.java +++ b/src/main/java/laboratoire4/PusherBoard.java @@ -37,32 +37,79 @@ public class PusherBoard { public int move (String from, String to){ //FORMAT ex : from {D2}, to {D3} - int from_i = (int) from.charAt(0) - 65; - int from_j = Integer.parseInt(String.valueOf(from.charAt(1)))-1; - int to_i = (int) to.charAt(0) - 65; - int to_j = Integer.parseInt(String.valueOf(to.charAt(1)))-1; + 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; - return move(from_i, from_j, to_i, to_j); + return move(from_col, from_row, to_col, to_row); } - public int move(int from_i, int from_j, int to_i, int to_j){ - //FORMAT ex : from_i {3}, from_j {2}, to_i {3}, to_j {3} + 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_i+""+from_j + "-"+ to_i+""+to_j); - if(isValid(from_i,from_j,to_i,to_j)){ - Pawn pawn = this.getBoard()[from_j][from_i]; - System.out.println("Pawn (0,0) :" + this.getBoard()[1][0]); - System.out.println("Pawn to move : " + pawn); - this.getBoard()[from_j][from_i] = null; - this.getBoard()[to_j][to_i] = pawn; + 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; } return -1; } - private boolean isValid(int from_i, int from_j, int to_i, int to_j){ - //TODO + 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; + + //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 Date: Mon, 20 Mar 2023 16:08:46 -0400 Subject: [PATCH 3/6] validation --- src/main/java/laboratoire4/PusherBoard.java | 4 ++-- src/main/java/laboratoire4/Test.java | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/laboratoire4/PusherBoard.java b/src/main/java/laboratoire4/PusherBoard.java index 043ed28..d6d4319 100644 --- a/src/main/java/laboratoire4/PusherBoard.java +++ b/src/main/java/laboratoire4/PusherBoard.java @@ -48,8 +48,8 @@ public class PusherBoard { 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)); + //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]; diff --git a/src/main/java/laboratoire4/Test.java b/src/main/java/laboratoire4/Test.java index d98c54a..f560894 100644 --- a/src/main/java/laboratoire4/Test.java +++ b/src/main/java/laboratoire4/Test.java @@ -3,6 +3,11 @@ 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"); @@ -20,8 +25,5 @@ public class Test { pusherBoard.move("A8","A7"); pusherBoard.move("D3","D4"); pusherBoard.move("C3","D4"); - - - pusherBoard.printBoard(); } } From ec7f3568e502f2dfefc8a0dcbedd8f53757e48e1 Mon Sep 17 00:00:00 2001 From: FyloZ Date: Mon, 20 Mar 2023 17:41:13 -0400 Subject: [PATCH 4/6] Movement logic --- .idea/uiDesigner.xml | 124 ++++++++++++++++++++ .idea/workspace.xml | 58 ++++++--- src/main/java/laboratoire4/GameTree.java | 3 +- src/main/java/laboratoire4/Pawn.java | 58 ++++++++- src/main/java/laboratoire4/Pushed.java | 26 ++++ src/main/java/laboratoire4/Pusher.java | 14 +++ src/main/java/laboratoire4/PusherBoard.java | 108 ++++++++--------- 7 files changed, 312 insertions(+), 79 deletions(-) create mode 100644 .idea/uiDesigner.xml create mode 100644 src/main/java/laboratoire4/Pushed.java create mode 100644 src/main/java/laboratoire4/Pusher.java diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 58f9c6e..4910537 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,12 +4,13 @@ + + + @@ -44,19 +52,19 @@