我在HTML设计一个国际象棋游戏,我想改变一个字符从一个位置到另一个

Im designing a chess game in HTML,i want to change one charecter from on position to another

本文关键字:一个 字符 改变 另一个 位置 国际象棋 HTML 我在 游戏      更新时间:2023-09-26

我用按钮设计了象棋棋盘。最初,按钮上的所有值都为空,在加载页面时,所有的片段都出现在它们上面,代码片段如下

<input type="button" id="A8" value="" style="background:#FFE4C4;font-size: 70px;height:90;width:100" onclick="check(this.id)">

和onLoad函数中,棋子的ASCII字符分配如下:

document.getElementById('A1').value=String.fromCharCode(9814);

现在我想要的是改变一块从一个按钮到另一个点击两个按钮。我用下面的脚本

做了很多尝试
function check(clicked_id) {
    var Button_2 = "";
    if (i < 2) {
        i++;
        // alert("i:"+i);        
        if (i == 1) {
            Button_1 = clicked_id;
            B1_val = document.getElementById(Button_1).value;
            alert("B1 Button val:" + B1_val);
        }
        if (i == 2) {
            var Button_2 = clicked_id;
            B2_val = document.getElementById(Button_2).value;
            alert("b1 val:" + B1_val);
            alert("B2 val:" + B2_val);
            B2_val = B1_val;
            B1_val = "";
            alert("B1 val:" + B1_val + "B2 val:" + B2_val);
        }
    } else {
        alert("Only 2 butons should press..i:" + i);
        i = 0;
    }
    // alert("clcked a button:"+clicked_id);
}

但是代码不工作

如果您只想将值从第一次点击的位置移动到第二次点击的位置,那么您可以简单地这样做:

var lastClick;
function check(id) {
    var src, dest;
    if (!lastClick) {
        // no previous click so just store the location of this first click
        lastClick = id;
    } else {
        // move value from lastClick id to new id
        src = document.getElementById(lastClick);
        dest = document.getElementById(id);
        dest.value = src.value;
        src.value = "";
        lastClick = null;
    }
}

我认为一个真正的应用程序需要各种各样的错误处理,不允许你把一个块放在另一个块的上面,忽略空白的第一次点击,只强制合法的移动,等等…