Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Commit

Permalink
ChessSet piece placement/removal is what most of the code is doing so…
Browse files Browse the repository at this point in the history
… no need to prefix with ChessSet. Rearrange function prototypes into alphabetical order.
  • Loading branch information
lorenzo-stoakes committed Jul 17, 2012
1 parent e8e4a2f commit db84401
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 61 deletions.
54 changes: 27 additions & 27 deletions chessset.c
@@ -1,15 +1,5 @@
#include "weak.h"

// Get the bitboard which encodes a mask of all squares threatened by the specified side in the
// chess set.
BitBoard
KingThreats(ChessSet *chessSet, Side side)
{
return PawnKingThreats(chessSet, side) | RookKingThreats(chessSet, side) |
KnightKingThreats(chessSet, side) | BishopKingThreats(chessSet, side) |
QueenKingThreats(chessSet, side) | KingKingThreats(chessSet, side);
}

bool
Checked(ChessSet *chessSet, Side side)
{
Expand All @@ -18,24 +8,14 @@ Checked(ChessSet *chessSet, Side side)
return (KingThreats(chessSet, OPPOSITE(side))&king) != EmptyBoard;
}

void
ChessSetPlacePiece(ChessSet *chessSet, Side side, Piece piece, Position pos)
{
chessSet->Occupancy |= POSBOARD(pos);
chessSet->EmptySquares = ~chessSet->Occupancy;

SetPlacePiece(&chessSet->Sets[side], piece, pos);
}

void
ChessSetRemovePiece(ChessSet *chessSet, Side side, Piece piece, Position pos)
// Get the bitboard which encodes a mask of all squares threatened by the specified side in the
// chess set.
BitBoard
KingThreats(ChessSet *chessSet, Side side)
{
BitBoard complement = ~POSBOARD(pos);

chessSet->Occupancy &= complement;
chessSet->EmptySquares = ~chessSet->Occupancy;

SetRemovePiece(&chessSet->Sets[side], piece, pos);
return PawnKingThreats(chessSet, side) | RookKingThreats(chessSet, side) |
KnightKingThreats(chessSet, side) | BishopKingThreats(chessSet, side) |
QueenKingThreats(chessSet, side) | KingKingThreats(chessSet, side);
}

ChessSet
Expand Down Expand Up @@ -63,3 +43,23 @@ NewEmptyChessSet()

return ret;
}

void
PlacePiece(ChessSet *chessSet, Side side, Piece piece, Position pos)
{
chessSet->Occupancy |= POSBOARD(pos);
chessSet->EmptySquares = ~chessSet->Occupancy;

SetPlacePiece(&chessSet->Sets[side], piece, pos);
}

void
RemovePiece(ChessSet *chessSet, Side side, Piece piece, Position pos)
{
BitBoard complement = ~POSBOARD(pos);

chessSet->Occupancy &= complement;
chessSet->EmptySquares = ~chessSet->Occupancy;

SetRemovePiece(&chessSet->Sets[side], piece, pos);
}
60 changes: 30 additions & 30 deletions game.c
Expand Up @@ -24,21 +24,21 @@ DoCastleKingSide(Game *game)
{
int offset = game->WhosTurn*8*7;

ChessSetRemovePiece(&game->ChessSet, game->WhosTurn, King, E1 + offset);
ChessSetPlacePiece(&game->ChessSet, game->WhosTurn, King, G1 + offset);
ChessSetRemovePiece(&game->ChessSet, game->WhosTurn, Rook, H1 + offset);
ChessSetPlacePiece(&game->ChessSet, game->WhosTurn, Rook, F1 + offset);
RemovePiece(&game->ChessSet, game->WhosTurn, King, E1 + offset);
PlacePiece(&game->ChessSet, game->WhosTurn, King, G1 + offset);
RemovePiece(&game->ChessSet, game->WhosTurn, Rook, H1 + offset);
PlacePiece(&game->ChessSet, game->WhosTurn, Rook, F1 + offset);
}

void
DoCastleQueenSide(Game *game)
{
int offset = game->WhosTurn*8*7;

ChessSetRemovePiece(&game->ChessSet, game->WhosTurn, King, E1 + offset);
ChessSetPlacePiece(&game->ChessSet, game->WhosTurn, King, C1 + offset);
ChessSetRemovePiece(&game->ChessSet, game->WhosTurn, Rook, A1 + offset);
ChessSetPlacePiece(&game->ChessSet, game->WhosTurn, Rook, D1 + offset);
RemovePiece(&game->ChessSet, game->WhosTurn, King, E1 + offset);
PlacePiece(&game->ChessSet, game->WhosTurn, King, C1 + offset);
RemovePiece(&game->ChessSet, game->WhosTurn, Rook, A1 + offset);
PlacePiece(&game->ChessSet, game->WhosTurn, Rook, D1 + offset);
}

// Attempt to move piece.
Expand Down Expand Up @@ -87,12 +87,12 @@ DoMove(Game *game, Move *move)
if(piece != Pawn) {
panic("Piece taken via en passant is %s, not pawn.", StringPiece(piece));
}
ChessSetRemovePiece(&game->ChessSet, opposite, piece, enPassant);
RemovePiece(&game->ChessSet, opposite, piece, enPassant);
AppendPiece(&game->History.CapturedPieces, piece);
}

ChessSetRemovePiece(&game->ChessSet, game->WhosTurn, move->Piece, move->From);
ChessSetPlacePiece(&game->ChessSet, game->WhosTurn, move->Piece, move->To);
RemovePiece(&game->ChessSet, game->WhosTurn, move->Piece, move->From);
PlacePiece(&game->ChessSet, game->WhosTurn, move->Piece, move->To);

