C
C#2mo ago
olleeee

Binding problems, property not updating

Hi, im currently creating a snakes and ladders game where the user get to roll a dice and then move a gamepiece by it self on a board with numbered squares. The other player is a CPU and rolls the dice and moves automatically when its the CPUs turn. When landing on a ladder the player goes up to a new square, and on an snake it goes down. But i also want to implement a certain feature where if you land on a certain tile in the gameboard you swap positions with the other player. This is now only working for the cpu, and the cpu lands on the other humanplayer but the humanplayer still stands on the same tile and doesnt automatically goes to the cpus tile. I feel like it is a binding problem but don't really understand it since it works for the CPU and have implemented the same sort of code. Please Help!!
3 Replies
olleeee
olleeee2mo ago
BlazeBin - tadazmswsqrf
A tool for sharing your source code with the world!
Bailey
Bailey2mo ago
Hi, The following can help solving the issue. Put some breakpoints in the swap and check the values. of which swaps are done and if the right objects are selected.
The Father
The Father2mo ago
i'd presume it's because you set the cpu's position before the player's position, and used the cpu's current position and the player's new position, rather than the cpu's old position though i don't know where the logic for that is in your code The logic is here, right?
public void PerformSwap(PlayerViewModel currentPlayer, Canvas canvas)
{
// Hitta en annan spelare att byta plats med
var otherPlayer = Players.FirstOrDefault(p => p != currentPlayer && p.Position > 0 && p.Position <= 100);
int otherPLayerposition = otherPlayer.Position; // det har lades till
int currentplayerposition = currentPlayer.Position; // det har lades till

if (otherPlayer != null)
{
// Byt plats på de två spelarna
Square currentPlayerSquare = Player1.Board.First(s => s.Number == currentPlayer.Position);
Square otherPlayerSquare = Player1.Board.First(s => s.Number == otherPlayer.Position);

currentPlayer.Position = otherPLayerposition; // och detta
otherPlayer.Position = currentplayerposition; // och detta!

MovePieceToCanvasPosition(currentPlayer.GamePiece, otherPlayerSquare, canvas);
MovePieceToCanvasPosition(otherPlayer.GamePiece, currentPlayerSquare, canvas);
}


}
public void PerformSwap(PlayerViewModel currentPlayer, Canvas canvas)
{
// Hitta en annan spelare att byta plats med
var otherPlayer = Players.FirstOrDefault(p => p != currentPlayer && p.Position > 0 && p.Position <= 100);
int otherPLayerposition = otherPlayer.Position; // det har lades till
int currentplayerposition = currentPlayer.Position; // det har lades till

if (otherPlayer != null)
{
// Byt plats på de två spelarna
Square currentPlayerSquare = Player1.Board.First(s => s.Number == currentPlayer.Position);
Square otherPlayerSquare = Player1.Board.First(s => s.Number == otherPlayer.Position);

currentPlayer.Position = otherPLayerposition; // och detta
otherPlayer.Position = currentplayerposition; // och detta!

MovePieceToCanvasPosition(currentPlayer.GamePiece, otherPlayerSquare, canvas);
MovePieceToCanvasPosition(otherPlayer.GamePiece, currentPlayerSquare, canvas);
}


}
Seems like you accidentally set both to the human player, when the first should be Player2
Square currentPlayerSquare = [HERE] Player1 [HERE].Board.First(s => s.Number == currentPlayer.Position);
Square otherPlayerSquare = [HERE] Player1 [HERE].Board.First(s => s.Number == otherPlayer.Position);
Square currentPlayerSquare = [HERE] Player1 [HERE].Board.First(s => s.Number == currentPlayer.Position);
Square otherPlayerSquare = [HERE] Player1 [HERE].Board.First(s => s.Number == otherPlayer.Position);
Want results from more Discord servers?
Add your server