1

主要编辑:重新做了很多事情。但是类似的错误。

var white = 1;
var turn = white;
var table = document.getElementById("game");

function createTable(table, white) {
    var i,j,tr;
    for(i=0;i<8;i++) 
    {
        tr = document.createElement('tr');

        for(j=0;j<8;j++)
            tr.appendChild(document.createElement('td')).id = String.fromCharCode( (white == 1 ? j : 8-j) +97)+(white == 1 ? 8-i : i+1);

        table.appendChild(tr);
    }

    for(i=0;i<8;i++)
    {
        for(j=0;j<8;j++)
        {
            table.rows[i].cells[j].setAttribute("class","cell");
        }
    }
}

function onImageLoad(t) {

    console.log(t);
}

function createImageArray() {
    var w,c,p;
    var image = new Array(2);
    for(w=0;w<2;w++)
    {
        image[w] = new Array(2);
        for(c=0;c<2;c++)
        {
            image[w][c] = new Array(6);
            for(p=0;p<6;p++)
            {
                image[w][c][p] = new Array(2);
            }
        }
    }
    return image;
}

function createBlankimageArray() {
    var blank = new Array(2);
    return blank;
}

function bufferImages(image,blank) {

    var w, c, p, s, word;

    for(w=0;w<2;w++)
    {
        for(c=0;c<2;c++)
        {
            for(p=0;p<6;p++)
            {
                for(s=0;s<2;s++)
                {

                    word = w.toString() + c.toString() + (p+1).toString() + s.toString() + ".png";
                    //console.log(word);
                    image[w][c][p][s] = new Image();
                    image[w][c][p][s].onload = onImageLoad(word);
                    image[w][c][p][s].src='final images/'+ word;

                }
            }
        }
    }

    for(i=0;i<2;i++)
    {
        blank[i] = new Image();
        word = i.toString() + '.png';
        blank[i].onload = onImageLoad(word);
        blank[i].src= 'final images/'+ word;
    }
}

function intializeState() {

    var x,y,temp;
    var state = new Array(8);
    for(y=0;y<8;y++) 
    {
        state[y] = new Array(8);
        for(x=0;x<8;x++) 
        {
            state[y][x] = new Array(3);


            // Set Cell White or Black.
            state[y][x][0] = (x+y)%2;


            if(y==1 || y == 6) 
            {
                temp = 0;
                state[y][x][1] = temp;
                state[y][x][2] = ( white==1 ? 0 : 1);
            }
            else if(x==0 || x==7) {temp = 1;}
            else if(x==1 || x==6) {temp = 2;}
            else if(x==2 || x==5) {temp = 3;}
            else if(x==3) {temp = 4;}
            else if(x==4) {temp = 5;}

            if(temp!=0)
            {
                if(y==0)
                {
                    state[y][x][1] = temp;
                    state[y][x][2] = (white == 1 ? 0 : 1);
                }
                else if(y==7)
                {
                    state[y][x][1] = temp;
                    state[y][x][2] = (white == 1 ? 1 : 0);
                }
                else
                {   
                    state[y][x][1] = 7;
                    state[y][x][2] = 7;
                }
            }
        }
    }

    return state;
}

function drawState(table,state,image,blank) {

    var y,x;
    //var table = document.getElementById("game");
    var w,c,p;

    for(y=0;y<8;y++)
    {
        for(x=0;x<8;x++)
        {
            c = state[y][x][0];
            w = state[y][x][1];
            p = state[y][x][2];
            if(p!=7)
            {
                table.rows[y].cells[x].appendChild(image[w][c][p][0]);
            }
            else
            {
                table.rows[y].cells[x].appendChild(blank[c]);
            }
        }
    }
}

var state = intializeState();
var image = createImageArray();
var blank = createBlankimageArray();
createTable(table, white);
bufferImages(image,blank);
intializeState(state);
drawState(table,state,image,blank);

HTML 代码:

<!DOCTYPE html>
<html>
<head>
    <title>Anti Chess</title>
    <link rel="stylesheet" type="text/css" href="screen2.css">
</head>
<body>
    <h1 id="game_title">Anti Chess by theManikJindal</h1>
    <a href="http://thylavatory.wordpress.com" target="_blank">Visit Blog!</a>
    <br />
    <br />
    <table id="game"></table>
    <script src="req_logic.js"></script>
</body>
</html>

在初始位置创建棋盘需要上述脚本。我遇到的问题是在 drawState 函数中。

控制台:(加载后已打印出所有图像名称)之后:

Uncaught TypeError: Cannot read property '1' of undefined req_logic.js:154

这是哪一行:table.rows[y].cells[x].appendChild(image[w][c][p][0]);

所以我哪里出错了。

编辑:jsFiddle.net 链接:http: //jsfiddle.net/8H3Ha/1/

4

1 回答 1

0

将定义更改initializeState()为:

function intializeState() {

    var x,y,temp;
    var state = new Array(8);
    for(y=0;y<8;y++) 
    {
        state[y] = new Array(8);
        for(x=0;x<8;x++) 
        {
            state[y][x] = new Array(3);


            // Set Cell White or Black.
            state[y][x][0] = (x+y)%2;


            if(y==1 || y == 6) 
            {
                temp = 0;
                state[y][x][1] = temp;
                state[y][x][2] = ( white==1 ? 0 : 1);
            }
            else if(x==0 || x==7) {temp = 1;}
            else if(x==1 || x==6) {temp = 2;}
            else if(x==2 || x==5) {temp = 3;}
            else if(x==3) {temp = 4;}
            else if(x==4) {temp = 5;}

            if(temp!=0)
            {
                if(y==0)
                {
                    state[y][x][1] = temp;
                    state[y][x][2] = (white == 1 ? 0 : 1);
                }
                else if(y==7)
                {
                    state[y][x][1] = temp;
                    state[y][x][2] = (white == 1 ? 1 : 0);
                }
                else
                {   
                    state[y][x][1] = 7;
                    state[y][x][2] = 7;
                }
            }
        }
    }
    return state;
}

然后将其称为:

state = initializeState();

问题是名为的参数变量state隐藏了内部同名的全局变量initializeState

我还建议state数组的元素应该是对象,而不是数组。当元素一致时应该使用数组,但是这些子元素中的每一个都有不同的用途:[0] 是单元格颜色,[1] 是块,而 [2] 是块颜色。

于 2013-05-06T19:23:12.720 回答