16
\$\begingroup\$

Introduction

For the ones who never heard of this game before. You are playing a ball which needs to survive as long as possible. This is done by moving to the left or right, going to the holes. Since the map moves upwards, you need to go downwards to survive longer. If you search for images, you probably know which game I mean.

The Task

Given a positive integer n, output a falling ball map of n layers. Between the layers, there are 4 newlines. The width of the layer is made up of 25 underscore characters, with one hole of length 5. That means that the total width is equal to 30. This is randomly shifted after each layer. An example of a valid layer is:

_______________ __________

The hole can also be at the edges, as if the layer is like a cylinder:

 _________________________ 

Note that there are 2 leading spaces and 3 trailing spaces. Making a single hole of width 5.

Test cases

For n=4, this is a valid output:

_______________ __________
______ ___________________
_______________________ __
 _________________________

Note: the holes must be uniformly distributed. Trailing and/or leading newlines are allowed.

This is , so the submission with the least amount of bytes wins!

asked Mar 17, 2016 at 18:08
\$\endgroup\$
1
  • 1
    \$\begingroup\$ @DonMuesli Haha, that can happen to anyone :p \$\endgroup\$ Commented Mar 17, 2016 at 19:25

10 Answers 10

6
\$\begingroup\$

Pyth, 19 bytes

VQ.>+*\_25*d5O30*b4

Try it here!

Explanation

VQ.>+*\_25*d5O30*b4 # Q = input
VQ # Repeat Q times
 *\_25 # 25 underscores
 *d5 # 5 spaces
 + # concat the underscores and spaces
 .> O25 # rotate this string 0-29 times
 *b4 # print the seperating newlines
answered Mar 17, 2016 at 18:31
\$\endgroup\$
0
4
\$\begingroup\$

Ruby, (削除) 74 (削除ここまで) (削除) 71 (削除ここまで) 59

Thanks to Alex A. for reminding me that being basically one line means I can use the stabby lambda

f=->n{n.times{puts (('_'*25+' '*5)*2)[rand(30),30]+"\n"*5}}
answered Mar 17, 2016 at 18:45
\$\endgroup\$
6
  • \$\begingroup\$ puts adds a newline to the end (while Ruby's print doesn't), so it's been updated to 5 newlines. \$\endgroup\$ Commented Mar 17, 2016 at 18:59
  • \$\begingroup\$ All hail the times function for saving me 3 bytes over the for loop :D \$\endgroup\$ Commented Mar 17, 2016 at 19:02
  • 1
    \$\begingroup\$ 57 bytes: ->n{n.times{puts (('_'*25+' '*5)*2)[rand(30),30]+"\n"*5}} Try here \$\endgroup\$ Commented Mar 17, 2016 at 19:09
  • \$\begingroup\$ You don't need to assign the lambda as part of the submission since we don't require functions to be named. \$\endgroup\$ Commented Mar 17, 2016 at 19:15
  • 1
    \$\begingroup\$ You can do (->n{n.times{puts (('_'*25+' '*5)*2)[rand(30),30]+"\n"*5}})[input] or just specify that you need to assign it in order to call it. This is quite common on the site. \$\endgroup\$ Commented Mar 17, 2016 at 19:18
3
\$\begingroup\$

Julia, 72 bytes

n->join([join(circshift(["_"^25*" "^5...],rand(1:30)))"\n"^5 for i=1:n])

This is an anonymous function that accepts an integer and returns a string. To call it, assign it to a variable.

We construct a string of 25 underscores followed by 5 spaces, then splat that into an array of characters. We circularly shift it a random number of times (between 1 and 30), join it back into a string, and tack on 5 newlines. This process is repeated n times, where n is the input. The resulting array is joined and returned.

Try it here

answered Mar 17, 2016 at 19:58
\$\endgroup\$
3
\$\begingroup\$

PowerShell 65 Bytes

0..$args[0]|%{-join("_"*25+" "*5)[($r=Random 25)..($r-29)];,""*5}

Run a loop for n iterations. Get a random number and make a underscore string with 5 spaces at the end. Index into the the string as an array to pull starting from a random location. Negative indexing wraps from the end. Join that back up again and append 5 newlines.

Edit: I don't know why I thought I needed the parameter for -Maximum as it's positional. Thanks to TimmyD I shaved some bytes and forgot to get input so that drove me back up to 65.

answered Mar 18, 2016 at 11:17
\$\endgroup\$
2
  • \$\begingroup\$ @TimmyD I would be surprised if anyone still had 1.0. It was only available as a download and was not shipped with any OS. 2.0 should be considered the norm. \$\endgroup\$ Commented Mar 18, 2016 at 13:49
  • \$\begingroup\$ @TimmyD I do agree with that point just not where PowerShell 1.0 is concerned. If anyone is on this site using using that it would surprise me. If I used a 3.0 or higher feature I would highlight that for sure. \$\endgroup\$ Commented Mar 18, 2016 at 16:05
3
\$\begingroup\$

Pyth, 19 bytes

VQ.>r"5 25_"9O30*4b

Try it here

Thanks to FryAmTheEggman for catching a bug.

answered Mar 17, 2016 at 18:34
\$\endgroup\$
1
  • \$\begingroup\$ @AandN FryAmTheEggman is right, I have changed it to be randomly shifted by 0-29 characters instead of 0-25. \$\endgroup\$ Commented Mar 17, 2016 at 18:55
2
\$\begingroup\$

SpecBAS - 93 bytes

