Skip to content

Demo project #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ChessProject-Csharp/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vs
.idea
bin
obj
78 changes: 71 additions & 7 deletions ChessProject-Csharp/src/ChessBoard.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,90 @@
using System;
using src;

namespace SolarWinds.MSP.Chess
{
public class ChessBoard
{
public static readonly int MaxBoardWidth = 7;
public static readonly int MaxBoardHeight = 7;
private Pawn[,] pieces;
private ChessPieces pieces;

public ChessBoard ()
{
pieces = new Pawn[MaxBoardWidth, MaxBoardHeight];
pieces = new ChessPieces(Constants.ChessBoard.MaxBoardWidth, Constants.ChessBoard.MaxBoardHeight);
}

public void Add(Pawn pawn, int xCoordinate, int yCoordinate, PieceColor pieceColor)
{
throw new NotImplementedException("Need to implement ChessBoard.Add()");
// Add pawn with special logic unlice other pieces to be able to track counter
if (this.IsLegalBoardPosition(xCoordinate, yCoordinate) && pieces.CanAddPawn(xCoordinate, yCoordinate, pieceColor))
{
pawn.XCoordinate = xCoordinate;
pawn.YCoordinate = yCoordinate;
pawn.ChessBoard = this;
pieces.AddPawn(pawn);
}
else
{
// Set pawn's coordiantes to the invalid ones, do not add to the pieces collections and do not set the chessboard
pawn.XCoordinate = Constants.InvalidXCoordinate;
pawn.YCoordinate = Constants.InvalidYCoordinate;
}
}

public void Add(ChessPiece piece, int xCoordinate, int yCoordinate)
{
// Basic rules to add a new piece
if (this.IsEmptyBoardPosition(xCoordinate, yCoordinate))
{
piece.XCoordinate = xCoordinate;
piece.YCoordinate = yCoordinate;
piece.ChessBoard = this;
pieces.AddPiece(piece);
}
else
{
// Set pieces's coordiantes to the invalid ones, do not add to the pieces collections and do not set the chessboard
piece.XCoordinate = Constants.InvalidXCoordinate;
piece.YCoordinate = Constants.InvalidYCoordinate;
}
}

public bool IsLegalBoardPosition(int xCoordinate, int yCoordinate)
{
throw new NotImplementedException("Need to implement ChessBoard.IsLegalBoardPosition()");
return 0 <= xCoordinate && xCoordinate <= Constants.ChessBoard.MaxBoardWidth &&
0 <= yCoordinate && yCoordinate <= Constants.ChessBoard.MaxBoardHeight;
}

public bool IsEmptyBoardPosition(int xCoordinate, int yCoordinate)
{
return this.IsLegalBoardPosition(xCoordinate, yCoordinate) && this.pieces.IsEmptyPosition(xCoordinate, yCoordinate);
}

public ChessPiece GetPieceFromBoardPosition(int xCoordinate, int yCoordinate)
{
return this.IsLegalBoardPosition(xCoordinate, yCoordinate) ? this.pieces[xCoordinate, yCoordinate] : null;
}

/// <summary>
/// Invalidates the board and the pieces after an allowed (valid) move.
/// </summary>
public void PerformMoveOnBoard(ChessPiece piece, int newX, int newY)
{
this.pieces[piece.XCoordinate, piece.YCoordinate] = null;
this.pieces[newX, newY] = piece;

piece.XCoordinate = newX;
piece.YCoordinate = newY;
}

/// <summary>
/// Invalidates the board and the pieces after an allowed (valid) capture.
/// </summary>
public void PerformCaptureOnBoard(ChessPiece piece)
{
this.pieces[piece.XCoordinate, piece.YCoordinate] = null;

// Set as captured in order to be able to track history and restore moves in a future functionality via memento
piece.SetAsCaptured();
pieces.CapturedPieces.Add(piece);
}
}
}
77 changes: 77 additions & 0 deletions ChessProject-Csharp/src/ChessPiece.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using SolarWinds.MSP.Chess;
using System;

