16
\$\begingroup\$

In this challenge, you must output an ascii-art of a random die roll.

like this:

 ________
 /\ \
 / \ 6 \
{ 4 }-------}
 \ / 5 /
 \/_______/

Please note that:

 ________
 /\ \
 / \ 3 \
{ 4 }-------}
 \ / 5 /
 \/_______/

is invalid output, because that is not a possible result on a die

There are 6(faces that could be up)*4(faces that could be the left face after the top is determined)*1(faces that could be the right face after the other two are determined)=24 possibilities of die rolls.

Your program must output one of these die rolls in the form of an ascii art (modeled like the one below, with the x y and zs replaced with numbers) like above, with each output having>0 probability of occurring, but the probabilities do not have to be even (they are allowed to be trick dice, unlike in real life). Your program cannot output an invalid die roll, or a non die roll. Your program must have a probability of 1 of outputting a valid roll

Please note that your die does not necessarily have to be a right handed die like shown in the first image. (right- and left-handed describe the die's net)

right-handed die
 ________
 /\ \ net
 / \ z \ _|4|_ _
{ x }-------} |6|2|1|5|
 \ / y / |3|
 \/_______/
left handed die
 ________
 /\ \ net
 / \ y \ _|3|_ _
{ x }-------} |6|2|1|5|
 \ / z / |4|
 \/_______/

If your die is left handed, the following is valid output, but not if your die is right handed:

 ________
 /\ \
 / \ 2 \
{ 1 }-------}
 \ / 3 /
 \/_______/

While you can choose left handed or right handed, your die has to be consistent: it cannot change from left to right or vice versa

the following is a list of valid outputs for the die. Refer to the pictures above for positions of X-Y-Z:

X-Y-Z
-----
5-4-1
1-5-4
4-1-5
5-6-4
4-5-6
6-4-5
5-3-6
6-5-3
3-6-5
5-1-3
3-5-1
1-3-5
2-6-3
3-2-6
6-3-2
2-4-6
6-2-4
4-6-2
2-1-4
4-2-1
1-4-2
2-3-1
1-2-3
3-1-2

again this is , so fewer bytes is better

asked Aug 7, 2016 at 4:10
\$\endgroup\$
5
  • 4
    \$\begingroup\$ Solvers might be interested in a formula for the third die face given the other two. \$\endgroup\$ Commented Aug 7, 2016 at 4:15
  • \$\begingroup\$ @xnor and as such I think that's a strong argument that this question is a duplicate \$\endgroup\$ Commented Aug 7, 2016 at 4:20
  • 1
    \$\begingroup\$ I don't know though of any ASCII art to draw a cube in this projection. With the small size though, I don't know if one can do better than hardcoding in languages like Python. \$\endgroup\$ Commented Aug 7, 2016 at 4:25
  • \$\begingroup\$ Related: meta.codegolf.stackexchange.com/a/8197/17602 \$\endgroup\$ Commented Aug 7, 2016 at 9:17
  • \$\begingroup\$ @xnor indeed, even with some 7 and 8 char sequences in there. \$\endgroup\$ Commented Aug 7, 2016 at 17:41

7 Answers 7

9
\$\begingroup\$

Python 3, (削除) 197 196 192 (削除ここまで)190 bytes

from random import*;c=choice;r=range(1,7);u=c(r);l=c(list({*r}-{u,u^7}));print(r''' ________
 /\ \
 / \%4d \
{ %d }-------}
 \ /%4d /
 \/_______/'''%(u,l,3*u*l*(u*u-l*l)%7))

Try it online!

Right-handed (switch to a lefty by swapping u*u with l*l on the last line)

Bound to be beat - but let's get the dice rolling sigh - especially since all my attempts to golf the ASCII except going raw and using old-school formatting all failed to save bytes;

  • any further golfing tips for a n00b gladly appreciated.
answered Aug 7, 2016 at 17:28
\$\endgroup\$
5
  • 1
    \$\begingroup\$ You can save 1 byte by using from random import* and c=choice. \$\endgroup\$ Commented Aug 7, 2016 at 17:50
  • \$\begingroup\$ @daHugLenny - didn't now I could miss that white space before the *; ta! \$\endgroup\$ Commented Aug 7, 2016 at 18:20
  • 2
    \$\begingroup\$ Welcome to PPCG! Nice first post! \$\endgroup\$ Commented Aug 7, 2016 at 19:24
  • \$\begingroup\$ Thanks @GamrCorps - I have been meaning to do some for a while... now I have rep I can post a meaningful solution on a protected question... :D \$\endgroup\$ Commented Aug 7, 2016 at 19:37
  • 2
    \$\begingroup\$ Instead of having 3 spaces and then %d, use %4d instead and it'll pad it properly for you. You might be able to leverage it for other parts of the die as well. \$\endgroup\$ Commented Aug 8, 2016 at 11:32
