Simplified Chess Engine

Sort by

recency

|

36 Discussions

|

  • + 0 comments

    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

    int getCell(uint64_t board, int idx) { 
      return (board&(15LL<<(idx<<2)))>>(idx<<2); 
    }
    void setCell(uint64_t &board, int idx, int value) { 
      board = board &(~(15LL<<(idx<<2)))|(uint64_t(value)<<(idx<<2));
    }
    string simplifiedChessEngine(vector<vector<char>> const& whites,
    vector<vector<char>> const& blacks, int moves, 
    int curMove = 1, uint64_t board = 0) {
      if (curMove==1)             //board=4*4*{0FFP},F:figute,P:player
        for (int plr = 0; plr < 2; plr++)           
          for (auto& f : plr?blacks:whites)                    //ABCDE
            setCell(board, f[1]-'A' + ((f[2] - '1') << 2),     //FGHIJ
              plr | "60000000000040028"[f[0] - 'B'] - '0');    //KL*NO
      int plr = (curMove & 1) ^ 1, cell1 = 0, x = 0, y = 0;    //PQRST
      if (curMove > moves) return plr ? "YES" : "NO";          //UVWXY
      char figMoves[5][9]={"","NIHGLQRS","JDBFPVXT","IGQS","NHLR" };
      for (int i = 0; i < 16; i++)                            
        if ((cell1 = getCell(board, i)) && (cell1 & 1) == plr)                   
          for (char* m = figMoves[cell1 >> 1]; *m; m++)   
            for (int d = 1, e = ((cell1>>1)==2?1:3); d <= e; d++) {
              if ((x = (i & 3) + d * ((*m-'A')%5-2)) >= 0 && x < 4 &&
              (y = (i >> 2) + d * ((*m-'A')/5-2)) >= 0 && y < 4) {
                uint64_t oldBoard = board;
                int cell2 = getCell(board, x | (y << 2));
                if (cell2) {
                  if ((cell2 & 1) == plr) break;
                  else if ((cell2 >> 1) == 1) return"YES";
                }
                setCell(board, i, 0);
                setCell(board, x | (y << 2), cell1);
                if (simplifiedChessEngine(
                  whites, blacks, moves, curMove + 1, board) == "NO")
                  return "YES";
                board = oldBoard;
                if (cell2)break;
              } else break;
            }
      return "NO";
    }
    
  • + 0 comments

    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) { 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); } 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(); }

  • + 0 comments

    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); }

  • + 0 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); }

  • + 0 comments

    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