Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -75,6 +76,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 @@ -167,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
{
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