/// <summary> /// Возвращат найденую фигуру. Если фигура не найдена, null. /// </summary> /// <param name="pos"></param> /// <returns></returns> public Figure GetFigureOfPosition(PointV2 pos) { if (Figures.Exists(x => x.position.Equals(pos))) { return(Figures.Find(x => x.position.Equals(pos))); } else { return(null); } }
public bool GetFigure(Cell cell) { bool ifAssigned = false; Figures figure = Figures.Empty; if (cell != null && (cell is CellFigure)) { figure = (cell as CellFigure).Figure; ifAssigned = true; } Figure = figure; return(ifAssigned); }
private bool CheckForCheck() { King king = (King)Figures.First(x => x is King && x.Color == CurrentPlayer.Color); foreach (var figure in Figures.Where(x => x.Color != CurrentPlayer.Color)) { foreach (var field in figure.PossibleMoves) { if (field.IsFieldTheSame(king.Field)) { return(true); } } } return(false); }
private void Table_MouseDown(object sender, MouseButtonEventArgs e) { if (GetSelectedFigure((int)((e.GetPosition(this).X) / rowHeight), (int)((e.GetPosition(this).Y) / columnWidth))?.Color == nonPlayingColor) { MessageBox.Show("Opssss, it's not step of this color"); } else if (GetSelectedFigure((int)((e.GetPosition(this).X) / rowHeight), (int)((e.GetPosition(this).Y) / columnWidth)) != null) { isFigureCliked = true; clickedMousePosX = (int)((e.GetPosition(this).X) / rowHeight); clickedMousePosY = (int)((e.GetPosition(this).Y) / columnWidth); clickedFigure = GetSelectedFigure(clickedMousePosX, clickedMousePosY); clickedFigure.AddHint(ref hint, ref matrix, figures, GetSelectedFigure(clickedMousePosX, clickedMousePosY)?.Color); deltaX = e.GetPosition(this).X - image[clickedMousePosX, clickedMousePosY].Margin.Left; deltaY = e.GetPosition(this).Y - image[clickedMousePosX, clickedMousePosY].Margin.Top; } }
public CellFigure(Position position, ChessColor figureColor, Figures figure) : base(position) { _figure = figure; _figureColor = figureColor; }
private void Table_MouseUp(object sender, MouseButtonEventArgs e) { isFigureCliked = false; double mousePosX = (e.GetPosition(this).X) / rowHeight; double mousePosY = (e.GetPosition(this).Y) / columnWidth; double eatenPosX = (e.GetPosition(this).X) / rowHeight; double eatenPosY = (e.GetPosition(this).Y) / columnWidth; string anyColor = ""; if (GetSelectedFigure(clickedMousePosX, clickedMousePosY) != null) { image[(int)mousePosX, (int)mousePosY].Source = image[clickedMousePosX, clickedMousePosY].Source; image[(int)mousePosX, (int)mousePosY].Margin = new Thickness((int)mousePosX * rowHeight, (int)mousePosY * columnWidth, 0, 0); if ((int)mousePosX != clickedMousePosX || (int)mousePosY != clickedMousePosY) { image[clickedMousePosX, clickedMousePosY].Source = null; } if (matrix[(int)mousePosX, (int)mousePosY] != 2) { if (GetSelectedFigure((int)mousePosX, (int)mousePosY) != null || Figures.GetFigure((int)mousePosX, (int)mousePosY, figures)?.Color == player2Color) { image[(int)mousePosX, (int)mousePosY].Source = new BitmapImage(new Uri(Figures.GetFigure((int)mousePosX, (int)mousePosY, figures)?.ImgSource, UriKind.Relative)); } else { image[(int)mousePosX, (int)mousePosY].Source = null; } mousePosX = clickedMousePosX; mousePosY = clickedMousePosY; image[clickedMousePosX, clickedMousePosY].Margin = new Thickness(clickedMousePosX * rowHeight, clickedMousePosY * columnWidth, 0, 0); image[clickedMousePosX, clickedMousePosY].Source = new BitmapImage(new Uri(clickedFigure.ImgSource, UriKind.Relative)); } else { selectedSteps.Clear(); opponentSelectedSteps.Clear(); your_steps.Items.Clear(); opponent_steps.Items.Clear(); anyColor = nonPlayingColor; if (nonPlayingColor == "black") { nonPlayingColor = "white"; } else { nonPlayingColor = "black"; } foreach (var figure in figures) { if (figure.positionX == (int)eatenPosX && figure.positionY == (int)eatenPosY) { if (figure.Name != "king") { figures.Remove(figure); } else { if (figure.Color == "black") { MessageBox.Show("White Win"); } else { MessageBox.Show("Black Win"); } } break; } } } foreach (var figure in figures) { if (figure != null) { if (figure.positionX == clickedMousePosX && figure.positionY == clickedMousePosY) { figure.positionX = (int)mousePosX; figure.positionY = (int)mousePosY; if (matrix[(int)mousePosX, (int)mousePosY] == 2) { if (IsShax(anyColor)) { MessageBox.Show("Shaaax"); } //if (IsMate(anyColor)) //{ // MessageBox.Show("Mate"); //} } } } } if ((int)mousePosY == 0 && figures.Where(x => (x.positionX == (int)mousePosX && x.positionY == (int)mousePosY)).First().Name == "pawn") { Choose choose = new Choose(); choose.ShowDialog(); PawnCategory = choose.pawnCategory; string color = figures.Where(x => (x.positionX == (int)mousePosX && x.positionY == (int)mousePosY)).First().Color; Figures figure = figures.Where(x => (x.positionX == (int)mousePosX && x.positionY == (int)mousePosY)).First(); figure.ImgSource = color + "-" + PawnCategory + ".png"; figure.Name = PawnCategory; image[figure.positionX, figure.positionY].Source = new BitmapImage(new Uri(figure.ImgSource, UriKind.Relative)); } for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (matrix[i, j] == 2) { hint[i, j].Source = null; matrix[i, j] = 0; } } } } //if (figures.Where(x => x.Name == "king" && x.Color == "black").ToArray().Length == 0) //{ // MessageBox.Show("White Win"); //} //else if(figures.Where(x => x.Name == "king" && x.Color == "white").ToArray().Length == 0) //{ // MessageBox.Show("Black Win"); //} }
private void Table_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { if (GetSelectedFigure((int)((e.GetPosition(this).X) / rowHeight), (int)((e.GetPosition(this).Y) / columnWidth))?.Color == nonPlayingColor && matrix[(int)((e.GetPosition(this).X) / rowHeight), (int)((e.GetPosition(this).Y) / columnWidth)] != 2) { MessageBox.Show("Opssss, it's not step of this color"); } else if (GetSelectedFigure((int)((e.GetPosition(this).X) / rowHeight), (int)((e.GetPosition(this).Y) / columnWidth))?.Color == nonPlayingColor && matrix[(int)((e.GetPosition(this).X) / rowHeight), (int)((e.GetPosition(this).Y) / columnWidth)] == 2) { clickedLeftButtonPosX = (int)((e.GetPosition(this).X) / rowHeight); clickedLeftButtonPosY = (int)((e.GetPosition(this).Y) / columnWidth); step = $"{clickedLeftButtonPosX} / {clickedLeftButtonPosY}"; endX = clickedLeftButtonPosX; endY = clickedLeftButtonPosY; if (currentColor == player1Color) { opponent_steps.Items.Clear(); opponentSelectedSteps.Clear(); if (!selectedSteps.Contains(qar + step)) { your_steps.Items.Add(qar + step); selectedSteps.Add(qar + step); } } else { your_steps.Items.Clear(); selectedSteps.Clear(); if (!opponentSelectedSteps.Contains(qar + step)) { opponent_steps.Items.Add(qar + step); opponentSelectedSteps.Add(qar + step); } } calculatedSteps.Add(new Steps(startX, startY, endX, endY)); step = ""; } else { clickedLeftButtonPosX = (int)((e.GetPosition(this).X) / rowHeight); clickedLeftButtonPosY = (int)((e.GetPosition(this).Y) / columnWidth); if (GetSelectedFigure(clickedLeftButtonPosX, clickedLeftButtonPosY) != null) { currentColor = GetSelectedFigure(clickedLeftButtonPosX, clickedLeftButtonPosY)?.Color; qar = $"{GetSelectedFigure(clickedLeftButtonPosX, clickedLeftButtonPosY)?.Name} - {clickedLeftButtonPosX} / {clickedLeftButtonPosY} to "; startX = clickedLeftButtonPosX; startY = clickedLeftButtonPosY; } if (matrix[clickedLeftButtonPosX, clickedLeftButtonPosY] != 2) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (matrix[i, j] == 2) { hint[i, j].Source = null; matrix[i, j] = 0; } } } } else { step = $"{clickedLeftButtonPosX} / {clickedLeftButtonPosY}"; endX = clickedLeftButtonPosX; endY = clickedLeftButtonPosY; if (currentColor == player1Color) { opponent_steps.Items.Clear(); opponentSelectedSteps.Clear(); if (!selectedSteps.Contains(qar + step)) { your_steps.Items.Add(qar + step); selectedSteps.Add(qar + step); } } else { your_steps.Items.Clear(); selectedSteps.Clear(); if (!opponentSelectedSteps.Contains(qar + step)) { opponent_steps.Items.Add(qar + step); opponentSelectedSteps.Add(qar + step); } } calculatedSteps.Add(new Steps(startX, startY, endX, endY)); step = ""; } if (GetSelectedFigure(clickedLeftButtonPosX, clickedLeftButtonPosY) != null) { clickedFigure = GetSelectedFigure(clickedLeftButtonPosX, clickedLeftButtonPosY); clickedFigure.AddHint(ref hint, ref matrix, figures, GetSelectedFigure(clickedLeftButtonPosX, clickedLeftButtonPosY)?.Color); } } }