【问题标题】:Queen movement in chess game(Javascript)国际象棋游戏中的皇后移动(Javascript)
【发布时间】:2013-06-30 12:42:11
【问题描述】:

我正在学习 Javascript,并作为一个项目任务在其中制作国际象棋游戏。我已经编写了 Rook、Pawn、Knight 和 Bishop 移动的逻辑。现在我被困在女王运动上。皇后的走法基本上涉及主教和车的走法逻辑。

我想要做的是当女王移动时检查源图块的file 是否与目标图块相同。如果相同,则调用 Rook 的代码移动逻辑,否则调用 Bishop 的代码移动逻辑。例如,如果皇后被放置在 d4(源图块)并且被移动到 d8 或 g4(目标图块)。那么在这种情况下,应该调用 Rook 的 move 函数。

所有的片对象都有一个 move() 。所以在这种情况下,我想从 Queen's move() 中调用 Rook 的 move()。我被困在这里。请建议。相关代码贴在下面。我同样制作了 Rook 和其他物品。现在,我想从 Queen() 的这个 move() 调用 Rook/Bishop 的 move()。

        chess.QueenFactory =
            {
                instance: function(color, type)
                {
                    var Queen =
                            {
                                move: function(color, type)
                                {
                                    alert("In Queen");
                                }
                            };
                    createPiece.call(Queen, color, type);
                    return Queen;
                }
            };

我的Bishop移动功能是这样放置的

chess.BishopFactory = 
{
    instance: function(color, type) 
    {
        var Bishop =
        {
            move: function(source, destn) 
            { //Code here
            }
        }
    }
}

我想从 Queen 的 move() 中调用这个函数。我该怎么做?

请在下面的 html 链接中找到完整的代码。

https://github.com/varunpaprunia/ChessInJavascript/blob/master/ChessBoard_Tags_1.html

【问题讨论】:

  • 将相关代码放入您的问题中。
  • x = 'abcdefgh'.indexOf(destination_tile[0]), y = parseInt(destination_tile[1]) - 1;a, b 分别与source_tile 相同。皇后可以这样移动:a + b === x + y(左下、右上)、a - x === b - y(左上、右下)、a === x(从上到下)或b === y(从左到右)。

标签: javascript function-pointers prototypal-inheritance chess


【解决方案1】:

做以下测试来决定使用哪种方法

// source tile
var a = 'abcdefgh'.indexOf(source_tile[0]), // 0 to 7
    b = parseInt(source_tile[1]) - 1;       // 0 to 7
// destination tile
var x = 'abcdefgh'.indexOf(dest_tile[0]),   // 0 to 7
    y = parseInt(dest_tile[1]) - 1;         // 0 to 7
// test to see how it's moving
if (a + b === x + y || a - x === b - y) {   // bLeft to tRight || tLeft to bRight
    // queen is moving like a bishop
} else if ( a === x || b === y) {           // top to bottom || left to right
    // queen is moving like a rook
} else {                                    // impossible move
    // invalid move
}

您可以从 cmets 中看到在哪里调用哪个后续。如果a === x && b === y 则为source_tile === dest_tile,这不算移动棋子。这些不会检查路径是否被阻塞,你需要更多的逻辑。

【讨论】:

    【解决方案2】:

    我认为这样可以:

    function bishopFn(source, destn){ /* bishop move */}
    function rookFn(source, destn){ /* rook move */ }
    

    然后将它们分配给主教和车移动对象,在皇后的移动对象中,您只需根据您的条件调用任何对象

    move: function(source, destn){
        /* condition construction can be done here */
        if (/*condition*/) bishopFn(source, destn);
        else rookFn(source, destn);
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多