added classes for each piece, with possible moves. Also updated board initialization to include all pieces.

main
JoffreTS 2 years ago
parent b5dc5a4228
commit 17e81feac0

@ -0,0 +1,18 @@
import java.util.ArrayList;
public class Bishop extends Piece {
public Bishop(boolean black) {
super(black);
}
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
for(int i = 1; i <= 7; i++) {
possibleMoves.add(new BoardCoordinate(position.x - i, position.y - i));
possibleMoves.add(new BoardCoordinate(position.x - i, position.y + i));
possibleMoves.add(new BoardCoordinate(position.x + i, position.y - i));
possibleMoves.add(new BoardCoordinate(position.x + i, position.y + i));
}
return possibleMoves;
}
}

@ -40,10 +40,28 @@ public class Board {
board = new Piece[BOARD_SIZE][BOARD_SIZE];
// Initialize pieces
set(1, 0, new Knight(true));
set(6, 0, new Knight(true));
set(1, 7, new Knight(false));
set(6, 7, new Knight(false));
for(int i = 0; i <= 7; i++){
if(i == 0 || i == 7){
for(int j = 0; j <= 7; j++){
if(j == 0 || j == 7){
set(j, i, new Rook(i==0));
} else if(j == 1 || j == 6){
set(j, i, new Knight(i==0));
} else if(j == 2 || j == 5){
set(j, i, new Bishop(i==0));
} else if(j == 4){
set(j, i, new Queen(i==0));
} else {
set(j, i, new King(i==0));
}
}
}
if(i == 1 || i == 6){
for(int j = 0; j <= 7; j++){
set(j, i, new Pawn(i==1));
}
}
}
}
public boolean outOfBounds(int x, int y) {

@ -0,0 +1,20 @@
import java.util.ArrayList;
public class King extends Piece {
public King(boolean black) {
super(black);
}
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
possibleMoves.add(new BoardCoordinate(position.x - 1, position.y - 1));
possibleMoves.add(new BoardCoordinate(position.x - 1, position.y + 1));
possibleMoves.add(new BoardCoordinate(position.x - 1, position.y));
possibleMoves.add(new BoardCoordinate(position.x + 1, position.y - 1));
possibleMoves.add(new BoardCoordinate(position.x + 1, position.y + 1));
possibleMoves.add(new BoardCoordinate(position.x + 1, position.y));
possibleMoves.add(new BoardCoordinate(position.x, position.y + 1));
possibleMoves.add(new BoardCoordinate(position.x, position.y - 1));
return possibleMoves;
}
}

@ -0,0 +1,17 @@
import java.util.ArrayList;
public class Pawn extends Piece {
public Pawn(boolean black) {
super(black);
}
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
if (this.black) {
possibleMoves.add(new BoardCoordinate(position.x, position.y + 1));
} else {
possibleMoves.add(new BoardCoordinate(position.x, position.y - 1));
}
return possibleMoves;
}
}

@ -0,0 +1,22 @@
import java.util.ArrayList;
public class Queen extends Piece {
public Queen(boolean black) {
super(black);
}
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
for(int i = 1; i <= 7; i++) {
possibleMoves.add(new BoardCoordinate(position.x - i, position.y - i));
possibleMoves.add(new BoardCoordinate(position.x - i, position.y + i));
possibleMoves.add(new BoardCoordinate(position.x + i, position.y - i));
possibleMoves.add(new BoardCoordinate(position.x + i, position.y + i));
possibleMoves.add(new BoardCoordinate(position.x - i, position.y));
possibleMoves.add(new BoardCoordinate(position.x + i, position.y));
possibleMoves.add(new BoardCoordinate(position.x, position.y - i));
possibleMoves.add(new BoardCoordinate(position.x, position.y + i));
}
return possibleMoves;
}
}

@ -0,0 +1,18 @@
import java.util.ArrayList;
public class Rook extends Piece {
public Rook(boolean black) {
super(black);
}
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
for(int i = 1; i <= 7; i++) {
possibleMoves.add(new BoardCoordinate(position.x - i, position.y));
possibleMoves.add(new BoardCoordinate(position.x + i, position.y));
possibleMoves.add(new BoardCoordinate(position.x, position.y - i));
possibleMoves.add(new BoardCoordinate(position.x, position.y + i));
}
return possibleMoves;
}
}