From 9108143665af75c2960cbf9ab09e03cbd0184212 Mon Sep 17 00:00:00 2001 From: Andreas Date: Wed, 1 Feb 2023 23:42:43 +0100 Subject: [PATCH] #18: Forward-Backward-Button to move through the puzzle after it's over. --- src/ChessSharp/ChessGame.cs | 6 +--- src/ChessSharp/PuzzleGame.cs | 10 +++--- src/ChessUI/Form1.Designer.cs | 58 +++++++++++++++++++++++++++++++++++ src/ChessUI/Form1.cs | 40 ++++++++++++++++++++++-- 4 files changed, 102 insertions(+), 12 deletions(-) diff --git a/src/ChessSharp/ChessGame.cs b/src/ChessSharp/ChessGame.cs index dd65176..8410ad4 100644 --- a/src/ChessSharp/ChessGame.cs +++ b/src/ChessSharp/ChessGame.cs @@ -371,11 +371,7 @@ internal bool IsTherePieceInBetween(Square square1, Square square2) } - public ChessGame DeepClone() - { - var g = this.DeepTClone(); - return g; - } + public virtual ChessGame DeepClone() => this.DeepTClone(); protected LichessPuzzle puzzle; } diff --git a/src/ChessSharp/PuzzleGame.cs b/src/ChessSharp/PuzzleGame.cs index 75eb9a2..4147c6a 100644 --- a/src/ChessSharp/PuzzleGame.cs +++ b/src/ChessSharp/PuzzleGame.cs @@ -1,4 +1,5 @@ -using ChessSharp.Pieces; +using AwiUtils; +using ChessSharp.Pieces; using ChessSharp.SquareData; using System; using System.Linq; @@ -13,9 +14,6 @@ public PuzzleGame(string fenOrLichessPuzzle) : base(fenOrLichessPuzzle) MakeMove(CurrMove); } - /// - /// - /// /// /// true, if puzzle is finished. public bool MakeMoveAndAnswer(Action formMakeMove, Move tryMove) @@ -52,10 +50,12 @@ public bool TryMove(Move m) public int NErrors { get; private set; } + public override ChessGame DeepClone() => this.DeepTClone(); + private bool IsCorrectMove(Square from, Square to, PawnPromotion? pp) => HasMove && from == CurrMove.Source && to == CurrMove.Destination && pp == CurrMove.PromoteTo; - private void MakeMove(Action formMakeMove, Move? tryMove) + public void MakeMove(Action formMakeMove, Move tryMove = null) { // Lichess allows alternate solutions in mating puzzles as last move. // Therefore, not always i CurrMove the move to make. diff --git a/src/ChessUI/Form1.Designer.cs b/src/ChessUI/Form1.Designer.cs index 395e486..6a674bf 100644 --- a/src/ChessUI/Form1.Designer.cs +++ b/src/ChessUI/Form1.Designer.cs @@ -129,6 +129,10 @@ private void InitializeComponent() this.lblPuzzlesUntried = new System.Windows.Forms.Label(); this.lblPuzzlesWithError = new System.Windows.Forms.Label(); this.btDonate = new System.Windows.Forms.Button(); + this.btOnePlyForward = new System.Windows.Forms.Button(); + this.btOnePlyBack = new System.Windows.Forms.Button(); + this.btAllPliesBack = new System.Windows.Forms.Button(); + this.btAllPliesForward = new System.Windows.Forms.Button(); this.tlpSetState.SuspendLayout(); this.SuspendLayout(); // @@ -1158,11 +1162,61 @@ private void InitializeComponent() this.btDonate.UseVisualStyleBackColor = true; this.btDonate.Click += new System.EventHandler(this.btDonate_Click); // + // btOnePlyForward + // + this.btOnePlyForward.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.btOnePlyForward.Location = new System.Drawing.Point(719, 399); + this.btOnePlyForward.Name = "btOnePlyForward"; + this.btOnePlyForward.Size = new System.Drawing.Size(32, 30); + this.btOnePlyForward.TabIndex = 105; + this.btOnePlyForward.Text = ">"; + this.btOnePlyForward.UseVisualStyleBackColor = true; + this.btOnePlyForward.Click += new System.EventHandler(this.btOnePlyForward_Click); + // + // btOnePlyBack + // + this.btOnePlyBack.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.btOnePlyBack.Location = new System.Drawing.Point(682, 399); + this.btOnePlyBack.Name = "btOnePlyBack"; + this.btOnePlyBack.Size = new System.Drawing.Size(32, 30); + this.btOnePlyBack.TabIndex = 104; + this.btOnePlyBack.Text = "<"; + this.btOnePlyBack.UseVisualStyleBackColor = true; + this.btOnePlyBack.Click += new System.EventHandler(this.btOnePlyBack_Click); + // + // btAllPliesBack + // + this.btAllPliesBack.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.btAllPliesBack.Location = new System.Drawing.Point(642, 399); + this.btAllPliesBack.Margin = new System.Windows.Forms.Padding(0, 3, 0, 3); + this.btAllPliesBack.Name = "btAllPliesBack"; + this.btAllPliesBack.Size = new System.Drawing.Size(40, 30); + this.btAllPliesBack.TabIndex = 106; + this.btAllPliesBack.Text = "<<"; + this.btAllPliesBack.UseVisualStyleBackColor = true; + this.btAllPliesBack.Click += new System.EventHandler(this.btAllPliesBack_Click); + // + // btAllPliesForward + // + this.btAllPliesForward.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.btAllPliesForward.Location = new System.Drawing.Point(751, 399); + this.btAllPliesForward.Margin = new System.Windows.Forms.Padding(1, 3, 1, 3); + this.btAllPliesForward.Name = "btAllPliesForward"; + this.btAllPliesForward.Size = new System.Drawing.Size(40, 30); + this.btAllPliesForward.TabIndex = 107; + this.btAllPliesForward.Text = ">>"; + this.btAllPliesForward.UseVisualStyleBackColor = true; + this.btAllPliesForward.Click += new System.EventHandler(this.btAllPliesForward_Click); + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(869, 622); + this.Controls.Add(this.btAllPliesForward); + this.Controls.Add(this.btAllPliesBack); + this.Controls.Add(this.btOnePlyForward); + this.Controls.Add(this.btOnePlyBack); this.Controls.Add(this.btDonate); this.Controls.Add(this.tlpSetState); this.Controls.Add(this.btHelp); @@ -1373,6 +1427,10 @@ private void InitializeComponent() private System.Windows.Forms.Label lblPuzzlesWithError; private System.Windows.Forms.Label lblPuzzlesUntried; private System.Windows.Forms.Button btDonate; + private System.Windows.Forms.Button btOnePlyForward; + private System.Windows.Forms.Button btOnePlyBack; + private System.Windows.Forms.Button btAllPliesBack; + private System.Windows.Forms.Button btAllPliesForward; } } diff --git a/src/ChessUI/Form1.cs b/src/ChessUI/Form1.cs index 5bb5261..cd84081 100644 --- a/src/ChessUI/Form1.cs +++ b/src/ChessUI/Form1.cs @@ -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 lastGameStates = new Li(); @@ -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; @@ -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(); @@ -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()); @@ -569,6 +573,7 @@ private void btNext_Click(object sender, EventArgs e) { _isCurrPuzzleFinishedOk = false; _gameBoard = _puzzleSet.NextPuzzle(); + lastGameStates.Clear(); if (_gameBoard != null) { SetSideOf(); @@ -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)