0

目前我只是想显示棋盘,同时让我可以选择在以后更改棋盘设置。我在这里的代码旨在向网页显示棋盘(某种),但是,当我在 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);
}
4

2 回答 2

0

我做了一个工作小提琴。第一个循环仍然存在问题。你必须从 7 数到 0,否则你会在 Y 轴上得到 9 个正方形。

for (i = 7; i >= 0; i--) {

// 编辑

我设法让这些作品发挥作用。无需使用变量rank

于 2013-08-15T08:07:33.510 回答
0

您似乎===无处不在,即使您打算分配值而不是比较它们。浏览代码并更改===分配=值的位置,例如:

if (file === "a" || file === "h") {   // comparison, === is ok
    defClass = "brook";               // assignment, use single = instead 
    defValue = brookValue;            // --"--
}

等等。

下一个问题是您分配字符串值但严格与数字进行比较,这每次都会是错误的:

rank = "8";

// ..later:
if (rank === 8) {
于 2013-08-15T07:50:33.620 回答