Skip to content

Commit

Permalink
merge {dudecon:Human-Undo}: Fix for SebLague#305
Browse files Browse the repository at this point in the history
  • Loading branch information
VitorA29 committed Aug 17, 2023
1 parent b6630d1 commit 6003784
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -84,6 +85,22 @@ public ChallengeController()
StartNewGame(PlayerType.Human, PlayerType.MyBot);
}

public void UndoMoves(uint numToUndo)
{
List<Move> 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
Expand Down Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public void NotifyTurnToMove()
{
isTurnToMove = true;
}

public void CancelTurnToMove()
{
isTurnToMove = false;
}

public void SetPosition(string fen)
{
Expand Down
21 changes: 20 additions & 1 deletion Chess-Challenge/src/Framework/Application/UI/MenuUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6003784

Please sign in to comment.