We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Simplified Chess Engine
Simplified Chess Engine
Sort by
recency
|
36 Discussions
|
Please Login in order to post a comment
C++ solution. I tried to fit in 30 lines. Without success :) The main part of the algorithm is explained by nickww718 https://www.hackerrank.com/challenges/simplified-chess-engine/forum/comments/447437
Javascript solution
'use strict'; const fs = require('fs'); process.stdin.resume(); process.stdin.setEncoding('utf-8'); let inputString = ''; let currentLine = 0; process.stdin.on('data', inputStdin => { inputString += inputStdin; }); process.stdin.on('end', _ => { inputString = inputString.trim().split('\n').map(str => str.trim()); main(); }); function readLine() { return inputString[currentLine++]; } /* Complete the simplifiedChessEngine function below. */ 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); } function main() { const ws = fs.createWriteStream(process.env.OUTPUT_PATH); const g = parseInt(readLine(), 10); for (let gItr = 0; gItr < g; gItr++) { const wbm = readLine().split(' '); const w = parseInt(wbm[0], 10); const b = parseInt(wbm[1], 10); const m = parseInt(wbm[2], 10); let whites = Array(w); for (let whitesRowItr = 0; whitesRowItr < w; whitesRowItr++) { whites[whitesRowItr] = readLine().split(' ').map(whitesTemp => whitesTemp[0]); } let blacks = Array(b); for (let blacksRowItr = 0; blacksRowItr < b; blacksRowItr++) { blacks[blacksRowItr] = readLine().split(' ').map(blacksTemp => blacksTemp[0]); } let result = simplifiedChessEngine(whites, blacks, m); ws.write(result +\n
); } ws.end(); }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); }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); }Here is Simplified Chess Engine problem solution in Python Java C++ and c programming - https://programs.programmingoneonone.com/2021/07/hackerrank-simplified-chess-engine-problem-solution.html