{"id":20589139,"url":"https://github.com/alexander-scott/chesslogicsharp","last_synced_at":"2025-07-21T15:05:31.123Z","repository":{"id":85999224,"uuid":"187925763","full_name":"alexander-scott/ChessLogicSharp","owner":"alexander-scott","description":"A lightweight, fully-featured Chess solution for .NET Applications","archived":false,"fork":false,"pushed_at":"2019-05-30T13:30:30.000Z","size":61,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-06T13:30:57.713Z","etag":null,"topics":["chess","chess-ai","chess-board","chess-game","chessboard","csharp","net"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alexander-scott.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-21T23:01:50.000Z","updated_at":"2023-01-20T08:26:40.000Z","dependencies_parsed_at":"2023-03-13T08:08:31.685Z","dependency_job_id":null,"html_url":"https://github.com/alexander-scott/ChessLogicSharp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/alexander-scott/ChessLogicSharp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexander-scott%2FChessLogicSharp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexander-scott%2FChessLogicSharp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexander-scott%2FChessLogicSharp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexander-scott%2FChessLogicSharp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexander-scott","download_url":"https://codeload.github.com/alexander-scott/ChessLogicSharp/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexander-scott%2FChessLogicSharp/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266324463,"owners_count":23911226,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-21T11:47:31.412Z","response_time":64,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chess","chess-ai","chess-board","chess-game","chessboard","csharp","net"],"created_at":"2024-11-16T07:28:00.526Z","updated_at":"2025-07-21T15:05:30.990Z","avatar_url":"https://github.com/alexander-scott.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ChessLogicSharp\nA lightweight, fully-featured Chess solution for .NET Applications that exhibits correct piece movement with castling, en passant, pawn promotion and a minmax AI player.\n\n## Usage\n\n### Instantiation\n\nCreate an instance of a Board by using the static BoardFactory.CreateBoard function, creating a board with pieces in the [default positions](https://www.chess.com/article/view/how-to-set-up-a-chessboard). The board follows an X,Y grid system with (0,0) being the bottom left grid square and (7,7) being the top right grid square. The Player One pieces (white) are created on the two bottom rows and the Player Two pieces (black) are created on the top two rows. The Board also stores the current turn, which is Player One when the board is initially created and swaps when moves are made.\n\n```csharp\nBoard board = BoardFactory.CreateBoard();\n```\n\nYou must then create instances of ChessPlayers and add them to the Board. The ChessPlayer class itself is abstract so you must create a derived class or simply use the existing BasicPlayer. When instantiating a ChessPlayer you must also specific which team they are on, Player One or Player Two, and then adding them to the Board. ChessPlayer instances can then make moves by calling the base MakeMove function.\n\n```csharp\npublic abstract class ChessPlayer\n{\n    public virtual void Update(float deltaTime) { }   \n\n    protected virtual void OnGameStateChanged(GameState state) { }\n\n    protected virtual void OnTurnSwapped(Player player) { }\n\n    protected bool MovePiece(BoardPieceMove boardPieceMove) { }\n}\n```\n```csharp\n_board = BoardFactory.CreateBoard();   \n_board.AddPlayer(new UnityChessPlayer(_board, Player.PlayerOne));\n_board.AddPlayer(new AIChessPlayer(_board, Player.PlayerTwo)); \n```\n\nWith your custom ChessPlayers you can have specific player functionality, such as an AI moving for a player, or a 3rd party input manager applying moves for one of the players. \n\n```csharp\npublic class AIChessPlayer : ChessPlayer\n{\n    public AIChessPlayer(Board board, Player player) : base(board, player) { }\n    \n    protected override void BoardOnOnTurnSwapped(Player player)\n    {\n        if (player == _player)\n        {\n            ThreadPool.QueueUserWorkItem((state) =\u003e CalculateAndMove());\n        }\n    }\n\n    private void CalculateAndMove()\n    {\n         MovePiece(MinMaxMoveCalc.GetBestMove(Board));\n    }\n}\n```\n\n\n### Making Moves\n\nA move is defined by a From Position and a To Position. ChessPlayers can make moves by calling the MovePiece function if the have been added to the Board.\n\n```csharp\nvar pawnPos = new Vector2I(4, 1);\nvar pawnDest = new Vector2I(4, 3);\nvar move = new BoardPieceMove(pawnPos, pawnDest);\n_player.MovePiece(move);\n```\n\nThe ApplyMove function will return false if the move you made was invalid in any way and will return true if the move was successfully applied. A move can be invalid for the following reasons:\n- Attempting to move a piece which doesn't belong to the player whose turn it currently is.\n- Attempting to make a move that puts the current player's king in check.\n- Attempting to make a move with a piece that the piece is unable to do, as defined by the [piece movement rules](https://www.chessusa.com/chess-rules.html).\n- Attempting to make a move when the board state is Game Over, which is set when a player has obtained checkmate.\n\n### Callbacks\n\nYou are able to subscribe to various callbacks that are called throughout a chess game, making it easy for your application to react to events in the chess game. The OnBoardChanged callback is special because it gives you a list of every action applied to the board the previous turn, such as piece moved, piece taken, pawn promotion etc. and includes the move itself providing you with the To and From piece positions of the move. This can allow you to animate movement or just render the board.\n\n```csharp\n// Called when a player makes their move and its parameter is the current players go. \npublic event PlayerDelegate OnTurnSwapped;\n\n/// Called when a player is in checkmate and its parameter is the player in check.\npublic event PlayerDelegate OnPlayerInCheck;\n\n/// Called when a something on the board has changed and its parameter is a list of changes.\npublic event BoardChangesDelegate OnBoardChanged;\n\n/// Called when the state of the game changes, such as when a game is paused, resumed or ended.\npublic event BoardGameStateDelegate OnGameStateChanged;\n```\n\n### Current Limitations\n- The pawn promotion is currently hard-coded to Queen.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexander-scott%2Fchesslogicsharp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexander-scott%2Fchesslogicsharp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexander-scott%2Fchesslogicsharp/lists"}