/// <summary> /// Checks the endgame. /// </summary> /// <param name="oppositePlayer">The opposite player.</param> /// <returns> /// <c>GameEventType.None</c> if there were no threats from either side. /// <c>GameEventType.Check</c> if there was a check from the opposing side. /// <c>GameEventType.Checkmate</c> if there was a checkmate from the opposing side. /// <c>GameEventType.Stalemate</c> if there is a stalemate. /// </returns> public override GameEventType CheckEndgame(PlayerBase oppositePlayer) { List<Position> oppositeKingMoves; List<Position> attacks; GameEventType endgame = GameEventType.None; if (oppositePlayer == null || !(oppositePlayer is PlayerChess)) { throw new Exception("oppositePlayer is not of PlayerChess type"); } oppositeKingMoves = ((PlayerChess)oppositePlayer).King.AllMoveOptions; foreach (IPiece piece in this.PieceList) { attacks = piece.AttackOptions; foreach (Position target in attacks) { if (this.gameboard.Figures[target] != null && this.gameboard.Figures[target] is King) { endgame = GameEventType.Check; break; } } } // If there is no Check and no Checkmate, check for Stalemate. List<Position> posList = new List<Position>(); bool isStalemate = false; // Aggregate all the possible moves from this player's pieces foreach (IPiece piece in this.PieceList) { posList.AddRange(piece.AllMoveOptions.ToList()); posList.Add(piece.Position); } // Check each possible move of the opposite king's moves foreach (Position position in oppositeKingMoves) { isStalemate = true; if (!posList.Contains(position)) { isStalemate = false; break; } } if (isStalemate) { if (endgame == GameEventType.Check) { return GameEventType.Checkmate; } // else // { // return GameEventType.Stalemate; // } } return endgame; }