Skip to content

Commit 6003784

Browse files
committed
merge {dudecon:Human-Undo}: Fix for SebLague#305
1 parent b6630d1 commit 6003784

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Text;
99
using System.Threading;
1010
using System.Threading.Tasks;
11+
using System.Collections.Generic;
1112
using static ChessChallenge.Application.Settings;
1213
using static ChessChallenge.Application.ConsoleHelper;
1314

@@ -84,6 +85,22 @@ public ChallengeController()
8485
StartNewGame(PlayerType.Human, PlayerType.MyBot);
8586
}
8687

88+
public void UndoMoves(uint numToUndo)
89+
{
90+
List<Move> allMoves = board.AllGameMoves;
91+
numToUndo = Math.Min((uint)allMoves.Count, numToUndo);
92+
93+
for (int i = 0; i < numToUndo; i++)
94+
{
95+
Move moveToUndo = allMoves.Last();
96+
board.UndoMove(moveToUndo, false);
97+
}
98+
99+
var lastMove = (allMoves.Count == 0) ? new Move(0) : allMoves.Last() ;
100+
boardUI.UpdatePosition(board, lastMove);
101+
NotifyTurnToMove();
102+
}
103+
87104
public void StartNewGame(PlayerType whiteType, PlayerType blackType)
88105
{
89106
// End any ongoing game
@@ -180,6 +197,12 @@ void NotifyTurnToMove()
180197
{
181198
PlayerToMove.Human.SetPosition(FenUtility.CurrentFen(board));
182199
PlayerToMove.Human.NotifyTurnToMove();
200+
201+
if (PlayerNotOnMove.IsHuman)
202+
{
203+
// for the case of a human vs human match when a manual undo fires
204+
PlayerNotOnMove.Human.CancelTurnToMove();
205+
}
183206
}
184207
else
185208
{

Chess-Challenge/src/Framework/Application/Players/HumanPlayer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public void NotifyTurnToMove()
2828
{
2929
isTurnToMove = true;
3030
}
31+
32+
public void CancelTurnToMove()
33+
{
34+
isTurnToMove = false;
35+
}
3136

3237
public void SetPosition(string fen)
3338
{

Chess-Challenge/src/Framework/Application/UI/MenuUI.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,31 @@ public static class MenuUI
99
{
1010
public static void DrawButtons(ChallengeController controller)
1111
{
12-
Vector2 buttonPos = UIHelper.Scale(new Vector2(260, 210));
12+
Vector2 buttonPos = UIHelper.Scale(new Vector2(260, 144));
1313
Vector2 buttonSize = UIHelper.Scale(new Vector2(260, 55));
1414
float spacing = buttonSize.Y * 1.2f;
1515
float breakSpacing = spacing * 0.6f;
1616

17+
// Undo Button
18+
if (controller.PlayerWhite.IsHuman || controller.PlayerBlack.IsHuman)
19+
{
20+
var undoNum = (controller.PlayerWhite.IsBot || controller.PlayerBlack.IsBot) ? 2 : 1 ;
21+
if (NextButtonInRow("Undo Move", ref buttonPos, spacing, buttonSize))
22+
{
23+
controller.UndoMoves((uint)undoNum);
24+
}
25+
} else {
26+
buttonPos = UIHelper.Scale(new Vector2(260, 210));
27+
}
28+
29+
1730
// Game Buttons
31+
buttonPos.Y += breakSpacing;
32+
33+
if (NextButtonInRow("Human vs Human", ref buttonPos, spacing, buttonSize))
34+
{
35+
controller.StartNewGame(ChallengeController.PlayerType.Human, ChallengeController.PlayerType.Human);
36+
}
1837
if (NextButtonInRow("Human vs MyBot", ref buttonPos, spacing, buttonSize))
1938
{
2039
var whiteType = controller.HumanWasWhiteLastGame ? ChallengeController.PlayerType.MyBot : ChallengeController.PlayerType.Human;

0 commit comments

Comments
 (0)