Chessboard: ~~~~~~~~~~~ a b c d e f g h +-----+-----+-----+-----+-----+-----+-----+-----+ | |#####| |#####| |#####| |#####| 8 | R |##N##| B |##K##| Q |##B##| N |##R##| | |#####| |#####| |#####| |#####| +-----+-----+-----+-----+-----+-----+-----+-----+ |#####| |#####| |#####| |#####| | 7 |##P##| P |##P##| P |##P##| P |##P##| P | |#####| |#####| |#####| |#####| | +-----+-----+-----+-----+-----+-----+-----+-----+ | |#####| |#####| |#####| |#####| 6 | |#####| |#####| |#####| |#####| | |#####| |#####| |#####| |#####| +-----+-----+-----+-----+-----+-----+-----+-----+ |#####| |#####| |#####| |#####| | 5 |#####| |#####| |#####| |#####| | |#####| |#####| |#####| |#####| | +-----+-----+-----+-----+-----+-----+-----+-----+ | |#####| |#####| |#####| |#####| 4 | |#####| |#####| |#####| |#####| | |#####| |#####| |#####| |#####| +-----+-----+-----+-----+-----+-----+-----+-----+ |#####| |#####| |#####| |#####| | 3 |#####| |#####| |#####| |#####| | |#####| |#####| |#####| |#####| | +-----+-----+-----+-----+-----+-----+-----+-----+ | |#####| |#####| |#####| |#####| 2 | P |##P##| P |##P##| P |##P##| P |##P##| | |#####| |#####| |#####| |#####| +-----+-----+-----+-----+-----+-----+-----+-----+ |#####| |#####| |#####| |#####| | 1 |##R##| N |##B##| K |##Q##| B |##N##| R | |#####| |#####| |#####| |#####| | +-----+-----+-----+-----+-----+-----+-----+-----+ Piece ID: ~~~~~~~~~ White Pawn: #%00000001 White Rook: #%00000010 White kNight: #%00000011 White Bishop: #%00000100 White Queen: #%00000101 White King: #%00000110 Black Pawn: #%10000001 Black Rook: #%10000010 Black kNight: #%10000011 Black Bishop: #%10000100 Black Queen: #%10000101 Black King: #%10000110 Bit #3 signify virgin (0) or moved (1) piece. Bit #4 Signify En Passant Candidate (1) or not (0) Board Coordinates: ~~~~~~~~~~~~~~~~~~ 0, 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 Mapped to ZP adresses: $40 - $7f (0-63)
Then it's the AI.
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CheckLegalPawn: /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ RULES: ~~~~~~ Vertical+1: Pos+8 Vertical+2: Pos+16 <--- Only when virgin CapDia+1: Pos+7 CapDia+1: Pos+9 En Passant: Pos+7 <--- Only when adjacent to fresh pawn opponent (Pos+16) En Passant: Pos+9 <--- Only when adjacent to fresh pawn opponent (Pos+16) Upgrade: Pos = last line (0-7 / 56-63)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CheckLegalRook: /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ RULES: ~~~~~~ Vertical: Pos+8 x [1-8] Vertical: Pos-8 x [1-8] Horizontal: Pos+1->8 Horizontal: Pos-1->8 Castling: Pos+2 <--- Only when K & R are virgin Castling: Pos-3 <--- Only when K & R are virgin //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CheckLegalKing: /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ RULES: ~~~~~~ Pos+1 & Pos-1 pos+7 & pos-7 Pos+8 & Pos-8 pos+9 & pos-9 Castling: Pos-2 <--- Only when K & R are virgin Castling: Pos+3 <--- Only when K & R are virgin //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Checks the path between the two horizontal Squares //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ HorizontalLineClearCheck: // Find number of squares to check lda OldSquare sec sbc SelectedSquare bcs !+ eor #$ff adc #$01 !: tax // Find signed parameter to use in the slope checker lda OldSquare cmp SelectedSquare bcc !+ lda #$ff jmp CheckSlope !: lda #$01 jmp CheckSlope //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Checks the path between the two vertical squares //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ VerticalLineClearCheck: // Find number of squares between the squares (absolute) lda OldSquare sec sbc SelectedSquare bcs !+ eor #$ff adc #$01 !: lsr lsr lsr // Divide the difference with 8 tax // Find signed parameter to use in the slope checker lda OldSquare cmp SelectedSquare bcc !+ lda #$f8 jmp CheckSlope !: lda #$08 jmp CheckSlope //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Checks the path between the two diagonal squares //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ DiagonalLineClearCheck: // Check if the move is left or right lda OldSquare and #%00000111 sta $02 lda SelectedSquare and #%00000111 sec sbc $02 bcs !Left+ eor #$ff adc #$01 !Right: // XPOS SelectedSquare > OldSquare tax // Number of squares to check in X-REG // Find if move is up or down lda OldSquare cmp SelectedSquare bcs !RightUp+ !RightDown: lda #$07 jmp CheckSlope !RightUp: lda #$f7 jmp CheckSlope !Left: // XPOS OldSquare > SelectedSquare tax // Number of squares to check in X-REG // Find if move is up or down lda OldSquare cmp SelectedSquare bcs !LeftUp+ !LeftDown: lda #$09 jmp CheckSlope !LeftUp: lda #$f9 jmp CheckSlope //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Routine for checking if squares between two points are empty //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Parameters: // A- $02 - Loaded with Signed byte for slopecheck: // #$01 - Horizontal Right // #$ff - Horizontal Left // #$08 - Vertical Down // #$f8 - Vertical Up // #$07 - Diagonal Down-Left // #$09 - Diagonal Down-Right // #$f9 - Diagonal Up-Right // #$f7 - Diagonal Up-Left // X-Reg - Squares to check //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CheckSlope: sta $02 // Parameter = Signed number for slope check dex // Decrease with one square to check beq !end+ // If already zero, there was no squares to check // Find first square to check lda OldSquare clc adc $02 // Skip first square since it's occupied by the piece in oldsquare tay !loop: lda $40,y // Fetch Square,Y beq !+ sec rts !: tya clc adc $02 tay dex bne !loop- !end: clc rts