using System;
using System.Collections;
#endregion
namespace sudokuChecker
{
class SudokuMainExample
{
static void Main()
{
SudokuCheck objectSudokuCheck = new SudokuCheck();
int[,] Board=new int[9,9];
int countnumberofInput=0;
string menuStatus="y";
string readInput="n";
try
{
Console.WriteLine("You Are Entering In Sudoku Checker:----");
for (int indexRow = 0; indexRow < 9; indexRow++)
{
for (int indexColumn = 0; indexColumn < 9; indexColumn++)
{
Board[indexRow, indexColumn] = 0;
}
}
do
{
/*try
{
Console.WriteLine("PLease Enter The Size of Row To Start The Game:--");
int row = Int32.Parse(Console.ReadLine());
Console.WriteLine("Lease Enter The Size of Row To Start The Game:--");
int column = Int32.Parse(Console.ReadLine());
if ((row != 9) && (column != 9))
throw new Exception("error");
else
Board = new int[row, column];
for (int indexRow = 0; indexRow < row; indexRow++)
{
for (int indexColumn = 0; indexColumn < column; indexColumn++)
{
Board[indexRow, indexColumn] = 0;
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Your Input Is Not Valid Please Input A Right One ");
Console.WriteLine("If You Want To Input It Again then press :-Y otherwise Press N .");
readInput = (Console.ReadLine());
}*/
while ((menuStatus == "Y") || (menuStatus == "y"))
{
Console.WriteLine("You Have Some Choices:--");
Console.WriteLine("Press:-1 To Give Input In Sudoku Game:-");
Console.WriteLine("Press:-2 To Check Status");
Console.WriteLine("Press:-3 To Put Default Right Values:-");
Console.WriteLine("Press:-4 To Put Default Wrong Values:-");
Console.WriteLine("Press:-5 To Exit From The Menu Status:-");
int readChoice = Int32.Parse(Console.ReadLine());
switch (readChoice)
{
case 1:
int choiceCheck = 0;
while (choiceCheck != 1)
{
Console.WriteLine("Please Input A Row Number Where You Want To Put The Input");
int inputRow = Int32.Parse(Console.ReadLine());
Console.WriteLine("Please Input A Column Number Where You Want to Put The Input");
int inputColumn = Int32.Parse(Console.ReadLine());
Console.WriteLine("Please Input The Value You Want To Fill In This Particular Space");
int inputValue = Int32.Parse(Console.ReadLine());
if ((inputValue > 9) || (inputValue < 1))
{
Console.WriteLine("You Input Value Is Not Valid Please Input A Valid Value");
choiceCheck = 0;
}
else
{
choiceCheck = 1;
if (countnumberofInput != 81)
{
countnumberofInput++;
objectSudokuCheck.UserInputSudoku(inputRow, inputColumn, inputValue,Board);
}
else
{
Console.WriteLine("Board Has Already Filled");
Console.WriteLine("Now Check The status of Sudoku Game.");
}
}
}
Console.WriteLine("If You Again Want To Go To The Main menu Then Press Y To Continue Otherwise Press Any Button");
menuStatus = Console.ReadLine();
break;
case 2:
Console.WriteLine("We Are Going To Check the Status Of Sudoku Game");
SudokuCheck.Status obj= objectSudokuCheck.CheckSudokuStatus(Board);
Console.WriteLine(obj);
Console.WriteLine("If You Again Want To Go To The Main menu Then Press Y To Continue Otherwise Press Any Button");
menuStatus = Console.ReadLine();
break;
case 3:
Console.WriteLine("We Are Going To Put Default Right Values");
objectSudokuCheck.InputDefaultRightValues(Board);
Console.WriteLine("If You Again Want To Go To The Main menu Then Press Y To Continue Otherwise Press Any Button");
menuStatus = Console.ReadLine();
break;
case 4:
Console.WriteLine("We Are Going To Put Default Wrong Values");
objectSudokuCheck.InputDefaultWrongValues(Board);
Console.WriteLine("If You Again Want To Go To The Main menu Then Press Y To Continue Otherwise Press Any Button");
menuStatus = Console.ReadLine();
break;
case 5:
Console.WriteLine("We Are Exiting From The Program");
menuStatus = "N";
Console.WriteLine("If You Want To Input It Again then press :-Y otherwise Press N .");
readInput = (Console.ReadLine());
break;
default:
Console.WriteLine("Invalid Choice");
break;
}
}
} while (readInput == "y");
}
catch (Exception e)
{
Console.WriteLine("Your Input Is Not Valid:--");
Console.WriteLine("Do You Wanna Input It Again:--");
Console.ReadKey();
}
}
}
}
using System;
using System.Collections;
namespace sudokuChecker
{
class SudokuCheck
{
public enum Status
{
Success = 0,
Failure = 1,
Continue = 2
}
public void UserInputSudoku(int inputRow, int inputColumn, int inputValue,int [,]Board)
{
Board[inputRow,inputColumn] = inputValue;
}
public void InputDefaultWrongValues(int[,] Board)
{
Board[0, 0] = 9; Board[0, 1] = 6; Board[0, 2] = 1; Board[0, 3] = 1;
Board[0, 4] = 4; Board[0, 5] = 8; Board[0, 6] = 2; Board[0, 7] = 7;
Board[0, 8] = 3; Board[1, 0] = 8; Board[1, 1] = 7; Board[1, 2] = 2;
Board[1, 3] = 9; Board[1, 4] = 3; Board[1, 5] = 1; Board[1, 6] = 6;
Board[1, 7] = 4; Board[1, 8] = 5; Board[2, 0] = 6; Board[2, 1] = 5;
Board[2, 2] = 3; Board[2, 3] = 3; Board[2, 4] = 2; Board[2, 5] = 7;
Board[2, 6] = 8; Board[2, 7] = 9; Board[2, 8] = 1; Board[3, 0] = 5;
Board[3, 1] = 8; Board[3, 2] = 9; Board[3, 3] = 3; Board[3, 4] = 2;
Board[3, 5] = 2; Board[3, 6] = 1; Board[3, 7] = 6; Board[3, 8] = 7;
Board[4, 0] = 2; Board[4, 1] = 8; Board[4, 2] = 6; Board[4, 3] = 7;
Board[4, 4] = 1; Board[4, 5] = 5; Board[4, 6] = 4; Board[4, 7] = 3;
Board[4, 8] = 9; Board[5, 0] = 1; Board[5, 1] = 3; Board[5, 2] = 7;
Board[5, 3] = 4; Board[5, 4] = 9; Board[5, 5] = 6; Board[5, 6] = 5;
Board[5, 7] = 8; Board[5, 8] = 2; Board[6, 0] = 6; Board[6, 1] = 9;
Board[6, 2] = 4; Board[6, 3] = 1; Board[6, 4] = 5; Board[6, 5] = 3;
Board[6, 6] = 7; Board[6, 7] = 2; Board[6, 8] = 8; Board[7, 0] = 3;
Board[7, 1] = 2; Board[7, 2] = 5; Board[7, 3] = 8; Board[7, 4] = 7;
Board[7, 5] = 4; Board[7, 6] = 9; Board[7, 7] = 1; Board[7, 8] = 6;
Board[8, 0] = 7; Board[8, 1] = 1; Board[8, 2] = 8; Board[8, 3] = 2;
Board[8, 4] = 6; Board[8, 5] = 5; Board[8, 6] = 7; Board[8, 7] = 5;
Board[8, 8] = 4;
}
public void InputDefaultRightValues(int[,] Board)
{
Board[0,0]=9;Board[0,1]=6;Board[0,2]=1;Board[0,3]=5;
Board[0,4]=4;Board[0,5]=8;Board[0,6]=2;Board[0,7]=7;
Board[0,8]=3;Board[1,0]=8;Board[1,1]=7;Board[1,2]=2;
Board[1,3]=9;Board[1,4]=3;Board[1,5]=1;Board[1,6]=6;
Board[1,7]=4;Board[1,8]=5;Board[2,0]=4;Board[2,1]=5;
Board[2,2]=3;Board[2,3]=6;Board[2,4]=2;Board[2,5]=7;
Board[2,6]=8;Board[2,7]=9;Board[2,8]=1;Board[3,0]=5;
Board[3,1]=4;Board[3,2]=9;Board[3,3]=3;Board[3,4]=8;
Board[3,5]=2;Board[3,6]=1;Board[3,7]=6;Board[3,8]=7;
Board[4,0]=2;Board[4,1]=8;Board[4,2]=6;Board[4,3]=7;
Board[4,4]=1;Board[4,5]=5;Board[4,6]=4;Board[4,7]=3;
Board[4,8]=9;Board[5,0]=1;Board[5,1]=3;Board[5,2]=7;
Board[5,3]=4;Board[5,4]=9;Board[5,5]=6;Board[5,6]=5;
Board[5,7]=8;Board[5,8]=2;Board[6,0]=6;Board[6,1]=9;
Board[6,2]=4;Board[6,3]=1;Board[6,4]=5;Board[6,5]=3;
Board[6,6]=7;Board[6,7]=2;Board[6,8]=8;Board[7,0]=3;
Board[7,1]=2;Board[7,2]=5;Board[7,3]=8;Board[7,4]=7;
Board[7,5]=4;Board[7,6]=9;Board[7,7]=1;Board[7,8]=6;
Board[8,0]=7;Board[8,1]=1;Board[8,2]=8;Board[8,3]=2;
Board[8,4]=6;Board[8,5]=9;Board[8,6]=3;Board[8,7]=5;
Board[8,8]=4;
}
public Status CheckSudokuStatus(int[,] Board)
{
int flag = 0;
for (int rowCheck = 0; rowCheck < 9; rowCheck++)
{
flag = 0;
for (int columnCheck = 0; columnCheck < 9; columnCheck++)
{
if (Board[rowCheck, columnCheck] == 0)
continue;
if ((flag & (1 << Board[rowCheck, columnCheck])) != 0)
return Status.Failure;
flag |= (1 << Board[rowCheck, columnCheck]);
}
}
for (int columnCheck = 0; columnCheck < 9; columnCheck++)
{
flag = 0;
for (int rowCheck = 0; rowCheck < 9; rowCheck++)
{
if (Board[rowCheck, columnCheck] == 0)
continue;
if (((flag & (1 << Board[rowCheck, columnCheck])) != 0))
return Status.Failure;
flag |= (1 << Board[rowCheck, columnCheck]);
}
}
for (int rowCheck = 0; rowCheck < 3; rowCheck++)
{
for (int columnCheck = 0; columnCheck < 3; columnCheck++)
{
BoxCheck(Board, rowCheck* 3, columnCheck * 3);
}
}
return Status.Success;
}
private Status BoxCheck(int[,] Board, int Row, int Coloumn)
{
ArrayList ArrElementsCompare = new ArrayList();
ArrElementsCompare.Clear();
for (int rowIndex = Row; rowIndex < Row + 3; rowIndex++)
{
for (int columnIndex = Coloumn; columnIndex < Coloumn + 3; columnIndex++)
{
if (Board[rowIndex, columnIndex] == 0)
continue;
if (ArrElementsCompare.Contains(Board[rowIndex, columnIndex]))
return Status.Failure;
ArrElementsCompare.Add(Board[rowIndex, columnIndex]);
}
}
return Status.Success;
}
}
}
0 comments:
Post a Comment