views:

281

answers:

5

Hi,

I need to write a program that prints 100 stars on the screen (at random places), and then the stars disappear slowly - one after another. I'm not allowed to use loops nor recursions. I've tried to play with the constructors and the destructors but I can't get the stars to disappear one after another (and not all together). Any ideas?

Thanks, Li

Sorry - forgot to mention i'm using c++

My current access violating useless code:

class star {
    int x;
    int y;
public:
    star(){
        x = rand()%80;
        y = rand()%80;
        PaintcharOnRandomLocation('*',x,y);
    };
    ~star(){
        PaintcharOnRandomLocation(' ',x,y);
    };

};

class printAll{
    star* arr;
public:
    printAll(){
    arr = new star[100];
    };


    ~printAll(){
        delete[] arr;
    };


};
void doNothing(printAll L){
};

void main()
{
    srand ( time(NULL) );   
    doNothing(printAll());

     getch();
};
+15  A: 

Seems the only way possible without loops/recursion is something like this:

class Star
{
  Star() 
  { 
     //constructor shows star in a a random place
  }
  ~Star()
  {
    //destructor removes star and sleeps for a random amount of time
  }
};

int main() 
{
   Star S[100];
}

This is really just a dumb trick because the compiler has to run the constructor for each star to initialise the array and then the destructor for EACH star as it goes out of scope.

It is also a bad trick as all the workings that go in the main function are opaque and invisible. It would obviously be better to use a loop in this context and putting the delay inside a destructor like this is really confusing and unmaintainable.

Elemental
Beat me to it by seconds. +1
Beta
Nice idea. Here is my +1.
ereOn
The trouble is, that in your code, the destructor won't get called at all, because S is just a pointer that does nothing when it goes out of scope.
fat-lobyte
@fat-lobyte: `S` is not a pointer. `S` is an array of 100 `Star` objects.
James McNellis
+1: innovative idea
Chubsdad
+1  A: 

Based on your final comment, can you have the destructor of your star class do a delay? See for example the sleep or usleep functions.

Mark B
A: 

Since Destructors/Constructors are only an Idea, they're probably not the right title for your question. I don't know what system/environment you are in, but how about this:

Create a buffer that contains a string with your stars, simply manually by typing them in the code.

Next, write a function that displays the buffer to whatever output window you use.

Then, you would need a function that has a static(!) pointer to the back of the buffer, and that does the following:

  • Call the buffer printing function
  • Write a null byte under the current pointer position
  • Decrement the static pointer
  • Wait for a period of time
  • Raise a custom signal with raise()

In the main() function, you set the the Signal Handler for your custom signal to the function described above, and then you raise the custom signal.

fat-lobyte
+8  A: 

This is not a runtime recursion:

template<int N>
struct Star
{
   Star() { DrawAtRandomPlace(); }
   ~Star() { RemoveSlowly(); }
   Star<N-1> star;
};

template<> struct Star<0> {};

int main()
{
  Star<100> stars;
}

The code above will generate 100 different instantiations of the Star template. RAII will guarantee the order of drawing and removing.

Kirill V. Lyadvinsky
These are not the droids you are looking for.
Tom
+1 one - template loop unwinding ftw.
Elemental
+1 and maybe `RemoveAtRandomPace();`
Potatoswatter
+1 Very clever.
ShaderOp
A: 

Thanks guys :) The sleep trick is probably what they were looking for.