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 code-golf, so the submission with the least amount of bytes wins!
-
1\$\begingroup\$ @DonMuesli Haha, that can happen to anyone :p \$\endgroup\$Adnan– Adnan2016年03月17日 19:25:01 +00:00Commented Mar 17, 2016 at 19:25
10 Answers 10
Pyth, 19 bytes
VQ.>+*\_25*d5O30*b4
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
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}}
-
\$\begingroup\$
putsadds a newline to the end (while Ruby'sprintdoesn't), so it's been updated to5newlines. \$\endgroup\$Value Ink– Value Ink2016年03月17日 18:59:40 +00:00Commented Mar 17, 2016 at 18:59 -
\$\begingroup\$ All hail the
timesfunction for saving me 3 bytes over theforloop :D \$\endgroup\$Value Ink– Value Ink2016年03月17日 19:02:01 +00:00Commented Mar 17, 2016 at 19:02 -
1
-
\$\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.– Alex A.2016年03月17日 19:15:53 +00:00Commented 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.– Alex A.2016年03月17日 19:18:37 +00:00Commented Mar 17, 2016 at 19:18
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.
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.
-
\$\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– Matt2016年03月18日 13:49:49 +00:00Commented 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– Matt2016年03月18日 16:05:17 +00:00Commented Mar 18, 2016 at 16:05
-
\$\begingroup\$ @AandN FryAmTheEggman is right, I have changed it to be randomly shifted by 0-29 characters instead of 0-25. \$\endgroup\$Mike Bufardeci– Mike Bufardeci2016年03月17日 18:55:23 +00:00Commented Mar 17, 2016 at 18:55
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.
MATL, 23 bytes
:"95l25X"5Z"h30YrYS10ct
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
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
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.
-
\$\begingroup\$ By putting the i++ the last place i is used, you can save a byte. \$\endgroup\$Blue– Blue2016年03月18日 19:09:56 +00:00Commented 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– CAD972016年03月18日 19:14:25 +00:00Commented 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– Blue2016年03月18日 19:18:42 +00:00Commented Mar 18, 2016 at 19:18
-
\$\begingroup\$ Btw great answer \$\endgroup\$Blue– Blue2016年03月18日 19:19:02 +00:00Commented Mar 18, 2016 at 19:19
-
\$\begingroup\$ That's what I get for focusing on one way to do it. -34(!) bytes. \$\endgroup\$CAD97– CAD972016年03月18日 19:29:40 +00:00Commented Mar 18, 2016 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.
-
\$\begingroup\$ Does this also print the newlines in between? \$\endgroup\$Adnan– Adnan2016年03月25日 00:07:15 +00:00Commented Mar 25, 2016 at 0:07
-
\$\begingroup\$ @AandN Now it does. :) \$\endgroup\$R. Kap– R. Kap2016年03月25日 00:08:35 +00:00Commented Mar 25, 2016 at 0:08
-
\$\begingroup\$ You actually need to print 4 newlines :p. So changing
'\r'to'\r'*4should probably work. Also, I think you can changerange(a+1)torange(a). \$\endgroup\$Adnan– Adnan2016年03月25日 00:11:56 +00:00Commented Mar 25, 2016 at 0:11 -
\$\begingroup\$ @AandN Actually, using
range(a)actually prints outa-1number of lines, sorange(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– R. Kap2016年03月25日 00:14:08 +00:00Commented 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 changinga+1toawould probably fix that. \$\endgroup\$Adnan– Adnan2016年03月25日 00:15:58 +00:00Commented Mar 25, 2016 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]
-
\$\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
ntimes \$\endgroup\$Sp3000– Sp30002016年03月26日 00:36:19 +00:00Commented Mar 26, 2016 at 0:36