Browse code

(pig.tal) Added pig game in exercises

neauoire authored on 06/04/2022 18:24:43
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,70 @@
1
+( Pig:
2
+	Each turn you roll a die and add the total to your points.
3
+	You can stop at any time and keep that total, or keep rolling.
4
+	If you ever roll a 1 you lose all the points you accrued. )
5
+
6
+|10 @Console &vector $2 &read $1 &pad $5 &write $1 &error $1
7
+
8
+|0000 @t $1 ( Total saved )
9
+
10
+|0100 @game ( -> )
11
+
12
+	#00 .t STZ
13
+	;input-main .Console/vector DEO2
14
+	,input-main/main JMP
15
+
16
+@roll ( -- dice )
17
+
18
+	[ LIT2 &r f793 ]
19
+	( 5*R+35 ) #0005 MUL2 #0023 ADD2
20
+	( R-R/6547*6547 ) DUP2 #1993 DIV2 #1993 MUL2 SUB2
21
+	DUP2 #c5 DEI2 ADD2 ,&r STR2 ADD ( mod ) #06 DIVk MUL SUB
22
+
23
+JMP2r
24
+
25
+@input-main ( -> )
26
+
27
+	.Console/read DEI
28
+	LIT '0 EQUk NIP ,&no JCN
29
+	LIT '1 EQUk NIP ,&yes JCN
30
+	( ignore other inputs )
31
+	POP
32
+
33
+BRK
34
+	&no ( char -- )
35
+		POP ;score-txt ,pstr JSR .t LDZ ,pdec JSR ;byte-txt ,&halt JMP
36
+	&yes ( char -- )
37
+		POP ,roll JSR ;rolled-txt ,pstr JSR INCk ,pdec/d JSR DUP ,&not-bust JCN
38
+	&bust ( char -- )
39
+		POP ;bust-txt
40
+	&halt ( msg* -- )
41
+		,pstr JSR #0a .Console/write DEO #010f DEO BRK
42
+	&not-bust ( dice -- )
43
+		INC .t LDZ ADD .t STZ 
44
+	&main ( -- )
45
+		;total-txt ,pstr JSR .t LDZ ,pdec JSR ;roll-txt ,pstr JSR BRK
46
+
47
+@pdec ( value -- )
48
+
49
+	DUP #0a DIV ,&emit JSR
50
+	&d #0a DIVk MUL SUB ,&emit JSR
51
+	#0a .Console/write DEO
52
+
53
+JMP2r
54
+	&emit #30 ADD .Console/write DEO JMP2r
55
+
56
+@pstr ( str* -- )
57
+
58
+	&while
59
+		LDAk DUP LIT '_ EQU #3f MUL SUB .Console/write DEO
60
+		INC2 LDAk ,&while JCN
61
+	POP2
62
+
63
+JMP2r
64
+
65
+@total-txt "Your_current_total_is:_ $1
66
+@roll-txt "Would_you_like_to_roll?_(0_no,_1_yes)_ $1
67
+@score-txt "Your_score_is:_ $1
68
+@rolled-txt "You_rolled:_ $1
69
+@bust-txt "Busted! $1
70
+@byte-txt "Goodbye. $1