Check for duplicates in rows, columns and squares in Sudoku
This commit is contained in:
		
							parent
							
								
									01b59ef59d
								
							
						
					
					
						commit
						717209a7f1
					
				| @ -99,6 +99,14 @@ class Nikoli.Cell | |||||||
|   rowDuplicates: -> |   rowDuplicates: -> | ||||||
|     @duplicatesIn @game[@x] |     @duplicatesIn @game[@x] | ||||||
| 
 | 
 | ||||||
|  |   squareDuplicates: (from, size) -> | ||||||
|  |     square = [] | ||||||
|  |     for i in [from.x...(from.x + size)] | ||||||
|  |       for j in [from.y...(from.y + size)] | ||||||
|  |         square.push @game[i][j] | ||||||
|  | 
 | ||||||
|  |     @duplicatesIn square | ||||||
|  | 
 | ||||||
| class Nikoli.Stream | class Nikoli.Stream | ||||||
|   constructor: (@game) -> |   constructor: (@game) -> | ||||||
|     @cells = [] |     @cells = [] | ||||||
|  | |||||||
| @ -6,22 +6,15 @@ class Nikoli.Sudoku extends Nikoli.Game | |||||||
|     solution = @toArray() |     solution = @toArray() | ||||||
|     errors = [] |     errors = [] | ||||||
| 
 | 
 | ||||||
|     for i in [0..8] |     for i in [0...solution.length] | ||||||
|       square = [] |  | ||||||
|       column = [] |  | ||||||
|       row = solution[i] |       row = solution[i] | ||||||
|  |       for j in [0...row.length] | ||||||
|  |         cell = new Nikoli.Cell(i, j, solution) | ||||||
| 
 | 
 | ||||||
|       for j in [0..8] |         if cell.value == 0 | ||||||
|         column.push(solution[j][i]) |           errors.push {row: i, column: j, message: 'The cell has no value.'} | ||||||
|         square.push(solution[(Math.floor(i/3)*3) + Math.floor(j/3)][(i%3*3) + (j%3)]) |         else if cell.rowDuplicates() || cell.columnDuplicates() || cell.squareDuplicates({x: Math.floor(i/3)*3, y: Math.floor(j/3)*3}, 3) | ||||||
| 
 |           errors.push {row: i, column: j, message: 'The number appears more than once in the row, column or square.'} | ||||||
|       console.log square |  | ||||||
|       console.log column |  | ||||||
|       ### |  | ||||||
|       # check square [i%3*3][j/3*3] |  | ||||||
|       # check row [i][.] |  | ||||||
|       # check column [.][i] |  | ||||||
|       ### |  | ||||||
| 
 | 
 | ||||||
|     errors |     errors | ||||||
| 
 | 
 | ||||||
| @ -47,10 +40,14 @@ class Nikoli.Sudoku extends Nikoli.Game | |||||||
|     else |     else | ||||||
|       cell.classList.add 'black' |       cell.classList.add 'black' | ||||||
| 
 | 
 | ||||||
|   toArray: -> |   toArray: (solution = false) -> | ||||||
|     [].map.call @grid.querySelectorAll('.grid-row'), (row) -> |     [].map.call @grid.querySelectorAll('.grid-row'), (row) -> | ||||||
|       [].map.call row.querySelectorAll('.grid-cell'), (cell) -> |       [].map.call row.querySelectorAll('.grid-cell'), (cell) -> | ||||||
|         if cell.classList.contains('empty') |         if cell.classList.contains('empty') | ||||||
|           -parseInt(cell.querySelector('input').value) |           value = parseInt(cell.querySelector('input').value) | ||||||
|  |           if isNaN(value) | ||||||
|  |             0 | ||||||
|  |           else | ||||||
|  |             if solution then -value else value | ||||||
|         else |         else | ||||||
|           parseInt(cell.innerHTML) |           parseInt(cell.innerHTML) | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user