|
| 1 | +(module |
| 2 | + (memory (export "mem") 2) |
| 3 | + (data (i32.const 0) "_ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _\n") |
| 4 | + |
| 5 | + (global $White i32 (i32.const 87)) |
| 6 | + (global $Black i32 (i32.const 66)) |
| 7 | + |
| 8 | + ;; |
| 9 | + ;; Checks a queen positioning for validity and the ability to attack |
| 10 | + ;; |
| 11 | + ;; @param {i32} $positions |
| 12 | + ;; (4*8bit for white row, white column, black row, black column, each 0-7) |
| 13 | + ;; |
| 14 | + ;; @returns {i32} -1 if invalid, 0 if cannot attack, 1 if it can attack |
| 15 | + ;; |
| 16 | + (func $canAttack (export "canAttack") (param $positions i32) (result i32) |
| 17 | + ;; any position > 7 |
| 18 | + (if (i32.or (i32.and (local.get $positions) (i32.const 4177066232)) |
| 19 | + ;; identical positions |
| 20 | + (i32.eqz (i32.xor (i32.shr_u (local.get $positions) (i32.const 16)) |
| 21 | + (i32.and (local.get $positions) (i32.const 65535))))) |
| 22 | + (then (return (i32.const -1)))) |
| 23 | + ;; horizontal |
| 24 | + (i32.or (i32.or (i32.eq (i32.and (local.get $positions) (i32.const 255)) |
| 25 | + (i32.and (i32.shr_u (local.get $positions) (i32.const 16)) (i32.const 255))) |
| 26 | + ;; vertical |
| 27 | + (i32.eq (i32.and (i32.shr_u (local.get $positions) (i32.const 8)) (i32.const 255)) |
| 28 | + (i32.and (i32.shr_u (local.get $positions) (i32.const 24)) (i32.const 255)))) |
| 29 | + ;; diagonal |
| 30 | + (f32.eq (f32.abs (f32.convert_i32_s (i32.sub |
| 31 | + (i32.and (local.get $positions) (i32.const 255)) |
| 32 | + (i32.and (i32.shr_u (local.get $positions) (i32.const 16)) (i32.const 255))))) |
| 33 | + (f32.abs (f32.convert_i32_s (i32.sub |
| 34 | + (i32.and (i32.shr_u (local.get $positions) (i32.const 8)) (i32.const 255)) |
| 35 | + (i32.and (i32.shr_u (local.get $positions) (i32.const 24)) (i32.const 255))))))) |
| 36 | + ) |
| 37 | + |
| 38 | + ;; |
| 39 | + ;; Prints the chess board to linear memory |
| 40 | + ;; |
| 41 | + ;; @param {i32} $positions |
| 42 | + ;; (4*8bit for white row, white column, black row, black column, each 0-7) |
| 43 | + ;; |
| 44 | + ;; @returns {(i32,i32)} offset and length of chess board string in memory |
| 45 | + ;; |
| 46 | + (func (export "printBoard") (param $positions i32) (result i32 i32) |
| 47 | + (i32.store8 (i32.or |
| 48 | + (i32.shl (i32.and (i32.shr_u (local.get $positions) |
| 49 | + (i32.const 16)) (i32.const 7)) (i32.const 1)) |
| 50 | + (i32.shl (i32.and (i32.shr_u (local.get $positions) |
| 51 | + (i32.const 24)) (i32.const 7)) (i32.const 4))) |
| 52 | + (global.get $White)) |
| 53 | + (i32.store8 (i32.or |
| 54 | + (i32.shl (i32.and (local.get $positions) (i32.const 7)) (i32.const 1)) |
| 55 | + (i32.shl (i32.and (i32.shr_u (local.get $positions) (i32.const 8)) |
| 56 | + (i32.const 7)) (i32.const 4))) |
| 57 | + (global.get $Black)) |
| 58 | + (i32.const 0) (i32.const 128) |
| 59 | + ) |
| 60 | +) |
0 commit comments