1 a$="_"*25+" "*5: INPUT n: FOR i=1 TO n: r=1+INT(RND*30): ?a$(r TO)+a$( TO r-1)''''': NEXT i

Makes a string of 25 underscores + 5 spaces, picks a random start point and splices the string at that position and tacks the start of string to end.

The apostrophe forces a newline, so 1 at end of string and 4 empty lines.

answered Mar 18, 2016 at 11:03
\$\endgroup\$
2
\$\begingroup\$

MATL, 23 bytes

:"95l25X"5Z"h30YrYS10ct

Try it online!

Explanation

:" % take input n implicitly. Repeat loop n times
 95l25X" % row vector with number 95 ('_') repeated 25 times
 5Z" % row vector of 5 blank spaces
 h % concatenate vertically
 30Yr % random number uniformly distributed on [1,2,...,30]
 YS % circularly shift
 10c % newline character. Includes another newline when printed
 t % duplicate
 % implicitly end loop and display stack contents
answered Mar 17, 2016 at 19:20
\$\endgroup\$
2
\$\begingroup\$

Java, (削除) 177 (削除ここまで) 140 bytes

-1 byte to Blue moving the i++

-2 bytes to me leaving meaningless whitespace in the original

-34 bytes to Blue for realizing I didn't need the char[] at all

Because, ya know, Java.

n->{String s="";for(;n-->0;){int r=(int)(Math.random()*30);for(int i=0;i<30;){s+=i>r&i<r+6|r>24&i++<=r%25?" ":"_";}s+="\n\n\n\n";}return s;}

Slightly ungolfed:

Function<Integer, String> fallingBall = n -> {
 String s = "";
 for (;n-->0;) {
 int r = (int)(Math.random()*30);
 for (int i=0; i<30; i++) {
 s += i>r&i<r+6|r>24&i<=r%25?" ":"_";
 }
 s += "\n\n\n\n";
 }
 return s;
}

Since Java doesn't have a String 'rotate', I had to take a different approach. I generated a random int 0-29 that represents the start index of the hole (OBO because > is shorter than >=). Each individual character is then decided by the logic

l[index] = (index>random && index<random+6) || (random>24 && index<=random%25) ? ' ' : '_';

This handles the "wrapping" nature of the whole.

answered Mar 18, 2016 at 2:01
\$\endgroup\$
5
  • \$\begingroup\$ By putting the i++ the last place i is used, you can save a byte. \$\endgroup\$ Commented Mar 18, 2016 at 19:09
  • \$\begingroup\$ Smart. Hacky, but smart. Smarter still is me somehow leaving meaningless whitespace at the beginning of my snippet (s = ""). Both are fixed now. \$\endgroup\$ Commented Mar 18, 2016 at 19:14
  • \$\begingroup\$ You can also save a bunch more by getting rid of the char array, change the char literals to string literals, and use s+= in the inside for loop. I just tested it \$\endgroup\$ Commented Mar 18, 2016 at 19:18
  • \$\begingroup\$ Btw great answer \$\endgroup\$ Commented Mar 18, 2016 at 19:19
  • \$\begingroup\$ That's what I get for focusing on one way to do it. -34(!) bytes. \$\endgroup\$ Commented Mar 18, 2016 at 19:29
1
\$\begingroup\$

Python 3.5 - (削除) 109 (削除ここまで) 113 bytes (+4 for printing out 4 new lines between each result):

def g(a,j='_'*25+' '*5):
 for l in range(a):import random;n=random.randrange(len(j)+1);print(j[-n:]+j[:-n]+'\r'*4)

Ungolfed Form:

# j is the starting string with the 25 underscores and then 5 spaces at the end
def g(a,j='_'*25+' '*5):
 import random #Import the random module
 for l in range(a): # Do the following as many times as provided by the user
 n=random.randrange(len(j)+1) Get a random number in the range 0-29
 print(j[-n:]+j[:-n]+'\r'*4) #Shift the string characters right "n" times and print out the results with 4 new lines in between

Basically what is going on is that a string 'j', consisting of 25 underscore followed by 5 spaces, is given as an argument to the function "g". Then, the random module is imported, and, as many times as specified by the user, a random number is chosen in the range 0 => 29, and the string, "j", is shifted right that many times. The result is then printed out after each iteration, with 4 new lines in between each result.

answered Mar 25, 2016 at 0:05
\$\endgroup\$
8
  • \$\begingroup\$ Does this also print the newlines in between? \$\endgroup\$ Commented Mar 25, 2016 at 0:07
  • \$\begingroup\$ @AandN Now it does. :) \$\endgroup\$ Commented Mar 25, 2016 at 0:08
  • \$\begingroup\$ You actually need to print 4 newlines :p. So changing '\r' to '\r'*4 should probably work. Also, I think you can change range(a+1) to range(a). \$\endgroup\$ Commented Mar 25, 2016 at 0:11
  • \$\begingroup\$ @AandN Actually, using range(a) actually prints out a-1 number of lines, so range(a+1) is the best way to print out the exact number of lines the user provides. Also, it now prints 4 new lines in between each result. \$\endgroup\$ Commented Mar 25, 2016 at 0:14
  • \$\begingroup\$ Hmmm, that is weird, because I tested this with g(5) and it prints 6 strings instead of five, so changing a+1 to a would probably fix that. \$\endgroup\$ Commented Mar 25, 2016 at 0:15
1
\$\begingroup\$

Python, (削除) 119 (削除ここまで) 103 bytes

import random
s='_'*20+' '*5
def f(n,a=random.randint(0,25)):[print(s[a:]+s[:a]+3*'\n')for _ in' '*n]
\$\endgroup\$
1
  • \$\begingroup\$ Your latest update doesn't work because you've put the random in a default argument, so the function will always output the same row n times \$\endgroup\$ Commented Mar 26, 2016 at 0:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.