Commodore C64/128, C64Mini and compatibles, 101 95 92 tokenised BASIC bytes + 6 penalty bytes for only working in Commodore BASIC
As Commodore keyword abbreviations, you may enter it as follows:
0a$="i survived":fOi=0to5:a$(i)=a$:nE:a$(rN(.)*6)="":x=6*rN(.)
1?len(a$(x))/len(a$(x))"{left}{left}"a$
I found a six-tokenised byte saving by not using the INT
keyword when assigning a value with RND
and CBM BASIC will automatically convert the key value to it's integer by ignoring the decimal places.
Non-obfuscated for explanatory purposes:
0 let a$="i survived"
1 for i=0 to 5
2 let a$(i)=a$
3 next i
4 let a$(int(rnd(0)*6)=""
5 let x=int(rnd(0)*6)
6 print len(a$(x))/len(a$(x));"{left}{left}";a$(x)
Firstly, we set up a$
as a string and we later use it as an array; in Microsoft [Commodore] BASIC that appeared on the PET, VIC-20, C64 etc... you can set up the first 11 elements of a zero-indexed array without expressly declaring it first, though it saves space on the variable stack if you do and you have fewer than 11 elements.
So our string array of six elements are set to the string literal of i survived
. One of those elements is randomly reset to an empty string.
The x
variable is then used to select which of the six will be displayed (one of which is empty). Finally, we try to divide the length of that element with itself; usually this would produce an output of 1
except where the string length of the element is 0
, so 0/0
will produce an error (one in six chance) and halt the execution of the symbolic listing; otherwise, the print
command moves the cursor position back two spaces to overwrite the output with the string literal of the array element x
, or i survived
.
This answer assumes that the division by zero
error reported by BASIC (which halts the programs execution by the interpreter) is acceptable as the program crashing
as cited above.
randrange(5)
might be implemented asrandrange(MAX_INT)%6
. \$\endgroup\$