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