I don't know how to check if it sticks out of the array.

Asked 1 years ago, Updated 1 years ago, 25 views

I can't image the part of the code that passes through the top, bottom, left and right loops with the code below
How to use the remainder of cellIndex%SIDE_CELLS==SIDE_CELLS-1 especially on the left and right sides
I can't imagine , what does SIDE_CELL-1 mean, please tell me m(__)m

 function update(cellInfos, tempCellInfos){
  // If you cut it from the beginning to the bottom with slice, it turns out to be a copy.
  tempCellInfos=cellInfos.slice();
  for (var index=0; index<tempCellInfos.length;index++) {
    // I will check the life and death of each cell, so I will prepare a counter.
    varliveCellCount=0;
    // This code represents the previous row, the next row, the previous column, and the next column in two loops.
    for (varrowPointer=-1; rowPointer<2;rowPointer++) {
      for (var colPointer=-1; colPointer<2;colPointer++) {
        // If it's me, I won't count, so I'll skip it.
        if(rowPointer==0&colPointer==0){
          // Continue can pass this iteration
          continue;
        }
        // Calculate the cell number to check
        varcellIndex=index+rowPointer*SIDE_CELLS+colPointer;
        if(cellIndex<0||cellIndex>=tempCellInfos.length){
          // Pass the loop if it sticks out of the upper and lower areas
          continue;
        }
        if(index<cellIndex&cellIndex%SIDE_CELLS==0||index>cellIndex&cellIndex%SIDE_CELLS==SIDE_CELLS-1){
          // Pass through loop if left and right are not next to you
          continue;
}

javascript

2022-09-30 18:59

1 Answers

*
※ <
※ <
※ <
※ <
※ <
*
There are 5x5 cells like the one above and
Cells are internally held in a one-dimensional array.
When examining adjacent cells,
*
※ <
※ ◎ <
※ <
※ <
※ <
*
When ◎ is the cell currently investigating (index,
の When you try to check the adjacent cell (cellIndex) in the location of
The field is overhanging and needs to be checked out.
By the way, cells are not a two-dimensional array, but a one-dimensional array, so
*
※ <
※ ◎△
※ <
※ <
※ <
*
is
*
※ <
〇 ◎ <
※△ <
※ <
※ <
*
is in the
position. (The index of ◎ is 9, the right-hand side of the figure above △ is +1 and the position of index 10 is the position of index 10 and the position of index 10 is the position of △ below)
So,
cellIndex%SIDE_CELLS==0
is
If the remainder of the cell position divided by the width of the cell in one dimension is 0, cellIndex is in the leftmost column,
index<cellIndex
corresponds to △ and に for cells of ◎.
In other words, are the adjacent cells on the right (and lower right) to be examined protruding?
(Regarding に, are you probably investigating with the following code or ignoring the oblique position in the first place?)Maybe it's a bug)

index>cellIndex&cellIndex%SIDE_CELLS==SIDE_CELLS-1
Left over
in the section. In other words,
*
※ <
※ <
△◎ <
※ <
※ <
*
I was looking at and found that it was below the one-dimensional array
*
※ ※ <
※ △ <
※◎ ※ <
※ <
※ <
*
index>cellIndex—Current location is larger than the cell location you are trying to find in a one-dimensional array index
&:and
cellIndex%SIDE_CELLS==SIDE_CELLS-1—The rightmost cell.
So
△ and が are affected.

By the way,
The reason why the column position is determined by the remainder is
You are doing the inverse of the previous question.
Two-dimensional array subscripts and one-dimensional array subscripts
Second order array [line subscripts][Column subscripts] are the first array [Row subscripts x column subscripts + column subscripts]
It had to do with
In other words, the remainder of the one-dimensional array divided by the number of columns is the subscript of the column.
(Row subscripts × column subscripts + column subscripts) 列 column subscripts = column subscripts…remaining column subscripts

SIDE_CELL-1 is the number of columns -1 and is the subscript of the last column when the array starts at 0 (that is, for five columns, the subscripts in the column are 0, 1, 2, 3, 4 and 4).
This means a subscript in the rightmost column on the field.


2022-09-30 18:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.