-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
291 lines (233 loc) · 9.36 KB
/
index.js
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
const inc1 = document.getElementById("incre1")
const inc2 = document.getElementById("incre2")
const send = document.getElementById("send")
const White = document.getElementById("White")
const Black = document.getElementById("Black")
const mov = document.getElementById("move")
const url = 'https://corsproxy.io/?' + encodeURIComponent("https://chess-api.com/v1");
White.addEventListener("click",() =>{
let inc1_1 = 1
let inc2_1 = 1
let inc1_2 = 1
let inc2_2 = 1
let send1 = false
let FEN = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1"
inc1.addEventListener("click", () =>{
if (inc1_1 < 8 & !send1){
inc1_1 +=1
}
else if (inc1_2 < 8 & send1){
inc1_2 +=1
}
console.log(inc1_2)
})
inc2.addEventListener("click", () =>{
if (inc2_1 < 8 & !send1){
inc2_1 +=1
}
else if (inc2_2 < 8 & send1){
inc2_2 +=1
}
console.log(inc2_1)
})
send.addEventListener("click", ()=>{
if (send1){
send1 = false
updateFEN(inc1_1,inc2_1, inc1_2,inc2_2)
// Execute the function with the FEN position
postChessApi({
fen: FEN, // Replace 'FEN' with the actual FEN string
depth: 15, // Adjust the depth as needed, ensuring it's an integer less than 16
// Other parameters can be added if required by the API
}).then(() => {
// Additional data can be logged as needed
});
inc1_1 = 1
inc2_1 = 1
inc1_2 = 1
inc2_2 = 1
}
else{
send1 = true
}
})
function translateMove(move) {
mov.innerText = move
navigator.vibrate(200);
// Map the files 'a' to 'h' to numbers 1 to 8
const fileToNumber = { 'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8 };
// Extract the starting and ending positions from the move
const [startFile, startRank, endFile, endRank] = move.split('');
// Translate files to numbers and parse ranks as integers
let inc1_1 = parseInt(startRank, 10);
let inc2_1 = fileToNumber[startFile];
let inc1_2 = parseInt(endRank, 10);
let inc2_2 = fileToNumber[endFile];
// Log the values or return them as needed
console.log(`inc1_1: ${inc1_1}, inc2_1: ${inc2_1}, inc1_2: ${inc1_2}, inc2_2: ${inc2_2}`);
updateFEN(inc1_1,inc2_1, inc1_2,inc2_2)
let rows = FEN.split(' ')[0].split('/');
let fenParts = FEN.split(' ');
fenParts[1] = 'b';
// Join the rows to form the new FEN
FEN = rows.join('/') + ' ' + fenParts.slice(1).join(' ');
}
function updateFEN(inc1_1, inc2_1, inc1_2, inc2_2) {
// Convert numeric column positions to letters
const columns = 'abcdefgh';
const fromCol = columns[inc2_1 - 1];
const toCol = columns[inc2_2 - 1];
// Convert FEN to board array
let rows = FEN.split(' ')[0].split('/');
let board = rows.map(row => row.replace(/\d/g, match => '1'.repeat(match)));
// Find the piece using the starting position
const piece = board[8 - inc1_1][columns.indexOf(fromCol)];
// Remove the piece from the starting position
board[8 - inc1_1] = setCharAt(board[8 - inc1_1], columns.indexOf(fromCol), '1');
// Place the piece on the target position
board[8 - inc1_2] = setCharAt(board[8 - inc1_2], columns.indexOf(toCol), piece);
// Convert the board back to FEN rows
rows = board.map(row => row.replace(/1{1,8}/g, match => match.length));
// Update the turn to white
let fenParts = FEN.split(' ');
fenParts[1] = 'w';
// Join the rows to form the new FEN
FEN = rows.join('/') + ' ' + fenParts.slice(1).join(' ');
console.log(FEN);
}
// Helper function to replace a character in a string at a specific index
function setCharAt(str, index, char) {
return str.substring(0, index) + char + str.substring(index + 1);
}
async function postChessApi(data = {}) {
// Construct the query parameters from the data object
const queryParams = new URLSearchParams(data).toString();
// Append the query parameters to the endpoint URL
const url = `https://stockfish.online/api/s/v2.php?${queryParams}`;
// Perform the GET request without a body
const response = await fetch(url);
const responseData = await response.json(); // Parses the JSON returned by the API
if (responseData.success) {
// Extract only the move part from the 'bestmove' string
const move = responseData.bestmove.split(' ')[1];
console.log(move, responseData, responseData.bestmove)
translateMove(move);
} else {
// Handle error
console.error('Error:', responseData.data);
}
}
})
Black.addEventListener("click",() =>{
let inc1_1 = 1
let inc2_1 = 1
let inc1_2 = 1
let inc2_2 = 1
let send1 = false
let FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
inc1.addEventListener("click", () =>{
if (inc1_1 < 8 & !send1){
inc1_1 +=1
}
else if (inc1_2 < 8 & send1){
inc1_2 +=1
}
console.log(inc1_2)
})
inc2.addEventListener("click", () =>{
if (inc2_1 < 8 & !send1){
inc2_1 +=1
}
else if (inc2_2 < 8 & send1){
inc2_2 +=1
}
console.log(inc2_1)
})
send.addEventListener("click", ()=>{
if (send1){
send1 = false
updateFEN1(inc1_1,inc2_1, inc1_2,inc2_2)
// Execute the function with the FEN position
postChessApi({
fen: FEN, // Replace 'FEN' with the actual FEN string
depth: 15, // Adjust the depth as needed, ensuring it's an integer less than 16
// Other parameters can be added if required by the API
}).then(() => {
// Additional data can be logged as needed
});
inc1_1 = 1
inc2_1 = 1
inc1_2 = 1
inc2_2 = 1
}
else{
send1 = true
}
})
function translateMove1(move) {
mov.innerText = move
navigator.vibrate(200);
// Map the files 'a' to 'h' to numbers 1 to 8
const fileToNumber = { 'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8 };
// Extract the starting and ending positions from the move
const [startFile, startRank, endFile, endRank] = move.split('');
// Translate files to numbers and parse ranks as integers
let inc1_1 = parseInt(startRank, 10);
let inc2_1 = fileToNumber[startFile];
let inc1_2 = parseInt(endRank, 10);
let inc2_2 = fileToNumber[endFile];
// Log the values or return them as needed
console.log(`inc1_1: ${inc1_1}, inc2_1: ${inc2_1}, inc1_2: ${inc1_2}, inc2_2: ${inc2_2}`);
updateFEN1(inc1_1,inc2_1, inc1_2,inc2_2)
let rows = FEN.split(' ')[0].split('/');
let fenParts = FEN.split(' ');
fenParts[1] = 'w';
// Join the rows to form the new FEN
FEN = rows.join('/') + ' ' + fenParts.slice(1).join(' ');
}
function updateFEN1(inc1_1, inc2_1, inc1_2, inc2_2) {
// Convert numeric column positions to letters
const columns = 'abcdefgh';
const fromCol = columns[inc2_1 - 1];
const toCol = columns[inc2_2 - 1];
// Convert FEN to board array
let rows = FEN.split(' ')[0].split('/');
let board = rows.map(row => row.replace(/\d/g, match => '1'.repeat(match)));
// Find the piece using the starting position
const piece = board[8 - inc1_1][columns.indexOf(fromCol)];
// Remove the piece from the starting position
board[8 - inc1_1] = setCharAt(board[8 - inc1_1], columns.indexOf(fromCol), '1');
// Place the piece on the target position
board[8 - inc1_2] = setCharAt(board[8 - inc1_2], columns.indexOf(toCol), piece);
// Convert the board back to FEN rows
rows = board.map(row => row.replace(/1{1,8}/g, match => match.length));
// Update the turn to white
let fenParts = FEN.split(' ');
fenParts[1] = 'b';
// Join the rows to form the new FEN
FEN = rows.join('/') + ' ' + fenParts.slice(1).join(' ');
console.log(FEN, fenParts);
}
// Helper function to replace a character in a string at a specific index
function setCharAt(str, index, char) {
return str.substring(0, index) + char + str.substring(index + 1);
}
async function postChessApi(data = {}) {
// Construct the query parameters from the data object
const queryParams = new URLSearchParams(data).toString();
// Append the query parameters to the endpoint URL
const url = `https://stockfish.online/api/s/v2.php?${queryParams}`;
// Perform the GET request without a body
const response = await fetch(url);
const responseData = await response.json(); // Parses the JSON returned by the API
if (responseData.success) {
// Extract only the move part from the 'bestmove' string
const move = responseData.bestmove.split(' ')[1];
console.log(move, responseData, responseData.bestmove)
translateMove1(move);
} else {
// Handle error
console.error('Error:', responseData.data);
}
}
})