Skip to content

Commit

Permalink
#18: Forward-Backward-Button to move through the puzzle after it's over.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dumuzy committed Feb 1, 2023
1 parent 857529e commit 9108143
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 12 deletions.
6 changes: 1 addition & 5 deletions src/ChessSharp/ChessGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,7 @@ internal bool IsTherePieceInBetween(Square square1, Square square2)

}

public ChessGame DeepClone()
{
var g = this.DeepTClone<ChessGame>();
return g;
}
public virtual ChessGame DeepClone() => this.DeepTClone<ChessGame>();

protected LichessPuzzle puzzle;
}
Expand Down
10 changes: 5 additions & 5 deletions src/ChessSharp/PuzzleGame.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ChessSharp.Pieces;
using AwiUtils;
using ChessSharp.Pieces;
using ChessSharp.SquareData;
using System;
using System.Linq;
Expand All @@ -13,9 +14,6 @@ public PuzzleGame(string fenOrLichessPuzzle) : base(fenOrLichessPuzzle)
MakeMove(CurrMove);
}

/// <summary>
///
/// </summary>
/// <param name="formMakeMove"></param>
/// <returns>true, if puzzle is finished.</returns>
public bool MakeMoveAndAnswer(Action<Square, Square> formMakeMove, Move tryMove)
Expand Down Expand Up @@ -52,10 +50,12 @@ public bool TryMove(Move m)

public int NErrors { get; private set; }

public override ChessGame DeepClone() => this.DeepTClone<PuzzleGame>();

private bool IsCorrectMove(Square from, Square to, PawnPromotion? pp) => HasMove &&
from == CurrMove.Source && to == CurrMove.Destination && pp == CurrMove.PromoteTo;

private void MakeMove(Action<Square, Square> formMakeMove, Move? tryMove)
public void MakeMove(Action<Square, Square> formMakeMove, Move tryMove = null)
{
// Lichess allows alternate solutions in mating puzzles as last move.
// Therefore, not always i CurrMove the move to make.
Expand Down
58 changes: 58 additions & 0 deletions src/ChessUI/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 38 additions & 2 deletions src/ChessUI/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public partial class Form1 : Form
Size currSize;
readonly Point boardLeftTop = new Point(10, 28);
readonly Size origSquareSize = new Size(70, 66);
Li<PuzzleGame> lastGameStates = new Li<PuzzleGame>();



Expand Down Expand Up @@ -177,7 +178,8 @@ private void ResizeForm(double faktor, bool isCalledFromEvent = false)
var ddelta = squareSize.Width - oldSquareSize.Width;
foreach (var c in new Control[] { cbFlipBoard, btLichess, btNext, lblWhoseTurn, lblPuzzleNum,
cbPuzzleSets, cbPromoteTo, lblPromoteTo, btCreatePuzleSet, lblPuzzleId, btAbout, btHelp,
cbLanguage, lblRoundText, lblRound, lblPuzzleState, tlpSetState, btDonate})
cbLanguage, lblRoundText, lblRound, lblPuzzleState, tlpSetState, btDonate,
btOnePlyBack, btOnePlyForward, btAllPliesBack, btAllPliesForward})
c.Location = AddDxDy(c.Location, (int)(9.5 * ddelta), 0);
currSize = this.Size;
shallIgnoreResizeEvent = false;
Expand Down Expand Up @@ -421,6 +423,7 @@ void SetInfoLabels(bool? ok)
lblPuzzleState.Text = Res("Correct!");
else if (ok == false)
lblPuzzleState.Text = Res("Wrong!");
btOnePlyBack.Enabled = btOnePlyForward.Enabled = btAllPliesBack.Enabled = btAllPliesForward.Enabled = ok.HasValue;

donateButton.SetState();

Expand Down Expand Up @@ -549,6 +552,7 @@ private void MakeMove(Square source, Square destination)
MessageBox.Show("Invalid Move!", "Chess", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
lastGameStates.Add(_gameBoard.DeepClone() as PuzzleGame);
_gameBoard.MakeMove(move, isMoveValidated: true);

DrawBoard(GetPlayerInCheck());
Expand All @@ -569,6 +573,7 @@ private void btNext_Click(object sender, EventArgs e)
{
_isCurrPuzzleFinishedOk = false;
_gameBoard = _puzzleSet.NextPuzzle();
lastGameStates.Clear();
if (_gameBoard != null)
{
SetSideOf();
Expand Down Expand Up @@ -649,10 +654,41 @@ private void btHelp_Click(object sender, EventArgs e)
}
private int _helpState;

private void btOnePlyBack_Click(object sender, EventArgs e)
{
if (!lastGameStates.IsEmpty)
{
var g = lastGameStates.Last();
lastGameStates.RemoveAt(lastGameStates.Count - 1);
_gameBoard = g;
DrawBoard(GetPlayerInCheck());

Player whoseTurn = _gameBoard.WhoseTurn;
lblWhoseTurn.Text = Res(whoseTurn.ToString());
}
}

private void btOnePlyForward_Click(object sender, EventArgs e)
{
if (_gameBoard.HasMove)
_gameBoard.MakeMove(this.MakeMove);
}

private void btAllPliesBack_Click(object sender, EventArgs e)
{
while (!lastGameStates.IsEmpty)
btOnePlyBack_Click(sender, e);
}

private void btAllPliesForward_Click(object sender, EventArgs e)
{
while (_gameBoard.HasMove)
_gameBoard.MakeMove(this.MakeMove);
}

protected override void OnResizeEnd(EventArgs e)
{
iniFile.WriteWindowSizePercent();
// TODO: All positions of all labels and stuff should be corrected at latest here.
}

protected override void OnResize(EventArgs e)
Expand Down

0 comments on commit 9108143

Please sign in to comment.