var turn = "black"; // 先手は黒
var pieces = []; // 駒情報を格納する配列
// 盤面を初期化する関数
function init() {
var table = document.getElementById("board");
for (var i = 0; i < 8; i++) {
var row = table.insertRow(-1);
for (var j = 0; j < 8; j++) {
var cell = row.insertCell(-1);
cell.setAttribute("id", i + "_" + j);
cell.setAttribute("onclick", "move(this)");
if ((i + j) % 2 == 0) {
cell.className = "white";
} else {
cell.className = "black";
}
if (i == 0 || i == 7) {
if (j == 0 || j == 7) {
cell.innerHTML = "♖"; // ルーク
pieces.push({color: "black", type: "rook", location2: i + "_" + j});
} else if (j == 1 || j == 6) {
cell.innerHTML = "♘"; // ナイト
pieces.push({color: "black", type: "knight", location2: i + "_" + j});
} else if (j == 2 || j == 5) {
cell.innerHTML = "♗"; // ビショップ
pieces.push({color: "black", type: "bishop", location2: i + "_" + j});
} else if (j == 3) {
cell.innerHTML = "♕"; // クイーン
pieces.push({color: "black", type: "queen", location2: i + "_" + j});
} else {
cell.innerHTML = "♔"; // キング
pieces.push({color: "black", type: "king", location2: i + "_" + j});
}
} else if (i == 1) {
cell.innerHTML = "♙"; // ポーン
pieces.push({color: "black", type: "pawn", location2: i + "_" + j});
} else if (i == 6) {
cell.innerHTML = "♟"; // ポーン
pieces.push({color: "white", type: "pawn", location2: i + "_" + j});
}
}
}
}
// 駒を移動する関数
function move(cell) {
var location2 = cell.getAttribute("id");
var selected = pieces.filter(function(piece){return piece.location2 == location2})[0]; // 選択された駒を取得する
if (selected && selected.color == turn) { // 駒が選択された時
// 選択された駒をハイライトする
reset();
cell.style.backgroundColor = "orange";
cell.style.color = "white";
// 選択された駒の移動可能なマスをハイライトする
var moves = [];
switch (selected.type) {
case "pawn":
if (selected.color == "black") {
if (!pieces.some(function(piece){return piece.location2 == (parseInt(location2.charAt(0)) + 1) + "_" + location2.charAt(2)})) { // 直前に移動していない場合
moves.push([(parseInt(location2.charAt(0)) + 1) + "_" + location2.charAt(2), "normal"]);
if (location2.charAt(0) == "1" && !pieces.some(function(piece){return piece.location2 == (parseInt(location2.charAt(0)) + 2) + "_" + location2.charAt(2)})) { // 二歩できる場合
moves.push([(parseInt(location2.charAt(0)) + 2) + "_" + location2.charAt(2), "two"]);
}
}
if (location2.charAt(2) != "0" && pieces.some(function(piece){return piece.location2 == (parseInt(location2.charAt(0)) + 1) + "_" + (parseInt(location2.charAt(2)) - 1)}) && pieces.filter(function(piece){return piece.location2 == (parseInt(location2.charAt(0)) + 1) + "_" + (parseInt(location2.charAt(2)) - 1)})[0].color == "white") { // 左斜め前に白の駒がある場合
moves.push([(parseInt(location2.charAt(0)) + 1) + "_" + (parseInt(location2.charAt(2)) - 1), "capture"]);
}
if (location2.charAt(2) != "7" && pieces.some(function(piece){return piece.location2 == (parseInt(location2.charAt(0)) + 1) + "_" + (parseInt(location2.charAt(2)) + 1)}) && pieces.filter(function(piece){return piece.location2 == (parseInt(location2.charAt(0)) + 1) + "_" + (parseInt(location2.charAt(2)) + 1)})[0].color == "white") { // 右斜め前に白の駒がある場合
moves.push([(parseInt(location2.charAt(0)) + 1) + "_" + (parseInt(location2.charAt(2)) + 1), "capture"]);
}
} else {
if (!pieces.some(function(piece){return piece.location2 == (parseInt(location2.charAt(0)) - 1) + "_" + location2.charAt(2)})) { // 直前に移動していない場合
moves.push([(parseInt(location2.charAt(0)) - 1) + "_" + location2.charAt(2), "normal"]);
if (location2.charAt(0) == "6" && !pieces.some(function(piece){return piece.location2 == (parseInt(location2.charAt(0)) - 2) + "_" + location2.charAt(2)})) { // 二歩できる場合
moves.push([(parseInt(location2.charAt(0)) - 2) + "_" + location2.charAt(2), "two"]);
}
}
if (location2.charAt(2) != "0" && pieces.some(function(piece){return piece.location2 == (parseInt(location2.charAt(0)) - 1) + "_" + (parseInt(location2.charAt(2)) - 1)}) && pieces.filter(function(piece){return piece.location2 == (parseInt(location2.charAt(0)) - 1) + "_" + (parseInt(location2.charAt(2)) - 1)})[0].color == "black") { // 左斜め前に黒の駒がある場合
moves.push([(parseInt(location2.charAt(0)) - 1) + "_" + (parseInt(location2.charAt(2)) - 1), "capture"]);
}
if (location2.charAt(2) != "7" && pieces.some(function(piece){return piece.location2 == (parseInt(location2.charAt(0)) - 1) + "_" + (parseInt(location2.charAt(2)) + 1)}) && pieces.filter(function(piece){return piece.location2 == (parseInt(location2.charAt(0)) - 1) + "_" + (parseInt(location2.charAt(2)) + 1)})[0].color == "black") { // 右斜め前に黒の駒がある場合
moves.push([(parseInt(location2.charAt(0)) - 1) + "_" + (parseInt(location2.charAt(2)) + 1), "capture"]);
}
}
break;
case "knight":
if (location2.charAt(0) != "0" && location2.charAt(2) != "0" && (location2.charAt(0) != "1" || !pieces.some(function(piece){return piece.location2 == (parseInt(location2.charAt(0)) - 1) + "_" + (parseInt(location2.charAt(2)) - 2)}))) {
var target = (parseInt(location2.charAt(0)) - 1) + "_" + (parseInt(location2.charAt(2)) - 2);
if (!pieces.some(function(piece){return piece.location2 == target}) || pieces.filter(function(piece){return piece.location2 == target})[0].color != selected.color) {
moves.push([target, (pieces.some(function(piece){return piece.location2 == target}) ? "capture" : "normal")]);
}
}
if (location2.charAt(0) != "0" && location2.charAt(2) != "7" && (location2.charAt(0) != "1" || !pieces.some(function(piece){return piece.location2 == (parseInt(location2.charAt(0)) - 2) + "_" + (parseInt(location2.charAt(2)) + 1)}))) {
var target = (parseInt(location2.charAt(0)) - 2) + "_" + (parseInt(location2.charAt(2)) + 1);
if (!pieces.some(function(piece){return piece.location2 == target}) || pieces.filter(function(piece){return piece.location2 == target})[0].color != selected.color) {
moves.push([target, (pieces.some(function(piece){return piece.location2 == target}) ? "capture" : "normal")]);
}
}
if (location2.charAt(0) != "7" && location2.charAt(2) != "0" && (location2.charAt(0) != "6" || !pieces.some(function(piece){return piece.location2 == (parseInt(location2.charAt(0)) + 2) + "_" + (parseInt(location2.charAt(2)) - 2)}))) {
var target = (parseInt(location2.charAt(0)) + 2) + "_" + (parseInt(location2.charAt(2)) - 2);
if (!pieces.some(function(piece){return piece.location2 == target}) || pieces.filter(function(piece){return piece.location2 == target})[0].color != selected.color) {
moves.push([target, (pieces.some(function(piece){return piece.location2 == target}) ? "capture" : "normal")]);
}
}
if (location2.charAt(0) != "7" && location2.charAt(2) != "7" && (location2.charAt(0) != "6" || !pieces.some(function(piece){return piece.location2 == (parseInt(location2.charAt(0)) + 1) + "_" + (parseInt(location2.charAt(2)) + 2)}))) {
var target = (parseInt(location2.charAt(0)) + 1) + "_" + (parseInt(location2.charAt(2)) + 2);
if (!pieces.some(function(piece){return piece.location2 == target}) || pieces.filter(function(piece){return piece.location2 == target})[0].color != selected.color) {
moves.push([target, (pieces.some(function(piece){return piece.location2 == target}) ? "capture" : "normal")]);
}
}
break;
case "bishop":
for (var i = 1; i < 8; i++) {
if (location2.charAt(0) - i >= 0 && location2.charAt(2) - i >= 0) { // 左上
var target = (location2.charAt(0) - i) + "_" + (location2.charAt(2) - i);
if (!pieces.some(function(piece){return piece.location2 == target}) || pieces.filter(function(piece){return piece.location2 == target})[0].color != selected.color) {
moves.push([target, (pieces.some(function(piece){return piece.location2 == target}) ? "capture" : "normal")]);
}
if (pieces.some(function(piece){return piece.location2 == target})) {
break;
}
}
}
for (var i = 1; i < 8; i++) {
if (location2.charAt(0) - i >= 0 && location2.charAt(2) - (-i) <= 7) { // 右上
var target = (location2.charAt(0) - i) + "_" + (location2.charAt(2) - (-i));
if (!pieces.some(function(piece){return piece.location2 == target}) || pieces.filter(function(piece){return piece.location2 == target})[0].color != selected.color) {
moves.push([target, (pieces.some(function(piece){return piece.location2 == target}) ? "capture" : "normal")]);
}
if (pieces.some(function(piece){return piece.location2 == target})) {
break;
}
}
}
for (var i = 1; i < 8; i++) {
if (location2.charAt(0) - (-i) <= 7 && location2.charAt(2) - i >= 0) { // 左下
var target = (location2.charAt(0) - (-i)) + "_" + (location2.charAt(2) - i);
if (!pieces.some(function(piece){return piece.location2 == target}) || pieces.filter(function(piece){return piece.location2 == target})[0].color != selected.color) {
moves.push([target, (pieces.some(function(piece){return piece.location2 == target}) ? "capture" : "normal")]);
}
if (pieces.some(function(piece){return piece.location2 == target})) {
break;
}
}
}
for (var i = 1; i < 8; i++) {
if (location2.charAt(0) - (-i) <= 7 && location2.charAt(2) - (-i) <= 7) { // 右下
var target = (location2.charAt(0) - (-i)) + "_" + (location2.charAt(2) - (-i));
if (!pieces.some(function(piece){return piece.location2 == target}) || pieces.filter(function(piece){return piece.location2 == target})[0].color != selected.color) {
moves.push([target, (pieces.some(function(piece){return piece.location2 == target}) ? "capture" : "normal")]);
}
if (pieces.some(function(piece){return piece.location2 == target})) {
break;
}
}
}
break;
case "rook":
for (var i = 1; i < 8; i++) {
if (location2.charAt(0) - i >= 0) { // 上
var target = (location2.charAt(0) - i) + "_" + location2.charAt(2);
if (!pieces.some(function(piece){return piece.location2 == target}) || pieces.filter(function(piece){return piece.location2 == target})[0].color != selected.color) {
moves.push([target, (pieces.some(function(piece){return piece.location2 == target}) ? "capture" : "normal")]);
}
if (pieces.some(function(piece){return piece.location2 == target})) {
break;
}
}
}
for (var i = 1; i < 8; i++) {
if (location2.charAt(0) - (-i) <= 7) { // 下
var target = (location2.charAt(0) - (-i)) + "_" + location2.charAt(2);
if (!pieces.some(function(piece){return piece.location2 == target}) || pieces.filter(function(piece){return piece.location2 == target})[0].color != selected.color) {
moves.push([target, (pieces.some(function(piece){return piece.location2 == target}) ? "capture" : "normal")]);
}
if (pieces.some(function(piece){return piece.location2 == target})) {
break;
}
}
}
for (var i = 1; i < 8; i++) {
if (location2.charAt(2) - i >= 0) { // 左
var target = location2.charAt(0) + "_" + (location2.charAt(2) - i);
if (!pieces.some(function(piece){return piece.location2 == target}) || pieces.filter(function(piece){return piece.location2 == target})[0].color != selected.color) {
moves.push([target, (pieces.some(function(piece){return piece.location2 == target}) ? "capture" : "normal")]);
}
if (pieces.some(function(piece){return piece.location2 == target})) {
break;
}
}
}
for (var i = 1; i < 8; i++) {
if (location2.charAt(2) - (-i) <= 7) { // 右
var target = location2.charAt(0) + "_" + (location2.charAt(2) - (-i));
if (!pieces.some(function(piece){return piece.location2 == target}) || pieces.filter(function(piece){return piece.location2 == target})[0].color != selected.color) {
moves.push([target, (pieces.some(function(piece){return piece.location2 == target}) ? "capture" : "normal")]);
}
if (pieces.some(function(piece){return piece.location2 == target})) {
break;
}
}
}
break;
case "queen":
for (var i = 1; i < 8; i++) { // ビショップの動き
if (location2.charAt(0) - i >= 0 && location2.charAt(2) - i >= 0) { // 左上
var target = (location2.charAt(0) - i) + "_" + (location2.charAt(2) - i);
if (!pieces.some(function(piece){return piece.location2 == target}) || pieces.filter(function(piece){return piece.location2 == target})[0].color != selected.color) {
moves.push([target, (pieces.some(function(piece){return piece.location2 == target}) ? "capture" : "normal")]);
}
if (pieces.some(function(piece){return piece.location2 == target})) {
break
*This prompt didn't get me into the game this time. Plz try to generate it a few times.