0% found this document useful (0 votes)
24 views8 pages

Sudoku Solver

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views8 pages

Sudoku Solver

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

#include <iostream>

#include <fstream>
using namespace std;

#define AREA 9

/************************************************************************
* prompts for file
************************************************************************/
void getFileName(char fileName[])
{
cout << "Where is your board located? ";
cin >> fileName;
}

/************************************************************************
* checks file and parses info from file to create an array
************************************************************************/
void readFile(char fileName[], int board[][AREA])
{
ifstream fin(fileName);

if (fin.fail())
{
cout << "Error reading file \"" << fileName << "\"\n";
return;
}

for (int row = 0; row < AREA; row++)


{
for (int col = 0; col < AREA; col++)
fin >> board[row][col];
}
fin.close(); //close the file
}

/**********************************************************************
* displays board
***********************************************************************/
void displayBoard(int board[][AREA])
{
cout << " A B C D E F G H I\n";
int count = 1;
for (int boardRow = 0; boardRow < AREA; boardRow++)
{
cout << count << " ";
for (int boardCol = 0; boardCol < AREA; boardCol++)
{
if (board[boardRow][boardCol] == 0)
cout << " ";
else
cout << board[boardRow][boardCol];

if (boardCol == 2 || boardCol == 5)
cout << "|";
else
{
if (boardCol == (AREA - 1))
cout << endl;
else
cout << " ";
}
}
if (boardRow == 2 || boardRow == 5)
cout << " -----+-----+-----\n";
count++;
}
cout << endl;
}

/**********************************************************************
* check if input is legal
***********************************************************************/
bool checkSelection(int board[][AREA], int r, int c, int value)
{
bool isLegal = false;
int inputRow;
int inputCol;

for (int i = 0; i < AREA; i++)


{
if (board[r][i] == value)
return isLegal;
}

for (int j = 0; j < AREA; j++)


{
if (board[j][c] == value)
return isLegal;
}

if (r <= 2)
inputRow = 0;
else if (r <= 5)
inputRow = 3;
else if (r <= 8)
inputRow = 6;

if (c <= 2)
inputCol = 0;
else if (c <= 5)
inputCol = 3;
else if (c <= 8)
inputCol = 6;

int smallRow = inputRow + 3;


int smallCol = inputCol + 3;

for (inputRow; inputRow < smallRow; inputRow++)


for (inputCol; inputCol < smallCol; inputCol++)
{
if (board[inputRow][inputCol] == value)
{
isLegal = false;
return isLegal;
}
else
isLegal = true;
}

return isLegal;
}

/**********************************************************************
* checks if chosen area is filled, if not user input
***********************************************************************/
void editBoard(int board[][AREA], int row, char col)
{
int c = (static_cast<int>(col) - 65);
int r = row - 1;
int value;

if (board[row - 1][c] != 0)
{
cout << "ERROR: Square \'" << col << row << "\' is filled\n";
}
else
{
cout << "What is the value at \'" << col << row << "\': ";
cin >> value;
if (checkSelection(board, r, c, value) == true)
{
board[row - 1][c] = value;
}
else
{
cout << "ERROR: Value \'" << value << "\' in square \'" << col << row;
cout << "\' is invalid\n";
}
}
cout << endl;

return;
}

/**********************************************************************
* prompts for coordinates to edit
***********************************************************************/
void editSquare(int board[][AREA])
{
int row;
char col;
cout << "What are the coordinates of the square: ";
cin >> col >> row;
editBoard(board, row, col);

return;
}

/**********************************************************************
* Prompt user for a file name
***********************************************************************/
void getWriteFileName(char fileName[])
{
cout << "What file would you like to write your board to: ";
cin >> fileName;
}
/**********************************************************************
* saves data to the file
***********************************************************************/
void saveFile(char fileName[], const int board[][AREA])
{
ofstream fout(fileName);

if (fout.fail())
{
cout << "Error writing to file \"" << fileName << "\"\n";
return;
}

for (int row = 0; row < AREA; row++) // will write all data into new file.
{
for (int col = 0; col < AREA; col++)
fout << board[row][col] << " "; //space after each number
}
fout.close(); //Close the file

cout << "Board written successfully\n";

return;
}

