Package Piece
Class Piece
java.lang.Object
Piece.Piece
Represents an abstract chess piece, providing shared functionality for all specific piece types.
This class defines common fields and methods used by all chess pieces, such as
position, sprite rendering, movement status, and access to the associated Board
.
Subclasses are expected to implement their own movement validation logic via
isValidMove(int, int, Board)
.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionint
The column and row position of the piece on the board.protected PieceColor
The color of the piece (white or black).The name of the piece (e.g., "Bishop", "Knight").int
The column and row position of the piece on the board.int
The width and height of a single piece graphic within the sprite sheet.int
The on-screen pixel coordinates used for rendering.int
The on-screen pixel coordinates used for rendering. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetColor()
Returns the color of the piece (white or black).abstract boolean
isValidMove
(int toCol, int toRow, Board board) Determines whether a move to the specified destination is valid for this piece.void
paint
(Graphics2D g2) Draws the piece sprite on the screen at its current pixel position.boolean
Returns whether the piece has previously moved.void
Sets the madeMove flag to true, indicating the piece has moved at least once.
-
Field Details
-
col
public int colThe column and row position of the piece on the board. -
row
public int rowThe column and row position of the piece on the board. -
xpos
public int xposThe on-screen pixel coordinates used for rendering. -
ypos
public int yposThe on-screen pixel coordinates used for rendering. -
color
The color of the piece (white or black). -
name
The name of the piece (e.g., "Bishop", "Knight"). -
sTScale
public int sTScaleThe width and height of a single piece graphic within the sprite sheet.
-
-
Constructor Details
-
Piece
Constructs a chess piece and associates it with the specified board.- Parameters:
board
- the chess board this piece belongs to
-
-
Method Details
-
paint
Draws the piece sprite on the screen at its current pixel position.- Parameters:
g2
- the graphics context used to render the piece
-
getColor
Returns the color of the piece (white or black).- Returns:
- the color of this piece
-
switchMadeMove
public void switchMadeMove()Sets the madeMove flag to true, indicating the piece has moved at least once. -
readMadeMove
public boolean readMadeMove()Returns whether the piece has previously moved.- Returns:
true
if the piece has moved;false
otherwise
-
isValidMove
Determines whether a move to the specified destination is valid for this piece.This method must be implemented by subclasses such as
Bishop
,Rook
, etc., and defines thePiece
subclass movement logic.- Parameters:
toCol
- the destination columntoRow
- the destination rowboard
- the board on which the move is evaluated- Returns:
true
if the move is valid;false
otherwise
-