Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Computer Chess:

A computer chess is a game of computer architecture encompassing hardware and software


capable of playing chess autonomously without human guidance. Computer chess acts as
solo entertainment (allowing players to practice and to better themselves when no
sufficiently strong human opponents are available), as aids to chess analysis, for computer
chess competitions, and as research to provide insights into human cognition.

King (Chess)
In chess, the king (, ) is the most important piece. The object of the game is to threaten
the opponent's king in such a way that escape is not possible (checkmate). If a player's king
is threatened with capture, it is said to be in check, and the player must remove the threat
of capture on the next move. If this cannot be done, the king is said to be in checkmate,
resulting in a loss for that player. Although the king is the most important piece, it is usually
the weakest piece in the game until a later phase, the endgame. Players cannot make any
move that places the king in check.
White starts with the king on the first rank to the right of the queen. Black starts with the
king directly across from the white king. With the squares labeled as in algebraic notation,
the white king starts on e1 and the black king on e8.
A king can move one square in any direction (horizontally, vertically, or diagonally) unless
the square is already occupied by a friendly piece or the move would place the king in
check. As a result, the opposing kings may never occupy adjacent squares (see opposition),
but the king can give discovered check by unmasking a bishop, rook, or queen. The king
is also involved in the special move of castling
King Algorithm:
@Override
public Collection<Square>generatePossibleMoves() {
possibleMoves.clear ();
List<Square> moves = new ArrayList<>();
int[][] offsets = {
{1, 0},
{0, 1},
{-1, 0},
{0, -1},
{1, 1},
{-1, 1},
{-1, -1},
{1, -1}
};
for (int[] o : offsets) {
Square square = super.getSquare().neighbour(o[0], o[1]);
if (square != null && (square.getPiece() == null || isOpponent(square.getPiece()))) {
moves.add(square);
}
}
possibleMoves.addAll(moves);
if (getSquare().isSelected()) {
Piece[] pieces = {
PieceType.PAWN.create(getPieceColor()),
PieceType.ROOK.create(getPieceColor()),
PieceType.BISHOP.create (getPieceColor()),
PieceType.KNIGHT.create(getPieceColor()),
PieceType.QUEEN.create(getPieceColor()),
PieceType.KING.create(getPieceColor())};
Piece oldKing = this;
getSquare().removePiece();
for (Square kingMove : moves) {
if (kingMove.isEmpty()) {
for (Piece piece : pieces) {
piece.putPieceOnSquareFirstTime(kingMove);
piece.generatePossibleMoves();
for (Square enemy : piece.getPossibleMoves()) {
if (!enemy.isEmpty() &&enemy.getPiece().isOpponent(piece)
&&enemy.getPiece().getTypeNumber() == piece.getTypeNumber()) {
enemy.setBackground(Color.BLUE);
possibleMoves.remove(kingMove);
break;
}
}
}
kingMove.removePiece();
} else if (isOpponent(kingMove.getPiece())) {
Piece oldPiece = kingMove.getPiece();
for (Piece piece : pieces) {
kingMove.removePiece();
piece.putPieceOnSquareFirstTime (kingMove);
piece.generatePossibleMoves();
for (Square square1 : piece.getPossibleMoves()) {
if (!square1.isEmpty() && square1.getPiece().isOpponent(piece) &&
square1.getPiece().getTypeNumber() == piece.getTypeNumber()) {
possibleMoves.remove(kingMove);
break;
}
}
}
kingMove.removePiece();
oldPiece.putPieceOnSquareFirstTime(kingMove);
}
}
oldKing.putPieceOnSquareFirstTime(getSquare());
}
return possibleMoves;
}
Queen (Chess):
The queen (, ) is the strongest piece in the game of chess, able to move any
number of squares vertically, horizontally or diagonally. Each player starts the game with
one queen, placed in the middle of the first rank next to the king. Because of the value of a
queen, it is sometimes used as bait to lure an opponent into a trap by a queen sacrifice.
Another tactic is to use the queen to threaten the opponent's queen, to either retreat or
to exchange the queen (losing both of them) to reduce the game to less strong pieces. The
queen is often used in conjunction with another piece, such as teamed with
a bishop or rook, where the pieces could guard each other while threatening the opponent
pieces.

