Skip to content

Instantly share code, notes, and snippets.

@nst
Created October 22, 2025 09:47
Show Gist options
  • Select an option

  • Save nst/af7156a67a0f52d1e175165726fb5502 to your computer and use it in GitHub Desktop.

Select an option

Save nst/af7156a67a0f52d1e175165726fb5502 to your computer and use it in GitHub Desktop.
Companion code snippet to "Print To Play" article in PagedOut! #7
% a handwritten Tic-Tac-Toe program stored in a string
/prog(/d{def}def/e{exch}d/M{moveto}d/O{pop}d/g{getinterval
}d/l{length}d/L{lineto}d/I{if}d/P{putinterval}d/D(1234567\
89)d/nf{0 b{46 eq{1 add}I}forall}d/R{/q false d 0 1 8{/i e
d/A b dup l string cvs d A i 1 g(.)eq{[(X)(O)]{/p e d A i
p P A B{b i(X)P/q true d exit}I}forall}I q{exit}I}for q}d
/Q{/x rand nf mod d/c 0 d 0 1 b l 1 sub{/i e d b i 1 g(.)
eq{c x eq{b i(X)P exit}I/c c 1 add d}I}for}d/r{{(human (1\
-9)>)print flush(%lineedit)(r)file(________)readline O dup
l 0 gt{0 1 g}{O ( )}ifelse/o e d D o search{O O O b o cvi
1 sub 1 g(.)eq{o cvi 1 sub exit}I}{O}ifelse(bad input)= S}
loop}d/B{/z e d/N[[0 1 2][3 4 5][6 7 8][0 3 6][1 4 7][2 5
8][0 4 8][2 4 6]]d/V false d[(O)(X)]{/p e d N{/T e d/V
true d T{/U e d/V z U 1 g p eq V and d}forall V{exit}I}
forall V{exit}I}forall V}d/S{0.2 setlinewidth 10 10 scale
20 70 M 20 40 L 30 70 M 30 40 L 10 60 M 40 60 L 10 50 M 40
50 L stroke 0 1 b l 1 sub{/i e d b i 1 g(.)ne{gsave 10 i 3
mod 10 mul add 3 add 70 i 3 idiv 10 mul sub 7 sub M b i 1
g show grestore}I}for m null ne{10 30 M m show}I showpage}
d/C{/E e d/K e d/m null d nf 0 eq{/m(TIE)d/E true d}I b B{
/m(________WINS)d m 0 K P/E true d}I E{S}I m null ne{quit}
I}d/Courier findfont 5 scalefont setfont/b D d/m(HUMAN PL\
AYS O)d S/b(.........)d{/m null d b r(O)P(__HUMAN) false C
R not{Q}I(PRINTER)true C}loop)def
% leave the program on the printer's file system
/f (ttt.ps) (w) file def
f prog writestring f flushfile f closefile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment