Raven7 commited on
Commit
3dd7884
Β·
verified Β·
1 Parent(s): 066a426

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +78 -125
index.html CHANGED
@@ -11,8 +11,7 @@
11
  --primary-dark: #2c3e50;
12
  --primary-light: #34495e;
13
  --highlight: #f1c40f;
14
- --move-highlight: rgba(46, 204, 113, 0.4);
15
- --check-highlight: rgba(231, 76, 60, 0.4);
16
  }
17
 
18
  * {
@@ -22,39 +21,20 @@
22
  }
23
 
24
  body {
25
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Arial, sans-serif;
26
  background: var(--primary-dark);
27
  color: #ecf0f1;
28
  min-height: 100vh;
29
  display: flex;
30
- }
31
-
32
- .container {
33
- display: grid;
34
- grid-template-columns: var(--board-size) 300px;
35
- gap: 2rem;
36
- margin: auto;
37
- padding: 2rem;
38
- background: var(--primary-light);
39
- border-radius: 1rem;
40
- box-shadow: 0 10px 20px rgba(0,0,0,0.2);
41
  }
42
 
43
  .board-container {
44
  width: var(--board-size);
45
  height: var(--board-size);
46
- position: relative;
47
- }
48
-
49
- .chessboard {
50
- width: 100%;
51
- height: 100%;
52
  display: grid;
53
  grid-template-columns: repeat(8, 1fr);
54
  grid-template-rows: repeat(8, 1fr);
55
- border: 4px solid #2c3e50;
56
- border-radius: 4px;
57
- overflow: hidden;
58
  }
59
 
60
  .square {
@@ -62,74 +42,24 @@
62
  align-items: center;
63
  justify-content: center;
64
  font-size: calc(var(--square-size) * 0.7);
65
- cursor: pointer;
66
- transition: all 0.2s;
67
- position: relative;
68
  user-select: none;
 
69
  }
70
 