5
\$\begingroup\$

C,177

f(r){r=rand()%24;r=(5545>>r%4*3&63^256-(r*2&8))*513>>r/8*3;printf(" ________\n /\\%9s / \\%4d \\\n{ %d }-------}\n \\ /%4d /\n \\/_______/","\\\n",7&r,7&r/8,7&r/64);}

In test program

f(r){r=rand()%24;
r=(5545>>r%4*3&63^256-(r*2&8))*513>>r/8*3;
printf(" ________\n /\\%9s / \\%4d \\\n{ %d }-------}\n \\ /%4d /\n \\/_______/","\\\n",7&r,7&r/8,7&r/64);}
j;
main(){
 for(j=99;j--;puts(""))f();
}

Explanation

r= \\after calculation, assign to r (in order to use only one variable.)
(5545>>r%4*3&63 \5545円 is 12651 in octal. Select 2 digts for the equator
^256-(r*2&8)) \\if 4's bit of r is 0, prepend 4=256/64. Else prepend 3 and reverse one of the faces by xoring with 7. 256-8 = 248 = 3*64+7*8.
*513 \\now we have a 3 digit octal number. duplicate all digits by multiplying by 1001 octal.
>>r/8*3 \\rightshift 0,1 or 2 digits to rotate. 
answered Aug 9, 2016 at 0:10
\$\endgroup\$
5
\$\begingroup\$

Javascript (削除) 238 (削除ここまで) (削除) 232 (削除ここまで) (削除) 207 (削除ここまで) 201 bytes

var r=24*Math.random()|0,o=r%3,b=r-o,v="123513653263154214624564";console.log(` ________
 /\\ \\
 / \\ %s \\
{ %s }-------}
 \\ / %s /
 \\/_______/`,v[r],v[b+(o+1)%3],v[b+(o+2)%3])

which when ungolfed is:

var r = 24 * Math.random() | 0,
 o = r % 3,
 b = r - o,
 v = "123513653263154214624564";
console.log(
` ________
 /\\ \\
 / \\ %s \\
{ %s }-------}
 \\ / %s /
 \\/_______/`,
 
 v[r],
 v[b+(o+1)%3]
 ,v[b+(o+2)%3]
)

Algorithm

Consider that at each of the 8 corner intersections of a die, the intersecting die face values are fixed but can appear in any of 3 rotations. For example while looking down at the "1","2","3" corner, the die can be rotated about an axis through the corner and out the opposite corner, to show "1", "2" or "3" on top of the ASCII art.

v hard codes the die faces which intersect at each corner, b is an offset to the start of a random corner, and o is start of the rotation within corner data. The ASCII art is written to the console using a console.log format string.

