summaryrefslogtreecommitdiff
path: root/assets/board.js
blob: 6b8486ac2fda1521ca21134934e1fc23646b4d86 (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
const assets = [
  "wk.webp",
  "wq.webp",
  "wr.png",
  "wb.webp",
  "wn.png",
  "wp.png",
  "bk.webp",
  "bq.png",
  "br.webp",
  "bb.webp",
  "bn.webp",
  "bp.png"
].map(x => "./assets/" + x);
let hoveringPiece = -1;

const bitboards = [
  16n,
  8n,
  129n,
  36n,
  66n,
  65280n,
  1152921504606846976n,
  576460752303423488n,
  9295429630892703744n,
  2594073385365405696n,
  4755801206503243776n,
  71776119061217280n,
];
const WHITE_KING   =  1;
const WHITE_QUEEN  =  2;
const WHITE_ROOK   =  3;
const WHITE_BISHOP =  4;
const WHITE_KNIGHT =  5;
const WHITE_PAWN   =  6;
const BLACK_KING   =  7;
const BLACK_QUEEN  =  8;
const BLACK_ROOK   =  9;
const BLACK_BISHOP = 10;
const BLACK_KNIGHT = 11;
const BLACK_PAWN   = 12;

function trackBitboard(j, i, selected, output) {
  const mask = 1n << BigInt(i);
  if (selected) {
    bitboards[j] |= mask;
  } else {
    bitboards[j] &= ~mask;
  }
  output.textContent = bitboards[j].toString();
}

function checkForPiece(square, bitIndex, set = true) {
  for (let i = 0; i < bitboards.length; i++) {
    const mask = bitboards[i];
    if ((mask >> BigInt(bitIndex)) & 1n) {
      if (set) square.style.backgroundImage = `url(${assets[i]})`;
      return i;
    }
  }
  return null;
}

const selectedAction = trackBitboard;

window.onkeydown = (e) => {
  if (e.key == 'b') {
    console.log('Bitboards');
    console.log(`
position.bitboards[WHITE_KING] = ${bitboards[0].toString()}ULL;
position.bitboards[WHITE_QUEEN] = ${bitboards[1].toString()}ULL;
position.bitboards[WHITE_ROOK] = ${bitboards[2].toString()}ULL;
position.bitboards[WHITE_BISHOP] = ${bitboards[3].toString()}ULL;
position.bitboards[WHITE_KNIGHT] = ${bitboards[4].toString()}ULL;
position.bitboards[WHITE_PAWN] = ${bitboards[5].toString()}ULL;
position.bitboards[BLACK_KING] = ${bitboards[6].toString()}ULL;
position.bitboards[BLACK_QUEEN] = ${bitboards[7].toString()}ULL;
position.bitboards[BLACK_ROOK] = ${bitboards[8].toString()}ULL;
position.bitboards[BLACK_BISHOP] = ${bitboards[9].toString()}ULL;
position.bitboards[BLACK_KNIGHT] = ${bitboards[10].toString()}ULL;
position.bitboards[BLACK_PAWN] = ${bitboards[11].toString()}ULL;
      `)
  }
}
window.onload = () => {
  const output = document.getElementById("output");
  const board = document.getElementById("board");
  for (let i = 0; i < 64; i++) {
    if (i % 8 == 0) {
      const label = document.createElement("div");
      label.classList.add("label");
      label.textContent = ["h", "g", "f", "e", "d", "c", "b", "a"][i / 8];
      board.appendChild(label);
    }
    const square = document.createElement("div");
    square.classList.add("square");
    const shift = Math.floor(i / 8);
    square.classList.add((i + shift) % 2 ? "dark" : "light");
    const bitIndex = (i % 8) - 8 * Math.floor(i / 8) + 56;
    square.id = 'square-' + bitIndex.toString();
    checkForPiece(square, bitIndex);
    square.onclick = (e) => {
      if (!e.ctrlKey) {
        const piece = checkForPiece(square, bitIndex, false);
        if (
          (hoveringPiece < 0 && piece == null) ||
          (hoveringPiece >= 0 && piece != null)
        ) return;
        if (hoveringPiece < 0) {
          setHoverpiece(piece);
          square.style.backgroundImage = `none`;
          bitboards[piece] &= ~(1n << BigInt(bitIndex));
        } else {
          square.style.backgroundImage = `url(${assets[hoveringPiece]})`;
          bitboards[hoveringPiece] |= (1n << BigInt(bitIndex));
          setHoverpiece(-1);
        }
        return;
      }
      const selected = square.classList.contains("selected");
      selectedAction(0, bitIndex, !selected, output);
      if (selected) {
        square.classList.remove("selected");
      } else {
        square.classList.add("selected");
      }
    };
    board.appendChild(square);
  }
  const label = document.createElement("div");
  label.classList.add("horizontal_label");
  board.appendChild(label);
  for (let i = 1; i < 9; i++)  {
    const label = document.createElement("div");
    label.classList.add("horizontal_label");
    label.textContent = i;
    board.appendChild(label);
  }

  const mouse = document.getElementById("mouse");
  function setHoverpiece(i) {
    hoveringPiece = i;
    if (i < 0) {
      mouse.style.backgroundImage = `none`;
      return;
    }
    mouse.style.backgroundImage = `url(${assets[i]})`;
  }
  window.onmousemove = (e) => {
    const x = e.clientX;
    const y = e.clientY;
    mouse.style.left = x.toString() + 'px';
    mouse.style.top = y.toString() + 'px';
  }
}