All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class kpalin.alabra.ChessPlay

java.lang.Object
   |
   +----java.awt.Component
           |
           +----java.awt.Container
                   |
                   +----java.awt.Panel
                           |
                           +----kpalin.alabra.ChessPlay

public class ChessPlay
extends Panel
implements MouseListener, ChessCtrlCallback
This is the key class of the whole program. This is attached on application window or an applet panel and the whole chess stuff is under this class.

Author:
Kimmo Palin

Variable Index

 o canFortify
Possibility of fortification.
 o checkBlockers
This is the data about tiles that can be moved to block a check.
 o ctrl
The panel that holds all control etc.
 o grid
The panel that holds the chess board.
 o initSet
Initial settings got with an implementation specific way.
 o pPromotion
This is an instance of pawn promotion dialog.
 o prevTarget
The previous move.
 o processClicks
Process click-events.
 o startTile
The tile where is the piece to be moved.
 o whosTurn
Whos turn is it?.

Constructor Index

 o ChessPlay(ChessSettings)
Creates new chess play object.

Method Index

 o actionPerformed(ActionEvent)
Listens for the reset button on ctrlPanel.
 o blocksThreatAt(int, BoardTile)
An assistant method to validate moves.
 o blocksThreatAt(int, int, BoardTile)
An assistant method to validate moves.
 o canBishopMove(BoardTile)
Checks if bishop on the tile can move.
 o canKingMove(BoardTile)
Checks if king on the tile can move.
 o canKnightMove(BoardTile)
Checks if knight on the tile can move.
 o canMove(BoardTile)
Checks if a piece in specific tile can move.
 o canPawnMove(BoardTile)
Checks if pawn on the tile can move.
 o canQueenMove(BoardTile)
Checks if queen on the tile can move.
 o canRookMove(BoardTile)
Checks if rook on the tile can move.
 o canSideMove(int)
Checks if specified player can move any pieces.
 o canSimpleMove(BoardTile, int, int[][])
This checks if bishop,knight,rook or queen can move.
 o changeTurn()
Takes care of the boring routines when we change turn.
 o checkForMate(int)
Checks if side is on mate.
 o finishPromotion(PromotionListen)
Finalizes the pawn promotion.
 o gameOver(int)
Sets the game to game over-state.
 o getNChecks(int, int, BoardTile)
Finds all checks.
 o isCheckPos(int, BoardTile)
Checks for checks.
 o killAll()
Tries to clean up after stoping applet.
 o knightThreat(Vector, int, int, BoardTile)
Checks for threatening knights.
 o makeMove(BoardTile, BoardTile)
Validate and make the move.
 o mouseClicked(MouseEvent)
This is the callback method for the BoardTiles.
 o mouseEntered(MouseEvent)
 o mouseExited(MouseEvent)
 o mousePressed(MouseEvent)
 o mouseReleased(MouseEvent)
 o movePiece(BoardTile, BoardTile)
Moves the piece to target without any checking of validity of the move.
 o movePiece(BoardTile, BoardTile, Piece)
Moves the piece to target without any checking of validity of the move.
 o otherThreat(Vector, int, int, BoardTile)
Checks for threatening rooks, bishops, and queen.
 o pause()
Pause the timers.
 o pawnThreat(Vector, int, int, BoardTile)
Checks for threatening pawns.
 o promotePawn(int, BoardTile, BoardTile)
Starts pawnPromotion procedure.
 o recordGameOver(int)
Adds result of the game to movelist, if game is over.
 o resetGame()
Reset the game
 o unpause()
Unpause the timers.
 o update(Observable, Object)
Listener for pawn promotion.
 o validBishopMove(BoardTile, BoardTile)
Checks the validity of a move for a Bishop.
 o validEnPassant(BoardTile, BoardTile)
Checks validity of en'passant-move
 o validFort(BoardTile, BoardTile)
Checks the validity of a fortification.
 o validKingMove(BoardTile, BoardTile)
Checks the validity of a move for a King.
 o validKnightMove(BoardTile, BoardTile)