/**********************************************************************
* checks if user input is legal
***********************************************************************/
bool checkSquare(int board[][AREA], int r, int c, int numberCheck)
{
int inputRow;
int inputCol;
bool isLegal = true;

if (c <= 2)
inputCol = 0;
else if (c <= 5)
inputCol = 3;
else if (c <= 8)
inputCol = 6;

if (r <= 2)
inputRow = 0;
else if (r <= 5)
inputRow = 3;
else if (r <= 8)
inputRow = 6;

int rowLimit = inputRow + 3;


int colLimit = inputCol + 3;

for (inputRow; inputRow < rowLimit; inputRow++)


{
for (inputCol; inputCol < colLimit; inputCol++)
{
if (board[inputRow][inputCol] == numberCheck)
{
isLegal = false;
return isLegal;
}
else
isLegal = true;
}
inputCol -= 3;

}
return isLegal;
}

/**********************************************************************
* checks numbers all rows and columns if legal
***********************************************************************/
void checkPossibleValues(int board[][AREA], int array[], int row, char col)
{
bool number = true;
bool inputRow = true;
bool inputCol = true;
int c = (static_cast<int>(col) - 65);
int r = row - 1;
int cycleNumber = 1;
int start = 0;

for (int count = 0; count < AREA; count++)


{
for (int i = 0; i < AREA; i++)
{
if (board[r][i] == cycleNumber)
inputRow = false;
}

for (int j = 0; j < AREA; j++)


{
if (board[j][c] == cycleNumber)
inputCol = false;
}

number = checkSquare(board, r, c, cycleNumber);

if (number == true && inputRow == true && inputCol == true)


{
array[start] = cycleNumber;
cycleNumber++;
start++;
}
else
cycleNumber++;

number = true;
inputRow = true;
inputCol = true;
}
}

/**********************************************************************
* displays possible values
***********************************************************************/
void displayPossible(int board[][AREA])
{
int row;
char col;
int array[9];
int arrayS = 0;
cout << "What are the coordinates of the square: ";
cin >> col >> row;
checkPossibleValues(board, array, row, col);

cout << "The possible values for \'" << col << row << "\' are: ";

for (int size = 0; size < AREA; size++)


{
if (array[size] == 1 || array[size] == 2 || array[size] == 3 ||
array[size] == 4 || array[size] == 5 || array[size] == 6 ||
array[size] == 7 || array[size] == 8 || array[size] == 9)
arrayS++;
}

for (int j = 0; j < arrayS; j++)


{
if (j != arrayS - 1)
{
cout << array[j] << ", ";
}
else
cout << array[j] ;
}
cout << endl << endl;
}

/*
*
*/
solveSudoku()
{

/**********************************************************************
* displays instruction menu and prompts user
***********************************************************************/
void getPrompt(int board[][AREA])
{
bool quit = false;
char option;
cout << "Options:\n";
cout << " ? Show these instructions\n";
cout << " D Display the board\n";
cout << " E Edit one square\n";
cout << " S Show the possible values for a square\n";
cout << " Q Save and Quit\n\n";
displayBoard(board); //also have to display board on initial load
do
{
cout << "> ";
cin >> option;

switch (option)
{
case '?':
cout << "Options:\n";
cout << " ? Show these instructions\n";
cout << " D Display the board\n";
cout << " E Edit one square\n";
cout << " S Show the possible values for a square\n";
cout << " Q Save and Quit\n\n";
cout << " A Fill board with answers";
cout << endl;
break;
case 'd':
case 'D':
displayBoard(board);
break;
case 'e':
case 'E':
editSquare(board);
break;
case 'q':
case 'Q':
quit = true;
break;
case 's':
case 'S':
displayPossible(board);
break;
case 'a':
case 'A':
solveSudoku(board);
break;
}
} while (quit == false);

return;
}

/**********************************************************************
* main to call primary functions
***********************************************************************/
int main()
{
char fileName[256];
int board[AREA][AREA]; //current size is 9X9

getFileName(fileName); //Get file name


readFile(fileName, board);
getPrompt(board);
getWriteFileName(fileName);
saveFile(fileName, board);
return 0;
}

You might also like