-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmoves.asm
144 lines (127 loc) · 3.74 KB
/
moves.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
Check to see if the selected piece has any moves. We don't even want the player to
be able to select a piece if it can't move anywhere.
Carry Clear = no moves
Carry Set = 1 or more valid moves
*/
HasValidMoves:
ldx movefromindex
lda BoardState, x
sec
rts
/*
Check to make sure the selected piece can move to the moveto location.
Carry Clear = invalid move
Carry Set = valid move
*/
ValidateMove:
sec
rts
/*
Validate that the selected movefrom location contains a piece of the correct color
*/
ValidateFrom:
clf movefromisvalid
ldx movefromindex // Get the piece at the selected location
chk_empty !emptysquare+
chk_mine !notyours+ // My piece?
jsr HasValidMoves // Does this piece have valid moves?
bcs !moveisvalid+
jmp !novalidmoves+
!moveisvalid:
jsr FlashPieceOn // Start flashing the selected piece
jsr DisplayMoveToPrompt
sef movefromisvalid // Set the 'movefromisvalid' flag
jmp !exit+
!emptysquare:
CopyMemory(NoPieceStart, ScreenAddress(ErrorPos), NoPieceEnd - NoPieceStart)
FillMemory(ColorAddress(ErrorPos), NoPieceEnd - NoPieceStart, WHITE)
jmp !clearinput+
!notyours:
CopyMemory(NotYourPieceStart, ScreenAddress(ErrorPos), NotYourPieceEnd - NotYourPieceStart)
FillMemory(ColorAddress(ErrorPos), NotYourPieceEnd - NotYourPieceStart, WHITE)
jmp !clearinput+
!novalidmoves:
CopyMemory(NoMovesStart, ScreenAddress(ErrorPos), NoMovesEnd - NoMovesStart)
FillMemory(ColorAddress(ErrorPos), NoMovesEnd - NoMovesStart, WHITE)
!clearinput:
jsr ResetInput
stb #BIT8:movefromindex
!exit:
rts
/*
Validate that the selected moveto location is valid for the piece selected
*/
ValidateTo:
clf movetoisvalid // Reset the valid move flag
ldx movetoindex // Is the destination an empty square?
chk_empty !checkvalid+ // Empty square?
chk_mine !checkvalid+ // My piece?
!alreadyyours:
CopyMemory(AlreadyYoursStart, ScreenAddress(ErrorPos), AlreadyYoursEnd - AlreadyYoursStart)
FillMemory(ColorAddress(ErrorPos), AlreadyYoursEnd - AlreadyYoursStart, WHITE)
jsr ResetInput
stb #BIT8:movetoindex
jmp !exit+
!checkvalid:
jsr ValidateMove // Are we good?
bcs !isvalid+ // If the move is good, set the carry flag
CopyMemory(InvalidMoveStart, ScreenAddress(ErrorPos), InvalidMoveEnd - InvalidMoveStart)
FillMemory(ColorAddress(ErrorPos), InvalidMoveEnd - InvalidMoveStart, WHITE)
jmp !exit+
!isvalid:
sef movetoisvalid
!exit:
rts
/*
After we've validated that this is a valid move, do the bit shuffling. If there's
a piece in moveto, capture it first and then move the piece.
*/
MovePiece:
jsr FlashPieceOff // Turn off the flashing of the selected piece
ldx movetoindex
chk_empty !movepiece+ // If there's no piece in moveto, just move our piece
and #LOWER7 // Strip color information
cmp #PAWN_SPR // Capture a pawn?
beq !capturepawn+
cmp #KNIGHT_SPR // A knight?
beq !captureknight+
cmp #BISHOP_SPR // A bishop?
beq !capturebishop+
cmp #ROOK_SPR // A rook?
beq !capturerook+
cmp #QUEEN_SPR // A queen?
beq !capturequeen+
jmp !movepiece+
!capturepawn:
ldx #CAP_PAWN
jmp !capturepiece+
!captureknight:
ldx #CAP_KNIGHT
jmp !capturepiece+
!capturebishop:
ldx #CAP_BISHOP
jmp !capturepiece+
!capturerook:
ldx #CAP_ROOK
jmp !capturepiece+
!capturequeen:
ldx #CAP_QUEEN
!capturepiece:
jne currentplayer:#WHITES_TURN:!incrementblack+
inc whitecaptured, x
jmp !movepiece+
!incrementblack:
inc blackcaptured, x
!movepiece:
ldx movetoindex // Move it to the moveto location
stb selectedpiece:BoardState, x
ldx movefromindex // Empty the movefrom location
stb #EMPTY_SPR:BoardState, x
!exit:
rts
/*
Check to see if the current player's king is in check.
*/
CheckKingInCheck:
rts