Skip to content

Commit

Permalink
Merge pull request #17 from CPSSD/gui
Browse files Browse the repository at this point in the history
Merging sprint 4 of gui into master.
  • Loading branch information
GoldenBadger committed Apr 22, 2015
2 parents 5c14a77 + 8c41a1e commit 7c1f38b
Show file tree
Hide file tree
Showing 12 changed files with 950 additions and 70 deletions.
70 changes: 63 additions & 7 deletions gui/GUI/Board.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ public enum PieceColour { White, Black };
public enum PieceType { Pawn, Knight, Bishop, Rook, Queen, King };
// The colour before the game result indicates which colour has lost.
public enum GameStatus { Inactive, Active, Stalemate, WhiteCheckmate,
BlackCheckmate, WhiteAdjudicate, BlackAdjudicate, WhiteTime, BlackTime };
BlackCheckmate, WhiteAdjudicate, BlackAdjudicate, WhiteTime, BlackTime,
DrawRepetition, DrawFifty, DrawInsuffientMaterial };
// If a move was a castle, capture, etc.
public enum MoveResult { None, PawnMove, Capture, KingsideCastle, QueensideCastle }

/**
* @brief Representation of the board as a whole.
Expand All @@ -22,10 +25,16 @@ public class Board
public virtual bool BlackCastledL { get; set; }
public virtual bool WhiteCastledL { get; set; }
public virtual PieceColour PlayerToMove { get; set; }
public virtual byte EnPassantSquare { get; protected set; }
public virtual PieceColour EnPassantColour { get; protected set; }

public readonly byte[] castleDestinations = { 2, 6, 56, 62 };
public readonly byte[] pawnPromotionDestinations = { 0, 1, 2, 3, 4, 5, 6, 7,
56, 57, 58, 59, 60, 61, 62, 63 };
public readonly byte[] enPassantStartSquares = { 8, 9, 10, 11, 12, 13, 14, 15,
48, 49, 50, 51, 52, 53, 54, 55 };
public readonly byte[] enPassantEndSquares = { 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39 };

/**
* @brief Default constructor.
Expand All @@ -42,6 +51,8 @@ public Board (bool empty = false)
BlackCastledL = true;
WhiteCastledL = true;
PlayerToMove = PieceColour.White;
EnPassantSquare = 0;
EnPassantColour = PieceColour.White;

if (empty) {
Squares = new Square[64];
Expand Down Expand Up @@ -94,13 +105,13 @@ public Board (bool empty = false)
*
* Copies the list of pieces from one board to another.
*/
public Board(Board other)
public Board(Board other, bool copyLists = false)
{
Squares = new Square[64];
for (int i = 0; i < 64; i++) {
Squares [i] = new Square ();
if (other.Squares [i].Piece != null) {
Squares [i].Piece = new Piece (other.Squares [i].Piece);
Squares [i].Piece = new Piece (other.Squares [i].Piece, copyLists);
}
}
BlackCheck = other.BlackCheck;
Expand All @@ -110,6 +121,8 @@ public Board(Board other)
BlackCastledR = other.BlackCastledR;
WhiteCastledR = other.WhiteCastledR;
PlayerToMove = other.PlayerToMove;
EnPassantSquare = other.EnPassantSquare;
EnPassantColour = other.EnPassantColour;
}

