Created
June 23, 2017 12:57
-
-
Save dan-cooke/6f45a73062a59a6093dd9f304c4d1685 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/macelabeto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script src="https://unpkg.com/@reactivex/[email protected]/dist/global/Rx.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/expect/1.20.2/expect.js"></script> | |
<script id="jsbin-javascript"> | |
"use strict"; | |
var pieces = [{ piece: "king", owner: 1, x: 4, y: 0 }, { piece: "king", owner: 0, x: 4, y: 7 }, { piece: "pawn", owner: 1, x: 5, y: 6 }]; | |
// Returns an array of threats if the arrangement of | |
// the pieces is a check, otherwise false | |
function isCheck(pieces, player) { | |
var blackKing = getPiece('king', 1); | |
var whiteKing = getPiece('king', 0); | |
function getPiece(name, owner) { | |
return pieces.find(function (piece) { | |
return piece.owner == owner && piece.piece == name; | |
}); | |
} | |
var getMoves = function getMoves(p) { | |
var x = p.x; | |
var y = p.y; | |
var getPawnMoves = function getPawnMoves(p, x, y) { | |
if (p.owner == 0) { | |
//white | |
return [{ x: x + 1, y: y + 1 }, { x: x - 1, y: y + 1 }]; | |
} else { | |
//black | |
return [{ x: x + 1, y: y - 1 }, { x: x - 1, y: y - 1 }]; | |
} | |
}; | |
var getRookMoves = function getRookMoves(p, x, y) { | |
var moves = []; | |
//move 8 vert and 8 hor - remove any negatives or any greater than 7 | |
for (var i = 0; i < 8; i++) { | |
for (var j = 0; j < 8; j++) { | |
var newX = { | |
positive: x + i > 7 ? undefined : x + i, | |
negative: x - i < 0 ? undefined : x - i | |
}; | |
var newY = { | |
positive: y + i > 7 ? undefined : y + i, | |
negative: y - i < 0 ? undefined : y - i | |
}; | |
if (newX.positive) { | |
moves.push({ x: newX.positive, y: y }); | |
} | |
if (newX.negative) { | |
moves.push({ x: newX.negative, y: y }); | |
} | |
if (newY.positive) { | |
moves.push({ x: x, y: newY.positive }); | |
} | |
if (newY.negative) { | |
moves.push({ x: x, y: newY.negative }); | |
} | |
} | |
} | |
}; | |
var getKnightMoves = function getKnightMoves(p, x, y) {}; | |
var getQueenMoves = function getQueenMoves(p, x, y) {}; | |
var getBishopMoves = function getBishopMoves(p, x, y) {}; | |
switch (p.piece) { | |
case 'pawn': | |
return getPawnMoves(p, x, y); | |
case 'rook': | |
return getRookMoves(p, x, y); | |
case 'bishop': | |
return getBishopMoves(p, x, y); | |
case 'queen': | |
return getQueenMoves(p, x, y); | |
case 'knight': | |
return getKnightMoves(p, x, y); | |
} | |
}; | |
var isChecking = function isChecking(p, king) { | |
if (getMoves(p).includes({ x: king.x, y: king.y })) { | |
return true; | |
} else { | |
return false; | |
} | |
}; | |
var getCheckingPieces = function getCheckingPieces() { | |
var pieces = []; | |
pieces.forEach(function (p) { | |
if (isChecking(p, whiteKing)) { | |
pieces.push(p); | |
} | |
if (isChecking(p, blackKing)) { | |
pieces.push(p); | |
} | |
}); | |
return pieces && pieces.length > 0 ? pieces : false; | |
}; | |
return getCheckingPieces(); | |
} | |
isCheck(pieces, 0); | |
// Returns true if the arrangement of the | |
// pieces is a check mate, otherwise false | |
function isMate(pieces, player) {} | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var pieces = [ | |
{piece: "king", owner: 1, x: 4, y: 0}, | |
{piece: "king", owner: 0, x: 4, y: 7}, | |
{piece: "pawn", owner: 1, x: 5, y: 6} | |
]; | |
// Returns an array of threats if the arrangement of | |
// the pieces is a check, otherwise false | |
function isCheck(pieces, player) | |
{ | |
let blackKing = getPiece('king', 1); | |
let whiteKing = getPiece('king', 0); | |
function getPiece(name, owner) { | |
return pieces.find(piece => piece.owner == owner && piece.piece == name); | |
} | |
const getMoves = (p) => { | |
const x = p.x; | |
const y = p.y; | |
const getPawnMoves = (p, x, y) => { | |
if(p.owner == 0){ | |
//white | |
return [ | |
{x: x+1, y:y+1}, | |
{x: x-1, y:y+1} | |
] | |
} | |
else{ | |
//black | |
return [ | |
{x: x+1, y:y-1}, | |
{x: x-1, y:y-1} | |
] | |
} | |
} | |
const getRookMoves = (p, x, y) => { | |
let moves = []; | |
//move 8 vert and 8 hor - remove any negatives or any greater than 7 | |
for(let i = 0; i < 8; i++){ | |
for(let j =0; j < 8; j++){ | |
let newX = { | |
positive: x + i > 7 ? undefined : x + i, | |
negative: x - i < 0 ? undefined : x - i | |
} | |
let newY = { | |
positive: y + i > 7 ? undefined : y + i, | |
negative: y - i < 0 ? undefined : y - i | |
} | |
if(newX.positive){ | |
moves.push({x: newX.positive, y: y}) | |
} | |
if(newX.negative){ | |
moves.push({x: newX.negative, y: y}) | |
} | |
if(newY.positive){ | |
moves.push({x: x, y: newY.positive}) | |
} | |
if(newY.negative){ | |
moves.push({x: x, y: newY.negative}) | |
} | |
} | |
} | |
} | |
const getKnightMoves = (p, x, y) => { | |
} | |
const getQueenMoves = (p, x, y) => { | |
} | |
const getBishopMoves = (p, x, y) => { | |
} | |
switch(p.piece){ | |
case 'pawn' : return getPawnMoves(p, x, y); | |
case 'rook' : return getRookMoves(p, x, y); | |
case 'bishop' : return getBishopMoves(p, x, y); | |
case 'queen' : return getQueenMoves(p,x,y); | |
case 'knight' : return getKnightMoves(p, x, y); | |
} | |
} | |
const isChecking = (p, king) =>{ | |
if(getMoves(p).includes({x: king.x, y:king.y})){ | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
const getCheckingPieces = () => { | |
let pieces = []; | |
pieces.forEach(p => { | |
if(isChecking(p, whiteKing)) { | |
pieces.push(p); | |
} | |
if(isChecking(p, blackKing)){ | |
pieces.push(p); | |
} | |
}) | |
return pieces && pieces.length > 0 ? pieces : false; | |
} | |
return getCheckingPieces(); | |
} | |
isCheck(pieces, 0) | |
// Returns true if the arrangement of the | |
// pieces is a check mate, otherwise false | |
function isMate(pieces, player) | |
{ | |
}</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var pieces = [{ piece: "king", owner: 1, x: 4, y: 0 }, { piece: "king", owner: 0, x: 4, y: 7 }, { piece: "pawn", owner: 1, x: 5, y: 6 }]; | |
// Returns an array of threats if the arrangement of | |
// the pieces is a check, otherwise false | |
function isCheck(pieces, player) { | |
var blackKing = getPiece('king', 1); | |
var whiteKing = getPiece('king', 0); | |
function getPiece(name, owner) { | |
return pieces.find(function (piece) { | |
return piece.owner == owner && piece.piece == name; | |
}); | |
} | |
var getMoves = function getMoves(p) { | |
var x = p.x; | |
var y = p.y; | |
var getPawnMoves = function getPawnMoves(p, x, y) { | |
if (p.owner == 0) { | |
//white | |
return [{ x: x + 1, y: y + 1 }, { x: x - 1, y: y + 1 }]; | |
} else { | |
//black | |
return [{ x: x + 1, y: y - 1 }, { x: x - 1, y: y - 1 }]; | |
} | |
}; | |
var getRookMoves = function getRookMoves(p, x, y) { | |
var moves = []; | |
//move 8 vert and 8 hor - remove any negatives or any greater than 7 | |
for (var i = 0; i < 8; i++) { | |
for (var j = 0; j < 8; j++) { | |
var newX = { | |
positive: x + i > 7 ? undefined : x + i, | |
negative: x - i < 0 ? undefined : x - i | |
}; | |
var newY = { | |
positive: y + i > 7 ? undefined : y + i, | |
negative: y - i < 0 ? undefined : y - i | |
}; | |
if (newX.positive) { | |
moves.push({ x: newX.positive, y: y }); | |
} | |
if (newX.negative) { | |
moves.push({ x: newX.negative, y: y }); | |
} | |
if (newY.positive) { | |
moves.push({ x: x, y: newY.positive }); | |
} | |
if (newY.negative) { | |
moves.push({ x: x, y: newY.negative }); | |
} | |
} | |
} | |
}; | |
var getKnightMoves = function getKnightMoves(p, x, y) {}; | |
var getQueenMoves = function getQueenMoves(p, x, y) {}; | |
var getBishopMoves = function getBishopMoves(p, x, y) {}; | |
switch (p.piece) { | |
case 'pawn': | |
return getPawnMoves(p, x, y); | |
case 'rook': | |
return getRookMoves(p, x, y); | |
case 'bishop': | |
return getBishopMoves(p, x, y); | |
case 'queen': | |
return getQueenMoves(p, x, y); | |
case 'knight': | |
return getKnightMoves(p, x, y); | |
} | |
}; | |
var isChecking = function isChecking(p, king) { | |
if (getMoves(p).includes({ x: king.x, y: king.y })) { | |
return true; | |
} else { | |
return false; | |
} | |
}; | |
var getCheckingPieces = function getCheckingPieces() { | |
var pieces = []; | |
pieces.forEach(function (p) { | |
if (isChecking(p, whiteKing)) { | |
pieces.push(p); | |
} | |
if (isChecking(p, blackKing)) { | |
pieces.push(p); | |
} | |
}); | |
return pieces && pieces.length > 0 ? pieces : false; | |
}; | |
return getCheckingPieces(); | |
} | |
isCheck(pieces, 0); | |
// Returns true if the arrangement of the | |
// pieces is a check mate, otherwise false | |
function isMate(pieces, player) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment