编码AI算法的建议

时间:2017-08-16 17:11:00

标签: javascript algorithm artificial-intelligence

我要做的是制作一艘战列舰游戏,但我仍然坚持使用AI的船只放置算法。电路板本身是一个二维数组对象。这就是我现在所拥有的:

pickFirstCell() {
    let selectCol = () => {
      let colIndex = Math.floor(Math.random() * this.npcSelectionField.length);
      let selectCell = () => {
        let cellIndex = Math.floor(Math.random() * this.npcSelectionField[colIndex].length);
        if (this.npcSelectionField[colIndex][cellIndex].isEmpty === false) {
          selectCell();
        } else {
          this.npcSelectionField[colIndex][cellIndex].isEmpty = false;
          this.pickDirection(this.npcSelectionField, colIndex, cellIndex);
        }

      }

      selectCell();
    }

    selectCol();
}

上述功能在棋盘上挑选一个随机单元

pickDirection(field, col, cell) {
  let neighbors = [].concat(
    (field[col + 1] || [])[cell] || [],
    (field[col - 1] || [])[cell] || [],
    (field[col] || [])[cell + 1] || [],
    (field[col] || [])[cell - 1] || []
  );

  let randDir = () => {
    let randIndex = neighbors[Math.floor(Math.random() * neighbors.length)];
    if (randIndex.isEmpty === false) {
      randDir();
    } else {
      randIndex.isEmpty = false;
    }
  }

  randDir();

}

后面是另一个函数,它找到所选单元格的相邻单元格并随机选择一个单元格。这适用于放置2个单元尺寸的船舶。

我遇到的问题是进一步让人工智能选择3,4和5格单元的船只,但我坚持想法。我应该更改现有算法还是可以继续使用我已有的算法?

非常感谢任何类型的输入。

0 个答案:

没有答案
相关问题