Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const readline = require('readline');
- const rl = readline.createInterface({
- input: process.stdin,
- output: process.stdout
- });
- function isValidPosition(x, y) {
- $pass = 1 <= x <= 8 && 1 <= y <= 8;
- if (x == '' || y == '' || !$pass) {
- return false;
- }
- return true;
- }
- function blackOrWhite(x, y) {
- if ((x % 2 == 0 && y % 2 == 0) || (x % 2 != 0 && y % 2 != 0)) {
- return 'black';
- }
- return 'white';
- }
- function findKnightNextMove(x, y) {
- const next = [];
- const curColor = blackOrWhite(x, y);
- // Lap qua cac vi tri xung quanh con ma trong vong ban kinh 2 o
- for (let i = x - 2; i <= x + 2; i++) {
- for (let j = y - 2; j <= y + 2; j++) {
- // giong voi vi tri hien tai cua con ma
- const isCurPos = x == i && y == j;
- // trong vong ban kinh 1 o
- let isRadiusLTE1 = false;
- if ((x - i === 1 || x === i || i - x === 1) && (y - j === 1 || y === j || j - y === 1)) {
- isRadiusLTE1 = true;
- }
- if (!isValidPosition(i, j) || isCurPos || isRadiusLTE1) {
- continue;
- }
- if (blackOrWhite(i, j) != curColor) {
- next.push(`${i}|${j}`);
- }
- }
- }
- return next;
- }
- function ham() {
- rl.question('Nhap vi tri truc X ', function (A) {
- rl.question('Nhap vi tri truc y ', function (B) {
- A = +A;
- B = +B;
- if (!isValidPosition(A, B)) {
- console.log('Vi tri khong hop le');
- ham();
- return;
- }
- console.log('Cac vi tri con ma duoc di la: ', findKnightNextMove(A, B));
- rl.close();
- });
- });
- }
- ham();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement