Code viewer for World:
Chess
document.write(`
<!DOCTYPE html>
<html>
<head>
<title>Chess</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="flex justify-center items-center h-screen bg-gray-200">
<div class="flex">
<div>
<h1 class="text-3xl font-bold underline mb-4">Chess</h1>
<table>
${(() => {
let board = '';
for (let i = 1; i <= 8; i++) {
board += '<tr>';
for (let j = 1; j <= 8; j++) {
let color = (i + j) % 2 === 0 ? 'bg-white' : 'bg-gray-700';
board += `<td class="h-16 w-16 ${color}" id="${i}/${j}"></td>`;
}
board += '</tr>';
}
return board;
})()}
</table>
</div>
<div class="ml-8">
<label class="flex cursor-pointer items-center justify-between p-1 border rounded bg-gray-200">
<div id="enterkey"></div>
<input class="w-64 px-4 py-2 border rounded-lg text-gray-700 focus:outline-none focus:ring-2 focus:ring-blue-400" maxlength='2000' NAME="apikey" id="apikey" VALUE=''>
<button onclick='setkey();' class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:ring-2 focus:ring-blue-400">Set API key</button>
</label>
<div class="mt-4">
<button onclick="sendchat();" class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:ring-2 focus:ring-green-400">Next Turn</button>
</div>
<div class="mt-4">
<h3 class="text-xl font-bold">GPT replies</h3>
<div id="them"></div>
</div>
</div>
</div>
</body>
</html>
`);
let myTurn = true;
let selected = ["s_none", "d_none"];
let piecesAssets = [
{coord: "1/1", path: "/uploads/bumbuf2/black_rook.png", team: "black", category: moveRook, initialPos: true},
{coord: "1/2", path: "/uploads/bumbuf2/black_horse.png", team: "black", category: moveHorse, initialPos: true},
{coord: "1/3", path: "/uploads/bumbuf2/black_bishop.png", team: "black", category: moveBishop, initialPos: true},
{coord: "1/4", path: "/uploads/bumbuf2/black_queen.png", team: "black", category: moveQueen, initialPos: true},
{coord: "1/5", path: "/uploads/bumbuf2/black_king.png", team: "black", category: moveKing, initialPos: true},
{coord: "1/6", path: "/uploads/bumbuf2/black_bishop.png", team: "black", category: moveBishop, initialPos: true},
{coord: "1/7", path: "/uploads/bumbuf2/black_horse.png", team: "black", category: moveHorse, initialPos: true},
{coord: "1/8", path: "/uploads/bumbuf2/black_rook.png", team: "black", category: moveRook, initialPos: true},
{coord: "2/1", path: "/uploads/bumbuf2/black_pawn.png", team: "black", category: movePawn, initialPos: true},
{coord: "2/2", path: "/uploads/bumbuf2/black_pawn.png", team: "black", category: movePawn, initialPos: true},
{coord: "2/3", path: "/uploads/bumbuf2/black_pawn.png", team: "black", category: movePawn, initialPos: true},
{coord: "2/4", path: "/uploads/bumbuf2/black_pawn.png", team: "black", category: movePawn, initialPos: true},
{coord: "2/5", path: "/uploads/bumbuf2/black_pawn.png", team: "black", category: movePawn, initialPos: true},
{coord: "2/6", path: "/uploads/bumbuf2/black_pawn.png", team: "black", category: movePawn, initialPos: true},
{coord: "2/7", path: "/uploads/bumbuf2/black_pawn.png", team: "black", category: movePawn, initialPos: true},
{coord: "2/8", path: "/uploads/bumbuf2/black_pawn.png", team: "black", category: movePawn, initialPos: true},
{coord: "7/1", path: "/uploads/bumbuf2/white_pawn.png", team: "white", category: movePawn, initialPos: true},
{coord: "7/2", path: "/uploads/bumbuf2/white_pawn.png", team: "white", category: movePawn, initialPos: true},
{coord: "7/3", path: "/uploads/bumbuf2/white_pawn.png", team: "white", category: movePawn, initialPos: true},
{coord: "7/4", path: "/uploads/bumbuf2/white_pawn.png", team: "white", category: movePawn, initialPos: true},
{coord: "7/5", path: "/uploads/bumbuf2/white_pawn.png", team: "white", category: movePawn, initialPos: true},
{coord: "7/6", path: "/uploads/bumbuf2/white_pawn.png", team: "white", category: movePawn, initialPos: true},
{coord: "7/7", path: "/uploads/bumbuf2/white_pawn.png", team: "white", category: movePawn, initialPos: true},
{coord: "7/8", path: "/uploads/bumbuf2/white_pawn.png", team: "white", category: movePawn, initialPos: true},
{coord: "8/1", path: "/uploads/bumbuf2/white_rook.png", team: "white", category: moveRook, initialPos: true},
{coord: "8/2", path: "/uploads/bumbuf2/white_horse.png", team: "white", category: moveHorse, initialPos: true},
{coord: "8/3", path: "/uploads/bumbuf2/white_bishop.png", team: "white", category: moveBishop, initialPos: true},
{coord: "8/4", path: "/uploads/bumbuf2/white_queen.png", team: "white", category: moveQueen, initialPos: true},
{coord: "8/5", path: "/uploads/bumbuf2/white_king.png", team: "white", category: moveKing, initialPos: true},
{coord: "8/6", path: "/uploads/bumbuf2/white_bishop.png", team: "white", category: moveBishop, initialPos: true},
{coord: "8/7", path: "/uploads/bumbuf2/white_horse.png", team: "white", category: moveHorse, initialPos: true},
{coord: "8/8", path: "/uploads/bumbuf2/white_rook.png", team: "white", category: moveRook, initialPos: true},
];
function updateBoard() {
document.querySelectorAll('td').forEach(td => {
td.innerHTML = '';
let piece = piecesAssets.find((piece) => piece.coord === td.id);
if (piece) {
td.innerHTML = `<img src="${piece.path}" alt="Chess piece" class="w-96 h-14">`;
}
});
piecesAssets = Array.from(piecesAssets);
}
let possibleMoves = [];
let isCheck = false;
let win = false;
let defeat = false;
function movePawn(fromCoord, toCoord) {
let pieceState = piecesAssets.findIndex((piece) => piece.coord === fromCoord);
let possible = [];
fromCoord = fromCoord.split("/");
let posEnemy1 = piecesAssets.findIndex((piece) => piece.coord === ((Number(fromCoord[0]) - 1) + "/" + (Number(fromCoord[1]) - 1)));
if (posEnemy1 !== -1) {
possible.push(piecesAssets[posEnemy1].coord);
}
let posEnemy2 = piecesAssets.findIndex((piece) => piece.coord === ((Number(fromCoord[0]) - 1) + "/" + (Number(fromCoord[1]) + 1)));
if (posEnemy2 !== -1) {
possible.push(piecesAssets[posEnemy2].coord);
}
let forward = Number(fromCoord[0]) - 1 + "/" + fromCoord[1];
if (piecesAssets[piecesAssets.findIndex((piece) => piece.coord === forward)] === undefined) {
possible.push(forward);
}
let jumpStart = (Number(fromCoord[0]) - 2 + "/" + fromCoord[1]);
if (piecesAssets[pieceState].initialPos === true && toCoord === jumpStart) {
possibleMoves.push(toCoord);
piecesAssets[pieceState].initialPos = false;
return possibleMoves;
}
if (possible.find(pos => pos === toCoord)) {
possibleMoves.push(toCoord);
if (piecesAssets[pieceState].initialPos === true) {
piecesAssets[pieceState].initialPos = false;
}
}
return possibleMoves;
}
function moveRook(fromCoord, toCoord) {
console.log(fromCoord, toCoord);
let pieceState = piecesAssets.findIndex((piece) => piece.coord === fromCoord);
let possible = [];
fromCoord = fromCoord.split("/");
let cordY = Number(fromCoord[0]);
let cordX = Number(fromCoord[1]);
let encounteredX = false;
let encounteredY = false;
let firstFound = false;
// above
while (cordY > 1) {
cordY--;
let posY = cordY + "/" + fromCoord[1];
let pieceIndex = piecesAssets.findIndex((piece) => piece.coord === posY);
if (piecesAssets[pieceIndex] !== undefined) {
if (piecesAssets[pieceIndex].team === piecesAssets[pieceState].team) {
encounteredY = true;
}
}
if (pieceIndex === -1 && encounteredY === false) {
possible.push(posY);
} else {
if (piecesAssets[pieceIndex] !== undefined) {
if (piecesAssets[pieceIndex].team !== piecesAssets[pieceState].team && firstFound === false && encounteredY === false) {
firstFound = true;
possible.push(posY);
encounteredY = true;
} else {
encounteredY = true;
}
}
}
}
firstFound = false;
encounteredX = false;
cordY = Number(fromCoord[0]);
// left
while (cordX > 1) {
cordX--;
let posX = fromCoord[0] + "/" + cordX;
let pieceIndex = piecesAssets.findIndex((piece) => piece.coord === posX);
if (piecesAssets[pieceIndex] !== undefined) {
if (piecesAssets[pieceIndex].team === piecesAssets[pieceState].team) {
encounteredX = true;
}
}
if (pieceIndex === -1 && encounteredX === false) {
possible.push(posX);
} else {
if (piecesAssets[pieceIndex] !== undefined) {
if (piecesAssets[pieceIndex].team !== piecesAssets[pieceState].team && firstFound === false && encounteredX === false) {
firstFound = true;
possible.push(posX);
encounteredX = true;
} else {
encounteredX = true;
}
}
}
}
firstFound = false;
encounteredY = false;
cordX = Number(fromCoord[1]);
// below
while (cordY < 8) {
cordY++;
let posY = cordY + "/" + fromCoord[1];
let pieceIndex = piecesAssets.findIndex((piece) => piece.coord === posY);
if (piecesAssets[pieceIndex] !== undefined) {
if (piecesAssets[pieceIndex].team === piecesAssets[pieceState].team) {
encounteredY = true;
}
}
if (pieceIndex === -1 && encounteredY === false) {
possible.push(posY);
} else {
if (piecesAssets[pieceIndex] !== undefined) {
if (piecesAssets[pieceIndex].team !== piecesAssets[pieceState].team && firstFound === false && encounteredY === false) {
firstFound = true;
possible.push(posY);
encounteredY = true;
} else {
encounteredY = true;
}
}
}
}
firstFound = false;
encounteredX = false;
cordY = Number(fromCoord[0]);
// right
while (cordX < 8) {
cordX++;
let posX = fromCoord[0] + "/" + cordX;
let pieceIndex = piecesAssets.findIndex((piece) => piece.coord === posX);
if (piecesAssets[pieceIndex] !== undefined) {
if (piecesAssets[pieceIndex].team === piecesAssets[pieceState].team) {
encounteredX = true;
}
}
if (pieceIndex === -1 && encounteredX === false) {
possible.push(posX);
} else {
if (piecesAssets[pieceIndex] !== undefined) {
if (piecesAssets[pieceIndex].team !== piecesAssets[pieceState].team && firstFound === false && encounteredX === false) {
firstFound = true;
possible.push(posX);
encounteredY = true;
} else if (piecesAssets[pieceIndex].team === piecesAssets[pieceState].team && firstFound === false) {
encounteredY = true;
firstFound = true;
}
}
}
}
console.log(possible);
if (possible.find(pos => pos === toCoord)) {
possibleMoves.push(toCoord);
if (piecesAssets[pieceState].initialPos === true) {
piecesAssets[pieceState].initialPos = false;
}
}
return possibleMoves;
}
function moveHorse(fromCoord, toCoord) {
console.log(fromCoord, toCoord);
let pieceState = piecesAssets.findIndex((piece) => piece.coord === fromCoord);
let possible = [];
fromCoord = fromCoord.split("/");
let cordY = Number(fromCoord[0]);
let cordX = Number(fromCoord[1]);
let X = [2, 1, -1, -2, -2, -1, 1, 2];
let Y = [1, 2, 2, 1, -1, -2, -2, -1];
for (let i = 0; i < 8; i++) {
let x = cordX + X[i];
let y = cordY + Y[i];
if (x >= 1 && y >= 1 && x <= 8 && y <= 8) {
let pos = y + "/" + x;
let pieceIndex = piecesAssets.findIndex((piece) => piece.coord === pos);
if (pieceIndex === -1 || piecesAssets[pieceIndex].team !== piecesAssets[pieceState].team) {
possible.push(pos);
}
}
}
console.log(possible);
if (possible.find(pos => pos === toCoord)) {
possibleMoves.push(toCoord);
if (piecesAssets[pieceState].initialPos === true) {
piecesAssets[pieceState].initialPos = false;
}
}
return possibleMoves;
}
function moveBishop(fromCoord, toCoord) {
console.log(fromCoord, toCoord);
let pieceState = piecesAssets.findIndex((piece) => piece.coord === fromCoord);
let possible = [];
fromCoord = fromCoord.split("/");
let cordY = Number(fromCoord[0]);
let cordX = Number(fromCoord[1]);
// top-right
let x = cordX + 1;
let y = cordY + 1;
while (x <= 8 && y <= 8) {
let pos = y + "/" + x;
let pieceIndex = piecesAssets.findIndex((piece) => piece.coord === pos);
if (pieceIndex === -1) {
possible.push(pos);
} else {
if (piecesAssets[pieceIndex].team !== piecesAssets[pieceState].team) {
possible.push(pos);
}
break;
}
x++;
y++;
}
// top-left
x = cordX - 1;
y = cordY + 1;
while (x >= 1 && y <= 8) {
let pos = y + "/" + x;
let pieceIndex = piecesAssets.findIndex((piece) => piece.coord === pos);
if (pieceIndex === -1) {
possible.push(pos);
} else {
if (piecesAssets[pieceIndex].team !== piecesAssets[pieceState].team) {
possible.push(pos);
}
break;
}
x--;
y++;
}
// bottom-right
x = cordX + 1;
y = cordY - 1;
while (x <= 8 && y >= 1) {
let pos = y + "/" + x;
let pieceIndex = piecesAssets.findIndex((piece) => piece.coord === pos);
if (pieceIndex === -1) {
possible.push(pos);
} else {
if (piecesAssets[pieceIndex].team !== piecesAssets[pieceState].team) {
possible.push(pos);
}
break;
}
x++;
y--;
}
// bottom-left
x = cordX - 1;
y = cordY - 1;
while (x >= 1 && y >= 1) {
let pos = y + "/" + x;
let pieceIndex = piecesAssets.findIndex((piece) => piece.coord === pos);
if (pieceIndex === -1) {
possible.push(pos);
} else {
if (piecesAssets[pieceIndex].team !== piecesAssets[pieceState].team) {
possible.push(pos);
}
break;
}
x--;
y--;
}
console.log(possible);
if (possible.find(pos => pos === toCoord)) {
possibleMoves.push(toCoord);
if (piecesAssets[pieceState].initialPos === true) {
piecesAssets[pieceState].initialPos = false;
}
}
return possibleMoves;
}
function moveQueen(fromCoord, toCoord) {
console.log(fromCoord, toCoord);
let pieceState = piecesAssets.findIndex((piece) => piece.coord === fromCoord);
let possible = [];
let rookMoves = moveRook(fromCoord, toCoord);
possible = possible.concat(rookMoves);
let bishopMoves = moveBishop(fromCoord, toCoord);
possible = possible.concat(bishopMoves);
console.log(possible);
if (possible.find(pos => pos === toCoord)) {
possibleMoves.push(toCoord);
if (piecesAssets[pieceState].initialPos === true) {
piecesAssets[pieceState].initialPos = false;
}
}
return possibleMoves;
}
function moveKing(fromCoord, toCoord) {
console.log(fromCoord, toCoord);
let pieceState = piecesAssets.findIndex((piece) => piece.coord === fromCoord);
let possible = [];
let notPossible = [];
fromCoord = fromCoord.split("/");
//> The king has 10 possible moves he can make each turn
// - (General) Left, Right, Up, Down, Top-Right, Top-Left, Bottom-Right, Bottom-Left (8) [X]
// - (Special) Left Castling, Right Castling [X] [X]
//> The king is also in charge of the winning conditions so we must take care of that as well
// - The king also cant move
//> GENERAL MOVES
let pos1 = (Number(fromCoord[0]) + 1) + "/" + fromCoord[1];
if (piecesAssets.findIndex((piece) => piece.coord === pos1) === -1 || piecesAssets[(piecesAssets.findIndex((piece) => piece.coord === pos1))].team !== piecesAssets[pieceState].team) {
possible.push(pos1);
}
let pos2 = (Number(fromCoord[0]) - 1) + "/" + fromCoord[1];
if (piecesAssets.findIndex((piece) => piece.coord === pos2) === -1 || piecesAssets[(piecesAssets.findIndex((piece) => piece.coord === pos2))].team !== piecesAssets[pieceState].team) {
possible.push(pos2);
}
let pos3 = (Number(fromCoord[0]) + 1) + "/" + (Number(fromCoord[1]) + 1);
if (piecesAssets.findIndex((piece) => piece.coord === pos3) === -1 || piecesAssets[(piecesAssets.findIndex((piece) => piece.coord === pos3))].team !== piecesAssets[pieceState].team) {
possible.push(pos3);
}
let pos4 = (Number(fromCoord[0]) + 1) + "/" + (Number(fromCoord[1]) - 1);
if (piecesAssets.findIndex((piece) => piece.coord === pos4) === -1 || piecesAssets[(piecesAssets.findIndex((piece) => piece.coord === pos4))].team !== piecesAssets[pieceState].team) {
possible.push(pos4);
}
let pos5 = (Number(fromCoord[0]) - 1) + "/" + (Number(fromCoord[1]) + 1);
if (piecesAssets.findIndex((piece) => piece.coord === pos5) === -1 || piecesAssets[(piecesAssets.findIndex((piece) => piece.coord === pos5))].team !== piecesAssets[pieceState].team) {
possible.push(pos5);
}
let pos6 = (Number(fromCoord[0]) - 1) + "/" + (Number(fromCoord[1]) - 1);
if (piecesAssets.findIndex((piece) => piece.coord === pos6) === -1 || piecesAssets[(piecesAssets.findIndex((piece) => piece.coord === pos6))].team !== piecesAssets[pieceState].team) {
possible.push(pos6);
}
let pos7 = fromCoord[0] + "/" + (Number(fromCoord[1]) + 1);
if (piecesAssets.findIndex((piece) => piece.coord === pos7) === -1 || piecesAssets[(piecesAssets.findIndex((piece) => piece.coord === pos7))].team !== piecesAssets[pieceState].team) {
possible.push(pos7);
}
let pos8 = fromCoord[0] + "/" + (Number(fromCoord[1]) - 1);
if (piecesAssets.findIndex((piece) => piece.coord === pos8) === -1 || piecesAssets[(piecesAssets.findIndex((piece) => piece.coord === pos8))].team !== piecesAssets[pieceState].team) {
possible.push(pos8);
}
//> CASTLING
let rightCastling = fromCoord[0] + "/" + (Number(fromCoord[1]) + 2);
let cordY = Number(fromCoord[0]);
let cordX = Number(fromCoord[1]);
let firstFound = false;
while (cordX < 8) {
cordX++;
let posX = fromCoord[0] + "/" + cordX;
let pieceIndex = piecesAssets.findIndex((piece) => piece.coord === posX);
if (piecesAssets[pieceIndex] !== undefined) {
if (piecesAssets[pieceIndex].team === piecesAssets[pieceState].team) {
encounteredX = true;
}
}
if (piecesAssets[pieceIndex] !== undefined) {
if (piecesAssets[pieceIndex].team === piecesAssets[pieceState].team && firstFound === false) {
if (piecesAssets[pieceIndex].path === "/uploads/bumbuf2/white_rook.png" && piecesAssets[pieceIndex].initialPos === true && piecesAssets[pieceState].initialPos === true) {
possible.push(rightCastling);
piecesAssets[pieceIndex].coord = fromCoord[0] + "/" + (Number(fromCoord[1]) + 1);
console.log("Right Castle");
}
firstFound = true;
}
}
}
let leftCastling = fromCoord[0] + "/" + (Number(fromCoord[1]) - 2);
cordY = Number(fromCoord[0]);
cordX = Number(fromCoord[1]);
firstFound = false;
while (cordX > 1) {
cordX--;
let posX = fromCoord[0] + "/" + cordX;
let pieceIndex = piecesAssets.findIndex((piece) => piece.coord === posX);
if (piecesAssets[pieceIndex] !== undefined) {
if (piecesAssets[pieceIndex].team === piecesAssets[pieceState].team) {
encounteredX = true;
}
}
if (piecesAssets[pieceIndex] !== undefined) {
if (piecesAssets[pieceIndex].team === piecesAssets[pieceState].team && firstFound === false) {
console.log(piecesAssets[pieceIndex]);
if (piecesAssets[pieceIndex].path === "/uploads/bumbuf2/white_rook.png" && piecesAssets[pieceIndex].initialPos === true && piecesAssets[pieceState].initialPos === true) {
possible.push(leftCastling);
piecesAssets[pieceIndex].coord = fromCoord[0] + "/" + (Number(fromCoord[1]) - 1);
console.log("left castling!!");
}
firstFound = true;
}
}
}
//> CHECK ROOK
let enemyRook = piecesAssets.filter((piece) => piece.path === "/uploads/bumbuf2/black_rook.png");
//> CHECK BISHOP
let enemyBishop = piecesAssets.filter((piece) => piece.path === "/uploads/bumbuf2/black_bishop.png");
//> CHECK HORSE
let enemyHorse = piecesAssets.filter((piece) => piece.path === "/uploads/bumbuf2/black_horse.png");
//> CHECK PAWN
// - i will have to check for myself
// - the rule is...the pos you want to go to +1y + 1x and if there is a black piece there dont add to possible
let enemyPawn = piecesAssets.filter((piece) => piece.path === "/uploads/bumbuf2/black_pawn.png");
// console.log(enemyPawn);
//> CHECK QUEEN
let enemyQueen = piecesAssets.filter((piece) => piece.path === "/uploads/bumbuf2/black_queen.png");
//> VALIDATE
if (possible.find(pos => pos === toCoord) && notPossible.includes(toCoord) === false) {
possibleMoves.push(toCoord);
if (piecesAssets[pieceState].initialPos === true) {
piecesAssets[pieceState].initialPos = false;
}
}
return possibleMoves;
}
window.onload = function() {
updateBoard();
document.querySelectorAll('td').forEach(td => {
td.addEventListener('click', function() {
let pass = false;
let val = this.getElementsByTagName('img');
if (val.length === 0) {
pass = true
} else if (val.length > 0) {
imgVal = val[0].src.includes("black");
if (imgVal === false || selected[0] !== "s_noned") {
pass = true
}
}
if (myTurn === true && pass === true) {
let imgs = this.getElementsByTagName('img');
if (selected[0] === "s_none" && imgs.length > 0) {
console.log(`Selected ${this.id}`);
selected[0] = this.id;
imgSrc = imgs[0].src.replace("https://run.ancientbrain.com", "");
} else if (selected[0] !== "s_none" && imgs.length > 0 && this.id !== selected[0]) {
selected[1] = imgs[0].src.replace("https://run.ancientbrain.com", "");
}
if (selected[0] !== "s_none" && this.id !== selected[0]) {
if (selected[1] !== "d_none") {
let selectedIndex = piecesAssets.findIndex((piece) => piece.coord === selected[0]);
let selectedTeam = piecesAssets[selectedIndex].team;
let dropIndex = piecesAssets.findIndex((piece) => selected[1].includes(piece.path) && piece.coord === this.id);
console.log(piecesAssets[dropIndex].team);
let dropTeam = piecesAssets[dropIndex].team;
if (selectedTeam !== dropTeam) {
console.log(`Piece moved to ${this.id}`);
if (piecesAssets[selectedIndex].category(piecesAssets[selectedIndex].coord, this.id).includes(this.id)) {
piecesAssets[selectedIndex].coord = this.id;
piecesAssets.splice(dropIndex, 1);
updateBoard();
possibleMoves = [];
} else {
console.log("Wrong move");
possibleMoves = [];
}
}
} else {
let pieceIndex = piecesAssets.findIndex((piece) => piece.coord === selected[0]);
let possible = piecesAssets[pieceIndex].category(piecesAssets[pieceIndex].coord, this.id);
if (possible.includes(this.id)) {
console.log(`Piece moved to ${this.id}`);
piecesAssets[pieceIndex].coord = this.id;
updateBoard();
possibleMoves = [];
myTurn = false;
} else {
console.log("Wrong move");
possibleMoves = [];
}
}
selected[0] = "s_none";
selected[1] = "d_none";
imgSrc = "";
}
}});
});
};
let theprompt = `As a chess bot, your task is to move a black piece to the optimal position.
Specify the piece moved and its new position in the format: "Move: {pieceName} {fromCoord} {toCoord}", where {fromCoord} and {toCoord} are the coordinates (1-8) in the format y/x.
You are only allowed to play black pieces. You are allowed to move only black pieces.
Black Pieces start from 1 aka at the top of the board.
You are not allowed to move any other pieces than black pieces.
You are allowed to output only the from and to coordinates. Your response should be in the format "Move: {pieceName} {fromCoord} {toCoord}".
Only use the words: move, from, to, and the "chess piece name". Do not use any other words.
Ensure you are moving a black piece. The y coordinate should be given first, followed by the x coordinate.
The pieces available are: ${piecesAssets} you are allowed to utilise only the ones with team:black`;
function updatePiecePosition(response) {
let parts = response.split(" ");
let fromCoord = parts[2];
let toCoord = parts[3];
console.log("Test", fromCoord);
console.log("TEST", toCoord);
let pieceIndex = piecesAssets.findIndex((piece) => piece.coord === fromCoord);
console.log(pieceIndex);
if (pieceIndex !== -1) {
piecesAssets[pieceIndex].coord = toCoord;
console.log(piecesAssets);
}
let result = "From: " + fromCoord + " To: " + toCoord
updateBoard();
myTurn = true;
return result;
}
let apikey;
const openaiURL = "https://api.openai.com/v1/chat/completions"; // can POST to this 3rd party URL
const themodel = "gpt-3.5-turbo"; // the OpenAI model we are going to talk to
function setkey() {
apikey = jQuery("input#apikey").val().trim();
$("#enterkey").html("<b> API key has been set. </b>");
}
function sendchat() {
// MH edit
console.log ( theprompt );
const thedata = {
"model": themodel,
"temperature": 0.7,
"messages": [{
"role": "user",
"content": theprompt
}]
};
const thedatastring = JSON.stringify(thedata);
// MH edit
console.log ( thedatastring );
$.ajax({
url: openaiURL,
type: 'POST',
data: thedatastring,
contentType: 'application/json',
headers: {
"Authorization": `Bearer ${apikey}`
},
success: successfn,
error: errorfn
});
}
function successfn(data) {
if (data["choices"] && data["choices"].length > 0) {
const answer = data["choices"][0].message.content;
console.log(answer);
$("#them").html(updatePiecePosition(answer));
} else {
console.error('No choices in the response data');
}
}
function errorfn(error) {
console.error("Error: ", error);
if (apikey === "") {
$("#them").html("<font color=red><b>Enter API key to be able to chat.</b></font>");
} else {
$("#them").html("<font color=red><b>Error: " + error + "</b></font>");
}
}