Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.08 KB | None | 0 0
  1. // Returns an array of threats if the arrangement of
  2. // the pieces is a check, otherwise false
  3. var getMoves = function getMoves(p, board) {
  4. var x = p.x;
  5. var y = p.y;
  6.  
  7.  
  8. var getPawnMoves = function getPawnMoves(p, x, y) {
  9.  
  10. if (p.owner == 0) {
  11. //white
  12. return [{
  13. x: x + 1,
  14. y: y - 1
  15. }, {
  16. x: x - 1,
  17. y: y - 1
  18. }];
  19. } else {
  20. //black
  21. return [{
  22. x: x + 1,
  23. y: y + 1
  24. }, {
  25. x: x - 1,
  26. y: y + 1
  27. }];
  28. }
  29. };
  30. var getRookMoves = function getRookMoves(p, x, y) {
  31. var moves = [];
  32. //move 8 vert and 8 hor - remove any negatives or any greater than 7
  33. var availableDirections = {
  34. UP: true,
  35. LEFT: true,
  36. RIGHT: true,
  37. DOWN: true
  38. };
  39. for (var i = 1; i < 8; i++) {
  40.  
  41. var newX = {
  42. positive: x + i > 7 ? undefined : x + i,
  43. negative: x - i < 0 ? undefined : x - i
  44. };
  45. var newY = {
  46. positive: y + i > 7 ? undefined : y + i,
  47. negative: y - i < 0 ? undefined : y - i
  48. };
  49.  
  50. availableDirections = getAvailableMovementDirections(p, newX, newY, availableDirections,board);
  51.  
  52. //move right
  53. if (newX.positive && availableDirections.RIGHT) {
  54. moves.push({
  55. x: newX.positive,
  56. y: y
  57. });
  58. }
  59. if (newX.negative && availableDirections.LEFT) {
  60. moves.push({
  61. x: newX.negative,
  62. y: y
  63. });
  64. }
  65. if (newY.positive && availableDirections.DOWN) {
  66. moves.push({
  67. x: x,
  68. y: newY.positive
  69. });
  70. }
  71. if (newY.negative && availableDirections.UP) {
  72. moves.push({
  73. x: x,
  74. y: newY.negative
  75. });
  76. }
  77.  
  78. }
  79.  
  80. return moves;
  81. };
  82. var getKnightMoves = function getKnightMoves(p, x, y) {
  83. var moves = [];
  84.  
  85. if (!isPositionOccupied(board, {
  86. x: x + 1,
  87. y: y - 2
  88. }) || isOppositeKing(p, {
  89. x: x + 1,
  90. y: y - 2
  91. },board )) {
  92. moves.push({
  93. x: x + 1,
  94. y: y - 2
  95. });
  96. }
  97. if (!isPositionOccupied(board,{
  98. x: x + 1,
  99. y: y + 2
  100. }) || isOppositeKing(p, {
  101. x: x + 1,
  102. y: y + 2
  103. },board )) {
  104. moves.push({
  105. x: x + 1,
  106. y: y + 2
  107. });
  108. }
  109.  
  110. if (!isPositionOccupied(board,{
  111. x: x + 2,
  112. y: y - 1
  113. }) || isOppositeKing(p, {
  114. x: x + 2,
  115. y: y - 1
  116. },board )) {
  117. moves.push({
  118. x: x + 2,
  119. y: y - 1
  120. });
  121. }
  122.  
  123. if (!isPositionOccupied(board,{
  124. x: x + 2,
  125. y: y + 1
  126. }) || isOppositeKing(p, {
  127. x: x + 2,
  128. y: y + 1
  129. },board )) {
  130. moves.push({
  131. x: x + 2,
  132. y: y + 1
  133. });
  134. }
  135.  
  136.  
  137. if (!isPositionOccupied(board,{
  138. x: x - 1,
  139. y: y - 2
  140. }) || isOppositeKing(p, {
  141. x: x - 1,
  142. y: y - 2
  143. },board )) {
  144. moves.push({
  145. x: x - 1,
  146. y: y - 2
  147. });
  148. }
  149.  
  150. if (!isPositionOccupied(board,{
  151. x: x - 1,
  152. y: y + 2
  153. }) || isOppositeKing(p, {
  154. x: x - 1,
  155. y: y + 2
  156. },board )) {
  157. moves.push({
  158. x: x - 1,
  159. y: y + 2
  160. });
  161. }
  162.  
  163. if (!isPositionOccupied(board,{
  164. x: x - 2,
  165. y: y - 1
  166. }) || isOppositeKing(p, {
  167. x: x - 2,
  168. y: y - 1
  169. },board )) {
  170. moves.push({
  171. x: x - 2,
  172. y: y - 1
  173. });
  174. }
  175.  
  176. if (!isPositionOccupied(board,{
  177. x: x - 2,
  178. y: y + 1
  179. }) || isOppositeKing(p, {
  180. x: x - 2,
  181. y: y + 1
  182. },board )) {
  183. moves.push({
  184. x: x - 2,
  185. y: y + 1
  186. });
  187. }
  188. return moves;
  189.  
  190. };
  191. var getQueenMoves = function getQueenMoves(p, x, y) {
  192. var moves = [];
  193.  
  194. var availableDirections = {
  195. UP: true,
  196. LEFT: true,
  197. RIGHT: true,
  198. DOWN: true,
  199. UP_LEFT: true,
  200. UP_RIGHT: true,
  201. DOWN_LEFT: true,
  202. DOWN_RIGHT: true
  203. };
  204. for (var i = 1; i < 8; i++) {
  205.  
  206.  
  207. var newX = {
  208. positive: x + i > 7 ? undefined : x + i,
  209. negative: x - i < 0 ? undefined : x - i
  210. };
  211. var newY = {
  212. positive: y + i > 7 ? undefined : y + i,
  213. negative: y - i < 0 ? undefined : y - i
  214. };
  215.  
  216. availableDirections = getAvailableMovementDirections(p, newX, newY, availableDirections,board);
  217. //move right
  218. if (availableDirections.RIGHT && newX.positive) {
  219. moves.push({
  220. x: newX.positive,
  221. y: y
  222. });
  223. }
  224. //move up and right
  225. if (availableDirections.UP_RIGHT && newX.positive && newY.negative) {
  226. moves.push({
  227. x: newX.positive,
  228. y: newY.negative
  229. });
  230. }
  231. //move up
  232. if (availableDirections.UP && newY.negative) {
  233. moves.push({
  234. x: x,
  235. y: newY.negative
  236. });
  237. }
  238. //move up and left
  239. if (availableDirections.UP_LEFT && newY.negative && newX.negative) {
  240. moves.push({
  241. x: newX.negative,
  242. y: newY.negative
  243. });
  244. }
  245. //move left
  246. if (availableDirections.LEFT && newX.negative) {
  247. moves.push({
  248. x: newX.negative,
  249. y: y
  250. });
  251. }
  252. //move left and down
  253. if (availableDirections.DOWN_LEFT && newX.negative && newY.positive) {
  254. moves.push({
  255. x: newX.negative,
  256. y: newY.positive
  257. });
  258. }
  259. //move down
  260. if (availableDirections.DOWN && newY.positive) {
  261. moves.push({
  262. x: x,
  263. y: newY.positive
  264. });
  265. }
  266. //move down and right
  267. if (availableDirections.DOWN_RIGHT && newX.positive && newY.positive) {
  268. moves.push({
  269. x: newX.positive,
  270. y: newY.positive
  271. });
  272. }
  273.  
  274.  
  275. }
  276.  
  277. return moves;
  278. };
  279. var getBishopMoves = function getBishopMoves(p, x, y) {
  280. var moves = [];
  281.  
  282. var availableDirections = {
  283. UP_LEFT: true,
  284. UP_RIGHT: true,
  285. DOWN_LEFT: true,
  286. DOWN_RIGHT: true
  287. };
  288. //move 8 vert and 8 hor - remove any negatives or any greater than 7
  289. for (var i = 1; i < 8; i++) {
  290.  
  291. var newX = {
  292. positive: x + i > 7 ? undefined : x + i,
  293. negative: x - i < 0 ? undefined : x - i
  294. };
  295. var newY = {
  296. positive: y + i > 7 ? undefined : y + i,
  297. negative: y - i < 0 ? undefined : y - i
  298. };
  299.  
  300. availableDirections = getAvailableMovementDirections(p, newX, newY, availableDirections,board);
  301.  
  302. //move up and right
  303. if (availableDirections.UP_RIGHT && newX.positive && newY.negative) {
  304. moves.push({
  305. x: newX.positive,
  306. y: newY.negative
  307. });
  308. }
  309.  
  310. //move up and left
  311. if (availableDirections.UP_LEFT && newY.negative && newX.negative) {
  312. moves.push({
  313. x: newX.negative,
  314. y: newY.negative
  315. });
  316. }
  317.  
  318. //move left and down
  319. if (availableDirections.DOWN_LEFT && newX.negative && newY.positive) {
  320. moves.push({
  321. x: newX.negative,
  322. y: newY.positive
  323. });
  324. }
  325.  
  326. //move down and right
  327. if (availableDirections.DOWN_RIGHT && newX.positive && newY.positive) {
  328. moves.push({
  329. x: newX.positive,
  330. y: newY.positive
  331. });
  332. }
  333.  
  334.  
  335. }
  336.  
  337. return moves;
  338. };
  339. var getKingMoves = function getKingMoves(p, x, y) {
  340. let moves = [];
  341.  
  342. var availableDirections = {
  343. UP: true,
  344. LEFT: true,
  345. RIGHT: true,
  346. DOWN: true
  347. };
  348. var newX = {
  349. positive: x + 1 > 7 ? undefined : x + 1,
  350. negative: x - 1 < 0 ? undefined : x - 1
  351. };
  352. var newY = {
  353. positive: y + 1 > 7 ? undefined : y + 1,
  354. negative: y - 1 < 0 ? undefined : y - 1
  355. };
  356.  
  357. availableDirections = getAvailableMovementDirections(p, newX, newY, availableDirections, board);
  358.  
  359. //move right
  360. if (availableDirections.RIGHT && newX.positive) {
  361. moves.push({
  362. x: newX.positive,
  363. y: y
  364. });
  365. }
  366. //move up and right
  367. if (availableDirections.UP_RIGHT && newX.positive && newY.negative) {
  368. moves.push({
  369. x: newX.positive,
  370. y: newY.negative
  371. });
  372. }
  373. //move up
  374. if (availableDirections.UP && newY.negative) {
  375. moves.push({
  376. x: x,
  377. y: newY.negative
  378. });
  379. }
  380. //move up and left
  381. if (availableDirections.UP_LEFT && newY.negative && newX.negative) {
  382. moves.push({
  383. x: newX.negative,
  384. y: newY.negative
  385. });
  386. }
  387. //move left
  388. if (availableDirections.LEFT && newX.negative) {
  389. moves.push({
  390. x: newX.negative,
  391. y: y
  392. });
  393. }
  394. //move left and down
  395. if (availableDirections.DOWN_LEFT && newX.negative && newY.positive) {
  396. moves.push({
  397. x: newX.negative,
  398. y: newY.positive
  399. });
  400. }
  401. //move down
  402. if (availableDirections.DOWN && newY.positive) {
  403. moves.push({
  404. x: x,
  405. y: newY.positive
  406. });
  407. }
  408. //move down and right
  409. if (availableDirections.DOWN_RIGHT && newX.positive && newY.positive) {
  410. moves.push({
  411. x: newX.positive,
  412. y: newY.positive
  413. });
  414. }
  415.  
  416.  
  417. return moves;
  418. };
  419. switch (p.piece) {
  420. case 'pawn':
  421. return getPawnMoves(p, x, y);
  422. case 'rook':
  423. return getRookMoves(p, x, y);
  424. case 'bishop':
  425. return getBishopMoves(p, x, y);
  426. case 'queen':
  427. return getQueenMoves(p, x, y);
  428. case 'knight':
  429. return getKnightMoves(p, x, y);
  430. case 'king':
  431. return getKingMoves(p, x, y);
  432. }
  433. };
  434.  
  435.  
  436. function getAvailableMovementDirections(piece, newX, newY, directions, board) {
  437. const x = piece.x;
  438. const y = piece.y;
  439.  
  440. if (directions.RIGHT && isPositionOccupied(board, {
  441. x: newX.positive,
  442. y: y
  443. })) {
  444.  
  445. directions.RIGHT = isOppositeKing(piece, {
  446. x: newX.positive,
  447. y: y
  448. }, board) ? directions.RIGHT : false;
  449. }
  450. if (directions.LEFT && isPositionOccupied(board, {
  451. x: newX.negative,
  452. y: y
  453. })) {
  454.  
  455. directions.LEFT = isOppositeKing(piece, {
  456. x: newX.negative,
  457. y: y
  458. }, board) ? directions.LEFT : false;
  459. }
  460. if (directions.DOWN && isPositionOccupied(board, {
  461. x: x,
  462. y: newY.positive
  463. })) {
  464.  
  465. directions.DOWN = isOppositeKing(piece, {
  466. x: x,
  467. y: newY.positive
  468. }, board) ? directions.DOWN : false;
  469. }
  470. if (directions.UP && isPositionOccupied(board, {
  471. x: x,
  472. y: newY.negative
  473. })) {
  474.  
  475. directions.UP = isOppositeKing(piece, {
  476. x: x,
  477. y: newY.negative
  478. }, board) ? directions.UP : false;
  479. }
  480. if (directions.UP_RIGHT && isPositionOccupied(board, {
  481. x: newX.positive,
  482. y: newY.negative
  483. })) {
  484.  
  485. directions.UP_RIGHT = isOppositeKing(piece, {
  486. x: newX.positive,
  487. y: newY.negative
  488. }, board) ? directions.UP_RIGHT : false;
  489. }
  490. if (directions.UP_LEFT && isPositionOccupied(board, {
  491. x: newX.negative,
  492. y: newY.negative
  493. })) {
  494.  
  495. directions.UP_LEFT = isOppositeKing(piece, {
  496. x: newX.negative,
  497. y: newY.negative
  498. }, board) ? directions.UP_LEFT : false;
  499. }
  500. if (directions.DOWN_LEFT && isPositionOccupied(board, {
  501. x: newX.negative,
  502. y: newY.positive
  503. })) {
  504.  
  505. directions.DOWN_LEFT = isOppositeKing(piece, {
  506. x: newX.negative,
  507. y: newY.positive
  508. }, board) ? directions.DOWN_LEFT : false;
  509. }
  510. if (directions.DOWN_RIGHT && isPositionOccupied(board, {
  511. x: newX.positive,
  512. y: newY.positive
  513. })) {
  514.  
  515. directions.DOWN_RIGHT = isOppositeKing(piece, {
  516. x: newX.positive,
  517. y: newY.positive
  518. }, board) ? directions.DOWN_RIGHT : false;
  519. }
  520.  
  521. return directions;
  522. }
  523.  
  524.  
  525. function getPiece(pieces, name, owner) {
  526. return pieces.find(function(piece) {
  527. return piece.owner == owner && piece.piece == name;
  528. });
  529. }
  530.  
  531. function isPositionOccupied(pieces, pos) {
  532. return pieces.findIndex(p => p.x == pos.x && p.y == pos.y) > -1;
  533. }
  534.  
  535. function isOppositeKing(piece, position, board) {
  536. let p = getPieceAtPosition(board, position);
  537. return p.owner != piece.owner && p.piece == 'king';
  538. }
  539.  
  540. function getPieceAtPosition(pieces, pos) {
  541. return pieces.find(p => p.x == pos.x && p.y == pos.y);
  542. }
  543.  
  544. function isCheck(pieces, player) {
  545. var king = getPiece(pieces, 'king', player);
  546.  
  547. var isChecking = function isChecking(p, king) {
  548.  
  549. if (getMoves(p, pieces).findIndex(m => m.x == king.x && m.y == king.y) > -1) {
  550.  
  551. return true;
  552. } else {
  553.  
  554. return false;
  555. }
  556. };
  557. var getCheckingPieces = function getCheckingPieces(candidatePieces) {
  558. let checkingPieces = [];
  559.  
  560. candidatePieces.forEach(function(p) {
  561.  
  562. if (p == king) return;
  563. if (isChecking(p, king)) {
  564.  
  565. checkingPieces.push(p);
  566. }
  567. });
  568. return checkingPieces && checkingPieces.length > 0 ? checkingPieces : false;
  569. };
  570.  
  571.  
  572. return getCheckingPieces(pieces);
  573. }
  574.  
  575. const isMate = (pieces, player) => {
  576. let king = getPiece(pieces, 'king', player);
  577. let checkMate = true;
  578.  
  579. //check all possible moves from king against isCheck function
  580. let possibleMoves = getMoves(king, pieces);
  581.  
  582. possibleMoves.forEach(move => {
  583. if(!checkMate) return;
  584.  
  585. let kingIndex = pieces.findIndex(p => p.piece == 'king' && p.owner == player);
  586. let newKing = {piece: 'king', owner: player, x: move.x, y: move.y};
  587.  
  588. //construct a new board with the potential move
  589. let board = [...pieces.slice(0, kingIndex), newKing, ...pieces.slice(kingIndex +1, pieces.length)];
  590.  
  591. if(isCheck(board, player).length == 0){
  592. checkMate = false;
  593. return;
  594. }
  595. })
  596.  
  597. return checkMate;
  598. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement