Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question
    
Thanks! I had read "are not allowed" :-D – Luis Mendo Mar 17 at 19:24
1  
@DonMuesli Haha, that can happen to anyone :p – Adnan Mar 17 at 19:25

10 Answers 10

up vote 7 down vote accepted

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
share|improve this answer

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}}
share|improve this answer
    
puts adds a newline to the end (while Ruby's print doesn't), so it's been updated to 5 newlines. – Kevin Lau - not Kenny Mar 17 at 18:59
    
All hail the times function for saving me 3 bytes over the for loop :D – Kevin Lau - not Kenny Mar 17 at 19:02
1  
57 bytes: ->n{n.times{puts (('_'*25+' '*5)*2)[rand(30),30]+"\n"*5}} Try here – Alex A. Mar 17 at 19:09
    
You don't need to assign the lambda as part of the submission since we don't require functions to be named. – Alex A. Mar 17 at 19:15
1  
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. – Alex A. Mar 17 at 19:18

Pyth, 19 bytes

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

Try it here

Thanks to FryAmTheEggman for catching a bug.

Breakdown to come if this answer hasn't been beaten by the time I get to a computer.

share|improve this answer
    
@AandN FryAmTheEggman is right, I have changed it to be randomly shifted by 0-29 characters instead of 0-25. – Mike Bufardeci Mar 17 at 18:55

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

share|improve this answer

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.

share|improve this answer
    
@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. – Matt Mar 18 at 13:49
    
@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. – Matt Mar 18 at 16:05

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.

share|improve this answer

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
share|improve this answer

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.

share|improve this answer
    
By putting the i++ the last place i is used, you can save a byte. – Blue Mar 18 at 19:09
    
Smart. Hacky, but smart. Smarter still is me somehow leaving meaningless whitespace at the beginning of my snippet (s = ""). Both are fixed now. – CAD97 Mar 18 at 19:14
    
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 – Blue Mar 18 at 19:18
    
Btw great answer – Blue Mar 18 at 19:19
    
That's what I get for focusing on one way to do it. -34(!) bytes. – CAD97 Mar 18 at 19:29

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.

share|improve this answer
    
Does this also print the newlines in between? – Adnan Mar 25 at 0:07
    
@AandN Now it does. :) – R. Kap Mar 25 at 0:08
    
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). – Adnan Mar 25 at 0:11
    
@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. – R. Kap Mar 25 at 0:14
    
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. – Adnan Mar 25 at 0:15

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]
share|improve this answer
    
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 – Sp3000 Mar 26 at 0:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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