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

定義 otinitializeState()を次のように変更します。

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 に答える