Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
599 views
in Technique[技术] by (71.8m points)

javascript - Javascript P5似乎存储了稍后更改的数组值,即使我在更改之前记录了它,也记录了更新版本(Javascript P5 seems to be storing an array value I change later, even though I log it before it's changed, logs updates version)

I'm using P5.js to make a connect 4 game that I later want to use to make some training data for a ML project.

(我使用P5.js制作了一个connect 4游戏,后来我想用它为ML项目制作一些训练数据。)

I'm just currently working on making some logic.

(我目前正在研究一些逻辑。)

In a separate file, (separate just so I can test ideas) I have it so you hit a number 1-7 as a row number, and then it will color in your spot on the board.

(在一个单独的文件中,(请分开使用,以便我测试想法),所以您将数字1-7用作行号,然后它将在板上的位置上显示颜色。)

I'm using the logic system to know how far down the colored block needs to go.

(我正在使用逻辑系统来了解彩色块需要走多远。)

I have some arrays corresponding to the columns, and for testing purposes, I have 4 columns of 3 down.

(我有一些与列对应的数组,出于测试目的,我有4列,每列3个向下。)

A 1 represents somewhere a piece is, and a 0 is an open space.

(1表示一块是某处,0表示开放空间。)

When in a column, I use a for loop to iterate through, and if i is a 1, and i-1 is a 0, change i-1 to be a 1. This effectively simulates the gravity of dropping a piece down.

(在列中时,我使用for循环进行迭代,如果i为1,i-1为0,则将i-1更改为1。这有效地模拟了放下一块的重力。)

The problem is, when I run console.log, both before and after my logic, it gives my the same result, but it's the post logic result.

(问题是,当我在逻辑之前和之后运行console.log时,它给出的结果相同,但这是后逻辑结果。)

I don't know why it won't log the correct pre-logic array.

(我不知道为什么它不会记录正确的逻辑前阵列。)

My code is:

(我的代码是:)

row = [2];
nums = [
  [0,0,0],
  [0,0,1],
  [0,1,1],
  [1,1,1]
]

console.log(nums[row])

//console.log(nums[row].length - 1)
for (i = 0; i<= ((nums[row].length) - 1); i++) {
  //console.log(nums[row])
  // console.log(i)
  if ((nums[row][i]) == 1 && nums[row][i-1] == 0) {
    nums[row][i-1] = 1
  }

  /*console.log(nums[row][i])*/
}

console.log(nums[row])

Before I run the logic, it shoud log [0,1,1] and after it should be [1,1,1].

(在运行逻辑之前,它应该记录日志[0,1,1],之后应为[1,1,1]。)

Instead, any time I run it on a row that gets changed, it logs the output twice.

(相反,每当我在更改的行上运行它时,它都会记录两次输出。)

I don't know why it isn't logging the array before it gets changed first.

(我不知道为什么它不先更改数组就记录它。)

Any help would be great!

(任何帮助将是巨大的!)

  ask by joelanbanks3 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...