danielle
danielle

Reputation: 11

want to use getRow inside allRowsValid for sudoku solver how can i solve it?

So i have been trying to write a sudoku solver and i had to complet methods inside an extend class here's the head of the code:

class Board extends EventEmitter {
  constructor(board) {
    super();

    this.board = board || [
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
    ];
  }

  getRow(index) {
    return this.board[index];
  }

here i have to check if all the rows inside that board are valid (from 1 to 9 no repet):

allrowsValid() {
  for (let c = 0; c < 9; ++c) {
    **var row = this.getRow(c)** ***//what i need to fix***

      for ( let num = 1; num <= 9; ++num){
        if (this.board[row][c] === num) {
          return false;
      }
      }
    }
    return true;
    }

How can i solve it ? thanks !

Upvotes: 0

Views: 151

Answers (2)

traktor
traktor

Reputation: 19366

It depends whether "valid" means "full house" (all numbers in range [1-9]), or not invalid (some numbers in range [1-9] without repetition). I've used bit arithmetic to specifically address this in Sudoku in the past:

// class methods...

validHouse( array) {
    // no non zero repetitions
    let allowed = 0b1111111110;
    for( let index = 9; index--;) {
        if( array[index]) {
            let position  = 1 << array[index];
            if( !(allowed & position)) {
                 return false; // repetition
            }
            allowed &= ~position;
        }
    }
    return true;
}

fullHouse(array) {
    // all house numbers completed
    let required = 0b1111111110;
    for( let index = 9; index--;) {
        if( array[index]) {
            let position  = 1 << array[index];
            required &= ~position;
        }
    }
    return required == 0;
}

So if you wanted to know if all rows were complete or valid you could use

allRowsFull() {
    return this.board.every(row => this.fullHouse(row));
}

allRowsValid() {
   return this.board.every(row => this.validHouse(row));
}

The point here is not to push you into using binary arithmetic (it's an acquired taste1) but to point out that a row is only one of the three types of Sudoku houses (row, column and box) that need to be considered.


1 Answer updated to use the bit-wise complement operator (~) instead of the logical not operator (!) in bit clearing operations. They are not interchangeable.

Upvotes: 1

pxDav
pxDav

Reputation: 1834

if (this.board[row][c] === num)

The problem is at this line, you are trying to get the row by using the row. The row variable is already referencing the row.

So the solution is to replace this.board[row][c] with row[c]

for ( let num = 1; num <= 9; ++num){
    if (row[c] === num) {
        return false;
    }
}

Upvotes: 0

Related Questions