From ea9d781e0456c149e1aa91c5f39ca5e0e8b4e0b1 Mon Sep 17 00:00:00 2001 From: Andreas Date: Tue, 1 Nov 2022 22:34:53 +0100 Subject: [PATCH] T#10 Better info if wrong move. --- src/ChessSharp/ChessGame.cs | 2 + src/ChessUI/Form1.cs | 68 +++++++++++++------- src/ChessUI/Properties/Resources.Designer.cs | 9 +++ src/ChessUI/Properties/Resources.resx | 3 + 4 files changed, 57 insertions(+), 25 deletions(-) diff --git a/src/ChessSharp/ChessGame.cs b/src/ChessSharp/ChessGame.cs index 14f10bd..2ed94b1 100644 --- a/src/ChessSharp/ChessGame.cs +++ b/src/ChessSharp/ChessGame.cs @@ -436,6 +436,8 @@ public bool MakeMoveAndAnswer(Action formMakeMove, Move tryMove) return !HasMove; } + /// Checks if the move m is the correct move for the puzzle. + /// True, if it is the correct move. False otherwise. public bool TryMove(Move m) { bool isOk = IsCorrectMove(m.Source, m.Destination, m.PromoteTo); diff --git a/src/ChessUI/Form1.cs b/src/ChessUI/Form1.cs index d2b95ba..a780bdc 100644 --- a/src/ChessUI/Form1.cs +++ b/src/ChessUI/Form1.cs @@ -312,9 +312,16 @@ private void SquaresLabels_Click(object sender, EventArgs e) var tryMove = new Move(_selectedSourceSquare.Value, targetSquare, _gameBoard.WhoseTurn, GetPromotion(_selectedSourceSquare, targetSquare)); if (_gameBoard.TryMove(tryMove)) + { _isCurrPuzzleFinishedOk = _gameBoard.MakeMoveAndAnswer(MakeMove, tryMove); + SetInfoLabels(null); + } else + { _puzzlesWithError.TryAdd(_puzzleSet.CurrentLichessId, DateTime.Now); + SetNormalSquareColor(_selectedSourceSquare.Value); + SetInfoLabels(false); + } if (_isCurrPuzzleFinishedOk) { var currentRound = _puzzleSet.CurrentRound; @@ -346,6 +353,8 @@ void SetInfoLabels(bool? ok) lblPuzzleState.Text = ""; else if (ok == true) lblPuzzleState.Text = Res("Correct!"); + else if (ok == false) + lblPuzzleState.Text = Res("Wrong!"); } private bool DoesCurrentPuzzleCountAsCorrect() @@ -361,10 +370,37 @@ private bool DoesCurrentPuzzleCountAsCorrect() return ok; } + /// Sets the backcolor of the square to its normal value. + /// The label for the square. + Label SetNormalSquareColor(Square sq) + { + var c = (((int)(sq.File) + (int)(sq.Rank)) % 2 == 0) ? darkColor : lightColor; + return SetSquareColor(sq, c); + } + + /// The label for the square. + Label SetSquareColor(Square sq, Color c) + { + Label lbl = _squareLabels.First(m => (m.Tag as SquareTag).Square == sq); + if (c != lbl.BackColor) + lbl.BackColor = c; + return lbl; + } + + /// Adds an RGB amount to the color of the square. + void AddToSquareColor(Square sq, int r, int g, int b) + { + var lbl = _squareLabels.First(lbl => (lbl.Tag as SquareTag).Square == sq); + var c = lbl.BackColor; + var nc = Color.FromArgb(Math.Min(c.R + r, 255), Math.Min(c.G + g, 255), Math.Min(c.B + b, 255)); + lbl.BackColor = nc; + } + + static readonly Color darkColor = Color.FromArgb(181, 136, 99); + static readonly Color lightColor = Color.FromArgb(240, 217, 181); + private void DrawBoard(Player? playerInCheck = null) { - var lightColor = Color.FromArgb(240, 217, 181); - var darkColor = Color.FromArgb(181, 136, 99); for (var i = 0; i < 8; i++) { for (var j = 0; j < 8; j++) @@ -372,11 +408,8 @@ private void DrawBoard(Player? playerInCheck = null) var file = (Linie)i; var rank = (Rank)j; var square = new Square(file, rank); - Label lbl = _squareLabels.First(m => (m.Tag as SquareTag).Square == square); + Label lbl = SetNormalSquareColor(square); Piece piece = _gameBoard[file, rank]; - var newColor = ((i + j) % 2 == 0) ? darkColor : lightColor; - if (newColor != lbl.BackColor) - lbl.BackColor = newColor; if (piece == null) { if (lbl.BackgroundImage != null) @@ -399,10 +432,7 @@ private void DrawBoard(Player? playerInCheck = null) // draw special colors for last move. var last = _gameBoard.Moves.Last(); foreach (var sq in new Square[] { last.Source, last.Destination }) - { - var sqLabel = _squareLabels.First(lbl => (lbl.Tag as SquareTag).Square == sq); - AddToBackColor(sqLabel, 0, 35, 0); - } + AddToSquareColor(sq, 0, 35, 0); } if (playerInCheck != null) @@ -411,18 +441,10 @@ private void DrawBoard(Player? playerInCheck = null) Square checkedKingSquare = _gameBoard.Board.SelectMany(x => x) .Select((p, i) => new { Piece = p, Square = new Square((ChessSharp.SquareData.Linie)(i % 8), (Rank)(i / 8)) }) .First(m => m.Piece is King && m.Piece.Owner == playerInCheck).Square; - var checkLabel = _squareLabels.First(lbl => (lbl.Tag as SquareTag).Square == checkedKingSquare); - AddToBackColor(checkLabel, 20, -40, -40); + AddToSquareColor(checkedKingSquare, 20, -40, -40); } } - void AddToBackColor(System.Windows.Forms.Label lbl, int r, int g, int b) - { - var c = lbl.BackColor; - var nc = Color.FromArgb(Math.Min(c.R + r, 255), Math.Min(c.G + g, 255), Math.Min(c.B + b, 255)); - lbl.BackColor = nc; - } - private void MakeMove(Square source, Square destination) { try @@ -567,13 +589,9 @@ private void btHelp_Click(object sender, EventArgs e) if (currMove != null) { _puzzlesWithError.TryAdd(_puzzleSet.CurrentLichessId, DateTime.Now); - Label lbl = _squareLabels.First(m => (m.Tag as SquareTag).Square == currMove.Source); - lbl.BackColor = Color.Yellow; + SetSquareColor(currMove.Source, Color.Yellow); if (_helpState > 0) - { - Label lbl2 = _squareLabels.First(m => (m.Tag as SquareTag).Square == currMove.Destination); - lbl2.BackColor = Color.Yellow; - } + SetSquareColor(currMove.Destination, Color.Yellow); _helpState++; } } diff --git a/src/ChessUI/Properties/Resources.Designer.cs b/src/ChessUI/Properties/Resources.Designer.cs index d96fc9b..28bb0d0 100644 --- a/src/ChessUI/Properties/Resources.Designer.cs +++ b/src/ChessUI/Properties/Resources.Designer.cs @@ -404,5 +404,14 @@ internal static System.Drawing.Bitmap WhiteRook { return ((System.Drawing.Bitmap)(obj)); } } + + /// + /// Looks up a localized string similar to Falsch!. + /// + internal static string Wrong_ { + get { + return ResourceManager.GetString("Wrong!", resourceCulture); + } + } } } diff --git a/src/ChessUI/Properties/Resources.resx b/src/ChessUI/Properties/Resources.resx index 0fbdc6d..fc10ee0 100644 --- a/src/ChessUI/Properties/Resources.resx +++ b/src/ChessUI/Properties/Resources.resx @@ -229,4 +229,7 @@ ..\Resources\WhiteRook.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + Falsch! + \ No newline at end of file