public static bool TakePiece(int p, Pieces ps, List <Pieces> boardPieces) { bool res = false; Pieces pt = (from b in boardPieces where b.Position.Equals(p) && b.IsActive select b).SingleOrDefault(); if (pt.Color != ps.Color) { int newP = 0; RowColl rc = new RowColl(p); if (ps.Color == ColorPeace.black) { if (((p - ps.Position) == 7 && rc.col > 1) || ((ps.Position - p) == 9 && rc.col < 6)) { newP = p + (p - ps.Position); res = PlayMove.IsEmptyPlace(newP, boardPieces); } } else { if (((ps.Position - p) == 9 && rc.col > 1) || ((p - ps.Position) == 7 && rc.col < 6)) { newP = p - (ps.Position - p); res = PlayMove.IsEmptyPlace(newP, boardPieces); } } res = true; } return(res); }
/// <summary> /// Inicia la Manipulacion de la ficha, determinando si puede o no mover. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Ellipse_ManipulationStarted(object sender, System.Windows.Input.ManipulationStartedEventArgs e) { this.ellipse = sender as Ellipse; Pieces p = (from b in boardPieces where b.Name.Equals(this.ellipse.GetValue(NameProperty)) select b).Single(); if (!this.turn && p.Color == ColorPeace.white || this.turn && p.Color == ColorPeace.black) { this.txt.Text = "No es tu turno."; this.canMove = false; } else { this.txt.Text = ""; this.canMove = PlayMove.CanMove(p, boardPieces); if (!this.canMove) { this.txt.Text = "La ficha seleccionada no se puede mover."; } } }
public static bool CanMove(Pieces p, List <Pieces> boardPieces) { if (!p.IsActive) { return(false); } bool res = false; Pieces pc = null; RowColl rc = new RowColl(p.Position); if (p.Color == ColorPeace.black) { #region BlackPieces if (rc.row < 8) { switch (rc.col) { case 0: rc.col++; pc = (from pos in boardPieces where pos.Position.Equals(rc.col + ((rc.row + 1) * 8)) && pos.IsActive select pos).FirstOrDefault(); if (pc != null) { res = false; if (pc.Color == ColorPeace.white && PlayMove.IsEmptyPlace(p.Position + 18, boardPieces)) { res = true; } } else { res = true; } break; case 7: rc.col--; pc = (from pos in boardPieces where pos.Position.Equals(rc.col + ((rc.row + 1) * 8)) && pos.IsActive select pos).FirstOrDefault(); if (pc != null) { res = false; if (pc.Color == ColorPeace.white && PlayMove.IsEmptyPlace(p.Position + 14, boardPieces)) { res = true; } } else { res = true; } break; default: pc = (from pos in boardPieces where pos.Position.Equals((rc.col + 1) + ((rc.row + 1) * 8)) && pos.IsActive select pos).FirstOrDefault(); if (pc != null) { res = false; if (pc.Color == ColorPeace.white && PlayMove.IsEmptyPlace(p.Position + 18, boardPieces)) { res = true; } } else { res = true; } if (res) { break; } pc = (from pos in boardPieces where pos.Position.Equals((rc.col - 1) + ((rc.row + 1) * 8)) && pos.IsActive select pos).FirstOrDefault(); if (pc != null) { res = false; if (pc.Color == ColorPeace.white && PlayMove.IsEmptyPlace(p.Position + 14, boardPieces)) { res = true; } } else { res = true; } break; } } #endregion } else { #region WhitePieces if (rc.row > 0) { switch (rc.col) { case 0: rc.col++; pc = (from pos in boardPieces where pos.Position.Equals(rc.col + ((rc.row - 1) * 8)) && pos.IsActive select pos).FirstOrDefault(); if (pc != null) { res = false; if (pc.Color == ColorPeace.black && PlayMove.IsEmptyPlace(p.Position - 14, boardPieces)) { res = true; } } else { res = true; } break; case 7: rc.col--; pc = (from pos in boardPieces where pos.Position.Equals(rc.col + ((rc.row - 1) * 8)) && pos.IsActive select pos).FirstOrDefault(); if (pc != null) { res = false; if (pc.Color == ColorPeace.black && PlayMove.IsEmptyPlace(p.Position - 18, boardPieces)) { res = true; } } else { res = true; } break; default: pc = (from pos in boardPieces where pos.Position.Equals((rc.col + 1) + ((rc.row - 1) * 8)) && pos.IsActive select pos).FirstOrDefault(); if (pc != null) { res = false; if (pc.Color == ColorPeace.black && PlayMove.IsEmptyPlace(p.Position - 14, boardPieces)) { res = true; } } else { res = true; } if (res) { break; } pc = (from pos in boardPieces where pos.Position.Equals((rc.col - 1) + ((rc.row - 1) * 8)) && pos.IsActive select pos).FirstOrDefault(); if (pc != null) { res = false; if (pc.Color == ColorPeace.black && PlayMove.IsEmptyPlace(p.Position - 18, boardPieces)) { res = true; } } else { res = true; } break; } } #endregion } return(res); }
/// <summary> /// Se termina la manipuralción de la ficha y determina si puede o no ubicarse en el lugar /// indicado, de lo contrario vuelve a su posición inicial. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Ellipse_ManipulationCompleted(object sender, System.Windows.Input.ManipulationCompletedEventArgs e) { if (this.canMove) { Pieces p = (from b in boardPieces where b.Name.Equals(this.ellipse.GetValue(NameProperty)) select b).Single(); RowColl initialPosition = new RowColl(p.Position); RowColl finalPosition = new RowColl(p.Position); CompositeTransform cp = this.ellipse.RenderTransform as CompositeTransform; #region Set_Move if (p.Color == ColorPeace.black) { if (e.TotalManipulation.Translation.Y > 0) { //Evaluamos si debemos sumar o restar una columna if (e.TotalManipulation.Translation.X > 0) { if (e.TotalManipulation.Translation.Y > 40.5 && e.TotalManipulation.Translation.X > 40.5) { finalPosition.col = initialPosition.col + 1; finalPosition.row = initialPosition.row + 1; } } else { if (e.TotalManipulation.Translation.Y > 40.5 && e.TotalManipulation.Translation.X < -40.5) { finalPosition.col = initialPosition.col - 1; finalPosition.row = initialPosition.row + 1; } } } } else { if (e.TotalManipulation.Translation.Y < 0) { //Evaluamos si debemos sumar o restar una columna if (e.TotalManipulation.Translation.X > 0) { if (e.TotalManipulation.Translation.Y < -40.5 && e.TotalManipulation.Translation.X > 40.5) { finalPosition.col = initialPosition.col + 1; finalPosition.row = initialPosition.row - 1; } } else { if (e.TotalManipulation.Translation.Y < -40.5 && e.TotalManipulation.Translation.X < -40.5) { finalPosition.col = initialPosition.col - 1; finalPosition.row = initialPosition.row - 1; } } } } #endregion #region Movement if (PlayMove.IsEmptyPlace(finalPosition.GetPosition(), boardPieces)) { cp.TranslateX -= e.TotalManipulation.Translation.X; cp.TranslateY -= e.TotalManipulation.Translation.Y; this.ellipse.SetValue(Grid.ColumnProperty, finalPosition.col); this.ellipse.SetValue(Grid.RowProperty, finalPosition.row); (boardPieces.Where(piece => piece.Name.Equals(this.ellipse.GetValue(NameProperty))).ToList <Pieces>()).ForEach(pi => pi.Position = finalPosition.GetPosition()); this.turn = !this.turn; } else { if (PlayMove.TakePiece(finalPosition.GetPosition(), p, boardPieces)) { Pieces tp = (from t in boardPieces where t.Position.Equals(finalPosition.GetPosition()) && t.IsActive select t).Single(); bool takedPiece = false; #region Take_Piece if (p.Color == ColorPeace.black) { if (e.TotalManipulation.Translation.Y > 0) { //Evaluamos si debemos sumar o restar una columna if (e.TotalManipulation.Translation.X > 0) { if (e.TotalManipulation.Translation.Y > 94.5 && e.TotalManipulation.Translation.X > 94.5) { finalPosition.col = initialPosition.col + 2; finalPosition.row = initialPosition.row + 2; takedPiece = true; } } else { if (e.TotalManipulation.Translation.Y > 94.5 && e.TotalManipulation.Translation.X < -94.5) { finalPosition.col = initialPosition.col - 2; finalPosition.row = initialPosition.row + 2; takedPiece = true; } } } } else { if (e.TotalManipulation.Translation.Y < 0) { //Evaluamos si debemos sumar o restar una columna if (e.TotalManipulation.Translation.X > 0) { if (e.TotalManipulation.Translation.Y < -94.5 && e.TotalManipulation.Translation.X > 94.5) { finalPosition.col = initialPosition.col + 2; finalPosition.row = initialPosition.row - 2; takedPiece = true; } } else { if (e.TotalManipulation.Translation.Y < -94.5 && e.TotalManipulation.Translation.X < -94.5) { finalPosition.col = initialPosition.col - 2; finalPosition.row = initialPosition.row - 2; takedPiece = true; } } } } #endregion if (takedPiece) { foreach (var item in Board.Children) { // Solo nos interesan los objetos tipo Ellipse if (item.GetType() == typeof(Ellipse)) { Ellipse eli = item as Ellipse; if (eli.GetValue(NameProperty).ToString() == tp.Name) { ImageBrush hib = new ImageBrush(); hib.ImageSource = new BitmapImage(new Uri(@"pieceEmpty.png", UriKind.Relative)); eli.Fill = hib; eli.SetValue(Grid.ColumnProperty, 0); eli.SetValue(Grid.RowProperty, 0); boardPieces.Where(pc => pc.Name.Equals(tp.Name)).ToList().ForEach(ph => ph.IsActive = false); break; } } } cp.TranslateX -= e.TotalManipulation.Translation.X; cp.TranslateY -= e.TotalManipulation.Translation.Y; this.ellipse.SetValue(Grid.ColumnProperty, finalPosition.col); this.ellipse.SetValue(Grid.RowProperty, finalPosition.row); boardPieces.Where(pc => pc.Name.Equals(this.ellipse.Name)).ToList().ForEach(ph => ph.Position = finalPosition.GetPosition()); this.turn = !this.turn; } else { cp.TranslateX -= e.TotalManipulation.Translation.X; cp.TranslateY -= e.TotalManipulation.Translation.Y; } } else { cp.TranslateX -= e.TotalManipulation.Translation.X; cp.TranslateY -= e.TotalManipulation.Translation.Y; } } #endregion if (PlayMove.IsCrow(finalPosition.GetPosition(), p.Color)) { ImageBrush qib = new ImageBrush(); boardPieces.Where(pc => pc.Name.Equals(this.ellipse.Name)).ToList <Pieces>().ForEach(pi => pi.IsQueen = true); if (p.Color == ColorPeace.black) { qib.ImageSource = new BitmapImage(new Uri(@"blackCrown.png", UriKind.Relative)); } else { qib.ImageSource = new BitmapImage(new Uri(@"whiteCrown.png", UriKind.Relative)); } this.ellipse.Fill = qib; this.txt1.Text = string.Format("Damas Blancas {0} vs Damas Negras {1}", boardPieces.Where(pc => pc.Color == ColorPeace.white && pc.IsActive && pc.IsQueen).ToList().Count.ToString(), boardPieces.Where(pc => pc.Color == ColorPeace.black && pc.IsActive && pc.IsQueen).ToList().Count.ToString()); } } this.txt.Text = string.Format("Blancas {0} vs Negras {1}", boardPieces.Where(p => p.Color == ColorPeace.white && p.IsActive).ToList().Count.ToString(), boardPieces.Where(p => p.Color == ColorPeace.black && p.IsActive).ToList().Count.ToString()); if (boardPieces.Where(p => p.Color == ColorPeace.white && p.IsActive).ToList().Count == 0) { this.txt.Text = "Ganan las Rojas"; } if (boardPieces.Where(p => p.Color == ColorPeace.black && p.IsActive).ToList().Count == 0) { this.txt.Text = "Ganan las Amarillas"; } }