private IBasicPolygon GetCell(Point3d coordinates)
 {
     return Game.Board.Cells.Where(c => c.Coordintes.X == coordinates.X &&
                                             c.Coordintes.Y == coordinates.Y &&
                                             c.Coordintes.Z == coordinates.Z)
                                             .FirstOrDefault();
 }
        private void CheckCellExists(Point3d coordinates)
        {
            var cell = GetCell(coordinates);

            if (cell == null)
            {
                throw new InvalidOperationException("Cell with coordinates {coordinates} not found.");
            }
        }
 private IBasicPolygon GetMiddleCell(Point3d from, Point3d to)
 {
     return GetCell(new Point3d() { X = (from.X + to.X) / 2, Y = (from.Y + to.Y) / 2, Z = (from.Z + to.Z) / 2 });
 }
 private void CheckMoveCoordiantes(Point3d from, Point3d to)
 {
     CheckCellExists(from);
     CheckCellExists(to);
     CheckTargetCellIsSolid(to);
     CheckCoordiantes(from, to);
     CheckMoveState(from, to);
 }
        private void CheckMoveState(Point3d from, Point3d to)
        {
            var cellFrom = GetCell(from);
            var cellTo = GetCell(to);

            var middleCell = GetMiddleCell(from, to);

            if (middleCell == null ||
                middleCell.State != PolygonState.Filled ||
                cellFrom.State != PolygonState.Filled ||
                cellTo.State != PolygonState.Empty)
            {
                throw new InvalidOperationException(string.Format("Can't move from {0}.", from));
            }
        }
        private void CheckCoordiantes(Point3d from, Point3d to)
        {
            var xDiff = Math.Abs(from.X - to.X);
            var yDiff = Math.Abs(from.Y - to.Y);

            if ((xDiff == 2 && yDiff == 0) || (yDiff == 2 && xDiff == 0))
            {
                return;
            }
            else
            {
                throw new InvalidOperationException(string.Format("Can't move {0} -> {1}.", from, to));
            }
        }
        private void CheckTargetCellIsSolid(Point3d coordinates)
        {
            var cell = GetCell(coordinates);

            if (cell.State == PolygonState.Solid)
            {
                throw new InvalidOperationException(string.Format("Cell with coordinates {0} is solid.", coordinates));
            }
        }