/// <summary> /// Move the turn to next player /// </summary> public void MoveNextCommandMethod() { 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!"; } }
/// <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 } } }
/// <summary> /// Rolls the dice /// </summary> /// <returns></returns> public async void RollDice() { // if rolled score of dice is 6 and count is less than 2 keep rolling the dice if ((value == 6 && rollCount < 4) || rollCount == 0 || CanRollDice) { value = rollDice.Next(1, 7); DiceText = value.ToString(); Dice.Add(value); rollCount++; if (value == 6) { CanRollDice = true; } else { CanRollDice = false; } bool IsAllPiecesClosed = true; switch (ActivePlayerType) { case PlayerType.Red: { foreach (Piece item in RedPlayer.Pieces) { if (item.IsOpen == true) { IsAllPiecesClosed = false; break; } } } break; case PlayerType.Blue: { foreach (Piece item in BluePlayer.Pieces) { if (item.IsOpen == true) { IsAllPiecesClosed = false; break; } } } break; case PlayerType.Green: { foreach (Piece item in GreenPlayer.Pieces) { if (item.IsOpen == true) { IsAllPiecesClosed = false; break; } } } break; case PlayerType.Yellow: { foreach (Piece item in YellowPlayer.Pieces) { if (item.IsOpen == true) { IsAllPiecesClosed = false; break; } } } break; } if (!Dice.Contains(6) && IsAllPiecesClosed) { AlertText = "Next player turn"; await Task.Delay(1500); 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!"; } AlertText = ""; } } }