{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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
Image of page 1

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

View Full Document Right 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 (pos > -1 && pos < 4) 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 (pos > -1 && pos < 4) 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
Image of page 2
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 } if (posVal.Length > 0)
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern