Compare commits

..

No commits in common. "86e3b5acc2f655a590583d25a1766d694727acd7" and "456ed3f15ba0a61bbcfe66d69541b71fd87c2072" have entirely different histories.

3 changed files with 17 additions and 108 deletions

View File

@ -37,79 +37,32 @@ public class PusherBoard {
public int 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;
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;
return move(from_col, from_row, to_col, to_row);
return move(from_i, from_j, to_i, to_j);
}
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}
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}
//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;
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;
}
return -1;
}
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;
}
}
private boolean isValid(int from_i, int from_j, int to_i, int to_j){
//TODO
return true;
}

View File

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

View File

@ -1,29 +0,0 @@
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"));
}
}