namespace src
{
public abstract class ChessPiece
{
private ChessBoard chessBoard;
private int xCoordinate;
private int yCoordinate;
private PieceColor pieceColor;
private bool isCaptured;

public ChessPiece(PieceColor pieceColor)
{
this.pieceColor = pieceColor;
}

public ChessBoard ChessBoard
{
get { return chessBoard; }
set { chessBoard = value; }
}

public int XCoordinate
{
get { return xCoordinate; }
set { xCoordinate = value; }
}

public int YCoordinate
{
get { return yCoordinate; }
set { yCoordinate = value; }
}

public PieceColor PieceColor
{
get { return pieceColor; }
private set { pieceColor = value; }
}

public bool IsCaptured
{
get
{
return this.isCaptured;
}
private set
{
this.isCaptured = value;
}
}

public void SetAsCaptured()
{
// Move piece "out of the board"
this.isCaptured = true;
this.XCoordinate = Constants.InvalidXCoordinate;
this.YCoordinate = Constants.InvalidYCoordinate;
}

protected abstract bool CanMove(int newX, int newY);

protected abstract bool CanCapture(int newX, int newY, out ChessPiece pieceToCapture);

public override string ToString()
{
return CurrentPositionAsString();
}

protected string CurrentPositionAsString()
{
return string.Format("Current X: {1}{0}Current Y: {2}{0}Piece Color: {3}", Environment.NewLine, XCoordinate, YCoordinate, PieceColor);
}
}
}
85 changes: 85 additions & 0 deletions ChessProject-Csharp/src/ChessPieces.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using SolarWinds.MSP.Chess;
using System;
using System.Collections.Generic;

namespace src
{
internal class ChessPieces
{
private int MaxAllowedNumberOfPawnsPerColor = 8;
private ChessPiece[,] allPieces;

private List<ChessPiece> capturedPieces;

[Obsolete]
private Pawn[,] pawns;
private Dictionary<PieceColor, short> pawnsCounter;

// Declare all other pieces collections

internal ChessPieces(int maxBoardWidth, int maxBoardHeight)
{
this.allPieces = new ChessPiece[maxBoardWidth + 1, maxBoardHeight + 1];
this.capturedPieces = new List<ChessPiece>();

// init all individual collections of pieces
this.pawns = new Pawn[maxBoardWidth + 1, maxBoardHeight + 1];
this.pawnsCounter = new Dictionary<PieceColor, short>()
{
{ PieceColor.White, 0 },
{ PieceColor.Black, 0 }
};
}

public ChessPiece this[int xCoordinate, int yCoordinate]
{
get
{
return this.allPieces[xCoordinate, yCoordinate];
}
set
{
this.allPieces[xCoordinate, yCoordinate] = value;
}
}

public List<ChessPiece> CapturedPieces
{
get
{
return this.capturedPieces;
}
}

internal bool CanAddPawn(int xCoordinate, int yCoordinate, PieceColor pieceColor)
{
/* Conditions:
- position is valid (aka within the board)
- there is not a different piece there aka empty
- you have not reached maximum number of pawns allowed
*/

return this.IsEmptyPosition(xCoordinate, yCoordinate) && pawnsCounter[pieceColor] < MaxAllowedNumberOfPawnsPerColor;
//pawns[xCoordinate, yCoordinate] == null;
}

internal void AddPawn(Pawn pawn)
{
pawns[pawn.XCoordinate, pawn.YCoordinate] = pawn;
pawnsCounter[pawn.PieceColor]++;

this.AddPiece(pawn);
}

internal void AddPiece(ChessPiece piece)
{
this.allPieces[piece.XCoordinate, piece.YCoordinate] = piece;
}

internal bool IsEmptyPosition(int xCoordinate, int yCoordinate)
{
// Loop all collections and see if position is empty or (better) just keep track of pieces in one array regardless of type
return this.allPieces[xCoordinate, yCoordinate] == null;
}
}
}
30 changes: 30 additions & 0 deletions ChessProject-Csharp/src/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using SolarWinds.MSP.Chess;
using System;
using System.Collections.Generic;
using System.Text;

namespace src
{
public class Constants
{
public class ChessBoard
{
public const int MaxBoardWidth = 7;
public const int MaxBoardHeight = 7;

/// <summary>
/// The first row for the <see cref="SolarWinds.MSP.Chess.PieceColor.Black"/> pawns, aka from where they are allowed to start with a 2-position move.
/// </summary>
public const int BlackStartRow = 6;
/// <summary>
/// The first row for the <see cref="SolarWinds.MSP.Chess.PieceColor.White"/> pawns, aka from where they are allowed to start with a 2-position move.
/// </summary>
public const int WhiteStartRow = 1;
}

public const int InvalidXCoordinate = -1;
public const int InvalidYCoordinate = -1;

public static readonly Type DefaultPawnPromotionType = typeof(Queen);
}
}
Loading