Skip to content

Instantly share code, notes, and snippets.

@JamesKohli
Last active August 29, 2015 14:01
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 JamesKohli/0cd2493bfdab7b730a11 to your computer and use it in GitHub Desktop.
Save JamesKohli/0cd2493bfdab7b730a11 to your computer and use it in GitHub Desktop.
########
#SPRITES
########
: flagsprite
0b01110000
0b01110000
0b01110000
0b01000000
0b01000000
: blank
0b00000000
0b00000000
0b00100000
0b00000000
0b00000000
: cursor
0b11111000
0b10001000
0b10001000
0b10001000
0b11111000
: mine
0b10001000
0b01010000
0b00100000
0b01010000
0b10001000
: winner
0b01010100
0b01010100
0b01010100
0b01111100
0b00000000
0b00010000
0b00010000
0b00010000
0b00010000
0b00000000
0b01000100
0b01100100
0b01010100
0b01001100
##############
#CONSTANTS
##############
#Number of neighboring mines for each space (filled by :populateneighbors)
: spacevalue
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
#Player view of each space. 0 = not checked, 1 = visible, 2 = flagged
: spacestate
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
#Spaces with mines (filled by :generatemine)
: mines
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
#GRID LAYOUT NEIGHBORS LAYOUT
#0 1 2 3 4 5 6 7 0 7 6
#8 9 10 11 12 13 14 15 1 5
#16 17 18 19 20 21 22 23 2 3 4
#24 25 26 27 28 29 30 31
: neighbors
255 255 255 8 9 1 255 255
255 0 8 9 10 2 255 255
255 1 9 10 11 3 255 255
255 2 10 11 12 4 255 255
255 3 11 12 13 5 255 255
255 4 12 13 14 6 255 255
255 5 13 14 15 7 255 255
255 6 14 15 255 255 255 255
255 255 255 16 17 9 1 0
0 8 16 17 18 10 2 1
1 9 17 18 19 11 3 2
2 10 18 19 20 12 4 3
3 11 19 20 21 13 5 4
4 12 20 21 22 14 6 5
5 13 21 22 23 15 7 6
6 14 22 23 255 255 255 7
255 255 255 24 25 17 9 8
8 16 24 25 26 18 10 9
9 17 25 26 27 19 11 10
10 18 26 27 28 20 12 11
11 19 27 28 29 21 13 12
12 20 28 29 30 22 14 13
13 21 29 30 31 23 15 14
14 22 30 31 255 255 255 15
255 255 255 255 255 25 17 16
16 24 255 255 255 26 18 17
17 25 255 255 255 27 19 18
18 26 255 255 255 28 20 19
19 27 255 255 255 29 21 20
20 28 255 255 255 30 22 21
21 29 255 255 255 31 23 22
22 30 255 255 255 255 255 23
: coordinates
0 0
8 0
16 0
24 0
32 0
40 0
48 0
56 0
0 8
8 8
16 8
24 8
32 8
40 8
48 8
56 8
0 16
8 16
16 16
24 16
32 16
40 16
48 16
56 16
0 24
8 24
16 24
24 24
32 24
40 24
48 24
56 24
##################
#DEBUG TOOLS
##################
: drawcheckedmine
i := coordinates
vA <<= vA
i += vA
load v1
vA >>= vA
i := mine
sprite v0 v1 5
;
: checkmine
i := mines
i += vA
load v0
if v0 == 1 then drawcheckedmine
;
: drawmines
vA := 0
loop
checkmine
while vA != 31
vA += 1
again
;
: drawvalue
i := spacevalue
i += v9
load v0
vA := v0
i := coordinates
v9 <<= v9
i += v9
v9 >>= v9
load v1
i := hex vA
sprite v0 v1 5
;
: drawvalues
v9 := 0
loop
drawvalue
while v9 != 31
v9 += 1
again
;
: drawregisters
vA := 0
vB := 0
i := hex v0
sprite vA vB 5
vA := 8
i := hex v1
sprite vA vB 5
vA := 16
i := hex v2
sprite vA vB 5
vA := 24
i := hex v3
sprite vA vB 5
vA := 32
i := hex v4
sprite vA vB 5
vA := 40
i := hex v5
sprite vA vB 5
vA := 48
i := hex v6
sprite vA vB 5
vA := 56
i := hex v7
sprite vA vB 5
;
##########
#METHODS
############
: populate
if vD == 255 then return
i := mines
i += vD
load v0
if v0 == 1 then v9 += 1
;
: loadneighbors
#v9 will be our mine counter
v9 := 0
#we're loading in sets of 8, so vE needs to be x8
vE <<= vE
vE <<= vE
vE <<= vE
#get all the neighboring squares to check
i := neighbors
i += vE
load v7
#v0-v7 each have a neighbor, check each one and increment v9 if necessary
vD := v0
populate
vD := v1
populate
vD := v2
populate
vD := v3
populate
vD := v4
populate
vD := v5
populate
vD := v6
populate
vD := v7
populate
#need to shift vE back to its original flavor
vE >>= vE
vE >>= vE
vE >>= vE
i := spacevalue
i += vE
v0 := v9
save v0
;
#figure out how many neighboring mines each space has
: populateneighbors
#we load neighbors once for each square
vE := 0
loop
loadneighbors
while vE != 31
vE += 1
again
;
#randomly put a mine somewhere in :mines
: generatemine
i := mines
v0 := random 0b00011111
i += v0
v0 := 1
save v0
;
: sync
v2 := 4
delay := v2
loop
v2 := delay
if v2 != 0 then
again
;
#Handles WASD cursor movement, also draws the cursor at the moment
: drawcursor
#get cursor coordinates and draw
vC <<= vC
i := coordinates
i += vC
vC >>= vC
load v1
i := cursor
sprite v0 v1 5
;
#start the game over
: reset
vC := 0
v9 := 0
loop
i := mines
i += v9
v0 := 0
save v0
i := spacestate
i += v9
v0 := 0
save v0
i := spacevalue
i += v9
v0 := 0
save v0
while v9 != 31
v9 += 1
again
generatemine
generatemine
generatemine
generatemine
generatemine
generatemine
generatemine
populateneighbors
#this is to break out of a gameover or win loop
v1 := 1
;
: gameover
v2 := 0
loop
i := coordinates
v2 <<= v2
i += v2
load v1
v2 >>= v2
i := mine
sprite v0 v1 5
while v2 != 31
v2 += 1
again
v1 := 0
loop
v0 := 0x0D
if v0 key then reset
while v1 != 1
again
;
: getspacevalue
i := spacevalue
i += v9
load v0
i := hex v0
;
#draw the board
: draw
v9 := 0
loop
#get the coordinates at v9
i := coordinates
v9 <<= v9
i += v9
v9 >>= v9
load v1
v3 := v0
v4 := v1
#get the gamestate at v9
i := spacestate
i += v9
load v0
#pick which sprite to use
if v0 == 0 then i := blank
if v0 == 2 then i := flagsprite
if v0 == 1 then getspacevalue
sprite v3 v4 5
while v9 != 31
v9 += 1
again
;
#if a mine has been correctly flagged by a player, increment the win counter
: confirmflagged
i := spacestate
i += v9
load v0
if v0 == 2 then v7 += 1
v8 := 1
;
#if a space has been checked by the player, increment the win counter
: confirmspacechecked
i := spacestate
i += v9
load v0
if v0 == 1 then v7 += 1
;
#Win loop
: youwin
clear
i := winner
v0 := 30
v1 := 8
sprite v0 v1 14
v1 := 0
loop
v0 := 0x0D
if v0 key then reset
while v1 != 1
again
;
#if all mines have flags and all other spacestates are 1, you win
: checkwin
v9 := 0
#v7 is our win counter... we need 32 correct spaces to win
v7 := 0
loop
#v8 will be used as a mine flag
v8 := 0
#check for a mine
i := mines
i += v9
load v0
if v0 == 1 then confirmflagged
if v8 == 0 then confirmspacechecked
while v9 != 31
v9 += 1
if v9 != v7 then return
again
if v7 == 32 then youwin
;
: no ; # a no-op stub for unused entries
: lf if vC != 0 then vC += 255 ;
: rt if vC != 31 then vC += 1 ;
: up
v1 := vC
v1 >>= v1
v1 >>= v1
v1 >>= v1
if v1 != 0 then vC += 248 ;
: dn
v1 := vC
v1 >>= v1
v1 >>= v1
v1 >>= v1
if v1 != 3 then vC += 8 ;
#handles a cursor click on a space (Q key)
: click
#check if space is mined, if so, gameover
i := mines
i += vC
load v0
if v0 == 1 then gameover
#set spacestate = 1
i := spacestate
i += vC
v0 := 1
save v0
;
#handles a flag placing click on a space (E key)
: flag
v1 := 2
i := spacestate
i += vC
load v0
i := spacestate
i += vC
if v0 == v1 then v1 := 0
v0 := v1
save v0
;
#Jump table for key presses
: code jump no jump no jump no jump no
jump click jump up jump flag jump lf
jump dn jump rt jump no jump no
jump no jump reset jump no jump no
: launch
# launch is a sub so that jump
# entries can return to the caller.
# jump table entries are
# 2 bytes, so double the key code:
v0 <<= v0
jump0 code
###########
#THE GRAND FINALE
###########
: main
vC := 0
generatemine
generatemine
generatemine
generatemine
generatemine
generatemine
generatemine
populateneighbors
loop
clear
#drawmines
#drawvalues
draw
sync
drawcursor
v0 := key
launch
checkwin
again
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment