Added Player

This commit is contained in:
Filip Znachor 2023-04-17 19:49:53 +02:00
parent c5a3eca894
commit fd4e5387aa
13 changed files with 170 additions and 57 deletions

View file

@ -37,19 +37,22 @@ public abstract class AbstractPiece implements IPiece {
*/
protected PieceColor color;
protected Player player;
public int moveCount = 0;
/**
* Create new piece
* @param chessboard chessboard
* @param player player
* @param x piece's X location
* @param y piece's Y location
* @param color piece's color
*/
public AbstractPiece(Chessboard chessboard, int x, int y, PieceColor color) {
public AbstractPiece(Player player, int x, int y, PieceColor color) {
this.x = x;
this.y = y;
this.chessboard = chessboard;
this.player = player;
this.chessboard = player.getChessboard();
this.color = color;
chessboard.addPiece(this, x, y);
}
@ -201,10 +204,14 @@ public abstract class AbstractPiece implements IPiece {
if(x == this.x && y == this.y) return;
if(x < 0 || x >= moves.length || y < 0 || y >= moves.length) return;
IPiece piece = chessboard.getPiece(new PiecePosition(x, y));
if(piece != null && color == piece.getColor()) return;
if(piece != null && player == piece.getPlayer()) return;
moves[y][x] = true;
}
public boolean isEndangered() {
return chessboard.isEndangered(color, x, y);
}
public boolean move(PiecePosition pos) {
boolean[][] moves = getPossibleMoves();
if(moves[pos.y][pos.x]) {
@ -231,4 +238,8 @@ public abstract class AbstractPiece implements IPiece {
}
}
public Player getPlayer() {
return player;
}
}

View file

@ -11,13 +11,13 @@ public class Bishop extends AbstractPiece {
/**
* Create new Bishop piece
* @param chessboard chessboard
* @param player player
* @param x piece's X location
* @param y piece's Y location
* @param color piece's color
*/
public Bishop(Chessboard chessboard, int x, int y, PieceColor color) {
super(chessboard, x, y, color);
public Bishop(Player player, int x, int y, PieceColor color) {
super(player, x, y, color);
}
/**

View file

@ -20,21 +20,9 @@ public class Chess {
Chessboard chessboard = new Chessboard();
PieceColor[] colors = new PieceColor[]{PieceColor.WHITE, PieceColor.BLACK};
for (int i = 0; i < colors.length; i++) {
int first = i == 0 ? 7 : 0;
int second = i == 0 ? 6 : 1;
new Rook(chessboard, 0, first, colors[i]);
new Knight(chessboard, 1, first, colors[i]);
new Rook(chessboard, 7, first, colors[i]);
new Knight(chessboard, 6, first, colors[i]);
new Queen(chessboard, 3, first, colors[i]);
new Bishop(chessboard, 5, first, colors[i]);
new Bishop(chessboard, 2, first, colors[i]);
new King(chessboard, 4, first, colors[i]);
for (int j = 0; j < 8; j++) {
new Pawn(chessboard, j, second, colors[i]);
}
}
StartPosition[] startPositions = new StartPosition[]{StartPosition.BOTTOM, StartPosition.TOP};
chessboard.setPlayer1(new Player(chessboard, startPositions[0], colors[0]));
chessboard.setPlayer2(new Player(chessboard, startPositions[1], colors[1]));
okno.add(chessboard);
okno.pack();

View file

@ -56,6 +56,12 @@ public class Chessboard extends JPanel {
private boolean[][] lastMove;
private Player player1;
private Player player2;
private Player activePlayer;
/**
* Constructor of the chessboard
*/
@ -74,6 +80,7 @@ public class Chessboard extends JPanel {
* @param y piese's Y location
*/
public void addPiece(IPiece piece, int x, int y) {
if(!isOnBoard(x, y)) return;
pieces[y][x] = piece;
}
@ -84,6 +91,7 @@ public class Chessboard extends JPanel {
* @return removed piece
*/
public IPiece removePiece(int x, int y) {
if(!isOnBoard(x, y)) return null;
IPiece piece = pieces[y][x];
pieces[y][x] = null;
return piece;
@ -114,7 +122,7 @@ public class Chessboard extends JPanel {
AffineTransform beforeSquares = g2.getTransform();
for(int i=0; i<SQUARE_COUNT; i++) {
isBlack = !isBlack;
if(SQUARE_COUNT % 2 == 0) isBlack = !isBlack;
for(int j=0; j<SQUARE_COUNT; j++) {
g2.setTransform(beforeSquares);
if(isBlack) g2.setColor(Color.LIGHT_GRAY);
@ -125,11 +133,15 @@ public class Chessboard extends JPanel {
}
}
isBlack = true;
g2.setStroke(new BasicStroke(10));
for(int i=0; i<SQUARE_COUNT; i++) {
isBlack = !isBlack;
for(int j=0; j<SQUARE_COUNT; j++) {
if(lastMove != null && lastMove[j][i]) {
g2.setTransform(beforeSquares);
g2.translate(i*SQUARE_SIZE, j*SQUARE_SIZE);
g2.setColor(new Color(50, 50, 250, 25));
g2.fillRect(0, 0, SQUARE_SIZE, SQUARE_SIZE);
}
if(possibleMoves != null && possibleMoves[j][i]) {
g2.setTransform(beforeSquares);
g2.translate(i*SQUARE_SIZE, j*SQUARE_SIZE);
@ -138,12 +150,6 @@ public class Chessboard extends JPanel {
g2.setColor(new Color(255, 50, 50));
g2.drawRect(0, 0, SQUARE_SIZE, SQUARE_SIZE);
}
if(lastMove != null && lastMove[j][i]) {
g2.setTransform(beforeSquares);
g2.translate(i*SQUARE_SIZE, j*SQUARE_SIZE);
g2.setColor(new Color(0, 0, 200, 25));
g2.fillRect(0, 0, SQUARE_SIZE, SQUARE_SIZE);
}
}
}
@ -213,7 +219,7 @@ public class Chessboard extends JPanel {
* @return piece
*/
public IPiece getPiece(PiecePosition pos) {
if(pos.y < 0 || pos.y >= SQUARE_COUNT || pos.x < 0 || pos.x >= SQUARE_COUNT) return null;
if(!isOnBoard(pos.x, pos.y)) return null;
return pieces[pos.y][pos.x];
}
@ -223,6 +229,7 @@ public class Chessboard extends JPanel {
* @return piece
*/
public IPiece grabPiece(PiecePosition pos) {
if(!isOnBoard(pos.x, pos.y)) return null;
IPiece piece = getPiece(pos);
floating = piece;
this.repaint();
@ -250,11 +257,48 @@ public class Chessboard extends JPanel {
this.repaint();
}
void showPossibleMoves(boolean[][] moves) {
public boolean isEndangered(PieceColor color, int x, int y) {
for (IPiece[] pieces : pieces) {
for (IPiece piece : pieces) {
if(piece != null && piece.getColor() != color) {
if(piece.getPossibleMoves()[y][x] == true) return true;
}
}
}
return false;
}
public void showPossibleMoves(boolean[][] moves) {
possibleMoves = moves;
}
void showLastMove(boolean[][] move) {
public void showLastMove(boolean[][] move) {
lastMove = move;
}
public boolean isOnBoard(int x, int y) {
return !(y < 0 || y >= SQUARE_COUNT || x < 0 || x >= SQUARE_COUNT);
}
public void setPlayer1(Player player) {
player1 = player;
activePlayer = player;
}
public void setPlayer2(Player player) {
player2 = player;
}
public Player getActivePlayer() {
return activePlayer;
}
public void changeActivePlayer() {
if(activePlayer == player1) {
activePlayer = player2;
} else {
activePlayer = player1;
}
}
}

View file

@ -1,14 +1,11 @@
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
/**
* Chessboard mouse adapter class
*/
public class ChessboardMouseAdapter extends MouseAdapter {
PieceColor activeColor = PieceColor.WHITE;
/**
* Detect mouse press and select the piece
* @param me mouse event
@ -18,7 +15,7 @@ public class ChessboardMouseAdapter extends MouseAdapter {
PiecePosition pos = c.getPieceCoordinates(me.getX(), me.getY());
IPiece piece = c.getPiece(pos);
if (piece != null) {
if(piece.getColor() != activeColor) return;
if(piece.getPlayer() != c.getActivePlayer()) return;
c.grabPiece(pos);
double totalScale = c.pieceScale * c.boardScale;
piece.setOverride(me.getX() - 50 * totalScale, me.getY() - 50 * totalScale);
@ -53,8 +50,8 @@ public class ChessboardMouseAdapter extends MouseAdapter {
IPiece piece = c.getFloatingPiece();
if(piece != null) {
if(piece.move(pos)) {
List<PieceColor> colors = List.of(PieceColor.values());
activeColor = colors.get((colors.indexOf(activeColor)+1)%colors.size());
c.changeActivePlayer();
if(c.getActivePlayer().inCheck()) System.out.println("Player in check!");
}
}
c.getRootPane().repaint();

View file

@ -55,5 +55,9 @@ public interface IPiece {
boolean move(PiecePosition pos);
Player getPlayer();
boolean isEndangered();
}

View file

@ -12,13 +12,13 @@ public class King extends AbstractPiece {
/**
* Create new King piece
* @param chessboard chessboard
* @param player player
* @param x piece's X location
* @param y piece's Y location
* @param color piece's color
*/
public King(Chessboard chessboard, int x, int y, PieceColor color) {
super(chessboard, x, y, color);
public King(Player player, int x, int y, PieceColor color) {
super(player, x, y, color);
}
/**
@ -48,4 +48,5 @@ public class King extends AbstractPiece {
}
return moves;
}
}

View file

@ -7,13 +7,13 @@ public class Knight extends AbstractPiece {
/**
* Create new Knight piece
* @param chessboard chessboard
* @param player player
* @param x piece's X location
* @param y piece's Y location
* @param color piece's color
*/
public Knight(Chessboard chessboard, int x, int y, PieceColor color) {
super(chessboard, x, y, color);
public Knight(Player player, int x, int y, PieceColor color) {
super(player, x, y, color);
}
/**

View file

@ -7,13 +7,13 @@ public class Pawn extends AbstractPiece {
/**
* Create new Pawn piece
* @param chessboard chessboard
* @param player player
* @param x piece's X location
* @param y piece's Y location
* @param color piece's color
*/
public Pawn(Chessboard chessboard, int x, int y, PieceColor color) {
super(chessboard, x, y, color);
public Pawn(Player player, int x, int y, PieceColor color) {
super(player, x, y, color);
}
/**
@ -27,10 +27,11 @@ public class Pawn extends AbstractPiece {
}
/* TODO: Allow to get all possible moves if piece is in reach */
@Override
public boolean[][] getPossibleMoves() {
boolean[][] moves = new boolean[chessboard.SQUARE_COUNT][chessboard.SQUARE_COUNT];
int directionY = color == PieceColor.BLACK ? 1 : -1;
int directionY = player.getStartPosition() == StartPosition.TOP ? 1 : -1;
for(int i=-1; i<=1; i++) {
IPiece piece = chessboard.getPiece(new PiecePosition(x+i, y+directionY));
if((i != 0 && piece != null) || (i == 0 && piece == null)) setPossibleMove(moves, x+i, y+directionY);
@ -43,7 +44,7 @@ public class Pawn extends AbstractPiece {
public boolean move(PiecePosition pos) {
boolean result = super.move(pos);
int changeY = color == PieceColor.WHITE ? 0 : chessboard.SQUARE_COUNT-1;
if(y == changeY) new Queen(chessboard, x, y, color);
if(y == changeY) new Queen(player, x, y, color);
return result;
}
}

62
src/Player.java Normal file
View file

@ -0,0 +1,62 @@
public class Player {
private Chessboard chessboard;
private PieceColor color;
private StartPosition startPosition;
private King king;
private Rook leftRook;
private Rook rightRook;
public Player(Chessboard chessboard, StartPosition startPosition, PieceColor color) {
this.chessboard = chessboard;
this.color = color;
this.startPosition = startPosition;
createPieces();
}
public boolean inCheck() {
return king.isEndangered();
}
public void createPieces() {
int first = startPosition == StartPosition.BOTTOM ? 7 : 0;
int second = startPosition == StartPosition.BOTTOM ? 6 : 1;
leftRook = new Rook(this, 0, first, color);
new Knight(this, 1, first, color);
rightRook = new Rook(this, 7, first, color);
new Knight(this, 6, first, color);
new Queen(this, 3, first, color);
new Bishop(this, 5, first, color);
new Bishop(this, 2, first, color);
king = new King(this, 4, first, color);
for (int j = 0; j < 8; j++) {
new Pawn(this, j, second, color);
}
}
public Chessboard getChessboard() {
return chessboard;
}
public StartPosition getStartPosition() {
return startPosition;
}
public King getKing() {
return king;
}
public Rook getLeftRook() {
return leftRook;
}
public Rook getRightRook() {
return rightRook;
}
}

View file

@ -10,13 +10,13 @@ public class Queen extends AbstractPiece {
/**
* Create new Queen piece
* @param chessboard chessboard
* @param player player
* @param x piece's X location
* @param y piece's Y location
* @param color piece's color
*/
public Queen(Chessboard chessboard, int x, int y, PieceColor color) {
super(chessboard, x, y, color);
public Queen(Player player, int x, int y, PieceColor color) {
super(player, x, y, color);
}
/**

View file

@ -7,13 +7,13 @@ public class Rook extends AbstractPiece {
/**
* Create new Rook piece
* @param chessboard chessboard
* @param player player
* @param x piece's X location
* @param y piece's Y location
* @param color piece's color
*/
public Rook(Chessboard chessboard, int x, int y, PieceColor color) {
super(chessboard, x, y, color);
public Rook(Player player, int x, int y, PieceColor color) {
super(player, x, y, color);
}
/**

5
src/StartPosition.java Normal file
View file

@ -0,0 +1,5 @@
public enum StartPosition {
TOP, BOTTOM;
}