24
3
Challenge Description
You have to show a simulation of rain in terminal.
In the example given below its adding 100 raindrops at random (use the default random function which your language offers) coordinates, waiting for 0.2 seconds and then redrawing again until the given time expires. Any character can be used for representing the raindrop.
Parameters
- Wait time between redrawing in seconds.
- Time for which the rain will be visible. This is just an integer representing the number of iterations. [So, the net time for which the rain will be visible is this integer multiplied by the wait time]
- Message to be displayed when the rain ends. (This has to be centered)
- Number of raindrops to be displayed on the screen.
Rules
- A single byte should be used for representing a rain drop, and it can be anything, even cats and dogs.
- It doesn't have to be responsive to terminal size which means you don't have to handle the bug for varied terminal sizes. You can specify the terminal width and height on your own.
- Standard rules of golfing apply.
Code Sample and Output
This is an ungolfed version written in python 2.7 using ncurses.
import curses
import random
import time
myscreen = curses.initscr()
curses.curs_set(0) # no cursor please
HEIGHT, WIDTH = myscreen.getmaxyx()
RAIN = '/' # this is what my rain drop looks like
TIME = 10
def make_it_rain(window, tot_time, msg, wait_time, num_drops):
"""
window :: curses window
time :: Total time for which it rains
msg :: Message displayed when it stops raining
wait_time :: Time between redrawing scene
num_drops :: Number of rain drops in the scene
"""
for _ in range(tot_time):
for i in range(num_drops):
x,y=random.randint(1, HEIGHT-2),random.randint(1,WIDTH-2)
window.addstr(x,y,RAIN)
window.refresh()
time.sleep(wait_time)
window.erase()
window.refresh()
window.addstr(HEIGHT/2, int(WIDTH/2.7), msg)
if __name__ == '__main__':
make_it_rain(myscreen, TIME, 'IT HAS STOPPED RAINING!', 0.2, 100)
myscreen.getch()
curses.endwin()
Output -
3In the future instead of posting again please edit the original. If people think the specs are clear they will nominate it for reopening. – Post Rock Garf Hunter – 2017-01-20T20:03:14.527
6Does the text have to be centered? Is it randomly raining, does initial position of droplets matter? How are you measuring time? In milliseconds, seconds, minutes? What is "Extra Points"? – Magic Octopus Urn – 2017-01-20T20:03:21.923
1If you, A. Specify units. B. Specify terminal size or take it as input. and C. Remove the part about extra points; this challenge would be better defined. – Magic Octopus Urn – 2017-01-20T20:07:25.797
When you say "random", can we assume that means "uniformly random"?
– Digital Trauma – 2017-01-20T20:07:50.40031 day in the sandbox is often not enough. Remember that people are here recreationally and from diverse timezones - immediate feedback should not be expected. – Digital Trauma – 2017-01-20T20:09:42.010
@carusocomputing I have added the information. – hashcode55 – 2017-01-20T20:12:34.337
@DigitalTrauma Can be sampled from any distribution. – hashcode55 – 2017-01-20T20:13:42.110
Note that a constant pattern would qualify as any distribution You probably want the drop positions to be independently chosen in each frame. Also, you mention values like 200 ms but then say it's an input parameter. So is it fixed or is it an input? Same for the other parameters. Also, the concept of terminal may be ill-defined in certain languages. Can we assume say an MxN character rectangle? – Luis Mendo – 2017-01-20T21:06:03.600
@LuisMendo Yes they are inputs and I just called my own function to show it in the example code. You just have to write the function. And there are no constraints related to terminal so yes you can assume it. – hashcode55 – 2017-01-20T21:33:48.307
@LuisMendo What I actually meant was you can use the default random function which your language offers. I'll edit it. – hashcode55 – 2017-01-20T21:36:13.233
"Any character can be used for representing the raindrop." this is a bit broad. Like, one could use space and claim it's random. – Ave – 2017-01-21T11:44:39.600