Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone, new programmer here, i am trying to make a chess game and as i am using buttons for blocks on the chess board I need to determine the pattern of movement for the piece based on the piece being shown which is its image source for background. I have no idea how would i refer to the background and make it usable in a if statement to determine which piece i am working with. It would be extremely appreciated if someone could help out. Thank you

What I have tried:

private void ChessBlock_clicked(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            var piece = btn.Background;

            if (piece == "/Assets/WhitePawn.png")
            {
              // patterns
            }
            if (piece == "/Assets/WhiteKnight.png")
            {
               // patterns
            }

        }
Posted
Updated 12-Apr-20 4:17am

1 solution

Don't.
Instead, create an enum:
C#
private enum ChessPiece
    {
    Pawn = 1,
    Rook = 2,
    Knight = 3,
    Bishop = 4,
    Queen = 5,
    King = 6,
    Black = 16,
    White = 0,
    }
and assign the piece value to the Button.Tag field.
Then you can retrieve the piece type and compare that using a switch statement or an if, or ...
C#
private void ChessBlock_clicked(object sender, RoutedEventArgs e)
    {
    if (sender is Button btn)
        {
        ChessPiece piece = (ChessPiece)btn.Tag;
        switch (piece & ~ChessPiece.Black)
            {
            case ChessPiece.Pawn:
                // ...
                break;
            case ChessPiece.Rook:
                // ...
                break;
            // ...
            }
And your code becomes easier to read, and a lot less prone to errors.
 
Share this answer
 
Comments
QuantumNova 12-Apr-20 22:08pm    
Thank you very much. This helped clear things up a lot :)
OriginalGriff 13-Apr-20 2:14am    
You're welcome!
QuantumNova 13-Apr-20 2:48am    
Hi this was going well, but after some testing i found some problems. first, would you mind explaining what i would put into the tag as i have found when i put the numbers such as 1, 2 and 3 to corralate to the appropiate pieces it tells me i cannot convert from string to enum, secondly would you mind explaining what "if (sender is Button btn)" is actually doing? and also how fitting in both "(piece & ~ChessPiece.Black)" when you are only refering to one type of piece in the cases. Sorry to trouble you and thanks so much.
OriginalGriff 13-Apr-20 2:55am    
Why are you putting strings in the Tag? just put the enum value in!
Button blackKing = new Button();
blackKing.Tag = ChessPiece.Black | ChessPiece.King;

The if checks if the sender is a Button, and if so assigns it to a Button variable called btn.

The switch value is simple: ~ChessPiece.Black inverts all the bits in the Black enum value and the binary AND removes all trace of colour from the peice (so you don't have to check for black and white separately)
QuantumNova 13-Apr-20 4:48am    
ok nice, thanks very much you've been a massive help :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900