E
E
evg_962018-02-20 12:39:00
Game development
evg_96, 2018-02-20 12:39:00

How to check if cells match (react)?

class Grid extends Component {
  constructor() {
    super();

    this.state = {
      grid: [
        [8, 9, 0, 0, 0, 0, 0, 0, 4],
        [0, 3, 0, 0, 9, 1, 0, 0, 7],
        [5, 0, 0, 2, 3, 4, 0, 9, 0],
        [9, 0, 0, 0, 0, 0, 3, 0, 0],
        [0, 4, 5, 0, 1, 0, 0, 7, 0],
        [6, 0, 0, 0, 4, 0, 0, 1, 0],
        [0, 8, 9, 0, 5, 0, 0, 2, 0],
        [0, 2, 0, 0, 8, 0, 0, 0, 0],
        [4, 0, 6, 0, 0, 0, 7, 0, 0]
      ]
    };
  }

  render() {
    return (
      <table className="grid">
        <tbody>

          {
            this.state.grid.map((row, rowIndex) => (
              <tr key = { rowIndex }>
                {
                  row.map((item, itemIndex) => (
                    <td key = { itemIndex } className="grid-item">{ item !== 0 ? item : <input className="grid-input" type="text"/> }</td>
                  ))
                }
              </tr>
            ))
          }

        </tbody>
      </table>
    );
  }
}

5a8beb8367183648646032.png
Something I'm completely confused already in the implementation of sudoku on react. Please tell me how best to implement a check of the conditions that the entered value in the cell will not be repeated in the row in the column and in the 3x3 block?
How to check rows and columns seems to be clear in words, you just need to take the coordinates of the entered number (it's not clear how), and loop through the column and line that intersect this coordinate.
But how to check a 3x3 block with such a state model is not clear.
I tried to google, but I can’t figure out the sheet of code on my own.
In general, please tell me and explain on your fingers how to implement a condition check in this puzzle

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2018-02-20
@evg_96

I tried to google, but I can’t figure out the sheet of code on my own.

You googled something strange - there are ten lines for everything about everything, no "sheet".
Calculate the start coordinates of the current block, then iterate over it (two loops, one nested inside the other, from start coordinates to start + 3). Like so .

I
imhuman, 2018-02-20
@imhuman

make a new array according to the number of small squares, go through the matrix in a cycle and spread the numbers over the new array, having previously determined by the indices which number will go to. And then check in the same way as you check the strings

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question