Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 2 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Commits on Jul 17, 2012
@lorenzo-stoakes Add explanatory/separating comment. e8e4a2f
@lorenzo-stoakes ChessSet piece placement/removal is what most of the code is doing so…
… no need to prefix with ChessSet. Rearrange function prototypes into alphabetical order.
db84401
Showing with 62 additions and 61 deletions.
  1. +27 −27 chessset.c
  2. +30 −30 game.c
  3. +5 −4 weak.h
View
54 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)
{
@@ -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
@@ -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);
+}
View
60 game.c
@@ -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.
@@ -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:
@@ -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);
}
}
@@ -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;
}
@@ -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);
}
@@ -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);
@@ -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:
View
9 weak.h
@@ -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*);
@@ -440,6 +439,8 @@ StringBuilder NewStringBuilder(void);
void ReleaseStringBuilder(StringBuilder*);
void SetUnbufferedOutput(void);
+// Move/Capture function arrays. Initialised in InitEngine() in game.c
+
BitBoard (*GetMoveTargets[6])(ChessSet*, Side, BitBoard);
BitBoard (*GetCaptureTargets[6])(ChessSet*, Side, BitBoard);

No commit comments for this range

Something went wrong with that request. Please try again.