LOG320_Lab4/src/main/java/laboratoire4/PusherBoard.java
2023-03-20 16:08:46 -04:00

135 lines
4.3 KiB
Java

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<board.length;i++){
board[7][i] = Pawn.B_PUSHER;
}
for(int i = 0 ;i<board.length;i++){
board[6][i] = Pawn.B_PUSHED;
}
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;
}
}
public int move (String move){
//FORMAT ex : D2-D3
String[] split = move.split("-");
return move(split[0],split[1]);
}
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;
return move(from_col, from_row, to_col, to_row);
}
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));
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_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;
}
public Pawn[][] getBoard() {
return board;
}
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){
System.out.print(this.board[i][j] + " | ");
}else{
System.out.print(" | ");
}
}
System.out.println();
System.out.println("----------------------------------------------------------------------------------------");
}
}
}