answered Aug 8, 2016 at 6:55
\$\endgroup\$
2
  • \$\begingroup\$ You can probably shave a few bytes by using ` quotes which permit the use of literal newline characters instead of having to write \n all the time. \$\endgroup\$ Commented Aug 9, 2016 at 14:33
  • \$\begingroup\$ @Neil most appreciated, it worked well. Also removed the trailing ';' \$\endgroup\$ Commented Aug 9, 2016 at 23:44
4
\$\begingroup\$

TSQL 308 bytes

DECLARE @ char(87)=(SELECT
REPLACE(REPLACE(REPLACE(' ________
 /\ \ 
 / \ 7 \ 
{ 8 }-------}
 \ / 9 /
 \/_______/',7,a),8,b),9,3*ABS(a*a*a*b-a*b*b*b)%7)
FROM(SELECT*,SUBSTRING(REPLACE(STUFF(123456,a,1,''),7-a,''),CAST(RAND()*4as int)+1,1)b
FROM(SELECT CAST(RAND()*6as int)+1a)x)x)PRINT @

280 bytes(in Server Management Studio: Query - result to text)

SELECT
REPLACE(REPLACE(REPLACE(' ________
 /\ \ 
 / \ 7 \ 
{ 8 }-------}
 \ / 9 /
 \/_______/',7,a),8,b),9,3*ABS(a*a*a*b-a*b*b*b)%7)
FROM(SELECT*,SUBSTRING(REPLACE(STUFF(123456,a,1,''),7-a,''),CAST(RAND()*4as int)+1,1)b
FROM(SELECT CAST(RAND()*6as int)+1a)x)x

Note: by removing the print and the declare part - and output the result directly from the SELECT. However that would not work in the fiddle

Fiddle

answered Aug 8, 2016 at 10:58
\$\endgroup\$
2
  • \$\begingroup\$ Maybe you can change to ABS(a^3*b-a*b^3) to save 4 bytes? (I'm not sure - seems to work in the fiddle, but it is only ever giving me a single roll of a,b,c=5,1,3 so I may be mistaken) \$\endgroup\$ Commented Aug 9, 2016 at 0:16
  • \$\begingroup\$ @JonathanAllan thank you for your comment. Exponential is written POWER(a,3) in TSQL. ^ has a different meaning \$\endgroup\$ Commented Aug 9, 2016 at 8:20
4
\$\begingroup\$

Javascript, 251 bytes

r="replace";a=()=>(0|Math.random()*6)+1;b=(x,y)=>3*x*y*(x*x+6*y*y)%7;u=()=>{x=a(),y=a();z=b(x,y);if(z==0)u();return" ________\r\n \/\\ \\\r\n \/ \\ 1 \\\r\n{ 2 }-------}\r\n \\ \/ 3 \/\r\n \\\/_______\/"[r](1,x)[r](2,y)[r](3,z);}

Call using u();
It's long but it's an answer, and I haven't answered in a long time.

answered Aug 7, 2016 at 19:57
\$\endgroup\$
2
  • \$\begingroup\$ Missing a space at the top-left of the output test on ideone. \$\endgroup\$ Commented Aug 7, 2016 at 20:09
  • \$\begingroup\$ a=()=>(0|Math.random()*6)+1; should save 8 \$\endgroup\$ Commented Aug 8, 2016 at 4:06
3
\$\begingroup\$

Ruby, 150 bytes

All the string formatting abuse!!!

Credit for formula obtaining the last number goes to @xnor here.

u=1+rand(6)
l=([*1..6]-[u,u^7]).sample
$><<'%11s
 /\%8s
 / \%4d%4s
{ %d }%s}
 \ /%4d%4s
 \/%s/'%[?_*8,?\,円u,?\,円l,?-*7,3*u*l*(u*u-l*l)%7,?/,?_*7]
answered Aug 8, 2016 at 22:33
\$\endgroup\$
2
  • \$\begingroup\$ No - credit for formula goes to xnor \$\endgroup\$ Commented Aug 8, 2016 at 23:28
  • \$\begingroup\$ @JonathanAllan thanks for pointing me towards the proper credit. At any rate, how I formatted this answer was more what I had in mind when I said you could probably leverage string formatting more to your advantage to save bytes. \$\endgroup\$ Commented Aug 8, 2016 at 23:36
1
\$\begingroup\$

Python 3, 181 bytes

from random import*;a,b,c,d=sample(b"FGHJ",k=4);u,l=a-b,c-d;print(r''' ________
 /\ \
 / \%4d \
{ %d }-------}
 \ /%4d /
 \/_______/'''%(u%7,l%7,3*u*l*(u*u-l*l)%7))

Right-handed die. Try it online

Explanation

The bytes b"FGHJ" are equivalent to (0,1,2,4) mod 7. The 4!=24 possible permutations of these numbers happen to give all 24 possibilities for the first two die numbers. I'm not sure how to demonstrate this, except listing all the possibilities:

(0, 1, 2, 4) -> (6, 5)
(0, 1, 4, 2) -> (6, 2)
(0, 2, 1, 4) -> (5, 4)
(0, 2, 4, 1) -> (5, 3)
(0, 4, 1, 2) -> (3, 6)
(0, 4, 2, 1) -> (3, 1)
(1, 0, 2, 4) -> (1, 5)
(1, 0, 4, 2) -> (1, 2)
(1, 2, 0, 4) -> (6, 3)
(1, 2, 4, 0) -> (6, 4)
(1, 4, 0, 2) -> (4, 5)
(1, 4, 2, 0) -> (4, 2)
(2, 0, 1, 4) -> (2, 4)
(2, 0, 4, 1) -> (2, 3)
(2, 1, 0, 4) -> (1, 3)
(2, 1, 4, 0) -> (1, 4)
(2, 4, 0, 1) -> (5, 6)
(2, 4, 1, 0) -> (5, 1)
(4, 0, 1, 2) -> (4, 6)
(4, 0, 2, 1) -> (4, 1)
(4, 1, 0, 2) -> (3, 5)
(4, 1, 2, 0) -> (3, 2)
(4, 2, 0, 1) -> (2, 6)
(4, 2, 1, 0) -> (2, 1)

I found this formula partially by trial-and-error, guided by the fact that the set of possible pairs of numbers possesses certain symmetries (e.g., u can be replaced with -u by swapping a with b, and u and l can be swapped by swapping a with c and b with d). The formula for the third number is xnor's.

answered Dec 9, 2022 at 20:35
\$\endgroup\$

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.