Shop OBEX P1 Docs P2 Docs Learn Events
My Forth game "21 match sticks" — Parallax Forums

My Forth game "21 match sticks"

HydraHackerHydraHacker Posts: 77
edited 2022-06-12 09:56 in Forth

Hello Everyone,
Below is my Forth game "21 match sticks" you play against the P2. So to play the game just type in 21STICKS upper or lower-case it doesn't matter.

HydraHacker

\ constants
21 := TOT_NUM_STKS
7 := BELL
0 := HEADS
1 := TAILS
"t" := T_KEY 
"h" := H_KEY 
"y" := Y_KEY
"n" := N_KEY
0 := PLAYER
1 := P2

\ variables
byte
stksLft

byte
p2Wns

byte
plyrWns

byte
frstNxtMove

\ word definitions
pub PRNTSTKS ( n -- )
stksLft C@ 0= if 20 FOR 42 SPACES CRLF 20 ms NEXT NIP EXIT THEN
DUP TOT_NUM_STKS SWAP - 2*
20 FOR
   2DUP
   SPACES
   FOR
      219 EMIT 
      32 EMIT
   NEXT
   CRLF
   20 ms
   NEXT
2DROP
;

pub INSTRUC ( -- )
CRLF
CRLF
." To play the game '21 match sticks' you are allowed to select-" CRLF 
." between 1-4 match sticks. The Player who draws the last stick-" CRLF 
." 'loses' the game... so good luck!" CRLF
;

pub COINTOSS ( -- n )
GETRND 100 // 1+
FOR
   GETRND 2 // 
   I 1 = IF DUP THEN
   IF ." t" ELSE ." h" THEN 
   1 ms
NEXT
;

pub HEADSTAILS ( -- c )
." Press 'h' for heads or 't' for tails"
CRLF
BEGIN
   WKEY
   DUP H_KEY = OVER T_KEY = OR
   IF DUP EMIT CRLF EXIT ELSE BELL EMIT DROP THEN
AGAIN
;

pub P2MOVES ( st -- ns)
stksLft C@ DUP 6 < IF 1- DUP 0= ABS + NIP EXIT THEN
DROP
DUP
5 / 1+ 5 * SWAP -
DUP 5 = IF GETRND 4 U// DROP 1+ NIP THEN
;

pub WINSTOSS ( c n -- )
SWAP H_KEY = IF HEADS ELSE TAILS THEN =
DUP IF PLAYER frstNxtMove C! ELSE P2 frstNxtMove C! THEN
IF ." Player has 'won' the coin toss!" ELSE ." P2 has 'won' the coin toss!" THEN
;

pub INITVARS
0 0 plyrWns C! p2Wns C!
TOT_NUM_STKS stksLft C!
;

pub PLYRMOVES ( -- numStks)
BEGIN
WKEY
"0" -
DUP
1 stksLft C@ 4 < IF stksLft C@ ELSE 4 THEN
WITHIN
IF EXIT ELSE DROP BELL EMIT THEN
AGAIN
;

pub NEXTMOVE ( -- )
frstNxtMove C@ 1 XOR frstNxtMove C!
;

pub SCORES ( -- )
CRLF
CRLF
." Player #wins " plyrWns C@ . 5 SPACES ." P2 #wins " p2Wns C@ .
;

pub PLYP2MOVES ( -- )
frstNxtMove C@
SWITCH
   PLAYER CASE 
             CRLF 
             CRLF
             ." Player it is your turn " 
             PLYRMOVES 
             stksLft C@ SWAP - DUP stksLft C!
             CRLF
         CRLF
             PRNTSTKS
             BREAK
   P2     CASE 
             CRLF 
             CRLF
             ." Press 'any' key for P2 to make a move "
             WKEY
             DROP
             CRLF
             CRLF
             TOT_NUM_STKS stksLft C@ - P2MOVES 
             stksLft C@ SWAP - DUP stksLft C!
         CRLF
             CRLF
             PRNTSTKS
             BREAK
;

pub WHOWON ( -- )
stksLft C@  0<> if EXIT THEN
frstNxtMove C@ 
SWITCH
   PLAYER CASE
         plyrWns C@ 1+ plyrWns C!
             CRLF
             CRLF
             ." Congratulations 'Player' you have beaten the P2!"
             SCORES
             BREAK
   P2     CASE
         p2Wns C@ 1+ p2Wns C!
         CRLF
         CRLF
         ." The P2!!! has beaten you...sorry!"
             SCORES
         BREAK
;

pub NEWGAME ( -- )
stksLft C@ 0<> IF EXIT THEN
CRLF
CRLF
." Press 'y' for new-game ELSE Press 'n' to exit to TERMINAL "
BEGIN
   WKEY
   SWITCH
      Y_KEY CASE
             TOT_NUM_STKS stksLft C!  
         CRLF
         CRLF
         HEADSTAILS
         CRLF
         COINTOSS
         CRLF
         CRLF
         WINSTOSS
         CRLF
         CRLF
         21 PRNTSTKS
         BREAK
      N_KEY CASE
             TERM
             BREAK
      BELL EMIT
AGAIN
;

pub 21STICKS ( -- )
INITVARS
INSTRUC
CRLF
HEADSTAILS
CRLF
COINTOSS
CRLF
CRLF
WINSTOSS
CRLF
CRLF
21 PRNTSTKS
--- game loop
BEGIN
   PLYP2MOVES
   NEXTMOVE
   WHOWON
   NEWGAME
AGAIN
;

ModEdit: Added extra ticks for code display (triple ticks required)

Sign In or Register to comment.