Click here to Skip to main content
16,015,296 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys! Keep in mind that I'm still a beginner learning so I'm sorry for the long and probably annoying code.. I'm currently working on a (Reversi)game, and it works quite nicely so far (it allows the players to place their pieces on the "correct" spots). However now I just want to make the game pieces that another player takes over, to change to the player's own color. Here's what I have as my method to check if the player is making a valid move and therefore can "trap" the opponents game pieces (here is also where I'd like the opponents pieces to turn into the current players own color. I just don't know exactly where and how to put it. I would some tips and ideas on how I could think when doing this. My whole board fills up with rectangles that contain an ellipse, and this ellipse changes color with a converter. How could I in this method also change the color of the ellipses when they're trapped?

What I have tried:

<pre>public bool CapturePieces(object p)
        {
            var piece = GameBoard.First(t => t.Id == (int)p);
            PieceColor currentColor = CurrentPlayer.TypeOfColor;


            int x = piece.Coordinates.Item1;
            int y = piece.Coordinates.Item2;

            for (int dx = -1; dx <= 1; dx++)
            {
                for (int dy = -1; dy <= 1; dy++)
                {
                    if (dx == 0 && dy == 0)
                        continue;

                    if (x + dx < 0 || x + dx > 7 || y + dy < 0 || y + dy > 7)
                        continue;


                    if (GameBoard.Any(square => square.Coordinates == (x + dx, y + dy) && square.TypeOfColor != oppositeColor))
                        continue;

                    int i = 2;
                    while (i <= 7)
                    {
                        if (x + i * dx < 0 || x + i * dx > 7 || y + i * dy < 0 || y + i * dy > 7)
                            break;


                        if (GameBoard.Any(square => square.Coordinates == (x + i * dx, y + i * dy) && square.TypeOfColor == PieceColor.Empty))
                            break;


                        if (GameBoard.Any(square => square.Coordinates == (x + i * dx, y + i * dy) && piece.TypeOfColor == currentColor))
                        {
                            return true;
                        }

                        i++;
                    }

                }
            }
            return false;
        }
Posted
Updated 15-Sep-22 6:03am
v4

1 solution

Use the .Tag property of the Ellipse to identify who "owns" the Ellipse ("square"). When you "flip it", change the .Tag.

Add a DataTrigger (in the XAML) for the .Fill property of the Ellipse that flips based on .Tag.

DataTrigger Class (System.Windows) | Microsoft Docs[^]
 
Share this answer
 

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