Esempio n. 1
0
        /// <summary>
        /// Перемещает фигуру из начальной координаты в конечную, если это возможно
        /// </summary>
        /// <param name="startCoordinate"></param>
        /// <param name="endCoordinate"></param>
        public void Move(Coordinate startCoordinate, Coordinate endCoordinate)
        {
            DynamicArray <Coordinate> figurePath;
            Cell startCell = FindCell(startCoordinate), endCell = FindCell(endCoordinate);

            if (startCell.ContentPiece == null)
            {
                throw new Exception("В ячейке с выбранной начальной координатой нет фигуры");
            }

            CheckTurn(startCell.ContentPiece);
            ChessPiece movingPiece = startCell.ContentPiece;


            if (movingPiece.CheckMove(endCoordinate))
            {
                figurePath = movingPiece.Path(endCoordinate);
                CheckWithOtherPieces(figurePath, movingPiece);
                ChessPiece temp1 = startCell.ContentPiece, temp2 = endCell.ContentPiece;
                try
                {
                    endCell.ChangeContent(startCell.ContentPiece);
                    startCell.VoidContent();
                    movingPiece.SetCoordinate(endCoordinate.ToString());
                    CheckCheck();
                }
                catch (Exception)
                {
                    endCell.ChangeContent(temp2);
                    startCell.ChangeContent(temp1);
                    movingPiece.SetCoordinate(startCell.Coordinate.ToString());
                    throw new Exception("Шах");
                }
                movingPiece.SetCoordinate(endCoordinate.ToString());
                Log.Add(endCoordinate.ToString(), endCell.ContentPiece.Color, endCell.ContentPiece.Type);
                turnCount++;
                Render.ShowBoard(this);
            }
            else
            {
                throw new Exception("Невозможный ход");
            }
        }
Esempio n. 2
0
        public override bool CheckMove(Coordinate endCoordinate)
        {
            DynamicArray <Coordinate> Coordinates = Moves();

            for (int i = 0; i < Coordinates.Count(); i++)
            {
                if (Coordinates[i].ToString() == endCoordinate.ToString())
                {
                    return(true);
                }
            }
            return(false);
        }