nand2tetris/projects/09/TicTacToe/Board.vm

377 lines
4.8 KiB
Plaintext

function Board.new 1
push constant 2
call Memory.alloc 1
pop pointer 0
push constant 0
pop local 0
push constant 0
pop this 1
push constant 9
call Array.new 1
pop this 0
label WHILE_EXP0
push local 0
push constant 9
lt
not
if-goto WHILE_END0
push local 0
push this 0
add
push constant 0
pop temp 0
pop pointer 1
push temp 0
pop that 0
push local 0
push constant 1
add
pop local 0
goto WHILE_EXP0
label WHILE_END0
push pointer 0
return
function Board.isRowTaken 0
push argument 0
pop pointer 0
push argument 1
push constant 3
call Math.multiply 2
push this 0
add
pop pointer 1
push that 0
push argument 2
eq
push argument 1
push constant 3
call Math.multiply 2
push constant 1
add
push this 0
add
pop pointer 1
push that 0
and
push argument 2
eq
push argument 1
push constant 3
call Math.multiply 2
push constant 2
add
push this 0
add
pop pointer 1
push that 0
and
push argument 2
eq
if-goto IF_TRUE0
goto IF_FALSE0
label IF_TRUE0
push constant 0
not
return
label IF_FALSE0
push constant 0
return
function Board.isColumnTaken 0
push argument 0
pop pointer 0
push argument 1
push this 0
add
pop pointer 1
push that 0
push argument 2
eq
push argument 1
push constant 3
add
push this 0
add
pop pointer 1
push that 0
and
push argument 2
eq
push argument 1
push constant 6
add
push this 0
add
pop pointer 1
push that 0
and
push argument 2
eq
if-goto IF_TRUE0
goto IF_FALSE0
label IF_TRUE0
push constant 0
not
return
label IF_FALSE0
push constant 0
return
function Board.isWon 0
push argument 0
pop pointer 0
push pointer 0
push constant 0
push argument 1
call Board.isRowTaken 3
push pointer 0
push constant 1
push argument 1
call Board.isRowTaken 3
or
push pointer 0
push constant 1
push argument 1
call Board.isRowTaken 3
or
if-goto IF_TRUE0
goto IF_FALSE0
label IF_TRUE0
push constant 0
not
return
label IF_FALSE0
push pointer 0
push constant 0
push argument 1
call Board.isColumnTaken 3
push pointer 0
push constant 1
push argument 1
call Board.isColumnTaken 3
or
push pointer 0
push constant 2
push argument 1
call Board.isColumnTaken 3
or
if-goto IF_TRUE1
goto IF_FALSE1
label IF_TRUE1
push constant 0
not
return
label IF_FALSE1
push pointer 0
push argument 1
call Board.isDiagonalTaken 2
if-goto IF_TRUE2
goto IF_FALSE2
label IF_TRUE2
push constant 0
not
return
label IF_FALSE2
push constant 0
return
function Board.isDiagonalTaken 0
push argument 0
pop pointer 0
push constant 4
push this 0
add
pop pointer 1
push that 0
push argument 1
eq
not
if-goto IF_TRUE0
goto IF_FALSE0
label IF_TRUE0
push constant 0
return
label IF_FALSE0
push constant 0
push this 0
add
pop pointer 1
push that 0
push argument 1
eq
push constant 8
push this 0
add
pop pointer 1
push that 0
and
push argument 1
eq
if-goto IF_TRUE1
goto IF_FALSE1
label IF_TRUE1
push constant 0
not
return
label IF_FALSE1
push constant 2
push this 0
add
pop pointer 1
push that 0
push argument 1
eq
push constant 6
push this 0
add
pop pointer 1
push that 0
and
push argument 1
eq
if-goto IF_TRUE2
goto IF_FALSE2
label IF_TRUE2
push constant 0
not
return
label IF_FALSE2
push constant 0
return
function Board.winner 0
push argument 0
pop pointer 0
push pointer 0
push constant 1
call Board.isWon 2
if-goto IF_TRUE0
goto IF_FALSE0
label IF_TRUE0
push constant 1
return
label IF_FALSE0
push pointer 0
push constant 2
call Board.isWon 2
if-goto IF_TRUE1
goto IF_FALSE1
label IF_TRUE1
push constant 2
return
label IF_FALSE1
push constant 0
return
function Board.play 2
push argument 0
pop pointer 0
push constant 0
pop local 0
push argument 1
push constant 3
call Math.multiply 2
push argument 2
add
push this 0
add
pop pointer 1
push that 0
push constant 0
eq
not
if-goto IF_TRUE0
goto IF_FALSE0
label IF_TRUE0
push constant 0
return
label IF_FALSE0
push this 1
push constant 2
push this 1
push constant 2
call Math.divide 2
call Math.multiply 2
sub
pop local 1
push local 1
push constant 0
eq
if-goto IF_TRUE1
goto IF_FALSE1
label IF_TRUE1
push constant 1
pop local 0
goto IF_END1
label IF_FALSE1
push constant 2
pop local 0
label IF_END1
push argument 1
push constant 3
call Math.multiply 2
push argument 2
add
push this 0
add
push local 0
pop temp 0
pop pointer 1
push temp 0
pop that 0
push local 0
push constant 1
eq
if-goto IF_TRUE2
goto IF_FALSE2
label IF_TRUE2
push argument 1
push argument 2
call Draw.DrawX 2
pop temp 0
goto IF_END2
label IF_FALSE2
push local 0
push constant 2
eq
if-goto IF_TRUE3
goto IF_FALSE3
label IF_TRUE3
push argument 1
push argument 2
call Draw.DrawO 2
pop temp 0
label IF_FALSE3
label IF_END2
push pointer 0
push constant 1
call Board.isWon 2
if-goto IF_TRUE4
goto IF_FALSE4
label IF_TRUE4
push constant 0
not
return
label IF_FALSE4
push pointer 0
push constant 2
call Board.isWon 2
if-goto IF_TRUE5
goto IF_FALSE5
label IF_TRUE5
push constant 0
not
return
label IF_FALSE5
push this 1
push constant 8
eq
if-goto IF_TRUE6
goto IF_FALSE6
label IF_TRUE6
push constant 0
not
return
label IF_FALSE6
push this 1
push constant 1
add
pop this 1
push constant 0
return