Christmas Present (VCCC 2024)

Type
Graphics
Category
Code-golf challenge entry — character graphics
Author
Preppie
Published in
AtariAge forums, "Logiker's 2024 Christmas Challenge" thread (December 2024) — Vintage Computing Christmas Challenge 2024
Year
2024
Language
Atari BASIC
Requirements
Any 8-bit Atari with Atari BASIC (even a 16K Atari 400)
A 19-character square box drawn with plus, minus and exclamation characters, divided into four by a ribbon cross, with a bow centred above it

About the program

Every December, Logiker runs the Vintage Computing Christmas Challenge (VCCC). Everyone gets the same small picture to draw, and the aim is the shortest program that draws it on any vintage machine. It's a friendly code-golf contest, and in 2024 the task was a wrapped present. The box is 19 characters square, edged with +, - and !, with a ribbon cross through the middle and a \O/ bow on top. The results Logiker posted to AtariAge list 22 Atari 8-bit entries, in 6502 assembler, FastBasic, Turbo BASIC XL, Atari BASIC, Action!, Forth, Logo and even a pure display list.

This two-line listing is the version Preppie posted afterwards as "my BASIC version … much too long". It's easy to read, which makes it the best place to start. His submitted Atari BASIC entry did the same job in 96 characters and came 9th in the Atari table.

Typing it in

Plain ASCII. B$ in line 10 is eight spaces, so count them. The \O/ is a backslash, a capital O and a slash. On the forum, line 10 was wrapped after the final ?, so make sure you type it as a single program line. Location 82 is the left margin. POKE 82,10 moves every printed line ten columns in, which roughly centres the 19-character box.

A quirk worth knowing: the new margin only applies from the next line. The first PRINT goes onto the line the cursor is already on, which starts at the old column 2. As posted, the bow therefore came out at column 10 (2 + eight spaces) instead of above the middle of the box. The listing here adds an empty ? after the POKE, so the bow starts on a fresh line at the new margin and sits centred over the ribbon.

How it works

The present is just three kinds of row:

  • +--------+--------+: a border, printed by line 20 three times (top, middle ribbon, bottom).
  • ! ! !: a side, printed eight times between the borders. The middle ! is the vertical ribbon.
  • \O/: the bow, printed once, eight spaces in.

Line 20 prints a border and counts it in Z. While Z<3 it prints eight side rows and jumps back to itself, so the third border ends the drawing.

The golfing trick

To get down to 96 characters, Preppie dropped the strings and computed every character from its position in two FOR loops (X and Y both running 0–18). The heart of it is a piece of Boolean arithmetic:

W=X/9=INT(X/9)        : W is 1 on columns 0, 9 and 18, else 0
K=Y/9=INT(Y/9)        : K is 1 on rows 0, 9 and 18, else 0
PRINT CHR$(32+13*K+W-K*W*3);

Both zero gives 32 (space). W alone gives 33 (!). K alone gives 45 (-). Both together give 32+13+1−3 = 43 (+). In the forum thread, TGB1718 and Preppie explained this to another entrant. DMSC's FastBasic entry (74 characters) instead picks the character by indexing straight into the string " !-+". Try rewriting the two-line version along those lines and see how short you can get it in Atari BASIC.

Changes from the published listing

Line As published Now Why
10 …POKE 82,10:?B$;"\O/":Z=0 …POKE 82,10:?:?B$;"\O/":Z=0 The new left margin only applies from the next line, so the bow printed off-centre, over the box's left corner.

Listing

10 DIM A$(1),B$(8),C$(8):B$="        ":A$="!":C$="--------":POKE 82,10:?:?B$;"\O/":Z=0
20 ? "+";C$;"+";C$;"+":Z=Z+1:IF Z<3 THEN FOR I=1 TO 8:? A$;B$;A$;B$;A$:NEXT I:GOTO 20

Credits