Sudoku Solver
Sudoku Solver
#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;
}
/**********************************************************************
* 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;
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;
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
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;
}
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;
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: ";
/*
*
*/
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