public void AddPiece(PieceColour colour, PieceType type, int position)
Expand Down Expand Up @@ -154,8 +167,10 @@ public bool IsMoveValid(byte source, byte destination)
* Makes a move, switches the @c PlayerToMove variable,
* and updates piece legal moves.
*/
public void MakeMove(byte source, byte destination, PieceType? promoteTo = null)
public MoveResult MakeMove(byte source, byte destination, PieceType? promoteTo = null)
{
MoveResult ret = MoveResult.None;

if (!IsMoveValid (source, destination)) {
throw new InvalidOperationException ("Invalid move entered.");
}
Expand Down Expand Up @@ -188,6 +203,7 @@ public void MakeMove(byte source, byte destination, PieceType? promoteTo = null)
if (movingPiece.Type == PieceType.King &&
(source == 4 || source == 60) &&
Array.IndexOf (castleDestinations, destination) != -1) {
ret = destination - source > 0 ? MoveResult.KingsideCastle : MoveResult.QueensideCastle;
Square castleRookSquare = destination - source > 0 ?
Squares [destination + 1] : Squares [destination - 2];
Squares [destination].Piece = movingPiece;
Expand All @@ -197,6 +213,18 @@ Squares [destination - source > 0 ?
destination + 1].Piece = castleRookSquare.Piece;
castleRookSquare.Piece = null;
} else {
// Handle pawn moves
if (Squares [source].Piece.Type == PieceType.Pawn) {
ret = MoveResult.PawnMove;
// Handle en passant creation
if (Array.IndexOf (enPassantStartSquares, source) > -1 && Array.IndexOf (enPassantEndSquares, destination) > -1) {
EnPassantColour = Squares [source].Piece.Colour;
EnPassantSquare = EnPassantColour == PieceColour.White ? (byte)(destination + 8) : (byte)(destination - 8);
}
}
if (Squares [destination].Piece != null) {
ret = MoveResult.Capture;
}
switch (promoteTo) {
case PieceType.Bishop:
Squares [destination].Piece = new Piece (movingPiece.Colour, PieceType.Bishop);
Expand All @@ -215,8 +243,21 @@ Squares [destination - source > 0 ?
Squares [source].Piece = null;
break;
default:
Squares [destination].Piece = movingPiece;
Squares [source].Piece = null;
// Handle en passant capture
if (movingPiece.Type == PieceType.Pawn && destination == EnPassantSquare && EnPassantSquare != 0) {
byte captureSquare = EnPassantColour == PieceColour.White ? (byte)(destination - 8) : (byte)(destination + 8);
Squares [destination].Piece = movingPiece;
Squares [captureSquare].Piece = null;
Squares [source].Piece = null;
ret = MoveResult.Capture;
EnPassantSquare = 0;
} else {
Squares [destination].Piece = movingPiece;
Squares [source].Piece = null;
if (movingPiece.Type != PieceType.Pawn) {
EnPassantSquare = 0;
}
}
break;
}

Expand All @@ -232,6 +273,7 @@ Squares [destination - source > 0 ?
}
PiecePseudoLegalMoves.GeneratePseudoLegalMoves (this);
PieceLegalMoves.GenerateLegalMoves (this);
return ret;
}

/**
Expand Down Expand Up @@ -484,10 +526,24 @@ public Piece(PieceColour colour, PieceType type)
*
* @param other the piece from which to copy.
*/
public Piece(Piece other)
public Piece(Piece other, bool copyLists = false)
{
Colour = other.Colour;
Type = other.Type;
if (copyLists == true)
{
if (other.PseudoLegalMoves != null)
{
PseudoLegalMoves = new List<byte>(other.PseudoLegalMoves);
}
if (other.LegalMoves != null)
{
LegalMoves = new List<byte>(other.LegalMoves);
}
}
TimesAttacked = other.TimesAttacked;
TimesDefended = other.TimesDefended;
HasMoved = other.HasMoved;
}

/**
Expand Down
27 changes: 25 additions & 2 deletions gui/GUI/DummyBoard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class DummyBoard : Board
public override bool BlackCastledL { get; set; }
public override bool WhiteCastledL { get; set; }
public override PieceColour PlayerToMove { get; set; }
public override byte EnPassantSquare { get; protected set; }
public override PieceColour EnPassantColour { get; protected set; }

public DummyBoard (Board other)
{
Expand All @@ -41,6 +43,8 @@ public DummyBoard (Board other)
BlackCastledL = other.BlackCastledR;
WhiteCastledL = other.WhiteCastledR;
PlayerToMove = other.PlayerToMove;
EnPassantSquare = other.EnPassantSquare;
EnPassantColour = other.EnPassantColour;
}

// Returns value of captured piece, for use in DummyBoard.UndoMove
Expand All @@ -63,6 +67,16 @@ Squares [destination - source > 0 ?
destination + 1].Piece = castleRookSquare.Piece;
castleRookSquare.Piece = null;
} else {
// Handle en passant.
if (Squares [source].Piece.Type == PieceType.Pawn) {
if (Array.IndexOf (enPassantStartSquares, source) > -1 && Array.IndexOf (enPassantEndSquares, destination) > -1) {
EnPassantColour = Squares [source].Piece.Colour;
EnPassantSquare = EnPassantColour == PieceColour.White ? (byte)(destination + 8) : (byte)(destination - 8);
} else {
EnPassantSquare = 0;
}
}

capturedPiece = Squares [destination].Piece;
switch (promoteTo) {
case PieceType.Bishop:
Expand All @@ -82,8 +96,17 @@ Squares [destination - source > 0 ?
Squares [source].Piece = null;
break;
default:
Squares [destination].Piece = movingPiece;
Squares [source].Piece = null;
// Handle en passant capture
if (movingPiece.Type == PieceType.Pawn && destination == EnPassantSquare && EnPassantSquare != 0) {
byte captureSquare = EnPassantColour == PieceColour.White ? (byte)(destination - 8) : (byte)(destination + 8);
Squares [destination].Piece = movingPiece;
capturedPiece = Squares [captureSquare].Piece;
Squares [captureSquare].Piece = null;
Squares [source].Piece = null;
} else {
Squares [destination].Piece = movingPiece;
Squares [source].Piece = null;
}
break;
}
}
Expand Down
7 changes: 1 addition & 6 deletions gui/GUI/GUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,21 @@
<Reference Include="System" />
<Reference Include="gtk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<SpecificVersion>False</SpecificVersion>
<Package>gtk-sharp-2.0</Package>
</Reference>
<Reference Include="gdk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<SpecificVersion>False</SpecificVersion>
<Package>gtk-sharp-2.0</Package>
</Reference>
<Reference Include="glib-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<SpecificVersion>False</SpecificVersion>
<Package>glib-sharp-2.0</Package>
</Reference>
<Reference Include="glade-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<SpecificVersion>False</SpecificVersion>
<Package>glade-sharp-2.0</Package>
</Reference>
<Reference Include="pango-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<SpecificVersion>False</SpecificVersion>
<Package>gtk-sharp-2.0</Package>
</Reference>
<Reference Include="atk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<SpecificVersion>False</SpecificVersion>
<Package>gtk-sharp-2.0</Package>
</Reference>
<Reference Include="Mono.Posix" />
<Reference Include="Mono.Cairo" />
Expand Down Expand Up @@ -89,6 +83,7 @@
<Compile Include="gtk-gui\GUI.PawnPromotionDialog.cs" />
<Compile Include="EngineStrengthDialog.cs" />
<Compile Include="gtk-gui\GUI.EngineStrengthDialog.cs" />
<Compile Include="GameHistory.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
Expand Down
Loading

0 comments on commit 7c1f38b

Please sign in to comment.