break;
case PromoteKnight:
Expand All @@ -106,7 +106,7 @@ DoMove(Game *game, Move *move)
panic("No piece at %s when attempting capture %s.", StringPosition(move->To),
StringMove(move));
} else {
ChessSetRemovePiece(&game->ChessSet, opposite, piece, move->To);
RemovePiece(&game->ChessSet, opposite, piece, move->To);
AppendPiece(&game->History.CapturedPieces, piece);
}
}
Expand Down Expand Up @@ -137,8 +137,8 @@ DoMove(Game *game, Move *move)
panic("Impossible.");
}

ChessSetRemovePiece(&game->ChessSet, game->WhosTurn, move->Piece, move->From);
ChessSetPlacePiece(&game->ChessSet, game->WhosTurn, piece, move->To);
RemovePiece(&game->ChessSet, game->WhosTurn, move->Piece, move->From);
PlacePiece(&game->ChessSet, game->WhosTurn, piece, move->To);

break;
}
Expand Down Expand Up @@ -210,17 +210,17 @@ ExposesCheck(Game *game, BitBoard kingThreats, Move *move)

clone = game->ChessSet;

ChessSetRemovePiece(&clone, side, move->Piece, move->From);
RemovePiece(&clone, side, move->Piece, move->From);

if(move->Type == EnPassant) {
ChessSetRemovePiece(&clone, OPPOSITE(side), Pawn,
RemovePiece(&clone, OPPOSITE(side), Pawn,
move->To + (game->WhosTurn == White ? -8 : 8));
} else if(move->Capture) {
piece = PieceAt(&clone.Sets[OPPOSITE(side)], move->To);
ChessSetRemovePiece(&clone, OPPOSITE(side), piece, move->To);
RemovePiece(&clone, OPPOSITE(side), piece, move->To);
}

ChessSetPlacePiece(&clone, side, move->Piece, move->To);
PlacePiece(&clone, side, move->Piece, move->To);

return Checked(&clone, side);
}
Expand Down Expand Up @@ -442,8 +442,8 @@ Unmove(Game *game)
case PromoteRook:
case PromoteQueen:
case Normal:
ChessSetRemovePiece(&game->ChessSet, game->WhosTurn, piece, move.To);
ChessSetPlacePiece(&game->ChessSet, game->WhosTurn, move.Piece, move.From);
RemovePiece(&game->ChessSet, game->WhosTurn, piece, move.To);
PlacePiece(&game->ChessSet, game->WhosTurn, move.Piece, move.From);

if(move.Capture) {
captured = PopPiece(&game->History.CapturedPieces);
Expand All @@ -452,29 +452,29 @@ Unmove(Game *game)
if(move.Type == EnPassant) {
offset = -1 + game->WhosTurn*2;
to = POSITION(RANK(move.To)+offset, FILE(move.To));
ChessSetPlacePiece(&game->ChessSet, opposite, captured, to);
PlacePiece(&game->ChessSet, opposite, captured, to);
} else {
ChessSetPlacePiece(&game->ChessSet, opposite, captured, move.To);
PlacePiece(&game->ChessSet, opposite, captured, move.To);
}
}

break;
case CastleQueenSide:
offset = game->WhosTurn == White ? 0 : 8*7;

ChessSetRemovePiece(&game->ChessSet, game->WhosTurn, King, C1+offset);
ChessSetPlacePiece(&game->ChessSet, game->WhosTurn, King, E1+offset);
ChessSetRemovePiece(&game->ChessSet, game->WhosTurn, Rook, D1+offset);
ChessSetPlacePiece(&game->ChessSet, game->WhosTurn, Rook, A1+offset);
RemovePiece(&game->ChessSet, game->WhosTurn, King, C1+offset);
PlacePiece(&game->ChessSet, game->WhosTurn, King, E1+offset);
RemovePiece(&game->ChessSet, game->WhosTurn, Rook, D1+offset);
PlacePiece(&game->ChessSet, game->WhosTurn, Rook, A1+offset);

break;
case CastleKingSide:
offset = game->WhosTurn == White ? 0 : 8*7;

ChessSetRemovePiece(&game->ChessSet, game->WhosTurn, King, G1+offset);
ChessSetPlacePiece(&game->ChessSet, game->WhosTurn, King, E1+offset);
ChessSetRemovePiece(&game->ChessSet, game->WhosTurn, Rook, F1+offset);
ChessSetPlacePiece(&game->ChessSet, game->WhosTurn, Rook, H1+offset);
RemovePiece(&game->ChessSet, game->WhosTurn, King, G1+offset);
PlacePiece(&game->ChessSet, game->WhosTurn, King, E1+offset);
RemovePiece(&game->ChessSet, game->WhosTurn, Rook, F1+offset);
PlacePiece(&game->ChessSet, game->WhosTurn, Rook, H1+offset);

break;
default:
Expand Down
7 changes: 3 additions & 4 deletions weak.h
Expand Up @@ -317,14 +317,13 @@ BitBoard SoEaRay(Position);
BitBoard SoWeOne(BitBoard);
BitBoard SoWeRay(Position);


// chessset.c
BitBoard KingThreats(ChessSet*, Side);
bool Checked(ChessSet*, Side);
void ChessSetPlacePiece(ChessSet*, Side, Piece, Position);
void ChessSetRemovePiece(ChessSet*, Side, Piece, Position);
BitBoard KingThreats(ChessSet*, Side);
ChessSet NewChessSet(void);
ChessSet NewEmptyChessSet(void);
void RemovePiece(ChessSet*, Side, Piece, Position);
void PlacePiece(ChessSet*, Side, Piece, Position);

// game.c
bool Checkmated(Game*);
Expand Down

0 comments on commit db84401

Please sign in to comment.