public ulong WhoseTurn(ChessMatchStatusModel match) { if (match.Game == null) { match.Game = match.Match.HistoryList.Count == 0 ? new ChessGame() : new ChessGame(match.Match.HistoryList.Select(x => x.Move), true); } return(match.Game.WhoseTurn != Player.White ? match.Match.ChallengeeId : match.Match.ChallengerId); }
public async Task <ChessMatchStatusModel> Move(ulong guildId, ulong channelId, IUser player, string rawMove) { var rankToRowMap = new Dictionary <int, int> { { 1, 7 }, { 2, 6 }, { 3, 5 }, { 4, 4 }, { 5, 3 }, { 6, 2 }, { 7, 1 }, { 8, 0 } }; var moveInput = rawMove.Replace(" ", "").ToUpper(); if (!Regex.IsMatch(moveInput, "^[A-H][1-8][A-H][1-8][Q|N|B|R]?$")) { throw new ChessException("Error parsing move. Example move: a2a4"); } var match = _chessHelper.GetMatch(guildId, channelId, player.Id); if (match == null) { throw new ChessException("You are not currently in a game"); } var moves = match.HistoryList.Select(x => x.Move).ToList(); var game = moves.Count != 0 ? new ChessGame(moves, true) : new ChessGame(); var whoseTurn = game.WhoseTurn; var otherPlayer = whoseTurn == Player.White ? Player.Black : Player.White; if (whoseTurn == Player.White && player.Id != match.ChallengerId || whoseTurn == Player.Black && player.Id != match.ChallengeeId) { throw new ChessException("It's not your turn."); } var sourceX = moveInput[0].ToString(); var sourceY = moveInput[1].ToString(); var destX = moveInput[2].ToString(); var destY = moveInput[3].ToString(); var positionEnumValues = (IEnumerable <File>)Enum.GetValues(typeof(File)); var enumValues = positionEnumValues.ToList(); var sourcePositionX = enumValues.Single(x => x.ToString("g") == sourceX); var destPositionX = enumValues.Single(x => x.ToString("g") == destX); var originalPosition = new Position(sourcePositionX, int.Parse(sourceY)); var newPosition = new Position(destPositionX, int.Parse(destY)); var board = game.GetBoard(); var file = int.Parse(sourcePositionX.ToString("d")); var collumn = rankToRowMap[int.Parse(sourceY)]; if (board[collumn][file] == null) { throw new ChessException("Invalid move."); } var pieceChar = board[collumn][file].GetFenCharacter(); var isPawn = pieceChar.ToString().ToLower() == "p"; char?promotion; if (destY != "1" && destY != "8" || !isPawn) { promotion = null; } else { promotion = moveInput[4].ToString().ToLower()[0]; } var move = new Move(originalPosition, newPosition, whoseTurn, promotion); if (!game.IsValidMove(move)) { throw new ChessException("Invalid move."); } var chessMove = new ChessMoveModel { Move = move, MovedfenChar = pieceChar, MoveDate = DateTime.Now, Player = whoseTurn }; game.ApplyMove(move, true); match.HistoryList.Add(chessMove); var endCause = Cause.OnGoing; if (game.IsStalemated(otherPlayer)) { endCause = Cause.Stalemate; } else if (game.IsCheckmated(otherPlayer)) { endCause = Cause.Checkmate; } var imageLinkValues = await DrawBoard(match); var status = new ChessMatchStatusModel { Game = game, ImageLink = imageLinkValues.ImageLink, Status = endCause, WinnerId = endCause == Cause.Checkmate ? (ulong?)player.Id : null, IsCheck = game.IsInCheck(otherPlayer), IsCheckmated = game.IsCheckmated(otherPlayer), Match = match }; _chessHelper.UpdateChessGame(status); return(await Task.FromResult(status)); }