Some chess code which I cant use yet. This code was transferred out of os.asm on 13 feb 2019 ; chess piece move vectors. Put this near the chess code ; format is displacment, current multiplier, limit ; in the case of a knight/king/pawn the multiplier limit ; is 1 (only 1 hop) ; This code doesnt actually work to create arrays ; create 4 , 0 , 0 , 1 , 0 , 1 , 1 , 2 , 0 , does> ; imm : vknight create -25 c, 0 c, 1 c, -23 c, 0 c, 1 c, -14 c, 0 c, 1 c, -10 c, 0 c, 1 c, 14 c, 0 c, 1 c, 10 c, 0 c, 1 c, 25 c, 0 c, 1 c, 23 c, 0 c, 1 c, does> ; imm : vbishop create -13 c, 0 c, 8 c, -11 c, 0 c, 8 c, 13 c, 0 c, 8 c, 11 c, 0 c, 8 c, does> ; imm : vrook [ -12 c, 0 c, 8 c, -1 c, 0 c, 8 c, 12 c, 0 c, 8 c, 1 c, 0 c, 8 c, ] ; : vqueen [ -12 c, 0 c, 8 c, -1 c, 0 c, 8 c, 12 c, 0 c, 8 c, 1 c, 0 c, 8 c, -13 c, 0 c, 8 c, -11 c, 0 c, 8 c, 13 c, 0 c, 8 c, 11 c, 0 c, 8 c, ] ; : vking [ -12 c, 0 c, 1 c, -1 c, 0 c, 1 c, 12 c, 0 c, 1 c, 1 c, 0 c, 1 c, -13 c, 0 c, 1 c, -11 c, 0 c, 1 c, 13 c, 0 c, 1 c, 11 c, 0 c, 1 c, ] ; ; create a board data structure and game state : chess: create 0 c, ./ the turn white or black ; the piece vector will need to be a word since it ; points to a memory location. 0 c, ./ the current piece/square pointer ./ here a move vector pointer. ; The board structure, a 12x8 grid of squares ; 33 are the side squares (off the board) used to make finding ; legal moves easier. The actual board is empty ie 0 8 0 do 33 c, 33 c, 0 c, 0 c, 0 c, 0 c, 0 c, 0 c, 0 c, 0 c, 33 c, 33 c, loop ; a move stack ( for depth search ) ; piece vectors eg pawn +8 +7 +9 which gives possible ; legal moves does> 2 + ; imm ; the depth search will probably be recursive. ; print a square number (12*8 board: 0-97) in ; chess algebraic eg a3, b2 h1. : .square ( n -- ) dup 97 < if nop else ." ??" exit fi 12 /mod swap 2 - [char] a + emit 1+ . ; ; print a chess move where from and to are squares on a 12*8 board ; maybe this should be relative to current board structure ; eg .move ( C from to -- ) : .move ( from to -- ) swap .square ." - " .square ; ; update the board position of C with the ; given move. The Pvector in the C game object will be advanced, or ; the Svector (current piece/square) will be advanced with piece+. : move+ ( C from to -- ) ; get the value at C.from, set C.from = 0, set C.to = value >r >r dup r> ./ C C f r: to + dup ./ C C+f C+f r: to c@ swap c!0 ./ C piece r: to swap r> + ./ piece C.to c! ; ; A pointer to the board structure, for speed. ; set up a board for a new game. Pieces are just numbers. ; piece values are scaled by 2 for accuracy. eg ; rook=10 bishop=7 knight=6 ... ; can just use allot instead of here+ : game.new ( A -- ) ; also reset move stack, turn, square/piece vector etc ; white pieces. I should not use "c," here but c! or c!+ ; otherwise "here" will be corrupted. ; dup 2 - c!0 ./ set turn to white here! ; 33 swap c!+ 2 here+ 10 c, 6 c, 7 c, 18 c, 100 c, 7 c, 6 c, 10 c, 4 here+ 2 c, 2 c, 2 c, 2 c, 2 c, 2 c, 2 c, 2 c, ; now black, all negative 52 here+ -2 c, -2 c, -2 c, -2 c, -2 c, -2 c, -2 c, -2 c, 4 here+ -10 c, -6 c, -7 c, -18 c, -100 c, -7 c, -6 c, -10 c, ; ; make some visual rep for a piece, try asci chars ... : .piece ( n -- ) dup 0 < if abs 4 fg else 2 fg fi dup 100 = if [char] K emit drop exit fi dup 18 = if [char] Q emit drop exit fi dup 10 = if [char] R emit drop exit fi dup 7 = if [char] B emit drop exit fi dup 6 = if [char] N emit drop exit fi dup 2 = if [char] p emit drop exit fi 15 fg . ; ; shows the board. A is a reference to a game or board ; structure : .board ( A -- ) 8 0 do 12 0 do c@+ .piece space loop cr loop ; ; just shows square offset numbers : .grid ( -- ) 96 0 do ii dup 12 mod if u. else cr u. fi space loop ; ; puts a piece P on board at position x y ; eg 0 0 9 game puts a white queen at A1 : game.put ( x y P A -- ) ; ; show whos turn it is. : game.turn ( A -- ) 2 - c@ if ." black to play" else ." white to play" fi ; ; just for testing ; chess: C ; C game.new