71
  .white { background: #f0d9b5; }
72
  .black { background: #b58863; }
73
-
74
- .square.selected {
75
- background: var(--highlight) !important;
76
- }
77
-
78
- .square.valid-move::after {
79
- content: 'β€’';
80
- position: absolute;
81
  color: var(--move-highlight);
82
- font-size: 2em;
83
- }
84
-
85
- .square.attack-move::after {
86
- content: '';
87
  position: absolute;
88
- width: 100%;
89
- height: 100%;
90
- border: 2px solid rgba(231, 76, 60, 0.6);
91
- border-radius: 50%;
92
  }
93
 
94
- .piece {
95
- width: 100%;
96
- height: 100%;
97
- display: flex;
98
- align-items: center;
99
- justify-content: center;
100
- font-size: calc(var(--square-size) * 0.7);
101
- cursor: grab;
102
- color: #000;
103
- }
104
-
105
- .piece.white { color: #fff; }
106
-
107
- @media (max-width: 1024px) {
108
- .container {
109
- grid-template-columns: 1fr;
110
- grid-template-rows: auto auto;
111
- }
112
-
113
- .board-container {
114
- margin: auto;
115
- }
116
- }
117
  </style>
118
  </head>
119
  <body>
120
- <div class="container">
121
- <div class="board-container">
122
- <div class="chessboard" id="board"></div>
123
- </div>
124
- <div class="controls">
125
- <div class="status-panel">
126
- <h3>Position Analysis</h3>
127
- <p id="status">White to move</p>
128
- </div>
129
- <button onclick="resetBoard()">Reset Board</button>
130
- </div>
131
- </div>
132
-
133
  <script>
134
  class ChessGame {
135
  constructor() {
@@ -137,7 +67,7 @@
137
  this.currentPlayer = 'white';
138
  this.selectedPiece = null;
139
  this.validMoves = [];
140
- this.initializeBoard();
141
  }
142
 
143
  createInitialBoard() {
@@ -153,81 +83,104 @@
153
  ];
154
  }
155
 
156
- initializeBoard() {
157
- const boardElement = document.getElementById('board');
158
- boardElement.innerHTML = '';
 
159
  for (let row = 0; row < 8; row++) {
160
  for (let col = 0; col < 8; col++) {
161
  const square = document.createElement('div');
162
- square.className = `square ${(row + col) % 2 === 0 ? 'white' : 'black'}`;
163
  square.dataset.row = row;
164
  square.dataset.col = col;
165
- square.onclick = (e) => this.handleSquareClick(e);
166
-
167
- if (this.board[row][col]) {
168
- const piece = document.createElement('div');
169
- piece.className = 'piece';
170
- piece.textContent = this.board[row][col];
171
- square.appendChild(piece);
172
  }
173
- boardElement.appendChild(square);
 
 
 
 
 
 
 
 
 
174
  }
175
  }
176
- this.highlightValidMoves();
177
  }
178
 
179
- handleSquareClick(event) {
180
- const square = event.target.closest('.square');
181
- const row = parseInt(square.dataset.row);
182
- const col = parseInt(square.dataset.col);
183
  const piece = this.board[row][col];
184
- const isWhitePiece = 'β™”β™•β™–β™—β™˜β™™'.includes(piece);
185
 
186
- if (piece && ((this.currentPlayer === 'white' && isWhitePiece) ||
187
- (this.currentPlayer === 'black' && !isWhitePiece))) {
188
- this.selectPiece(row, col);
189
- } else if (this.selectedPiece) {
190
- this.tryMove(row, col);
 
 
 
 
 
 
 
191
  }
 
192
  }
193
 
194
- selectPiece(row, col) {
195
- this.selectedPiece = { row, col };
196
- this.validMoves = this.getValidMoves(row, col);
197
- this.initializeBoard();
198
- document.querySelector(`[data-row="${row}"][data-col="${col}"]`).classList.add('selected');
199
  }
200
 
201
  getValidMoves(row, col) {
202
  const piece = this.board[row][col];
203
  const moves = [];
204
  const directions = {
205
- 'β™–': [[-1,0], [1,0], [0,-1], [0,1]],
206
- 'β™—': [[-1,-1], [-1,1], [1,-1], [1,1]],
207
- 'β™•': [[-1,0], [1,0], [0,-1], [0,1], [-1,-1], [-1,1], [1,-1], [1,1]]
208
- };
209
-
210
- if (piece in directions) {
211
- for (const [dx, dy] of directions[piece]) {
212
- let r = row + dx, c = col + dy;
213
- while (r >= 0 && r < 8 && c >= 0 && c < 8 && !this.board[r][c]) {
214
- moves.push([r, c]);
215
- r += dx; c += dy;
216
- }
 
 
 
 
 
 
 
 
 
 
217
  }
218
  }
219
  return moves;
220
  }
221
 
222
- highlightValidMoves() {
223
- for (const [row, col] of this.validMoves) {
224
- document.querySelector(`[data-row="${row}"][data-col="${col}"]`).classList.add('valid-move');
225
- }
 
 
 
 
226
  }
227
  }
228
 
229
  const game = new ChessGame();
230
- function resetBoard() { new ChessGame(); }
231
  </script>
232
  </body>
233
  </html>
 
11
  --primary-dark: #2c3e50;
12
  --primary-light: #34495e;
13
  --highlight: #f1c40f;
14
+ --move-highlight: rgba(46, 204, 113, 0.4);
 
15
  }
16
 
17
  * {
 
21
  }
22
 
23
  body {
 
24
  background: var(--primary-dark);
25
  color: #ecf0f1;
26
  min-height: 100vh;
27
  display: flex;
28
+ align-items: center;
29
+ justify-content: center;
 
 
 
 
 
 
 
 
 
30
  }
31
 
32
  .board-container {
33
  width: var(--board-size);
34
  height: var(--board-size);
 
 
 
 
 
 
35
  display: grid;
36
  grid-template-columns: repeat(8, 1fr);
37
  grid-template-rows: repeat(8, 1fr);
 
 
 
38
  }
39
 
40
  .square {
 
42
  align-items: center;
43
  justify-content: center;
44
  font-size: calc(var(--square-size) * 0.7);
 
 
 
45
  user-select: none;
46
+ position: relative;
47
  }
48
 
49
  .white { background: #f0d9b5; }
50
  .black { background: #b58863; }
51
+ .valid-move::after {
52
+ content: '●';
53
+ font-size: 1.5rem;
 
 
 
 
 
54
  color: var(--move-highlight);
 
 
 
 
 
55
  position: absolute;
 
 
 
 
56
  }
57
 
58
+ .selected { background-color: var(--highlight); }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  </style>
60
  </head>
61
  <body>
62
+ <div class="board-container" id="board"></div>
 
 
 
 
 
 
 
 
 
 
 
 
63
  <script>
64
  class ChessGame {
65
  constructor() {
 
67
  this.currentPlayer = 'white';
68
  this.selectedPiece = null;
69
  this.validMoves = [];
70
+ this.renderBoard();
71
  }
72
 
73
  createInitialBoard() {
 
83
  ];
84
  }
85
 
86
+ renderBoard() {
87
+ const boardContainer = document.getElementById('board');
88
+ boardContainer.innerHTML = '';
89
+
90
  for (let row = 0; row < 8; row++) {
91
  for (let col = 0; col < 8; col++) {
92
  const square = document.createElement('div');
93
+ square.classList.add('square', (row + col) % 2 === 0 ? 'white' : 'black');
94
  square.dataset.row = row;
95
  square.dataset.col = col;
96
+ square.addEventListener('click', () => this.handleSquareClick(row, col));
97
+
98
+ const piece = this.board[row][col];
99
+ if (piece) {
100
+ const pieceElem = document.createElement('div');
101
+ pieceElem.textContent = piece;
102
+ square.appendChild(pieceElem);
103
  }
104
+
105
+ if (this.isValidMove(row, col)) {
106
+ square.classList.add('valid-move');
107
+ }
108
+
109
+ if (this.selectedPiece && this.selectedPiece.row === row && this.selectedPiece.col === col) {
110
+ square.classList.add('selected');
111
+ }
112
+
113
+ boardContainer.appendChild(square);
114
  }
115
  }
 
116
  }
117
 
118
+ handleSquareClick(row, col) {
 
 
 
119
  const piece = this.board[row][col];
 
120
 
121
+ if (this.selectedPiece) {
122
+ if (this.isValidMove(row, col)) {
123
+ this.makeMove(this.selectedPiece.row, this.selectedPiece.col, row, col);
124
+ this.selectedPiece = null;
125
+ this.validMoves = [];
126
+ } else {
127
+ this.selectedPiece = null;
128
+ this.validMoves = [];
129
+ }
130
+ } else if (piece && this.isCurrentPlayerPiece(piece)) {
131
+ this.selectedPiece = { row, col };
132
+ this.validMoves = this.getValidMoves(row, col);
133
  }
134
+ this.renderBoard();
135
  }
136
 
137
+ isCurrentPlayerPiece(piece) {
138
+ return this.currentPlayer === 'white' ? 'β™™β™–β™˜β™—β™•β™”'.includes(piece) : 'β™Ÿβ™œβ™žβ™β™›β™š'.includes(piece);
 
 
 
139
  }
140
 
141
  getValidMoves(row, col) {
142
  const piece = this.board[row][col];
143
  const moves = [];
144
  const directions = {
145
+ 'β™–': [[-1, 0], [1, 0], [0, -1], [0, 1]],
146
+ 'β™œ': [[-1, 0], [1, 0], [0, -1], [0, 1]],
147
+ 'β™—': [[-1, -1], [-1, 1], [1, -1], [1, 1]],
148
+ '♝': [[-1, -1], [-1, 1], [1, -1], [1, 1]],
149
+ 'β™•': [[-1, 0], [1, 0], [0, -1], [0, 1], [-1, -1], [-1, 1], [1, -1], [1, 1]],
150
+ 'β™›': [[-1, 0], [1, 0], [0, -1], [0, 1], [-1, -1], [-1, 1], [1, -1], [1, 1]],
151
+ 'β™˜': [
152
+ [-2, -1], [-2, 1], [2, -1], [2, 1],
153
+ [-1, -2], [-1, 2], [1, -2], [1, 2]
154
+ ],
155
+ 'β™ž': [
156
+ [-2, -1], [-2, 1], [2, -1], [2, 1],
157
+ [-1, -2], [-1, 2], [1, -2], [1, 2]
158
+ ]
159
+ }[piece] || [];
160
+
161
+ for (const [dx, dy] of directions) {
162
+ let r = row + dx, c = col + dy;
163
+ while (r >= 0 && r < 8 && c >= 0 && c < 8 && !this.board[r][c]) {
164
+ moves.push([r, c]);
165
+ r += dx; c += dy;
166
+ if ('β™–β™œβ™—β™οΏ½οΏ½οΏ½β™›'.includes(piece)) break;
167
  }
168
  }
169
  return moves;
170
  }
171
 
172
+ isValidMove(row, col) {
173
+ return this.validMoves.some(move => move[0] === row && move[1] === col);
174
+ }
175
+
176
+ makeMove(fromRow, fromCol, toRow, toCol) {
177
+ this.board[toRow][toCol] = this.board[fromRow][fromCol];
178
+ this.board[fromRow][fromCol] = null;
179
+ this.currentPlayer = this.currentPlayer === 'white' ? 'black' : 'white';
180
  }
181
  }
182
 
183
  const game = new ChessGame();
 
184
  </script>
185
  </body>
186
  </html>