IN C#:
C# ONLY
Here is a program that I need to that involves doing a sudoko puzzle. I have included the instructions below.
INSTRUCTIONS:
A few years ago, a style of puzzle became pretty popular. The name most people know it by is Sudoku. If you’ve played it, you know the rules. I’ll give them here in a nutshell. The user starts with a 9x9 grid. That grid is split into nine 3x3 blocks. There are a handful of cells filled in. The goal of the game is to fill in all the cells such that the following three rules are true:
each column uses each digit (1-9) exactly once
each row uses each digit exactly once
each of the 3x3 blocks uses each digit exactly once
Here are examples of a starting grid and a solution (lifted from Wikipedia).
"Sudoku-by-L2G-20050714 solution" by en:User:Cburnett
Your mission, should you choose to accept it, is to determine whether a given Sudoku answer is correct. This means that you will need to validate the three rules above for the given puzzle answer.
The puzzle will be in a .txt file. It will have nine rows of nine digits. The digits will be separated by commas. You will need to place the digits into a 9x9 two dimensional array. Then you will need to check each of the rules on the array. The numbers used will be 1-9. You do not need to worry about numbers outside the range.
At the end, you need to output VALID or INVALID. If the answer is invalid, you should write where you found it to be invalid. You must continue on to find and report all invalid locations, e.g.
ROW 0
COL 4
BLK 1
The rows are numbered left to right, the columns top to bottom, the blocks left-to-right, top to bottom, all starting with zero,
To be clear the blocks are numbered:
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
There are a number of txt files attached to the assignment, one good solution and a number of bad ones. There are notes in the file that give the expected errors. You may find these useful for testing your program. You are encouraged to create other good (and bad) solutions for further testing. Also, feel free to use any data structures in addition to a two-dimensional array that will help you in your validation.
Use good programming practices as you solve this problem. As always, you will be graded on efficiency, maintainability, etc. as well as correctness. Do not do this all in one big loop. Write methods to test each portion, e.g. one to test rows, one to test columns, etc. If you do not, you will find it difficult to think through your program logically; it will also be harder to maintain when your employer wants to add a ‘killer Sudoku’ checker.
As always, you will be graded on good programming practices, efficiency, clarity, etc.
For a grade of ‘B:
Report all invalid Rows and Columns.
For a grade of ‘A’:
Also report all the invalid Blocks.
Below is the txt files that was sent with the instructions. I don't know how to even start this program so please help me complete this so that I can get an 'A' on this program.
Suduko-bad-1.txt:
2,1,5,6,4,7,3,9,8
3,6,8,9,5,2,1,7,4
7,9,4,3,8,1,6,5,2
5,8,6,2,7,4,9,3,1
1,4,2,5,8,3,8,6,7
9,7,3,8,1,6,4,2,5
8,2,1,7,3,9,5,4,6
6,5,9,4,2,8,7,1,3
4,3,7,1,6,5,2,8,9
INVALID
ROW: 4
COL: 4
BLK: 4
Suduko-bad-2.txt:
2,1,5,6,4,7,3,9,8
3,6,8,9,5,2,1,7,4
7,9,4,3,8,1,6,5,2
5,8,6,2,7,4,9,3,1
1,4,2,5,9,3,8,6,7
9,7,3,8,1,6,4,2,5
8,2,1,7,3,9,5,4,6
6,5,9,4,2,8,7,1,3
4,3,7,6,1,5,2,8,9
INVALID
COL: 3
COL: 4
Suduko-bad-3.txt:
2,1,5,6,4,3,7,9,8
3,6,8,9,5,1,2,7,4
7,9,4,3,8,6,1,5,2
5,8,6,2,7,9,4,3,1
1,4,2,5,9,8,3,6,7
9,7,3,8,1,4,6,2,5
8,2,1,7,3,5,9,4,6
6,5,9,4,2,7,8,1,3
4,3,7,1,6,2,5,8,9
INVALID
BLK: 1
BLK: 2
BLK: 4
BLK: 5
BLK: 7
BLK: 8
Suduko-bad-4.txt:
2,1,5,6,4,7,3,9,8
3,6,8,9,5,2,1,7,4
7,9,4,3,8,1,6,5,2
5,8,6,2,7,4,9,3,1
1,4,2,5,9,3,8,6,7
9,7,3,8,1,6,4,2,5
8,2,1,7,3,9,5,4,6
6,5,9,5,2,8,7,1,3
4,3,7,1,6,4,2,8,9
INVALID
ROW: 7
ROW: 8
COL: 3
COL: 5
Suduko-good.txt:
2,1,5,6,4,7,3,9,8
3,6,8,9,5,2,1,7,4
7,9,4,3,8,1,6,5,2
5,8,6,2,7,4,9,3,1
1,4,2,5,9,3,8,6,7
9,7,3,8,1,6,4,2,5
8,2,1,7,3,9,5,4,6
6,5,9,4,2,8,7,1,3
4,3,7,1,6,5,2,8,9
Like I said, I don't know what the text files are suppose to be used for but can you please help me figure out this program so that I can get an 'A' on it.
// Method: GenerateGame // Purpose: Generates game based on complexity level. public void GenerateGame(GameLevel level) { // InitialiseSet // This first creates answer set by using Game combinations InitialiseSet(); int minPos,maxPos,noOfSets; // Now unmask positions and create problem set. switch(level) { case GameLevel.SIMPLE: minPos=4; maxPos=6; noOfSets=8; UnMask(minPos,maxPos,noOfSets); break; case GameLevel.MEDIUM: minPos=3; maxPos=5; noOfSets= 7; UnMask(minPos,maxPos,noOfSets); break; case GameLevel.COMPLEX: minPos=3; maxPos=5; noOfSets = 6; UnMask(minPos,maxPos,noOfSets); break; default: UnMask(3,6,7); break; }
// Property:GameSet // Return Problem Set as DataSet public DataSet ProblemSet { get{ return FormDataSet();} }
// Handler method for data validations.
private void CurrentSet_ColumnChanging(object sender,
System.Data.DataColumnChangeEventArgs e)
{
try
{
lblStatus.Text="";
int rowPos =
dataGrid1.CurrentCell.RowNumber;
string currentNumber =
e.ProposedValue as string;
int number
=Int32.Parse(currentNumber);
if((number <
1)||(number >9))
{
string errorMessage=
"Number should be between 1 and 9";
e.Row.SetColumnError(e.Column,errorMessage);
}
else
{
int col =e.Column.Ordinal;
bool answerChanged =
_newGame.CheckForAnswerChange(rowPos,col,number);
if(answerChanged)
{
lblStatus.Text="You can't change the answer";
e.ProposedValue = e.Row[e.Column];
}else if(_newGame.CheckForDuplicate(rowPos,col,number)){
e.Row.SetColumnError(e.Column,"Number is Duplicate");
}else
{
e.Row.ClearErrors();
bool answerComplete= IsSolutionComplete();
if(answerComplete)
{
lblStatus.Text= "Great!!! You have done it";
}
}
}
}
catch(Exception ex)
{
e.Row.SetColumnError(e.Column,
"Enter valid Number between 1 & 9");
}
}
/// DataGrid Paint overriden to paint border
private void DataGrid1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
// Override this handler to do custom
painting.
Point currentPoint = new Point(0,0);
Size size = new
Size(PREFERRED_COLUMN_WIDTH*3,
PREFERRED_ROW_HEIGHT*3);
Pen myPen = new Pen(Color.Red,3);
for(int i=0;i<3;i++)
{
for(int
j=0;j<3;j++)
{
currentPoint.X = i*PREFERRED_ROW_HEIGHT*3;
currentPoint.Y = j*PREFERRED_ROW_HEIGHT*3;
Rectangle rect = new Rectangle(currentPoint,size);
e.Graphics.DrawRectangle(myPen,rect);
}
}
}
// On edit, add scroll event handler, and display combobox
protected override void
Edit(System.Windows.Forms.CurrencyManager
source, int rowNum, System.Drawing.Rectangle
bounds, bool readOnly,
string instantText, bool cellIsVisible)
{
//Call base method which is important else
//edit will not function properly
base.Edit(source, rowNum, bounds,
readOnly,
instantText, cellIsVisible);
// Check if cell is not empty
if(this.TextBox.Text.TrimEnd()!="")
{
// Get Column
position
int dataValue =
Int32.Parse(this.TextBox.Text);
int pos =
this.MappingName.LastIndexOf("col");
if(pos > -1)
{
string colIndex = this.MappingName.Substring(pos+3);
int colPos = Int32.Parse(colIndex);
// Check whether it is answer spot.
_answerPostion=_game.CheckIfAnswerPosition(rowNum,
colPos,dataValue);
}
}
else
{
_answerPostion
=false;
}
if (!readOnly && cellIsVisible)
{
// Save current row in
the DataGrid and currency manager
// associated with the
data source for the DataGrid
this._currentRow =
rowNum;
this.cm = source;
if(!_answerPostion)
{
// Make parent of scrollbar same as parent.
this.vsBar.Parent = this.TextBox.Parent;
Rectangle rect =
this.DataGridTableStyle.DataGrid.GetCurrentCellBounds();
//Place this control to right.
this.vsBar.Location =
new Point(rect.Right-this.SpinnerWidth,rect.Top);
this.vsBar.Size =
new Size(this.SpinnerWidth,this.TextBox.Height);
// Make the scrollbar visible and place on top textbox
control
this.vsBar.Show();
// As textbox control also there let us bring this to front.
this.vsBar.BringToFront();
this.vsBar.Show();
// this.TextBox.Text=
this.vsBar.Value.ToString();
this.TextBox.ReadOnly=true;
//Set text color properties different as we are editing cell.
this.TextBox.BackColor = Color.Blue;
this.TextBox.ForeColor=Color.White;
}
else
{
this.TextBox.ReadOnly=true;
this.TextBox.BackColor=Color.White;
this.TextBox.ForeColor =Color.Black;
}
}
}
Get Answers For Free
Most questions answered within 1 hours.