Checks the validity of a move for a Knight.
 o validPawnMove(BoardTile, BoardTile)
Checks the validity of a move for a Pawn.
 o validQueenMove(BoardTile, BoardTile)
Checks the validity of a move for a Queen.
 o validRookMove(BoardTile, BoardTile)
Checks the validity of a move for a Rook.
 o validStraightMove(BoardTile, int, int, int)
This is a helpper method for pieces that move straight lines and diagonals.
 o whoCanBlock(BoardTile, BoardTile, int)
Finds out the pieces that can come to block a check.

Variables

 o grid
 private BoardGrid grid
The panel that holds the chess board.

 o initSet
 private final ChessSettings initSet
Initial settings got with an implementation specific way.

 o ctrl
 private CtrlPanel ctrl
The panel that holds all control etc. stuff on the screen.

 o processClicks
 private boolean processClicks
Process click-events.

 o whosTurn
 private int whosTurn
Whos turn is it?.

See Also:
WHITE, BLACK, GAMEOVER
 o pPromotion
 private PawnPromote pPromotion
This is an instance of pawn promotion dialog. Set in pawnPromote and null:ed in update or resetGame.

 o startTile
 private BoardTile startTile
The tile where is the piece to be moved. Not really good place for this stuff but can't help it. Only mouseClicked can use this!

See Also:
mouseClicked
 o prevTarget
 private BoardTile prevTarget
The previous move. This is needed for the en'passant.

 o canFortify
 private boolean canFortify[][]
Possibility of fortification. Left dimension is Piece.WHITE or Piece.Black, right Piece.LONGFORT or Piece.SHORTFORT

 o checkBlockers
 private Vector checkBlockers
This is the data about tiles that can be moved to block a check. If there is no current threat, this will be null.

Constructors

 o ChessPlay
 ChessPlay(ChessSettings set)
Creates new chess play object.

Methods

 o resetGame
 public void resetGame()
Reset the game

 o promotePawn
 void promotePawn(int side,
                  BoardTile from,
                  BoardTile to)
Starts pawnPromotion procedure. This is a bit dirty because we have to artificially block input to ChessPlay as long as this goes on. The control is returned to ChessPlay after update.

See Also:
update
 o update
 public void update(Observable o,
                    Object arg)
Listener for pawn promotion.

Parameters:
o - the observable object
arg - an argument (not used)
 o finishPromotion
 private void finishPromotion(PromotionListen plisten)
Finalizes the pawn promotion.

Parameters:
plisten - The communication object towards PawnPromotion.
 o actionPerformed
 public void actionPerformed(ActionEvent e)
Listens for the reset button on ctrlPanel.

 o mouseEntered
 public void mouseEntered(MouseEvent e)
 o mouseExited
 public void mouseExited(MouseEvent e)
 o mousePressed
 public void mousePressed(MouseEvent e)
 o mouseReleased
 public void mouseReleased(MouseEvent e)
 o mouseClicked
 public void mouseClicked(MouseEvent e)
This is the callback method for the BoardTiles. This is the best place for this because it needs connections to ctrl (change turn) and grid (information about the board status)

 o checkForMate
 private boolean checkForMate(int side)
Checks if side is on mate.

Parameters:
side - Side which can be on mate.
Returns:
true if side is on mate, false if not.
 o whoCanBlock
 private Vector whoCanBlock(BoardTile kingTile,
                            BoardTile threat,
                            int limit)
Finds out the pieces that can come to block a check.

Parameters:
kingTile - The tile where's the threatend king
threat - The tile where is the agressor. (Piece must be there)
limit - if >0 return max limit threats <=0 return all threats.
Returns:
Vector of the tiles from where pieces can come blocking. (not null)
 o changeTurn
 private void changeTurn()
Takes care of the boring routines when we change turn.

 o makeMove
 private void makeMove(BoardTile from,
                       BoardTile target)
Validate and make the move.

Parameters:
from - The tile where the move starts.
target - The target tile where to move the piece.
 o killAll
 public void killAll()
Tries to clean up after stoping applet.

 o blocksThreatAt
 private BoardTile blocksThreatAt(int side,
                                  BoardTile tile)
