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!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ @DonMuesli Haha, that can happen to anyone :p \$\endgroup\$
    – Adnan
    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
\$\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}}
\$\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\$
    – Value Ink
    Mar 17, 2016 at 18:59
  • \$\begingroup\$ All hail the times function for saving me 3 bytes over the for loop :D \$\endgroup\$
    – Value Ink
    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\$
    – Alex A.
    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\$
    – Alex A.
    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\$
    – Alex A.
    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

\$\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.

\$\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\$
    – Matt
    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\$
    – Matt
    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.

\$\endgroup\$
1
  • \$\begingroup\$ @AandN FryAmTheEggman is right, I have changed it to be randomly shifted by 0-29 characters instead of 0-25. \$\endgroup\$ 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.

\$\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
\$\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.

\$\endgroup\$
5
  • \$\begingroup\$ By putting the i++ the last place i is used, you can save a byte. \$\endgroup\$
    – Blue
    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\$
    – CAD97
    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\$
    – Blue
    Mar 18, 2016 at 19:18
  • \$\begingroup\$ Btw great answer \$\endgroup\$
    – Blue
    Mar 18, 2016 at 19:19
  • \$\begingroup\$ That's what I get for focusing on one way to do it. -34(!) bytes. \$\endgroup\$
    – CAD97
    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.

\$\endgroup\$
8
  • \$\begingroup\$ Does this also print the newlines in between? \$\endgroup\$
    – Adnan
    Mar 25, 2016 at 0:07
  • \$\begingroup\$ @AandN Now it does. :) \$\endgroup\$
    – R. Kap
    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\$
    – Adnan
    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\$
    – R. Kap
    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\$
    – Adnan
    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\$
    – Sp3000
    Mar 26, 2016 at 0:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.