diff --git a/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs b/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs index a94b63efb..6045ed048 100644 --- a/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs +++ b/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +using System.Collections.Generic; using static ChessChallenge.Application.Settings; using static ChessChallenge.Application.ConsoleHelper; @@ -84,6 +85,22 @@ public ChallengeController() StartNewGame(PlayerType.Human, PlayerType.MyBot); } + public void UndoMoves(uint numToUndo) + { + List allMoves = board.AllGameMoves; + numToUndo = Math.Min((uint)allMoves.Count, numToUndo); + + for (int i = 0; i < numToUndo; i++) + { + Move moveToUndo = allMoves.Last(); + board.UndoMove(moveToUndo, false); + } + + var lastMove = (allMoves.Count == 0) ? new Move(0) : allMoves.Last() ; + boardUI.UpdatePosition(board, lastMove); + NotifyTurnToMove(); + } + public void StartNewGame(PlayerType whiteType, PlayerType blackType) { // End any ongoing game @@ -180,6 +197,12 @@ void NotifyTurnToMove() { PlayerToMove.Human.SetPosition(FenUtility.CurrentFen(board)); PlayerToMove.Human.NotifyTurnToMove(); + + if (PlayerNotOnMove.IsHuman) + { + // for the case of a human vs human match when a manual undo fires + PlayerNotOnMove.Human.CancelTurnToMove(); + } } else { diff --git a/Chess-Challenge/src/Framework/Application/Players/HumanPlayer.cs b/Chess-Challenge/src/Framework/Application/Players/HumanPlayer.cs index b8396a069..301f55f5b 100644 --- a/Chess-Challenge/src/Framework/Application/Players/HumanPlayer.cs +++ b/Chess-Challenge/src/Framework/Application/Players/HumanPlayer.cs @@ -28,6 +28,11 @@ public void NotifyTurnToMove() { isTurnToMove = true; } + + public void CancelTurnToMove() + { + isTurnToMove = false; + } public void SetPosition(string fen) { diff --git a/Chess-Challenge/src/Framework/Application/UI/MenuUI.cs b/Chess-Challenge/src/Framework/Application/UI/MenuUI.cs index 57d12e7b8..054b6a1c7 100644 --- a/Chess-Challenge/src/Framework/Application/UI/MenuUI.cs +++ b/Chess-Challenge/src/Framework/Application/UI/MenuUI.cs @@ -9,12 +9,31 @@ public static class MenuUI { public static void DrawButtons(ChallengeController controller) { - Vector2 buttonPos = UIHelper.Scale(new Vector2(260, 210)); + Vector2 buttonPos = UIHelper.Scale(new Vector2(260, 144)); Vector2 buttonSize = UIHelper.Scale(new Vector2(260, 55)); float spacing = buttonSize.Y * 1.2f; float breakSpacing = spacing * 0.6f; + // Undo Button + if (controller.PlayerWhite.IsHuman || controller.PlayerBlack.IsHuman) + { + var undoNum = (controller.PlayerWhite.IsBot || controller.PlayerBlack.IsBot) ? 2 : 1 ; + if (NextButtonInRow("Undo Move", ref buttonPos, spacing, buttonSize)) + { + controller.UndoMoves((uint)undoNum); + } + } else { + buttonPos = UIHelper.Scale(new Vector2(260, 210)); + } + + // Game Buttons + buttonPos.Y += breakSpacing; + + if (NextButtonInRow("Human vs Human", ref buttonPos, spacing, buttonSize)) + { + controller.StartNewGame(ChallengeController.PlayerType.Human, ChallengeController.PlayerType.Human); + } if (NextButtonInRow("Human vs MyBot", ref buttonPos, spacing, buttonSize)) { var whiteType = controller.HumanWasWhiteLastGame ? ChallengeController.PlayerType.MyBot : ChallengeController.PlayerType.Human;