Queen Algorithm
@Override
public Collection<Square>generatePossibleMoves()
{
possibleMoves.clear();
Piece[] pieces =
{
PieceType.ROOK.create(getPieceColor()),
PieceType.BISHOP.create(getPieceColor())
};
for (Piece piece : pieces) {
piece.setSquare(getSquare());
possibleMoves.addAll(piece.generatePossibleMoves());
}
return possibleMoves;
}
Rook (Chess):
A rook ( borrowed from Persian , rokh) is a piece in the strategy board
game of chess. Formerly the piece was called the tower, marquess, rector,
and comes (Sunnucks 1970). The term castle is considered informal, incorrect, or old-
fashioned.[1][2] However, in Persian the word for "castling" is qal'eh raften (from qal'eh,
"castle") and not rokh raftan. Each player starts the game with two rooks, one in each of
the corner squares on their own side of the board.

In algebraic notation, the white rooks start on squares a1 and h1, while the black rooks start
on a8 and h8. The rook moves horizontally or vertically, through any number of
unoccupied squares (see diagram). As with captures by other pieces, the rook captures by
occupying the square on which the enemy piece sits. The rook also participates, with
the king, in a special move called castling.

Rook Algorithm
@Override
public Collection<Square>generatePossibleMoves() {
int row = super.getSquare().ROW;
int column = super.getSquare().COLUMN;
possibleMoves.clear();
//all possible moves in the up
for (inti = row + 1; i<Board.SIZE; i++) {
Square square = super.getSquare().getBoardSquare(i, column);
if (square.getPiece() == null) {
possibleMoves.add(square);
} else if (isOpponent(square.getPiece())) {
possibleMoves.add(square);
break;
} else {
break;
}
}
//all possible moves in the down
for (inti = row - 1; i> -1; i--) {
Square square = super.getSquare().getBoardSquare(i, column);
if (square.getPiece() == null) {
possibleMoves.add(square);
} else if (isOpponent(square.getPiece())) {
possibleMoves.add(square);
break;
} else {
break;
}
}
//all possible moves to the right
for (inti = column + 1; i<Board.SIZE; i++) {
Square square = super.getSquare().getBoardSquare(row, i);
if (square.getPiece() == null) {
possibleMoves.add(square);
} else if (isOpponent(square.getPiece())) {
possibleMoves.add(square);
break;
} else {
break;
}
}
//all possible moves to the left
for (inti = column - 1; i> -1; i--) {
Square square = super.getSquare().getBoardSquare(row, i);
if (square.getPiece() == null) {
possibleMoves.add(square);
} else if (isOpponent(square.getPiece())) {
possibleMoves.add(square);
break;
} else {
break;
}
}
return possibleMoves;
}

Bishop (Chess):
A bishop (,) is a piece in the board game of chess. Each player begins the game with
two bishops. One starts between the king's knight and the king, the other between the
queen's knight and the queen. In algebraic notation the starting squares are c1 and f1 for
White's bishops, and c8 and f8 for Black's bishops.

The bishop has no restrictions in distance for each move, but is limited
to diagonal movement. Bishops, like all other pieces except the knight, cannot jump over
other pieces. A bishop captures by occupying the square on which an enemy piece sits.
The bishops may be differentiated according to which wing they begin on, i.e. the king's
bishop and queen's bishop. As a consequence of its diagonal movement, each bishop
always remains on either the white or black squares, and so it is also common to refer to
them as light-squared or dark-squared bishops.
Bishop Algorithm:
@Override
public Collection<Square>generatePossibleMoves() {
int row = super.getSquare().ROW;
int column = super.getSquare().COLUMN;
possibleMoves.clear();
//all possible moves in the down positive diagonal
for (int j = column + 1, i = row + 1; j <Board.SIZE&&i<Board.SIZE; j++, i++) {
Square square = super.getSquare().getBoardSquare(i, j);
if (square.getPiece() == null) {
possibleMoves.add(square);
} else if (isOpponent(square.getPiece())) {
possibleMoves.add(square);
break;
} else {
break;
}
}
//all possible moves in the up positive diagonal
for (int j = column - 1, i = row + 1; j > -1 &&i<Board.SIZE; j--, i++) {
Square square = super.getSquare().getBoardSquare(i, j);
if (square.getPiece() == null) {
possibleMoves.add(square);
} else if (isOpponent(square.getPiece())) {
possibleMoves.add(square);
break;
} else {
break;
}
}
//all possible moves in the up negative diagonal
for (int j = column - 1, i = row - 1; j > -1 &&i> -1; j--, i--) {
Square square = super.getSquare().getBoardSquare(i, j);
if (square.getPiece() == null) {
possibleMoves.add(square);
} else if (isOpponent(square.getPiece())) {
possibleMoves.add(square);
break;
} else {
break;
}
}
//all possible moves in the down negative diagonal
for (int j = column + 1, i = row - 1; j <Board.SIZE&&i> -1; j++, i--) {
Square square = super.getSquare().getBoardSquare(i, j);
if (square.getPiece() == null) {
possibleMoves.add(square);
} else if (isOpponent(square.getPiece())) {
possibleMoves.add(square);
break;
} else {
break;
}
}
return possibleMoves;
}
Knight (Chess):
The knight ( ) is a piece in the game of chess, representing a knight (armored
cavalry). It is normally represented by a horse's head and neck. Each player starts with two
knights, which begin on the row closest to the player, one square from each corner.

