Compare commits

...

2 Commits

Author SHA1 Message Date
Storm-GH 86e3b5acc2 validation 2023-03-20 16:08:46 -04:00
Storm-GH 550fc9835f isValid TODO done 2023-03-20 15:56:34 -04:00
3 changed files with 108 additions and 17 deletions

View File

@ -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<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;
}

View File

@ -3,12 +3,27 @@ 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.printBoard();
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"));
}
}