/// <summary> /// Executes when a piece popup is clicked /// </summary> /// <param name="sender"></param> public async void Piece_Click(object sender) { var values = sender as object[]; Button btn = values[0] as Button; Piece piece = values[1] as Piece; if (ActivePlayerType > NumberOfPlayer) { return; } if (ActivePlayerType != piece.Type || piece.IsHome) { return; } //pi is Position To Increment //cp is Current Position Index //np is Next Position var pi = Convert.ToInt32(btn.Content); var cp = piece.CurrentPositionIndex; var np = pi + cp; var NewPosition = new Position(); //if piece is closed and player wants to open it if (!piece.IsOpen && pi == 6) { NewPosition = _Positions[piece.StartIndex]; Grid.SetColumn(piece, NewPosition.Column); Grid.SetRow(piece, NewPosition.Row); Dice.Remove(pi); piece.IsOpen = true; //index of the current piece in the grid int row = _Positions[piece.CurrentPositionIndex].Row; int column = _Positions[piece.CurrentPositionIndex].Column; //to get a list containing all pieces in a cell of a grid by index var list = grid.Children .Cast <Piece>() .Where(e => Grid.GetRow(e) == row && Grid.GetColumn(e) == column) .Select(e => e).ToList(); //to decrease the size of the piece if there are multiple pieces in the current cell if (list.Count > 1) { piece.Width = list[list.Count - 1].ActualWidth / 1.4; piece.Height = list[list.Count - 1].ActualHeight / 1.4; Panel.SetZIndex(piece, Panel.GetZIndex(list[list.Count - 1]) + 1); } //else make it fit the cell else { piece.Width = grid.Width / 15; piece.Height = grid.Height / 15; //Panel.SetZIndex(piece, 2); } } else if (piece.IsOpen) { switch (piece.Type) { case PlayerType.Red: { #region Calculate the next position of the piece //allow piece to only enter in its own(color) home column if ((cp < 13 && (cp + pi) >= 13) || (cp < 32 && (cp + pi) >= 32) || (cp < 51 && (cp + pi) >= 51)) { np += 6; } //if its in home column or about to enter in it if ((cp < 71 && (cp + pi) >= 71) || (cp > 70 && cp < 76)) { //if piece is home if (np == 76) { NewPosition = _HomePosition[piece.HomeIndex]; Grid.SetColumn(piece, NewPosition.Column); Grid.SetRow(piece, NewPosition.Row); piece.IsOpen = false; piece.IsHome = true; piece.Style = Application.Current.FindResource("DisablePieceButton") as Style; //arbitrary value to check if piece has already moved np = -2; Player currentPlayer = Players.Select(s => s.PlayerType == ActivePlayerType) as Player; bool IsPlayerWon = true; foreach (Piece item in currentPlayer.Pieces) { if (item.IsHome == false) { IsPlayerWon = false; break; } } if (IsPlayerWon) { MessageBox.Show($"Congratulations you have Won the game!"); (Application.Current.MainWindow.DataContext as WindowViewModel).CurrentPage = new StartUpMenu(); } } //if piece can move inside home column else if (np < 76) { piece.CurrentPositionIndex = np; } //if piece can't move else { //arbitrary value to check if piece can move np = -1; } } else //to reset the position if its at _Position's list last index if (np < _Positions.Count) { piece.CurrentPositionIndex = np; } //normal position where reset is not required else { piece.CurrentPositionIndex = np - _Positions.Count; } #endregion MovePiece(piece, btn, np, pi); } break; case PlayerType.Blue: { #region Calculate the next position of the piece //allow piece to only enter in its own(color) home column if ((cp < 32 && (cp + pi) >= 32) || (cp < 51 && (cp + pi) >= 51) || (cp < 71 && (cp + pi) >= 71)) { np += 6; } //if its in home column or about to enter in it if ((cp < 13 && (cp + pi) >= 13) || (cp > 13 && cp < 18)) { //if the piece is home if (np == 18) { NewPosition = _HomePosition[piece.HomeIndex]; Grid.SetColumn(piece, NewPosition.Column); Grid.SetRow(piece, NewPosition.Row); piece.IsOpen = false; piece.IsHome = true; piece.Background = Brushes.Black; //arbitrary value to check if piece has already moved np = -1; } //if piece can move inside home column else if (np < 18) { piece.CurrentPositionIndex = np; } //if piece can't move else { //arbitrary value to check if piece can move np = -1; } } else //to reset the position if its at _Position's list last index if (np < _Positions.Count) { piece.CurrentPositionIndex = np; } //normal position where reset is not required else { piece.CurrentPositionIndex = np - _Positions.Count; } #endregion MovePiece(piece, btn, np, pi); } break; case PlayerType.Green: { #region Calculate the next position of the piece //allow piece to only enter in its own(color) home column if ((cp < 13 && (cp + pi) >= 13) || (cp < 71 && (cp + pi) >= 71) || (cp < 51 && (cp + pi) >= 51)) { np += 6; } //if its in home column or about to enter in it if ((cp < 32 && (cp + pi) >= 32) || (cp > 32 && cp < 37)) { //if the piece is home if (np == 37) { NewPosition = _HomePosition[piece.HomeIndex]; Grid.SetColumn(piece, NewPosition.Column); Grid.SetRow(piece, NewPosition.Row); piece.IsOpen = false; piece.IsHome = true; piece.Background = Brushes.Black; //arbitrary value to check if piece has already moved np = -1; } //if piece can move inside home column else if (np < 37) { piece.CurrentPositionIndex = np; } //if piece can't move else { //arbitrary value to check if piece can move np = -1; } } else //to reset the position if its at _Position's list last index if (np < _Positions.Count) { piece.CurrentPositionIndex = np; } //normal position where reset is not required else { piece.CurrentPositionIndex = np - _Positions.Count; } #endregion MovePiece(piece, btn, np, pi); } break; case PlayerType.Yellow: { #region Calculate the next position of the piece //allow piece to only enter in its own(color) home column if ((cp < 13 && (cp + pi) >= 13) || (cp < 32 && (cp + pi) >= 32) || (cp < 70 && (cp + pi) >= 70)) { np += 6; } //if its in home column or about to enter in it if ((cp < 51 && (cp + pi) >= 51) || (cp > 51 && cp < 56)) { //if the piece is home if (np == 56) { NewPosition = _HomePosition[piece.HomeIndex]; Grid.SetColumn(piece, NewPosition.Column); Grid.SetRow(piece, NewPosition.Row); piece.IsOpen = false; piece.IsHome = true; piece.Background = Brushes.Black; //arbitrary value to check if piece has already moved np = -1; } //if piece can move inside home column else if (np < 56) { piece.CurrentPositionIndex = np; } //if piece can't move else { //arbitrary value to check if piece can move np = -1; } } else //to reset the position if its at _Position's list last index if (np < _Positions.Count) { piece.CurrentPositionIndex = np; } //normal position where reset is not required else { piece.CurrentPositionIndex = np - _Positions.Count; } #endregion MovePiece(piece, btn, np, pi); } break; } } //if piece is closed else { if (Convert.ToInt32(btn.Content) == 6) { piece.IsOpen = true; var i = piece.CurrentPositionIndex; Grid.SetColumn(piece, _Positions[i].Column); Grid.SetRow(piece, _Positions[i].Row); Dice.Remove(6); } else { AlertText = "Piece is closed"; await Task.Delay(2000); AlertText = ""; } } }
/// <summary> /// To Move the piece /// </summary> /// <param name="sender"></param> public async void MovePiece(Piece piece, Button btn, int np, int pi) { bool IsBeatPiece = false; { if (rollCount > 0 && Dice.Count != 0) { #region To handle beating of the piece if (np != -1) { //index of the current piece in the grid int row = _Positions[piece.CurrentPositionIndex].Row; int column = _Positions[piece.CurrentPositionIndex].Column; //to get a list containing all pieces in a cell of a grid by index var list = grid.Children .Cast <Piece>() .Where(e => Grid.GetRow(e) == row && Grid.GetColumn(e) == column) .Select(e => e).ToList(); //if there is any piece at current position beat it if (list.Count == 1) { //if the current posiion is a safe position don't beat bool IsPositionSafe = false; foreach (Position item in _SafePositions) { if (item.Column == (_Positions[piece.CurrentPositionIndex]).Column && item.Row == (_Positions[piece.CurrentPositionIndex]).Row) { IsPositionSafe = true; break; } } if (!IsPositionSafe) { if (list[0].Type != piece.Type) { //close the beaten piece and move to home var NewPosition = _HomePosition[list[0].HomeIndex]; Grid.SetColumn(list[0], NewPosition.Column); Grid.SetRow(list[0], NewPosition.Row); list[0].IsOpen = false; //Panel.SetZIndex(piece, 1); //to give player a extra dice roll for beating a piece CanRollDice = true; //make the piece size fit the cell piece.Width = grid.Width / 15; IsBeatPiece = true; } } } else //check if the cell has all simillar pieces(in that case we can't move our piece to the position) if (list.Count > 1) { if (list[0].Type != ActivePlayerType) { np = -1; } } //to decrease the size of the piece if there are multiple pieces in the current cell if (list.Count > 0 && !IsBeatPiece) { piece.Width = list[list.Count - 1].ActualWidth / 1.4; piece.Height = list[list.Count - 1].ActualHeight / 1.4; Panel.SetZIndex(piece, Panel.GetZIndex(list[list.Count - 1]) + 1); } //else make it fit the cell else { piece.Width = grid.Width / 15; piece.Height = grid.Height / 15; //Panel.SetZIndex(piece, 1); } } #endregion #region To move the piece //move the piece if (np != -1) { var NewPosition = _Positions[piece.CurrentPositionIndex]; Grid.SetColumn(piece, NewPosition.Column); Grid.SetRow(piece, NewPosition.Row); Dice.Remove(pi); } //show the alert if it can't be moved else if (np == -2) { AlertText = "Congratulations Your Piece is Home!"; await Task.Delay(1000); AlertText = ""; } else { AlertText = "Can not move the piece"; await Task.Delay(1000); AlertText = ""; } #endregion #region To alert if a piece is beaten //if piece is beaten show alert if (IsBeatPiece) { AlertText = "Congrats! You have beaten a piece :)"; await Task.Delay(3000); AlertText = ""; IsBeatPiece = false; } #endregion #region Transfer turn to next player if (Dice.Count == 0 && !CanRollDice) { if (ActivePlayerType < NumberOfPlayer) { ActivePlayerType++; rollCount = 0; Dice.Clear(); PlayerTurn = ActivePlayerType.ToString(); DiceText = "Roll Dice!"; } else { ActivePlayerType = 0; rollCount = 0; Dice.Clear(); PlayerTurn = ActivePlayerType.ToString(); DiceText = "Roll Dice!"; } } #endregion } } }