An assistant method to validate moves. Analyses how a piece in a specific tile blocks threats to king. The return tile is always in (1,1)-(-1,-1) box near the given tile.

Parameters:
side - The moving side.
tile - The tile whose blocking we're analyzing. No need to have piece in here.
Returns:
null if piece in tile doesn't block any threats, otherwise the tile in the direction of the threat.
 o blocksThreatAt
 private BoardTile blocksThreatAt(int side,
                                  int jumpcount,
                                  BoardTile tile)
An assistant method to validate moves. Analyses how a piece in a specific tile is possibly blocking threats to king. The return tile is always in (1,1)-(-1,-1) box near the given tile.

Parameters:
side - The moving side.
jumpcount - Number of pieces +1 that can be between king and threat.
tile - The tile whose blocking we're analyzing. No need to have piece in here.
Returns:
null if piece in tile doesn't block any threats, otherwise the tile in the direction of the threat.
 o validEnPassant
 private boolean validEnPassant(BoardTile from,
                                BoardTile target)
Checks validity of en'passant-move

Parameters:
from - Tile where the move starts. Here must be a piece.
target - Tile where player is moving a piece.
 o validPawnMove
 private boolean validPawnMove(BoardTile from,
                               BoardTile target)
Checks the validity of a move for a Pawn.

Parameters:
target - The target tile.
 o validBishopMove
 private boolean validBishopMove(BoardTile from,
                                 BoardTile target)
Checks the validity of a move for a Bishop.

Parameters:
from - Starting tile of the move.
target - The target tile.
 o knightThreat
 private void knightThreat(Vector baddies,
                           int side,
                           int count,
                           BoardTile kingPos)
Checks for threatening knights.

Parameters:
baddies - A vector where to add the threats.
side - The kings side. (Piece.WHITE or Piece.BLACK)
kingPos - The tile whose threat to analyse. (note: no need for a King to be in this tile.)
count - Maximum number of threat-tiles to return
 o pawnThreat
 private void pawnThreat(Vector baddies,
                         int side,
                         int count,
                         BoardTile kingPos)
Checks for threatening pawns.

Parameters:
baddies - A vector where to add the threats.
side - The kings side. (Piece.WHITE or Piece.BLACK)
kingPos - The tile whose threat to analyse. (note: no need for a King to be in this tile.)
count - Maximum number of threat-tiles to return
 o otherThreat
 private void otherThreat(Vector baddies,
                          int side,
                          int count,
                          BoardTile kingPos)
Checks for threatening rooks, bishops, and queen.

Parameters:
baddies - A vector where to add the threats.
side - The kings side. (Piece.WHITE or Piece.BLACK)
kingPos - The tile whose threat to analyse. (note: no need for a King to be in this tile.)
count - Maximum number of threat-tiles to baddies.
 o getNChecks
 private Vector getNChecks(int side,
                           int count,
                           BoardTile kingPos)
Finds all checks.

Parameters:
side - The kings side. (Piece.WHITE or Piece.BLACK)
kingPos - The tile whose threat to analyse. (note: no need for a King to be in this tile.)
count - How many threats to return.
Returns:
A Vector of threatening BoardTiles. Maximum size count.
 o isCheckPos
 private BoardTile isCheckPos(int side,
                              BoardTile kingPos)
Checks for checks.

Parameters:
side - The kings side. (Piece.WHITE or Piece.BLACK)
kingPos - The tile whose threat to analyse. (note: no need for a King to be in this tile.)
Returns:
The tile of the threat or null.
 o validFort
 private boolean validFort(BoardTile from,
                           BoardTile target)
Checks the validity of a fortification.

Parameters:
from - Starting position of the king.
target - The target tile.
Returns:
true if can move, false if not
 o validKingMove
 private boolean validKingMove(BoardTile from,
                               BoardTile target)
Checks the validity of a move for a King.

Parameters:
from - Starting position.
target - The target tile.
Returns:
true if can move, false if not
 o validKnightMove
 private boolean validKnightMove(BoardTile from,
                                 BoardTile target)
