Skip to content

Instantly share code, notes, and snippets.

@RobinBoers
Last active February 11, 2024 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobinBoers/52a5671f6e9caac6a611554c3376e042 to your computer and use it in GitHub Desktop.
Save RobinBoers/52a5671f6e9caac6a611554c3376e042 to your computer and use it in GitHub Desktop.
Hangman in Commodore 64 BASIC v2 :)
5 gosub 3000
10 input "pick word"; wo$
15 gosub 3000
20 gu$=""
30 wrng$=""
40 li%=10
50 rem initial value for guess.
60 for i = 1 to len(wo$)
70 gu$ = gu$ + "-"
80 next i
90 print "welcome to hangman :)"
100 print " "
110 print gu$
120 print "lifes left:"li%
130 print "guessed: "wrng$
140 print " "
150 input "guess"; y$
160 mult% = 0
170 if not (len(y$) = 1) then mult% = 1
180 if mult% then print "pick a single letter."
190 if mult% then print "try again."
200 if mult% then print " "
210 if mult% goto 150
220 alrdy% = 0
230 rem if(contains(gu, y))
240 x$ = gu$
250 gosub 2000
260 if r% then alrdy% = 1
270 rem if(contains(wrng, y))
280 x$ = wrng$
290 gosub 2000
300 if r% then alrdy% = 1
310 if alrdy% then print "already guessed."
320 if alrdy% then print "try again."
330 if alrdy% then print " "
340 if alrdy% goto 150
350 gosub 3000
360 rem if(contains(wo, y)) show-letters else lose-life
370 x$ = wo$
380 gosub 2000
390 if r% = 1 then gosub 6000
400 if r% = 0 then gosub 7000
410 over% = 0
420 if (gu$ = wo$) or (li% < 1) then over% = 1
430 if over% = 0 goto 100
440 if gu$ = wo$ then print "you won!"
450 if li% < 1 then print "you lost :("
455 print " "
460 print "the word was " + wo$
465 print " "
470 input "again [y/n]"; yn$
480 if yn$ = "y" goto 5
490 print "bye!"
500 end
1000 rem general functions.
1010 rem these functions can take up to three arguments, in x$, y$, and $z
1020 rem they can return something by mutating r$ or r%.
2000 rem contains(x: haystack, y: needle) -> bool
2010 r% = 0
2020 for i = 1 to len(x$)
2030 if mid$(x$, i, 1) = y$ then r% = 1
2040 next i
2050 return
3000 rem clear-screen() -> void
3010 print chr$(147)
3020 return
5000 rem game specific functions.
5010 rem These can mutate all game state.
6000 rem show-letters() -> void
6010 rem copies all the occurences of y$ in wo$ to gu$.
6020 print "correct!"
6030 for i = 1 to len(wo$)
6040 if mid$(wo$, i, 1) = y$ then gu$ = left$(gu$, i-1) + y$ + mid$(gu$, i+1)
6050 next i
6060 return
7000 rem lose-life() -> void
7010 rem adds y$ to wrng$ and decrements li% by one.
7020 print "wrong!"
7030 wrng$ = wrng$ + y$
7040 li% = li% - 1
7050 return
#!/usr/bin/bash -eux
# Depends on VICE.
IN="hangman.c64"
ASSEMBLED="out.prg"
DISK="out.d64"
build() {
petcat -w2 -o "$ASSEMBLED" -- "$IN"
c1541 -format diskname,id d64 "$DISK" -attach out.d64 -write "$ASSEMBLED" "$ASSEMBLED"
}
run() {
x64 "$DISK"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment