Skip to content

Commit

Permalink
T#10 Better info if wrong move.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dumuzy committed Nov 1, 2022
1 parent b7f0a24 commit ea9d781
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 25 deletions.
2 changes: 2 additions & 0 deletions src/ChessSharp/ChessGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ public bool MakeMoveAndAnswer(Action<Square, Square> formMakeMove, Move tryMove)
return !HasMove;
}

/// <summary> Checks if the move m is the correct move for the puzzle. </summary>
/// <returns> True, if it is the correct move. False otherwise. </returns>
public bool TryMove(Move m)
{
bool isOk = IsCorrectMove(m.Source, m.Destination, m.PromoteTo);
Expand Down
68 changes: 43 additions & 25 deletions src/ChessUI/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -361,22 +370,46 @@ private bool DoesCurrentPuzzleCountAsCorrect()
return ok;
}

/// <summary> Sets the backcolor of the square to its normal value. </summary>
/// <returns> The label for the square. </returns>
Label SetNormalSquareColor(Square sq)
{
var c = (((int)(sq.File) + (int)(sq.Rank)) % 2 == 0) ? darkColor : lightColor;
return SetSquareColor(sq, c);
}

/// <returns> The label for the square. </returns>
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;
}

/// <summary> Adds an RGB amount to the color of the square. </summary>
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++)
{
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)
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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++;
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/ChessUI/Properties/Resources.Designer.cs

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

3 changes: 3 additions & 0 deletions src/ChessUI/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,7 @@
<data name="WhiteRook" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\WhiteRook.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="Wrong!" xml:space="preserve">
<value>Falsch!</value>
</data>
</root>

0 comments on commit ea9d781

Please sign in to comment.