Compare commits
2 commits
014d170e6a
...
a56e77c378
| Author | SHA1 | Date | |
|---|---|---|---|
| a56e77c378 | |||
| 4f221ea7a8 |
3 changed files with 41 additions and 40 deletions
|
|
@ -3,46 +3,41 @@ import { Piece } from "./types";
|
|||
// export function fen2array(fen: string): Int8Array<ArrayBuffer> {
|
||||
// }
|
||||
|
||||
export const startFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0";
|
||||
const CHAR_TO_PIECE: Record<string, number> = {
|
||||
"K": Piece.WHITE | Piece.KING,
|
||||
"Q": Piece.WHITE | Piece.QUEEN,
|
||||
"R": Piece.WHITE | Piece.ROOK,
|
||||
"B": Piece.WHITE | Piece.BISHOP,
|
||||
"N": Piece.WHITE | Piece.KNIGHT,
|
||||
"P": Piece.WHITE | Piece.PAWN,
|
||||
"k": Piece.BLACK | Piece.KING,
|
||||
"q": Piece.BLACK | Piece.QUEEN,
|
||||
"r": Piece.BLACK | Piece.ROOK,
|
||||
"b": Piece.BLACK | Piece.BISHOP,
|
||||
"n": Piece.BLACK | Piece.KNIGHT,
|
||||
"p": Piece.BLACK | Piece.PAWN,
|
||||
};
|
||||
|
||||
export class Board {
|
||||
public board: Int8Array;
|
||||
public board = new Int8Array(8 ** 2);
|
||||
public details = {
|
||||
turn: "w",
|
||||
castling: {
|
||||
white: { king: true, queen: true },
|
||||
black: { king: true, queen: true },
|
||||
},
|
||||
enPassant: -1,
|
||||
halfMove: 0,
|
||||
fullMove: 0,
|
||||
};
|
||||
constructor() {
|
||||
const board = new Int8Array(8**2);
|
||||
board[0] = Piece.WHITE | Piece.ROOK;
|
||||
board[1] = Piece.WHITE | Piece.KNIGHT;
|
||||
board[2] = Piece.WHITE | Piece.BISHOP;
|
||||
board[3] = Piece.WHITE | Piece.QUEEN;
|
||||
board[4] = Piece.WHITE | Piece.KING;
|
||||
board[5] = Piece.WHITE | Piece.BISHOP;
|
||||
board[6] = Piece.WHITE | Piece.KNIGHT;
|
||||
board[7] = Piece.WHITE | Piece.ROOK;
|
||||
board[8] = Piece.WHITE | Piece.PAWN;
|
||||
board[9] = Piece.WHITE | Piece.PAWN;
|
||||
board[10] = Piece.WHITE | Piece.PAWN;
|
||||
board[11] = Piece.WHITE | Piece.PAWN;
|
||||
board[12] = Piece.WHITE | Piece.PAWN;
|
||||
board[13] = Piece.WHITE | Piece.PAWN;
|
||||
board[14] = Piece.WHITE | Piece.PAWN;
|
||||
board[15] = Piece.WHITE | Piece.PAWN;
|
||||
this.loadFEN();
|
||||
}
|
||||
|
||||
loadFEN(fen = startFEN) {
|
||||
const [position, turn, castling, enPassant, halfMove, fullMove] = fen.split(" ");
|
||||
|
||||
board[48] = Piece.BLACK | Piece.PAWN;
|
||||
board[49] = Piece.BLACK | Piece.PAWN;
|
||||
board[50] = Piece.BLACK | Piece.PAWN;
|
||||
board[51] = Piece.BLACK | Piece.PAWN;
|
||||
board[52] = Piece.BLACK | Piece.PAWN;
|
||||
board[53] = Piece.BLACK | Piece.PAWN;
|
||||
board[54] = Piece.BLACK | Piece.PAWN;
|
||||
board[55] = Piece.BLACK | Piece.PAWN;
|
||||
board[56] = Piece.BLACK | Piece.ROOK;
|
||||
board[57] = Piece.BLACK | Piece.KNIGHT;
|
||||
board[58] = Piece.BLACK | Piece.BISHOP;
|
||||
board[59] = Piece.BLACK | Piece.QUEEN;
|
||||
board[60] = Piece.BLACK | Piece.KING;
|
||||
board[61] = Piece.BLACK | Piece.BISHOP;
|
||||
board[62] = Piece.BLACK | Piece.KNIGHT;
|
||||
board[63] = Piece.BLACK | Piece.ROOK;
|
||||
|
||||
this.board = board;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
1
src/routes/+layout.ts
Normal file
1
src/routes/+layout.ts
Normal file
|
|
@ -0,0 +1 @@
|
|||
export const ssr = false;
|
||||
|
|
@ -1,8 +1,9 @@
|
|||
<script lang="ts">
|
||||
import { Board } from "$lib/chess/utils";
|
||||
import { Board, startFEN } from "$lib/chess/utils";
|
||||
|
||||
const board = new Board();
|
||||
let showIndex = $state(false);
|
||||
let fen = $state(startFEN);
|
||||
</script>
|
||||
|
||||
<div class="grid grid-cols-8 gap-0 border-2">
|
||||
|
|
@ -10,14 +11,18 @@
|
|||
<div class="h-20 aspect-square flex justify-center items-center
|
||||
{index % 2 === Math.floor(index / 8) % 2 ? 'bg-orange-200 text-stone-800' : 'bg-amber-800 text-stone-200'}
|
||||
">
|
||||
{#if square}<img src="assets/{square}.svg" alt="{square}" class="h-16 aspect-square"/>{/if}
|
||||
{#if square}<img src="assets/{square}.svg" alt="p{square}" class="h-16 aspect-square"/>{/if}
|
||||
{showIndex ? index : ''}
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
<div>
|
||||
<label>
|
||||
<label class="block">
|
||||
show index
|
||||
<input type="checkbox" bind:checked={showIndex} />
|
||||
</label>
|
||||
<label class="block">
|
||||
fen:
|
||||
<input type="text" onkeypress={(e) => e.key === 'Enter' && board.loadFEN(fen)} bind:value={fen} />
|
||||
</label>
|
||||
</div>
|
||||
Loading…
Add table
Add a link
Reference in a new issue