Add chess (Oh my gosh big commit)

This commit is contained in:
jheaps
2025-03-27 13:54:07 -06:00
parent 2edc40fb99
commit fbed5ae789
41 changed files with 1943 additions and 71 deletions
+11
View File
@@ -0,0 +1,11 @@
namespace JoshHeaps.Net.Models;
public class ChessPiece(string id, PieceType type, PieceColor color, Position position)
{
public string Id { get; set; } = id;
public PieceType Type { get; set; } = type;
public PieceColor Color { get; set; } = color;
public Position Position { get; set; } = position;
public bool HasMoved { get; set; } = false;
}
+17
View File
@@ -0,0 +1,17 @@
namespace JoshHeaps.Net.Models;
public enum PieceType
{
Pawn,
Rook,
Knight,
Bishop,
Queen,
King
}
public enum PieceColor
{
White,
Black
}
+52
View File
@@ -0,0 +1,52 @@
namespace JoshHeaps.Net.Models;
public class GameState
{
public Guid GameId { get; set; }
// 8x8 board of references. Null if no piece is present.
// row 0 at top -> row 7 at bottom (typical 0-based array).
public ChessPiece?[,] Board { get; set; }
// Whose turn is it?
public PieceColor CurrentPlayer { get; set; }
// To detect if en passant is possible, store the position of a pawn that just moved two squares.
// If no pawn is currently "en-passant capturable," this could be null.
public Position? EnPassantTarget { get; set; }
// Castling rights: have the rooks or king moved?
// In real chess notation, you track it per side, e.g. "KQkq" style. Let’s store boolean flags:
public bool WhiteCanCastleKingside { get; set; }
public bool WhiteCanCastleQueenside { get; set; }
public bool BlackCanCastleKingside { get; set; }
public bool BlackCanCastleQueenside { get; set; }
// Some game status
public bool IsCheck { get; set; }
public bool IsCheckmate { get; set; }
public bool IsStalemate { get; set; }
// Keep a history of moves if desired
public List<string> MoveHistory { get; set; }
// A list of all pieces to quickly reference them (optional but convenient).
// Alternatively, you can iterate the Board array.
public List<ChessPiece> Pieces { get; set; }
public bool WhiteJoined { get; set; } = false;
public bool BlackJoined { get; set; } = false;
public Guid WhitePlayerId { get; set; }
public Guid BlackPlayerId { get; set; }
// optional: convenience
public bool IsOpen => !WhiteJoined || !BlackJoined;
public GameState()
{
GameId = Guid.NewGuid();
Board = new ChessPiece[8, 8];
Pieces = [];
MoveHistory = [];
}
}
+20
View File
@@ -0,0 +1,20 @@
namespace JoshHeaps.Net.Models;
public class MoveDto
{
public Guid GameId { get; set; }
public Guid PlayerId { get; set; }
public string PieceId { get; set; }
// The square the piece is moving from
public int SourceRow { get; set; }
public int SourceCol { get; set; }
// The square the piece is moving to
public int TargetRow { get; set; }
public int TargetCol { get; set; }
// If this is a pawn promotion, specify the piece type to promote to; otherwise null
public PieceType? PromotionChoice { get; set; }
}
+10
View File
@@ -0,0 +1,10 @@
namespace JoshHeaps.Net.Models;
public class MoveResultDto
{
public bool Success { get; set; }
public string Message { get; set; }
public bool IsCheck { get; set; }
public bool IsCheckmate { get; set; }
public bool IsStalemate { get; set; }
}
+18
View File
@@ -0,0 +1,18 @@
namespace JoshHeaps.Net.Models;
public struct Position(int row, int col)
{
public int Row { get; set; } = row;
public int Col { get; set; } = col;
public override readonly string ToString()
{
return $"[{Row}, {Col}]";
}
internal readonly void Deconstruct(out int row, out int col)
{
row = Row; col = Col;
}
}