Assign1 - Code Sudoku.cs using using using using using...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Code Sudoku.cs using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; namespace SudokuSolver { class Sudoku { public Sudoku() { for ( int i = 0; i < 4; i++) //initially every element has a 0 value for ( int j = 0; j < 4; j++) { matrix[i, j] = 0; } } //The isValueValid method checks whether a cell can be set to a certain value. //It takes as arguments the row and column of the sudoku-matrix and the integer //value to be checked. public bool isValueValid( int row, int col, int value) { int startCol, startRow; for ( int i = 0; i < 4; i++) if (matrix[i, col] == value) return false ; //check for same value in column for ( int i = 0; i < 4; i++) if (matrix[row, i] == value) return false ; //check for same value in row startCol = col - col % 2; startRow = row - row % 2; for ( int i = 0; i < 2; i++) for ( int j = 0; j < 2; j++) if (matrix[startRow + i, startCol + j] == value) return false ; //check inside the minigrid return true ; } //The possibleValues method returns the possible values for a cell. //It checks the value of the other cells in the same row, column and minigrid. public string possibleValues( int row, int col) { 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
string posVal = "1234" ; //used to store the possible values for a cell int startCol, startRow; string buffer; int pos; for ( int i = 0; i < 4; i++) //check within a column for unassigned values if (matrix[i, col] != 0) { buffer = matrix[i, col].ToString(); //convert number to string pos = posVal.IndexOf(buffer); if (pos > -1 && pos < 4) //if a value from 1 to 4 is already assigned posVal = posVal.Remove(pos, 1); //remove it from the possible values of a cell } for ( int i = 0; i < 4; i++) //check within a row for unassigned values if (matrix[row, i] != 0) { buffer = matrix[row, i].ToString(); pos = posVal.IndexOf(buffer); if posVal = posVal.Remove(pos, 1); } startCol = col - col % 2; startRow = row - row % 2; for ( int i = 0; i < 2; i++) //check within a minigrid for unassigned values for ( int j = 0; j < 2; j++) if (matrix[startRow + i, startCol + j] != 0) { buffer = matrix[startRow + i, startCol + j].ToString(); pos = posVal.IndexOf(buffer); if posVal = posVal.Remove(pos, 1); } return posVal; } //The method isSolved checks whether the values entered for each cell are valid public bool isSolved() { string posVal; string buffer; int pos; for ( int row = 0; row < 4; row++) { //check by row posVal = "1234" ; for ( int col = 0; col < 4; col++) { buffer = matrix[row, col].ToString(); pos = posVal.IndexOf(buffer); 2
Background image of page 2
if (pos > -1 && pos < 4) //if a value from 1 to 4 is already assigned posVal = posVal.Remove(pos, 1);
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/24/2010 for the course COS 240 taught by Professor Boycheva during the Spring '10 term at American University in Bulgaria.

Page1 / 11

Assign1 - Code Sudoku.cs using using using using using...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online