Checks the validity of a move for a Knight.

Parameters:
target - The target tile.
 o validQueenMove
 private boolean validQueenMove(BoardTile from,
                                BoardTile target)
Checks the validity of a move for a Queen.

Parameters:
from - Start tile of the move.
target - The target tile.
 o validRookMove
 private boolean validRookMove(BoardTile from,
                               BoardTile target)
Checks the validity of a move for a Rook.

Parameters:
target - The target tile.
 o validStraightMove
 private boolean validStraightMove(BoardTile from,
                                   int signx,
                                   int signy,
                                   int delta)
This is a helpper method for pieces that move straight lines and diagonals.

Parameters:
signx - Direction to move on x acsis.
singy - Direction to move on y acsis.
delta - Length of the move. (Measured in (signx,signy))
Returns:
true if there's no pieces on the way.
 o movePiece
 private void movePiece(BoardTile from,
                        BoardTile target)
Moves the piece to target without any checking of validity of the move. This also removes eaten piece in en passant move. The move record is updated here because this is the only place we have enough information at hand.

Parameters:
from - Starting tile of the move.
target - The target tile where the piece is to be moved.
 o movePiece
 private void movePiece(BoardTile from,
                        BoardTile target,
                        Piece promoteTo)
Moves the piece to target without any checking of validity of the move. This also removes eaten piece in en passant move. The move record is updated here because this is the only place we have enough information at hand.

Parameters:
from - Starting tile of the move.
target - The target tile where the piece is to be moved.
promoteTo - A piece, to which the moving piece is to be promoted. null if no promotion.
 o recordGameOver
 private void recordGameOver(int winner)
Adds result of the game to movelist, if game is over.

Parameters:
winner - Side of the winner.
 o canSideMove
 boolean canSideMove(int who)
Checks if specified player can move any pieces.

Parameters:
who - Id of the side who we are looking for.
Returns:
true if who can move.
 o pause
 void pause()
Pause the timers.

 o unpause
 void unpause()
Unpause the timers. This will start the white's timer if the window is iconified and deiconified before first move. (good or bad??)

 o gameOver
 private void gameOver(int winner)
Sets the game to game over-state.

Parameters:
winner - Winner of the game or Piece.DRAW
 o canMove
 private boolean canMove(BoardTile tile)
Checks if a piece in specific tile can move. Note: tile must contain a piece.

Parameters:
tile - Tile on the board, containing a piece.
 o canSimpleMove
 private boolean canSimpleMove(BoardTile tile,
                               int side,
                               int cords[][])
This checks if bishop,knight,rook or queen can move.

Parameters:
tile - Tile where the piece is.
side - Side of the moving piece.
cords - The coordinates relative to tile, to where the piece can move.
See Also:
bishopMoves, knightMoves, rookMoves
 o canKingMove
 private boolean canKingMove(BoardTile tile)
Checks if king on the tile can move.

Parameters:
tile - The tile where the knight is.
Returns:
true if can move, false if not.
 o canKnightMove
 boolean canKnightMove(BoardTile tile)
Checks if knight on the tile can move.

Parameters:
tile - The tile where the knight is.
Returns:
true if can move, false if not.
 o canRookMove
 boolean canRookMove(BoardTile tile)
Checks if rook on the tile can move.

Parameters:
tile - The tile where the rook is.
Returns:
true if can move, false if not.
 o canQueenMove
 boolean canQueenMove(BoardTile tile)
Checks if queen on the tile can move.

Parameters:
tile - The tile where the queen is.
Returns:
true if can move, false if not.
 o canPawnMove
 boolean canPawnMove(BoardTile tile)
Checks if pawn on the tile can move.

Parameters:
tile - The tile where the pawn is.
Returns:
true if can move, false if not.
 o canBishopMove
 boolean canBishopMove(BoardTile tile)
Checks if bishop on the tile can move.

Parameters:
tile - The tile where the bishop is.
Returns:
true if can move, false if not.

All Packages  Class Hierarchy  This Package  Previous  Next  Index