From 930ba57d1dcece1104df7cb2f56c6c5d3850e763 Mon Sep 17 00:00:00 2001 From: Ian Harris Date: Sat, 22 Jul 2023 16:06:53 -0700 Subject: [PATCH 1/4] add human vs human button --- Chess-Challenge/src/Framework/Application/UI/MenuUI.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Chess-Challenge/src/Framework/Application/UI/MenuUI.cs b/Chess-Challenge/src/Framework/Application/UI/MenuUI.cs index 1e39fcf3f..b6b522e68 100644 --- a/Chess-Challenge/src/Framework/Application/UI/MenuUI.cs +++ b/Chess-Challenge/src/Framework/Application/UI/MenuUI.cs @@ -15,6 +15,10 @@ public static void DrawButtons(ChallengeController controller) float breakSpacing = spacing * 0.6f; // Game Buttons + 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; From 166d2bad9833937c27dfc15457b4fb0d327acc3b Mon Sep 17 00:00:00 2001 From: Paul Spooner Date: Mon, 24 Jul 2023 18:56:15 -0700 Subject: [PATCH 2/4] Undo implemented To-Do: Update board highlighting Debug both players able to move on undo, resulting in possible illegal moves Debug multi-undo soft lock when first player is bot. --- .../Application/Core/ChallengeController.cs | 16 ++++++++++++++++ .../src/Framework/Application/UI/MenuUI.cs | 17 ++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs b/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs index 6ef9b6086..3da21d6fb 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; @@ -75,6 +76,21 @@ 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); + } + + boardUI.UpdatePosition(board); + NotifyTurnToMove(); + } + public void StartNewGame(PlayerType whiteType, PlayerType blackType) { // End any ongoing game diff --git a/Chess-Challenge/src/Framework/Application/UI/MenuUI.cs b/Chess-Challenge/src/Framework/Application/UI/MenuUI.cs index b6b522e68..3b4eb84a6 100644 --- a/Chess-Challenge/src/Framework/Application/UI/MenuUI.cs +++ b/Chess-Challenge/src/Framework/Application/UI/MenuUI.cs @@ -9,12 +9,27 @@ 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); From 4511eb79856119f36376f1d05ba9373e4c2042b9 Mon Sep 17 00:00:00 2001 From: Paul Spooner Date: Mon, 24 Jul 2023 22:11:05 -0700 Subject: [PATCH 3/4] Undo improved Fixed board highlighting Fixed multi-undo soft lock when first player is bot. Fixed both human players able to move on undo --- .../Application/Core/ChallengeController.cs | 12 ++++++++++-- .../src/Framework/Application/Players/HumanPlayer.cs | 5 +++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs b/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs index 3da21d6fb..d72e7487e 100644 --- a/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs +++ b/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs @@ -87,7 +87,8 @@ public void UndoMoves(uint numToUndo) board.UndoMove(moveToUndo, false); } - boardUI.UpdatePosition(board); + var lastMove = (allMoves.Count == 0) ? new Move(0) : allMoves.Last() ; + boardUI.UpdatePosition(board, lastMove); NotifyTurnToMove(); } @@ -185,6 +186,12 @@ void NotifyTurnToMove() { PlayerToMove.Human.SetPosition(FenUtility.CurrentFen(board)); PlayerToMove.Human.NotifyTurnToMove(); + + if (PlayerNotToMove.IsHuman) + { + // for the case of a human vs human match when a manual undo fires + PlayerNotToMove.Human.CancelTurnToMove(); + } } else { @@ -431,7 +438,8 @@ public void StartNewBotMatch(PlayerType botTypeA, PlayerType botTypeB) } - ChessPlayer PlayerToMove => board.IsWhiteToMove ? PlayerWhite : PlayerBlack; + ChessPlayer PlayerToMove => (board.IsWhiteToMove) ? PlayerWhite : PlayerBlack; + ChessPlayer PlayerNotToMove => !(board.IsWhiteToMove) ? PlayerWhite : PlayerBlack; public int TotalGameCount => botMatchStartFens.Length * 2; public int CurrGameNumber => Math.Min(TotalGameCount, botMatchGameIndex + 1); public string AllPGNs => pgns.ToString(); 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) { From 4910c3c38afd9993d871d7ea127acc82082fb839 Mon Sep 17 00:00:00 2001 From: Paul Spooner Date: Wed, 26 Jul 2023 16:39:55 -0700 Subject: [PATCH 4/4] Complete manual code merge --- .../src/Framework/Application/Core/ChallengeController.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs b/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs index 6a777afbf..a65916fb1 100644 --- a/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs +++ b/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs @@ -184,6 +184,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 {