目前我只是想显示棋盘,同时让我可以选择在以后更改棋盘设置。我在这里的代码旨在向网页显示棋盘(某种类型),但是,当我在html中调用该函数时,我无法获得显示该棋盘的代码。如果我还在学习JavaScript语言,那么对此的任何帮助都将非常感激。
function displayBoard() {
//Initiating peice values for white
var wking = 10, wkingValue = "♕";
var wqueen = 9, wqueenValue = "♔";
var wrook = 5, wrookValue = "♖";
var wbishop = 3.5, wbishopValue = "♗";
var wknight = 3, wknightValue = "♘";
var wpawn = 1, wpawnValue = "♙";
//Initiating peice values for black
var bking = -wking, bkingValue = "♛";
var bqueen = -wqueen, bqueenValue = "♚";
var brook = -wrook, brookValue = "♜";
var bbishop = -wbishop, bbishopValue = "♝";
var bknight = -wknight, bknightValue = "♞";
var bpawn = -wpawn, bpawnValue = "♟";
//Initialising final string
var chessboardTable = "";
//Initialising board array
var defaultBoardArray = [[brook, bknight, bbishop, bqueen, bking, bbishop, bknight, brook],
[bpawn, bpawn, bpawn, bpawn, bpawn, bpawn, bpawn, bpawn],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[wpawn, wpawn, wpawn, wpawn, wpawn, wpawn, wpawn, wpawn],
[wrook, wknight, wbishop, wqueen, wking, wbishop, wknight, wrook]];
//Initialising search squares
var fLetter = "a";
var fNumber = "1";
var initialfillRank = false;
//Beginning main code functions
var peice = "";
var defValue = "";
var defClass = "";
var rank = "";
var file = "";
//Entering Looping Functions
chessboardTable += "<table id='chess_board' cellpadding='0' cellspacing='0'>";
chessboardTable += "<tr>";
// i = rank; k = file;
for (i = 8; i <= 0; i--) {
for (k = 1; k <= 8; k++) {
//Finding grid coordinates
switch (k) {
case (1):
file === "a";
break;
case (2):
file === "b";
break;
case (3):
file === "c";
break;
case (4):
file === "d";
break;
case (5):
file === "e";
break;
case (6):
file === "f";
break;
case (7):
file === "g";
break;
case (8):
file === "h";
break;
default:
break;
}
switch (i) {
case (8):
rank === "8";
initialfillRank = true;
break;
case (7):
rank === "7";
initialfillRank = true;
break;
case (6):
rank === "6";
break;
case (5):
rank === "5";
break;
case (4):
rank === "4";
break;
case (3):
rank === "3";
break;
case (2):
rank === "2";
initialfillRank = true;
break;
case (1):
rank === "1";
initialfillRank = true;
break;
default:
break;
}
//Finding the class names
if (rank === 8) {
if (file === "a" || file === "h") {
defClass === "brook";
defValue === brookValue;
}
else if (file === "b" || file === "g") {
defClass === "bknight";
defValue === bknightValue;
}
else if (file === "c" || file === "f") {
defClass === "bbishop";
defValue === bbishopValue;
}
else if (file === "d") {
defClass === "bqueen";
defValue === bqueenValue;
}
else if (file === "e") {
defClass === "bking";
defValue === bkingValue;
}
}
else if (rank === 7) {
defClass === "bpawn";
defValue === bpawnValue;
}
else if (rank === 1) {
if (file === "a" || file === "h") {
defClass === "wrook";
defValue === wrookValue;
}
else if (file === "b" || file === "g") {
defClass === "wknight";
defValue === wknightValue;
}
else if (file === "c" || file === "f") {
defClass === "wbishop";
defValue === wbishopValue;
}
else if (file === "d") {
defClass === "wqueen";
defValue === wqueenValue;
}
else if (file === "e") {
defClass === "wking";
defValue === wkingValue;
}
}
else if (rank === 2) {
defClass === "wpawn";
defValue === wpawnValue;
}
//Printing the code
if (initialfillRank === true) {
chessboardTable += "<td id='";
chessboardTable += file;
chessboardTable += rank;
chessboardTable += "'><a href='#' class='";
chessboardTable += defClass;
chessboardTable += "'>";
chessboardTable += defValue;
chessboardTable += "</a></td>";
}
else if (initialfillRank === false) {
chessboardTable += "<td id='";
chessboardTable += file;
chessboardTable += rank;
chessboardTable += "'></td>";
}
if (file === "h" && rank !== 1) {
chessboardTable += "</tr>";
chessboardTable += "<tr>";
}
}
}
chessboardTable += "</table>";
document.write(chessboardTable);
}
答案 0 :(得分:0)
您似乎在任何地方都使用===
,即使您指的是分配值而不是比较它们。浏览代码并将===
更改为您分配值的=
,例如:
if (file === "a" || file === "h") { // comparison, === is ok
defClass = "brook"; // assignment, use single = instead
defValue = brookValue; // --"--
}
等等。
下一个问题是您分配字符串值但严格比较数字,每次都是假的:
rank = "8";
// ..later:
if (rank === 8) {
答案 1 :(得分:0)
我做了working fiddle。第一个循环仍然存在问题。你必须从7到0计数,否则在Y轴上得到9个方格。
for (i = 7; i >= 0; i--) {
//编辑
我设法得到了pieces working。无需使用变量rank
。