You are viewing a single comment's thread. Return to all comments →
Easiest javascript solution..................... One and Only javascript solution
function simplifiedChessEngine(whites, blacks, moves) { const LETTERS = { A: 1, B: 2, C: 3, D: 4 }; whites.forEach((item) => { item[1] = LETTERS[item[1]]; item[2] = Number(item[2]); }); blacks.forEach((item) => { item[1] = LETTERS[item[1]]; item[2] = Number(item[2]); }); const queenMoves = (x, y, own, enemy) => { return [...bishopMoves(x, y, own, enemy), ...rookMoves(x, y, own, enemy)]; }; const knightMoves = (x, y, own) => { const result = []; if (x > 1 && y > 2 && own.every(([_, _x, _y]) => _x !== x - 1 || y !== y - 2)) { result.push([x - 1, y - 2]); } if (x > 1 && y < 3 && own.every(([, _x, _y]) => _x !== x - 1 || y !== y + 2)) { result.push([x - 1, y + 2]); } if (x < 4 && y > 2 && own.every(([, _x, _y]) => _x !== x + 1 || y !== y - 2)) { result.push([x + 1, y - 2]); } if (x < 4 && y < 3 && own.every(([, _x, _y]) => _x !== x + 1 || y !== y + 2)) { result.push([x + 1, y + 2]); } if (x > 2 && y > 1 && own.every(([, _x, _y]) => _x !== x - 2 || y !== y - 1)) { result.push([x - 2, y - 1]); } if (x > 2 && y < 4 && own.every(([, _x, _y]) => _x !== x - 2 || y !== y + 1)) { result.push([x - 2, y + 1]); } if (x < 3 && y > 1 && own.every(([, _x, _y]) => _x !== x + 2 || y !== y - 1)) { result.push([x + 2, y - 1]); } if (x < 3 && y < 4 && own.every(([, _x, _y]) => _x !== x + 2 || y !== y + 1)) { result.push([x + 2, y + 1]); } return result; }; const bishopMoves = (x, y, own, enemy) => { const result = []; for (let i = x - 1, j = y - 1; i > 0 && j > 0; i -= 1, j -= 1) { if (own.some(([, _x, _y]) => _x === i && y === j)) { break; } result.push([i, j]); if (enemy.some(([, _x, _y]) => _x === i && y === j)) { break; } } for (let i = x - 1, j = y + 1; i > 0 && j <= 4; i -= 1, j += 1) { if (own.some(([, _x, _y]) => _x === i && y === j)) { break; } result.push([i, j]); if (enemy.some(([, _x, _y]) => _x === i && y === j)) { break; } } for (let i = x + 1, j = y - 1; i <= 4 && j > 0; i += 1, j -= 1) { if (own.some(([, _x, _y]) => _x === i && y === j)) { break; } result.push([i, j]); if (enemy.some(([, _x, _y]) => _x === i && y === j)) { break; } } for (let i = x + 1, j = y + 1; i <= 4 && j <= 4; i += 1, j += 1) { if (own.some(([, _x, _y]) => _x === i && y === j)) { break; } result.push([i, j]); if (enemy.some(([, _x, _y]) => _x === i && y === j)) { break; } } return result; }; const rookMoves = (x, y, own, enemy) => { const result = []; for (let i = x - 1; i > 0; i -= 1) { if (own.some(([, _x, _y]) => _x === i && y === y)) { break; } result.push([i, y]); if (enemy.some(([, _x, _y]) => _x === i && y === y)) { break; } } for (let i = x + 1; i <= 4; i += 1) { if (own.some(([, _x, _y]) => _x === i && y === y)) { break; } result.push([i, y]); if (enemy.some(([, _x, _y]) => _x === i && y === y)) { break; } } for (let i = y - 1; i > 0; i -= 1) { if (own.some(([, _x, _y]) => _x === x && y === i)) { break; } result.push([x, i]); if (enemy.some(([, _x, _y]) => _x === x && y === i)) { break; } } for (let i = y + 1; i <= 4; i += 1) { if (own.some(([, _x, _y]) => _x === x && y === i)) { break; } result.push([x, i]); if (enemy.some(([, _x, _y]) => _x === x && y === i)) { break; } } return result; }; const pieceMoves = { Q: queenMoves, N: knightMoves, B: bishopMoves, R: rookMoves } const whiteMove = (w, b, m) => { if (m === 0) { return NO; } const [, qX, qY] = b.find(([piece]) => piece === Q); for (let i = 0; i < w.length; i += 1) { const [piece, x, y] = w[i]; const variants = pieceMoves[piece](x, y, w, b); if (m === 5) { console.log(piece); console.log(variants); } for (let j = 0; j < variants.length; j += 1) { const [_x, _y] = variants[j]; if (_x === qX && _y === qY) { return YES; } const _w = w .map((it, index) => index === i ? [piece, _x, _y] : [...it]); const b = b .map((it) => [...it]) .filter(([, __x, __y]) => __x !== _x || __y !== _y); if (blackMove(_w, b, m - 1) === YES) { return YES; } } } return NO; }; const blackMove = (w, b, m) => { if (m === 0) { return NO; } const [, qX, qY] = w.find(([piece]) => piece === Q); for (let i = 0; i < b.length; i += 1) { const [piece, x, y] = b[i]; const variants = pieceMoves[piece](x, y, b, w); for (let j = 0; j < variants.length; j += 1) { const [_x, _y] = variants[j]; if (_x === qX && _y === qY) { return NO; } const w = w .map((it) => [...it]) .filter(([, __x, __y]) => __x !== _x || __y !== _y) const _b = b .map((it, index) => index === i ? [piece, _x, _y] : [...it]); if (whiteMove(_w, _b, m - 1) === NO) { return NO; } } } return YES; }; return whiteMove(whites, blacks, moves); }
NO
Q
YES
Seems like cookies are disabled on this browser, please enable them to open this website
Simplified Chess Engine
You are viewing a single comment's thread. Return to all comments →
Easiest javascript solution..................... One and Only javascript solution
function simplifiedChessEngine(whites, blacks, moves) { const LETTERS = { A: 1, B: 2, C: 3, D: 4 }; whites.forEach((item) => { item[1] = LETTERS[item[1]]; item[2] = Number(item[2]); }); blacks.forEach((item) => { item[1] = LETTERS[item[1]]; item[2] = Number(item[2]); }); const queenMoves = (x, y, own, enemy) => { return [...bishopMoves(x, y, own, enemy), ...rookMoves(x, y, own, enemy)]; }; const knightMoves = (x, y, own) => { const result = []; if (x > 1 && y > 2 && own.every(([_, _x, _y]) => _x !== x - 1 || y !== y - 2)) { result.push([x - 1, y - 2]); } if (x > 1 && y < 3 && own.every(([, _x, _y]) => _x !== x - 1 || y !== y + 2)) { result.push([x - 1, y + 2]); } if (x < 4 && y > 2 && own.every(([, _x, _y]) => _x !== x + 1 || y !== y - 2)) { result.push([x + 1, y - 2]); } if (x < 4 && y < 3 && own.every(([, _x, _y]) => _x !== x + 1 || y !== y + 2)) { result.push([x + 1, y + 2]); } if (x > 2 && y > 1 && own.every(([, _x, _y]) => _x !== x - 2 || y !== y - 1)) { result.push([x - 2, y - 1]); } if (x > 2 && y < 4 && own.every(([, _x, _y]) => _x !== x - 2 || y !== y + 1)) { result.push([x - 2, y + 1]); } if (x < 3 && y > 1 && own.every(([, _x, _y]) => _x !== x + 2 || y !== y - 1)) { result.push([x + 2, y - 1]); } if (x < 3 && y < 4 && own.every(([, _x, _y]) => _x !== x + 2 || y !== y + 1)) { result.push([x + 2, y + 1]); } return result; }; const bishopMoves = (x, y, own, enemy) => { const result = []; for (let i = x - 1, j = y - 1; i > 0 && j > 0; i -= 1, j -= 1) { if (own.some(([, _x, _y]) => _x === i && y === j)) { break; } result.push([i, j]); if (enemy.some(([, _x, _y]) => _x === i && y === j)) { break; } } for (let i = x - 1, j = y + 1; i > 0 && j <= 4; i -= 1, j += 1) { if (own.some(([, _x, _y]) => _x === i && y === j)) { break; } result.push([i, j]); if (enemy.some(([, _x, _y]) => _x === i && y === j)) { break; } } for (let i = x + 1, j = y - 1; i <= 4 && j > 0; i += 1, j -= 1) { if (own.some(([, _x, _y]) => _x === i && y === j)) { break; } result.push([i, j]); if (enemy.some(([, _x, _y]) => _x === i && y === j)) { break; } } for (let i = x + 1, j = y + 1; i <= 4 && j <= 4; i += 1, j += 1) { if (own.some(([, _x, _y]) => _x === i && y === j)) { break; } result.push([i, j]); if (enemy.some(([, _x, _y]) => _x === i && y === j)) { break; } } return result; }; const rookMoves = (x, y, own, enemy) => { const result = []; for (let i = x - 1; i > 0; i -= 1) { if (own.some(([, _x, _y]) => _x === i && y === y)) { break; } result.push([i, y]); if (enemy.some(([, _x, _y]) => _x === i && y === y)) { break; } } for (let i = x + 1; i <= 4; i += 1) { if (own.some(([, _x, _y]) => _x === i && y === y)) { break; } result.push([i, y]); if (enemy.some(([, _x, _y]) => _x === i && y === y)) { break; } } for (let i = y - 1; i > 0; i -= 1) { if (own.some(([, _x, _y]) => _x === x && y === i)) { break; } result.push([x, i]); if (enemy.some(([, _x, _y]) => _x === x && y === i)) { break; } } for (let i = y + 1; i <= 4; i += 1) { if (own.some(([, _x, _y]) => _x === x && y === i)) { break; } result.push([x, i]); if (enemy.some(([, _x, _y]) => _x === x && y === i)) { break; } } return result; }; const pieceMoves = { Q: queenMoves, N: knightMoves, B: bishopMoves, R: rookMoves } const whiteMove = (w, b, m) => { if (m === 0) { return
NO
; } const [, qX, qY] = b.find(([piece]) => piece ===Q
); for (let i = 0; i < w.length; i += 1) { const [piece, x, y] = w[i]; const variants = pieceMoves[piece](x, y, w, b); if (m === 5) { console.log(piece); console.log(variants); } for (let j = 0; j < variants.length; j += 1) { const [_x, _y] = variants[j]; if (_x === qX && _y === qY) { returnYES
; } const _w = w .map((it, index) => index === i ? [piece, _x, _y] : [...it]); const b = b .map((it) => [...it]) .filter(([, __x, __y]) => __x !== _x || __y !== _y); if (blackMove(_w, b, m - 1) ===YES
) { returnYES
; } } } returnNO
; }; const blackMove = (w, b, m) => { if (m === 0) { returnNO
; } const [, qX, qY] = w.find(([piece]) => piece ===Q
); for (let i = 0; i < b.length; i += 1) { const [piece, x, y] = b[i]; const variants = pieceMoves[piece](x, y, b, w); for (let j = 0; j < variants.length; j += 1) { const [_x, _y] = variants[j]; if (_x === qX && _y === qY) { returnNO
; } const w = w .map((it) => [...it]) .filter(([, __x, __y]) => __x !== _x || __y !== _y) const _b = b .map((it, index) => index === i ? [piece, _x, _y] : [...it]); if (whiteMove(_w, _b, m - 1) ===NO
) { returnNO
; } } } returnYES
; }; return whiteMove(whites, blacks, moves); }