The knight move is unusual among chess pieces. When it moves, it can move to a square
that is two squares horizontally and one square vertically, or two squares vertically and one
square horizontally. The complete move therefore looks like the letter L. Unlike all other
standard chess pieces, the knight can 'jump over' all other pieces (of either color) to its
destination square.[1] It captures an enemy piece by replacing it on its square. The knight's
ability to "jump over" other pieces means it tends to be at its most powerful in closed
positions, in contrast to that of a bishop. The move is one of the longest-surviving moves
in chess, having remained unchanged since before the seventh century. Because of this it
also appears in most chess-related regional games. The knight moves alternately to light
and dark squares.
Knight Algorithm
@Override
public Collection<Square>generatePossibleMoves() {
possibleMoves.clear();
int[][] offsets = {
{-2, 1},
{-1, 2},
{1, 2},
{2, 1},
{2, -1},
{1, -2},
{-1, -2},
{-2, -1}
};
for (int[] o : offsets) {
Square square = super.getSquare().neighbour(o[0], o[1]);
if (square != null && (square.getPiece() == null || isOpponent(square.getPiece()))) {
possibleMoves.add(square);
}
}
return possibleMoves;
}
Pawn (Chess)
The pawn () is the most numerous piece in the game of chess, and in most
circumstances, also the weakest. It historically represents infantry, or more particularly,
armed peasants or pikemen.[1] Each player begins a game of chess with eight pawns, one
on each square of the rank immediately in front of the other pieces. (In algebraic notation,
the white pawns start on a2, b2, c2, ..., h2, while black pawns start on a7, b7, c7, ..., h7.)
Individual pawns are referred to by the file on which they stand. For example, one speaks
of "White's f-pawn" or "Black's b-pawn", or less commonly (using descriptive notation),
"White's king bishop pawn" or "Black's queen knight pawn". It is also common to refer to
a rook pawn, meaning any pawn on the a- or h-file, a knight pawn (on the b- or g-file),
a bishop pawn (on the c- or f-file), a queen pawn (on the d-file), a king pawn (on the e-
file), and a central pawn (on either the d- or e-file).
Pawn Algorithm:

public Collection<Square>generatePossibleMoves()
{
possibleMoves.clear();
boolean color = super.isWhite();
int dx = color ? -1 : 1;
Square ahead = super.getSquare().neighbour(dx, 0);
if (ahead.getPiece() == null)
{
possibleMoves.add(ahead);
if (super.getSquare().ROW == 6 && color)
{
Square aheadsecond = super.getSquare().neighbour(dx - 1, 0);
if (aheadsecond.getPiece() == null)
{
possibleMoves.add(aheadsecond);
}
}
else if (super.getSquare().ROW == 1 && !color)
{
Square aheadsecond = super.getSquare().neighbour(dx + 1, 0);
if (aheadsecond.getPiece() == null)
{
possibleMoves.add(aheadsecond);
}
}
}
Square aheadLeft = super.getSquare().neighbour(dx, -1);
if (aheadLeft != null &&aheadLeft.getPiece() != null
&&isOpponent(aheadLeft.getPiece()))
{
possibleMoves.add(aheadLeft);
}
Square aheadRight = super.getSquare().neighbour(dx, 1);
if (aheadRight != null &&aheadRight.getPiece() != null
&&isOpponent(aheadRight.getPiece()))
{
possibleMoves.add(aheadRight);
}
return possibleMoves;
}

You might also like