程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了将 type: object 的数组元素设置为 null 是否将对象值设置为 null 或仅设置数组索引?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决将 type: object 的数组元素设置为 null 是否将对象值设置为 null 或仅设置数组索引??

开发过程中遇到将 type: object 的数组元素设置为 null 是否将对象值设置为 null 或仅设置数组索引?的问题如何解决?下面主要结合日常开发的经验,给出你关于将 type: object 的数组元素设置为 null 是否将对象值设置为 null 或仅设置数组索引?的解决方法建议,希望对你解决将 type: object 的数组元素设置为 null 是否将对象值设置为 null 或仅设置数组索引?有所启发或帮助;

我正在开发一个国际象棋引擎,我对在复杂用例中赋值的工作方式有点模糊。 (我意识到这对你们中的某些人来说可能并不复杂,但对我来说却很复杂)我有一个 start 函数来初始化我的pIEces 对象和board 数组。

start() {
        this.pIEces = {};
        // first letter is rank,second letter is the pIEce,the second letter's cAPItalization is the color
        this.pIEces.aP = new Pawn([6,0],'w');
        this.pIEces.bP = new Pawn([6,1],'w');
        this.pIEces.cP = new Pawn([6,2],'w');
        this.pIEces.dP = new Pawn([6,3],'w');
        this.pIEces.eP = new Pawn([6,4],'w');
        this.pIEces.fP = new Pawn([6,5],'w');
        this.pIEces.gP = new Pawn([6,6],'w');
        this.pIEces.hP = new Pawn([6,7],'w');
        this.pIEces.ap = new Pawn([1,'b');
        this.pIEces.bp = new Pawn([1,'b');
        this.pIEces.cp = new Pawn([1,'b');
        this.pIEces.dp = new Pawn([1,'b');
        this.pIEces.ep = new Pawn([1,'b');
        this.pIEces.fp = new Pawn([1,'b');
        this.pIEces.gp = new Pawn([1,'b');
        this.pIEces.hp = new Pawn([1,'b');
        this.pIEces.aR = new Rook([7,'w');
        this.pIEces.bN = new Knight([7,'w');
        this.pIEces.cB = new Bishop([7,'w');
        this.pIEces.dQ = new Queen([7,'w');
        this.pIEces.eK = new King([7,'w');
        this.pIEces.fB = new Bishop([7,'w');
        this.pIEces.gN = new Knight([7,'w');
        this.pIEces.hR = new Rook([7,'w');
        this.pIEces.ar = new Rook([0,'b');
        this.pIEces.bn = new Knight([0,'b');
        this.pIEces.cb = new Bishop([0,'b');
        this.pIEces.dq = new Queen([0,'b');
        this.pIEces.ek = new King([0,'b');
        this.pIEces.fb = new Bishop([0,'b');
        this.pIEces.gn = new Knight([0,'b');
        this.pIEces.hr = new Rook([0,'b');

        this.board = [
            [this.pIEces.ar,this.pIEces.bn,this.pIEces.cb,this.pIEces.dq,this.pIEces.ek,this.pIEces.fb,this.pIEces.gn,this.pIEces.hr],[this.pIEces.ap,this.pIEces.bp,this.pIEces.cp,this.pIEces.dp,this.pIEces.ep,this.pIEces.fp,this.pIEces.gp,this.pIEces.hp],[null,null,null],[this.pIEces.aP,this.pIEces.bP,this.pIEces.cP,this.pIEces.dP,this.pIEces.eP,this.pIEces.fP,this.pIEces.gP,this.pIEces.hP],[this.pIEces.aR,this.pIEces.bN,this.pIEces.cB,this.pIEces.dQ,this.pIEces.eK,this.pIEces.fB,this.pIEces.gN,this.pIEces.hR]
        ];
    }

使用以下构造函数创建片段

class PIEce {
    constructor(position,color) {
        this.position = position
        this.color = color
    }
}

移动具有以下格式。 Element.position 指的是来自片段构造函数的相同位置。 [row,square] 被拆分,用于调用 this.board 矩阵中的数组元素。 PIEce 是棋子的名称(ap 是黑色棋子)。 capture 是移动是否为捕获,multiplIEdVector 是其他函数中使用的向量。

{
  source: element.position,target: [row,square],pIEce: pIEce,move: multiplIEdVector,capture: true
}

然后我有一个名为 immagineMove 的函数。调用时,移动参数具有上述结构。目的是它所做的只是更新位置,这样当我对计算机播放器实现逻辑时,我可以使用最少的计算机资源来期待。它所做的只是将棋盘矩阵中的目标方块设置为源,并将源设置为空。

imagineMove(movE) {
        this.savedposition = this.board;
        
        this.board[move.target[0]][move.target[1]] = this.board[move.source[0]][move.source[1]];
        this.board[move.source[0]][move.source[1]] = null

        this.switchTurns()
    }

当我将源设置为 null 时,实际对象 (this.pIEces) 是否会发生任何变化,或者它是否只是更改了棋盘矩阵?如果是后者,有没有办法通过矩阵访问对象?移动这并不重要,因为一切都是最终的,但是通过想象它们,当我要将棋盘恢复到 this.savedposition 时,我不想弄乱一块的数据。

解决方法

将棋盘格设置为空只会替换棋子,只会影响棋盘。 然而这部分是关于

this.savedPosition = this.board;

你需要确保棋盘是复制的,这样你仍然有一个保存的位置

this.savedPosition = [...this.board];

它被称为扩展运算符,它会复制您的电路板。

此外,我强烈建议让棋盘只保存棋子名称而不是实际棋子。您可以随时从 this.pieces 访问它们

大佬总结

以上是大佬教程为你收集整理的将 type: object 的数组元素设置为 null 是否将对象值设置为 null 或仅设置数组索引?全部内容,希望文章能够帮你解决将 type: object 的数组元素设置为 null 是否将对象值设置为 null 或仅设置数组索引?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。