Creating a falling ball map

16

2

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!

Adnan

Posted 2016-03-17T18:08:17.137

Reputation: 41 965

1@DonMuesli Haha, that can happen to anyone :p – Adnan – 2016-03-17T19:25:01.377

Answers

7

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

Denker

Posted 2016-03-17T18:08:17.137

Reputation: 6 639

4

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}}

Value Ink

Posted 2016-03-17T18:08:17.137

Reputation: 10 608

puts adds a newline to the end (while Ruby's print doesn't), so it's been updated to 5 newlines. – Value Ink – 2016-03-17T18:59:40.833

All hail the times function for saving me 3 bytes over the for loop :D – Value Ink – 2016-03-17T19:02:01.460

1

57 bytes: ->n{n.times{puts (('_'*25+' '*5)*2)[rand(30),30]+"\n"*5}} Try here

– Alex A. – 2016-03-17T19:09:19.113

You don't need to assign the lambda as part of the submission since we don't require functions to be named. – Alex A. – 2016-03-17T19:15:53.440

But then how am I supposed to call the function? – Value Ink – 2016-03-17T19:16:19.590

1You 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. – 2016-03-17T19:18:37.893

3

Pyth, 19 bytes

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

Try it here

Thanks to FryAmTheEggman for catching a bug.

Mike Bufardeci

Posted 2016-03-17T18:08:17.137

Reputation: 1 680

@AandN FryAmTheEggman is right, I have changed it to be randomly shifted by 0-29 characters instead of 0-25. – Mike Bufardeci – 2016-03-17T18:55:23.403

3

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

Alex A.

Posted 2016-03-17T18:08:17.137

Reputation: 23 761

3

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.

Matt

Posted 2016-03-17T18:08:17.137

Reputation: 1 075

@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 – 2016-03-18T13:49:49.590

@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 – 2016-03-18T16:05:17.927

2

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

Luis Mendo

Posted 2016-03-17T18:08:17.137

Reputation: 87 464

2

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.

CAD97

Posted 2016-03-17T18:08:17.137

Reputation: 1 367

By putting the i++ the last place i is used, you can save a byte. – Blue – 2016-03-18T19:09:56.773

Smart. Hacky, but smart. Smarter still is me somehow leaving meaningless whitespace at the beginning of my snippet (s = ""). Both are fixed now. – CAD97 – 2016-03-18T19:14:25.903

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 – 2016-03-18T19:18:42.050

Btw great answer – Blue – 2016-03-18T19:19:02.050

That's what I get for focusing on one way to do it. -34(!) bytes. – CAD97 – 2016-03-18T19:29:40.603

2

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.

Brian

Posted 2016-03-17T18:08:17.137

Reputation: 1 209

1

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]

DaveBensonPhillips

Posted 2016-03-17T18:08:17.137

Reputation: 111

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 – 2016-03-26T00:36:19.073

1

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.

R. Kap

Posted 2016-03-17T18:08:17.137

Reputation: 4 730

Does this also print the newlines in between? – Adnan – 2016-03-25T00:07:15.483

@AandN Now it does. :) – R. Kap – 2016-03-25T00:08:35.553

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 – 2016-03-25T00:11:56.583

@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 – 2016-03-25T00:14:08.243

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 – 2016-03-25T00:15:58.087

@AandN Actually, I just tested it right now, and you're right! I actually forgot that, even though it starts from 0 and goes up to 9, that's still 10 numbers. Therefore, I have now corrected by golfed code. It should work perfectly now. :) – R. Kap – 2016-03-25T00:19:37.650

That saves 2 bytes :). It's a very nice answer! – Adnan – 2016-03-25T00:21:08.327

@AandN Thanks! I really appreciate that! – R. Kap – 2016-03-25T00:22:50.543