Click here to Skip to main content
15,894,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i am extremely confused on this issue. below is my code, there are two switch statements that activate depending on the set tags for buttons. However when pressing a button that has the tags of "white" and "pawn", the case for "black" and "pawn" will also activate on top of the intended case for some unknown reason. But when pressing the black pawn button the reverse does not occur. Am i missing something?



For context: Chess game. 64 buttons in chess grid, counting left to right, bottom to top e.g) bottom left corner is Block1, Bottom right is Block8, Top Left Block 57. IsEnable is set to false to indicate to me where pieces can go at the moment. when pressing a white pawn the tile above and below both become disabled. when pressing black pawn only the below tile becomes disabled

What I have tried:

private enum ChessPiece
        {
            Pawn = 1,
            Rook = 2,
            Knight = 3,
            Bishop = 4,
            Queen = 5,
            King = 6,
            Black = 16,
            White = 0,
        }

        public void Setpieces()
        {
            Block16.Tag = ChessPiece.White | ChessPiece.Pawn;
            Block15.Tag = ChessPiece.White | ChessPiece.Pawn;
            Block14.Tag = ChessPiece.White | ChessPiece.Pawn;
            Block13.Tag = ChessPiece.White | ChessPiece.Pawn;
            Block12.Tag = ChessPiece.White | ChessPiece.Pawn;
            Block11.Tag = ChessPiece.White | ChessPiece.Pawn;
            Block10.Tag = ChessPiece.White | ChessPiece.Pawn;
            Block9.Tag = ChessPiece.White | ChessPiece.Pawn;

            Block1.Tag = ChessPiece.White | ChessPiece.Rook;
            Block2.Tag = ChessPiece.White | ChessPiece.Knight;
            Block3.Tag = ChessPiece.White | ChessPiece.Bishop;
            Block4.Tag = ChessPiece.White | ChessPiece.Queen;
            Block5.Tag = ChessPiece.White | ChessPiece.King;
            Block6.Tag = ChessPiece.White | ChessPiece.Bishop;
            Block7.Tag = ChessPiece.White | ChessPiece.Knight;
            Block8.Tag = ChessPiece.White | ChessPiece.Rook;

            Block49.Tag = ChessPiece.Black | ChessPiece.Pawn;
            Block50.Tag = ChessPiece.Black | ChessPiece.Pawn;
            Block51.Tag = ChessPiece.Black | ChessPiece.Pawn;
            Block52.Tag = ChessPiece.Black | ChessPiece.Pawn;
            Block53.Tag = ChessPiece.Black | ChessPiece.Pawn;
            Block54.Tag = ChessPiece.Black | ChessPiece.Pawn;
            Block55.Tag = ChessPiece.Black | ChessPiece.Pawn;
            Block56.Tag = ChessPiece.Black | ChessPiece.Pawn;

            Block57.Tag = ChessPiece.Black | ChessPiece.Rook;
            Block58.Tag = ChessPiece.Black | ChessPiece.Knight;
            Block59.Tag = ChessPiece.Black | ChessPiece.Bishop;
            Block60.Tag = ChessPiece.Black | ChessPiece.Queen;
            Block61.Tag = ChessPiece.Black | ChessPiece.King;
            Block62.Tag = ChessPiece.Black | ChessPiece.Bishop;
            Block63.Tag = ChessPiece.Black | ChessPiece.Knight;
            Block64.Tag = ChessPiece.Black | ChessPiece.Rook;

        }


        private void ChessBlock_clicked(object sender, RoutedEventArgs e)
        {
            List<Button> buttons = new List<Button>
            {
                    Block1, Block2, Block3, Block4, Block5, Block6, Block7, Block8, Block9, Block10,
                    Block11, Block12, Block13, Block14, Block15, Block16, Block17, Block18, Block19, Block20,
                    Block21, Block22, Block23, Block24, Block25, Block26, Block27, Block28, Block29, Block30,
                    Block31, Block32, Block33, Block34, Block35, Block36, Block37, Block38, Block39, Block40,
                    Block41, Block42, Block43, Block44, Block45, Block46, Block47, Block48, Block49, Block50,
                    Block51, Block52, Block53, Block54, Block55, Block56, Block57, Block58, Block59, Block60,
                    Block61, Block62, Block63, Block64
            };

            if (sender is Button btn)
            {

                ChessPiece piece = (ChessPiece)btn.Tag;

                switch (piece & ~ChessPiece.White)
                {
                    case ChessPiece.Pawn:
                        textBox3.Text = "W pawn";
                        int index = buttons.IndexOf(btn);
                        Button destination = buttons[(index + 8) % buttons.Count];
                        destination.Tag += "active";
                        destination.IsEnabled = false;
                        break;

                    // cases for other white pieces
                }

                switch (piece & ~ChessPiece.Black)
                {
                    case ChessPiece.Pawn:
                        textBox3.Text = "B Pawn";
                        int index = buttons.IndexOf(btn);
                        Button destination = buttons[(index - 8) % buttons.Count];
                        destination.Tag += "active";
                        destination.IsEnabled = false;

                        break;

                    // cases for other black pieces
                }
            }

        }
Posted
Updated 20-Apr-20 20:50pm
Comments
Richard MacCutchan 21-Apr-20 4:42am    
You could get rid of all those fixed labels by using a 2D array for the board.

1 solution

You are mixing bit flags with normal enums. Also conceptually you are mixing pieces with colors. To make your case work you need to extract piece and color separately from the tag:
var tag = (ChessPiece)btn.Tag;
var piece =(tag & (ChessPiece)7); // 7 is fake enum value to mask the lower 3 bits
var color = (tag & ChessPiece.Black); // this will translate to proper enum values as White=0

But this is all kind of hacky - you are exploiting C# benevolent treatment of enums. I would suggest you split the information into two parts (piece & color) and have a object with two properties to represent it. I assume you can store any object in Tag.
 
Share this answer
 
v2

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