aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorla-ninpre <aaoth@aaoth.xyz>2023-08-29 14:37:05 +0300
committerla-ninpre <aaoth@aaoth.xyz>2023-08-29 14:37:05 +0300
commit94c3b37d6926933cf96c409f543bdbc1ad6eb62a (patch)
tree631ca185f7c6f89913cedb41ccda1bda9b9d6e6a
parente55541a27cea7121d3c09ff10f99ade46d647bf4 (diff)
downloadtictactoe-94c3b37d6926933cf96c409f543bdbc1ad6eb62a.tar.gz
tictactoe-94c3b37d6926933cf96c409f543bdbc1ad6eb62a.zip
refactoring and optimization
- remove logic from drawing code (also simplify) - remove ability to make move with space key - fix bug where game instantly restarts at the end (key release event was triggering the restart)
-rw-r--r--README.md2
-rw-r--r--tictactoe.tal61
2 files changed, 35 insertions, 28 deletions
diff --git a/README.md b/README.md
index e62f4ba..28266e5 100644
--- a/README.md
+++ b/README.md
@@ -17,7 +17,7 @@ you need uxn assembler and emulator. for example, check out [uxn-sdl][1]
## controls
-**arrow keys** to move, **button A** (or control or space) to make your move.
+**arrow keys** to move, **button A** (or control) to make your move.
when the game is over, press any button to restart.
## licence
diff --git a/tictactoe.tal b/tictactoe.tal
index 168c870..a23b95f 100644
--- a/tictactoe.tal
+++ b/tictactoe.tal
@@ -49,38 +49,41 @@
;on-controller .Controller/vector DEO2
init-game BRK
+@on-controller-trap ( -> )
+ .Controller/button DEI ?on-reset/restart
+ BRK
+
@on-controller ( -> )
- [ LIT2 -game/y -game/x ] LDZ SWP LDZ
+ .game/x LDZ .game/y LDZ
( ) .Controller/button DEI
( ) #01 ANDk NIP ?&play
#10 ANDk NIP ?&up
#20 ANDk NIP ?&down
#40 ANDk NIP ?&left
#80 ANDk NIP ?&right
- POP .Controller/key DEI #20 EQUk NIP ?&play
POP POP2 BRK
&play ( x y key -> )
POP
( x y to addr ) SWPk #03 MUL ADD .game/cells ADD
- ( ) LDZk #00 EQU ?&draw
+ ( is empty ) LDZk #00 EQU ?&commit
POP POP2 BRK
- &draw ( x y addr -> )
- ( ) ROTk ROT clear-cursor POP2 POP
- ( ) .game/current-player LDZ SWP STZk
- ( ) POP ROT ROT STH2k draw-move-at
- ( redraw ) STH2r .game/current-player LDZ draw-cursor-at
+ &commit ( x y addr -> )
+ ( save move ) .game/current-player LDZk ROT STZk
+ ( display ) POP STHk SWP2 STH2k draw-move-at
+ ( swap player ) ROTr STHr #03 EOR SWPk STZ
+ ( update cursor ) STH2r ROT draw-cursor-at POP
( ) .game/moves LDZk INC SWP STZk
POP
( game can't be ended before 5 moves ) #05 LTH ?&end
check-win BRK
&up ( x y key -> )
- POP clear-cursor coord-dec !&redraw
+ POP clear-cursor move/- !&redraw
&down ( x y key -> )
- POP clear-cursor coord-inc !&redraw
+ POP clear-cursor move/+ !&redraw
&left ( x y key -> )
- POP clear-cursor SWP coord-dec SWP !&redraw
+ POP clear-cursor SWP move/- SWP !&redraw
&right ( x y key -> )
- POP clear-cursor SWP coord-inc SWP
+ POP clear-cursor SWP move/+ SWP
( >> )
&redraw ( x y -> )
( save x y ) SWPk .game/x STZk
@@ -113,9 +116,12 @@
@draw-cursor-at ( x y col -- )
STH pos-to-xy OVR2 OVR2 .Screen/y DEO2
.Screen/x DEO2
- #0e STHrk #01 draw-line #0e STHrk #02 draw-line .Screen/y DEO2
+ ( top ) #0e STHrk #01 draw-line
+ ( right ) #0e STHrk #02 draw-line
+ ( reset pos ) .Screen/y DEO2
.Screen/x DEO2
- #0e STHrk #02 draw-line #0f STHr #01 !draw-line
+ ( left ) #0e STHrk #02 draw-line
+ ( bottom ) #0f STHr #01 !draw-line
@draw-move-at ( player x y -- )
pos-to-xy
@@ -138,7 +144,7 @@
( win ) DUP ?&endgame
( draw ) .game/moves LDZ #09 EQU ?&endgame
POP JMP2r
- &endgame ;on-reset/restart .Controller/vector DEO2
+ &endgame ;on-controller-trap .Controller/vector DEO2
#0023 .Screen/x DEO2
#0043 .Screen/y DEO2
( >> )
@@ -161,14 +167,12 @@
JMP2r
&x ( fig -- )
;x .Screen/addr DEO2
- .Screen/sprite DEO
- ( | yes, drawing code does logic, i know )
- [ LIT2 02 -game/current-player ] STZk
- JMP2r
+ !&end
&o ( fig -- )
;o .Screen/addr DEO2
+ ( >> )
+ &end ( col -- )
.Screen/sprite DEO
- [ LIT2 01 -game/current-player ] STZ
JMP2r
@draw-line ( len col dir -- )
@@ -184,13 +188,16 @@
@clear-cursor ( x y -- x y )
DUP2 #00 !draw-cursor-at
-@coord-dec ( c -- c' )
- #00 EQUk NIP ?&end
- #01 SUB &end JMP2r
-
-@coord-inc ( c -- c' )
- #02 EQUk NIP ?&end
- INC &end JMP2r
+@move ( c -- c' )
+ &+ ( c -- c++ )
+ #02 EQUk NIP ?&end
+ INC !&end
+ &- ( c -- c-- )
+ #00 EQUk NIP ?&end
+ #01 SUB
+ ( >> )
+ &end ( -- )
+ JMP2r
@and3 ( a b c -- x )
LDZ SWP LDZ ROT LDZ AND AND JMP2r