Board Setup, All pieces and their moves are in
parent
62ae9df943
commit
c66a2664d0
|
@ -0,0 +1,15 @@
|
|||
package chess;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Bishop extends ChessPiece {
|
||||
|
||||
public Bishop(Board b, boolean isWhite, int x, int y) {
|
||||
super(b,isWhite,x,y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Move> getMoves() {
|
||||
return new ArrayList<Move>(super.getDiagonalMoves(b.size()));
|
||||
}
|
||||
}
|
|
@ -1,4 +1,48 @@
|
|||
package chess;
|
||||
|
||||
public class Board {
|
||||
|
||||
private final ChessPiece[][] board = new ChessPiece[8][8];
|
||||
|
||||
/**
|
||||
* create a basic chess board in default configuration
|
||||
*/
|
||||
public Board(){
|
||||
for (int i = 0; i < size(); i++) {
|
||||
board[i][1] = new Pawn(this, true, i, 1);
|
||||
board[i][size() - 2] = new Pawn(this, false, i, size() - 2);
|
||||
}
|
||||
// white
|
||||
board[0][0] = new Rook(this, true,0, 0);
|
||||
board[size()-1][0] = new Rook(this, true, size() - 1, 0);
|
||||
board[1][0] = new Knight(this, true, 1, 0);
|
||||
board[size()-2][0] = new Knight(this, true, size() - 2, 0);
|
||||
board[2][0] = new Bishop(this, true, 2, 0);
|
||||
board[size()-3][0] = new Bishop(this, true, size() - 3, 0);
|
||||
board[3][0] = new Queen(this, true, 3, 0);
|
||||
board[size()-4][0] = new King(this, true, size() - 4, 0);
|
||||
|
||||
// black
|
||||
board[0][0] = new Rook(this, false,0, 0);
|
||||
board[size()-1][0] = new Rook(this, false, size() - 1, 0);
|
||||
board[1][0] = new Knight(this, false, 1, 0);
|
||||
board[size()-2][0] = new Knight(this, false, size() - 2, 0);
|
||||
board[2][0] = new Bishop(this, false, 2, 0);
|
||||
board[size()-3][0] = new Bishop(this, false, size() - 3, 0);
|
||||
board[3][0] = new Queen(this, false, 3, 0);
|
||||
board[size()-4][0] = new King(this, false, size() - 4, 0);
|
||||
}
|
||||
|
||||
public ChessPiece get(int x, int y){
|
||||
if (x < 0 || x > board.length)
|
||||
return null;
|
||||
if (y < 0 || y > board.length)
|
||||
return null;
|
||||
return board[x][y];
|
||||
}
|
||||
|
||||
public int size(){
|
||||
return board.length;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +1,52 @@
|
|||
package chess;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public abstract class ChessPiece {
|
||||
|
||||
public abstract void move();
|
||||
protected Board b;
|
||||
protected int x, y;
|
||||
protected boolean isInDanger, isWhite;
|
||||
|
||||
public ChessPiece(Board b, boolean isWhite, int x, int y) {
|
||||
this.b = b;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.isWhite = isWhite;
|
||||
}
|
||||
|
||||
public void setInDanger(boolean isInDanger){
|
||||
this.isInDanger = isInDanger;
|
||||
}
|
||||
|
||||
public boolean isWhite(){
|
||||
return isWhite;
|
||||
}
|
||||
|
||||
public abstract ArrayList<Move> getMoves();
|
||||
|
||||
protected ArrayList<Move> getCardinalMoves(int length){
|
||||
ArrayList<Move> moves = new ArrayList<Move>();
|
||||
for (int i = 0; i < length; i++){
|
||||
// cardinals
|
||||
moves.add(new Move(x - i, 0));
|
||||
moves.add(new Move(x + i, 0));
|
||||
moves.add(new Move(0, y - i));
|
||||
moves.add(new Move(0, y + i));
|
||||
}
|
||||
return moves;
|
||||
}
|
||||
|
||||
protected ArrayList<Move> getDiagonalMoves(int length){
|
||||
ArrayList<Move> moves = new ArrayList<Move>();
|
||||
for (int i = 0; i < length; i++){
|
||||
// cardinals
|
||||
moves.add(new Move(x - i, y - i));
|
||||
moves.add(new Move(x + i, y + i));
|
||||
moves.add(new Move(x + i, y - i));
|
||||
moves.add(new Move(x - i, y + i));
|
||||
}
|
||||
return moves;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package chess;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class King extends ChessPiece {
|
||||
|
||||
public King(Board b, boolean isWhite, int x, int y) {
|
||||
super(b,isWhite,x,y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Move> getMoves() {
|
||||
ArrayList<Move> moves = new ArrayList<Move>();
|
||||
moves.addAll(super.getCardinalMoves(1));
|
||||
moves.addAll(super.getDiagonalMoves(1));
|
||||
return moves;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package chess;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Knight extends ChessPiece {
|
||||
|
||||
public Knight(Board b, boolean isWhite, int x, int y) {
|
||||
super(b,isWhite,x,y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Move> getMoves() {
|
||||
ArrayList<Move> moves = new ArrayList<Move>();
|
||||
moves.add(new Move(x + 2, y + 1));
|
||||
moves.add(new Move(x + 2, y - 1));
|
||||
moves.add(new Move(x - 2, y + 1));
|
||||
moves.add(new Move(x - 2, y - 1));
|
||||
moves.add(new Move(x - 1, y - 2));
|
||||
moves.add(new Move(x + 1, y - 2));
|
||||
moves.add(new Move(x - 1, y + 2));
|
||||
moves.add(new Move(x + 1, y + 2));
|
||||
return moves;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package chess;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Move {
|
||||
|
||||
private final int x,y;
|
||||
private final boolean enPassant;
|
||||
|
||||
public Move(int x, int y){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
enPassant = false;
|
||||
}
|
||||
public Move(int x, int y, boolean enPassant){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.enPassant = enPassant;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public boolean isEnPassant(){
|
||||
return enPassant;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package chess;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Pawn extends ChessPiece {
|
||||
|
||||
private final boolean isFirstMove = true;
|
||||
|
||||
public Pawn(Board b, boolean isWhite, int x, int y) {
|
||||
super(b,isWhite,x,y);
|
||||
}
|
||||
|
||||
public boolean isFirstMove(){
|
||||
return isFirstMove;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Move> getMoves() {
|
||||
ArrayList<Move> moves = new ArrayList<Move>();
|
||||
moves.add(new Move(x, y + 1));
|
||||
if (isFirstMove)
|
||||
moves.add(new Move(x, y + 2));
|
||||
ChessPiece neighbour = null;
|
||||
// En passant
|
||||
if ((neighbour = b.get(x-1, y)) != null && neighbour instanceof Pawn && ((Pawn) neighbour).isFirstMove())
|
||||
moves.add(new Move(x-1, 1, true));
|
||||
// En passant
|
||||
if ((neighbour = b.get(x+1, y)) != null && neighbour instanceof Pawn && ((Pawn) neighbour).isFirstMove())
|
||||
moves.add(new Move(x+1, 1, true));
|
||||
return moves;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package chess;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Queen extends ChessPiece {
|
||||
|
||||
public Queen(Board b, boolean isWhite, int x, int y) {
|
||||
super(b,isWhite,x,y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Move> getMoves() {
|
||||
ArrayList<Move> moves = new ArrayList<Move>();
|
||||
moves.addAll(super.getCardinalMoves(b.size()));
|
||||
moves.addAll(super.getDiagonalMoves(b.size()));
|
||||
return moves;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package chess;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Rook extends ChessPiece {
|
||||
|
||||
public Rook(Board b, boolean isWhite, int x, int y) {
|
||||
super(b,isWhite,x,y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Move> getMoves() {
|
||||
return new ArrayList<Move>(super.getCardinalMoves(b.size()));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue