Probably everyone knows about the methods of catching bugs. But I got the idea whether it is possible to adapt the methods of catching bugs for catching hedgehogs? Surely there are any methods or perhaps someone has already done this?

Maybe some kind of special mechanism like try catch?

  • Of course, I understand that today is April 1st. And yet, can you explain to the uninitiated, what's the salt? - avp
  • one
    @avp of some special hidden meaning here, obviously, no. Probably, a question is asked based on [article] [1] on Habré [1]: habrahabr.ru/post/217309 - DreamChild
  • one
    I fully confirm, unfortunately, with my fantasy, it is not particularly good, but the community wanted to congratulate with this wonderful holiday. - Sergey
  • By the way, it is very curious that this time they will invent Google. In any case, their previous jokes were funny. For example, the news about the support of two mice in Chrome and the opportunity to buy all the videos from YouTube on dvd-disks - DreamChild
  • one
    @Alex Krass youtube.com/watch?v=4YMD6xELI_k - andreich

4 answers 4

As you know, hedgehogs are different - forest , eared , anti-tank and even sea. They all differ in intelligence, ingenuity and deceit. That is, to catch a hedgehog is not a trivial task, involving numerous dangers and requiring comprehensive training and modern equipment. That is why it is difficult to overestimate the importance of modern technology in this difficult matter.

So let's get started. To begin, select the primary entity - Abstract Hedgehog.

public abstract class AbstractHedgehog { public abstract void TellAboutItself(); public virtual void Snort() { Console.WriteLine("Пых-пых"); } public virtual void Stomp() { Console.WriteLine("Бадабум"); } public virtual void Fly(Human human) { Console.WriteLine(human != null ? "Я лечуууу!!!!" : "Я очень гордая птица и никуда не полечу, пока меня не пнут"); } public bool IsCaught { get; set; } } 

As you can see, our Abstract Hedgehog can stomp and puff. In addition, modern science has established that you can also talk to a hedgehog . However, this is not all. In addition, hedgehogs can fly. True, they never do this without help, so a person will be needed for the flight. It is also worth noting that the hedgehog can be free or caught (IsCaught). In general, our Abstract Hedgehog can a lot. Now more about the types of hedgehogs. We will have three of them: Yozh (ordinary), Eared Yozh and Anti-Tank Yozh:

 public class Hedgehog : AbstractHedgehog { public override void TellAboutItself() { Console.WriteLine("Я простой ёж, и я умею пыхтеть"); } } public class EaredHedgehog : AbstractHedgehog { public override void TellAboutItself() { Console.WriteLine("Я очень ушастый ёж"); } } public class CzechHedgehog : AbstractHedgehog { public override void TellAboutItself() { Console.WriteLine("Я противотанковый ёж"); } public override void Snort() { Console.WriteLine("К сожалению, я не умею пыхтеть"); } public override void Stomp() { Console.WriteLine("Топать я тоже не умею. Печаль ;("); } public override void Fly(Human human) { Console.WriteLine("Противотанковые ежи не умеют летать"); } } 

To catch a hedgehog, we need a special tool - a hedgehog trap. We will model it with an interface - this will allow us to achieve a certain flexibility later):

 public interface IHedgehogTrap { bool Catch(AbstractHedgehog hedgehog); } 

Traps are different, but they all need to be able to catch a hedgehog (that is, implement the IHedgehogTrap interface). Consider two types - Simple Ezhovaya Trap (low reliability and low cost, not able to catch anti-tank hedgehogs) and Advanced Atomic Egg Trap (the road to use and maintain, but can catch any, even the most cunning and dodgy hedgehog)

 public class SimpleTrap : IHedgehogTrap { public bool Catch(AbstractHedgehog hedgehog) { if(hedgehog is CzechHedgehog) throw new Exception("Нельзя поймать противотанкового ежа обычной ловушкой"); var random = new Random(); hedgehog.IsCaught = random.Next(2) == 1; Console.WriteLine(hedgehog.IsCaught ? "Ёж успешно пойман" : "Коварный ёж ускользнул"); return hedgehog.IsCaught; } } public class NucleareTrap : IHedgehogTrap { public bool Catch(AbstractHedgehog hedgehog) { hedgehog.IsCaught = true; Console.WriteLine("Ёж успешно пойман"); return true; } } 

Well, the last of the entities under consideration is the Man (he is the Hedgehog Catcher):

 public class Human { public Human() { } public Human(IHedgehogTrap trap) { Trap = trap; } public IHedgehogTrap Trap { get; set; } public void Catch(AbstractHedgehog hedgehog) { if (Trap == null) throw new Exception("Нельзя ловить ежа голыми руками"); Trap.Catch(hedgehog); } public void Kick(AbstractHedgehog hedgehog) { hedgehog.Fly(this); } } 

In principle, it would also be nice to make a Man abstract, and to inherit several different descendants from him, but we are interested in precisely hedgehogs, and not people. A person may try to catch a hedgehog (Catch), give it an acceleration for flight (Kick), and may also have (or not) a trap for catching a hedgehog (Trap). Moreover, there can be any trap, it is only important that it implement the IHedgehogTrap interface - this allows changing the ways of catching a hedgehog in accordance with the “Strategy” pattern.

Now try to catch a few hedgehogs:

 public static void CatchEmAll(Human hunter, IEnumerable<AbstractHedgehog> hedgehogs) { try { foreach (var hedgehog in hedgehogs) { hunter.Catch(hedgehog); if (hedgehog.IsCaught) { hedgehog.TellAboutItself(); hedgehog.Snort(); hedgehog.Stomp(); hunter.Kick(hedgehog); } } } catch (Exception e) { Console.WriteLine("Случилось что-то ужасное: {0}. Чип и Дейл уже спешат на помощь", e.Message); } } .... public static void Main() { var hunter = new Human(); var hedgehogs = new AbstractHedgehog[] { new Hedgehog(), new EaredHedgehog(), new CzechHedgehog() }; CatchEmAll(hunter, hedgehogs); // {1} hunter.Trap = new SimpleTrap(); CatchEmAll(hunter, hedgehogs); // {2} hunter.Trap = new NucleareTrap(); CatchEmAll(hunter, hedgehogs); // {3} Console.ReadLine(); } 

As you can see, the hedgehogs were caught in three ways - with bare hands, a simple hedgehog trap and an improved atomic hedgehog trap. The best results were shown by the Advanced Atomic Egg Trap - all three hedgehogs were caught with its help.

In conclusion, it is also worth adding that in the hierarchy of hedgehogs described above, a mistake was deliberately made - Hedge Counter-Target was assigned to the descendants of the Abstract Hedgehog, which ultimately led to a lot of special logic in the Snort, Stomp and Fly methods and created more problems than advantages. This suggests that the Counter-Hedgehog should be excluded from this hierarchy.

  • Yeah, not weak! - avp
  • That's something I was waiting for here to see) Although you can go ahead and arrange in the form of some mini console game to catch hedgehogs, but it will be more difficult. - Alex Krass
  • @Alex Krass I originally wanted to make a console game, but I came home too late yesterday and I didn’t have enough time for it - DreamChild
  • one
    I would throw a NotSupportedException in CzechHedgehog.Snort() . By the way, the atomic trap taxis! - VladD
  • one
    @VladD in principle, of course, would be worth it (as well as throwing something other than Exception, but something more specific), but I didn’t want to overload the response with code — I would also need to handle these exceptionsDreamChild

First you need to set the processor registers to quantum superposition , then reduce this state by dividing by zero. Then we catch hedgehogs in the usual way by finding the pre-image of the result of the hash function. I think everything is clear!

  • one
    I think this is a very interesting approach, despite the fact that it is not related to C # - Sergey

I think this article will help you

UPD:
I am not an expert in C # but maybe I need Ezh instead of Exception?
sort of

 try { //тут что то происходит //и вдруг появляется ёж } catch(Ezh e)//тут ловушка для ежа { } 
  • Yes, I already read this article, but I'm afraid she is about manual ways. Of course, they are fascinating, but as a real computer lover, I can't help but think about automating the process. Therefore, I decided to ask the experts, maybe they once solved a similar task. - Sergey
  • @ Sergey updated the answer - andreich
  • it seems to me then (hedgehog e) or maybe you need to use the try construct - catch_hedgehog will try both options - Sergey
  • catch_hedgehog shows an error, but if catch (hedgehog e) for some reason gets an error: raw hedgehog - Sergey

Why not use the old grandfather's ways?

 class Init { static void Main() { Random random = new Random(); while (true) { if (random.NextDouble() > 0.9) { Console.WriteLine("Hedgehog caught"); } else { Console.WriteLine("hedgehog is not caught"); } System.Threading.Thread.Sleep(1000); } } } 

You want via try-catch, the same thing:

 class Hedgehog:Exception {} class Init { static void Main() { Random random = new Random(); try { while (true) { if (random.NextDouble() > 0.9) { throw new Hedgehog(); } else { Console.WriteLine("hedgehog is not caught"); } System.Threading.Thread.Sleep(1000); } } catch (Hedgehog hedgehog) { Console.WriteLine("Hedgehog caught"); } Console.ReadLine(); } } 
  • 2
    I'm afraid this way hedgehogs are not released and you can get a hedgehog buffer overflow - Sergey
  • 2
    I suggest that they should then be put into the garbage collector and taken to the forest automatically - Alex Krass
  • 2
    in the garbage collector hedgehogs will be uncomfortable and they will flow away. They should be lured into hedgehog collector - teanIch