FGH
FGH
#include <string>
#include <fstream>
#include <cctype>
using namespace std;
/************************************************************************
* 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[][9])
{
ifstream fin;
fin.open(fileName);
if (fin.fail())
cout << "Error reading file " << fileName << endl;
/************************************************************************
* displays array saved from the file
************************************************************************/
void displayBoard(int board[][9])
{
cout << " A B C D E F G H I\n";
if (board[x][y] == 0)
cout << ' ';
else
cout << board[x][y];
}
if (x == 2 || x == 5)
cout << "\n -----+-----+-----";
getPrompt(board);
}
/************************************************************************
* shows the instructions then immediately shows the board
************************************************************************/
void showInstructions(int board[][9])
{
cout << "Options:\n"
<< " ? Show these instructions\n"
<< " D Display the board\n"
<< " E Edit one square\n"
<< " S Show the possible values for a square\n"
<< " Q Save and Quit\n";
displayBoard(board);
}
/************************************************************************
* reminds the user of the instructions and without showing the board
************************************************************************/
void remindInstructions(int board[][9])
{
cout << "Options:\n"
<< " ? Show these instructions\n"
<< " D Display the board\n"
<< " E Edit one square\n"
<< " S Show the possible values for a square\n"
<< " Q Save and Quit\n";
getPrompt(board);
}
/************************************************************************
* edits an empty coordinate
************************************************************************/
void editBoard(int board[][9])
{
int boardRow;
char boardCol;
int value;
if (board[inputRow][inputCol] == 0)
{
cout << "What is the value at '" << boardCol << boardRow << "': ";
cin >> board[inputRow][inputCol];
}
else
cout << "ERROR: Square '" << boardCol << boardRow << "' is filled\n";
getPrompt(board);
}
/************************************************************************
* shows the possibles values of a chosen coordinate
************************************************************************/
int showPossibleValues(int board[][9])
{
bool valueList[10];
for (int i = 1; i < 10; i++)
valueList[i] = true;
int row;
char col;
int inputRow = row - 1;
int inputCol = col - 'A';
cout << "What square do you want possible values for: ";
cin >> col >> row;
col = toupper(col);
getPrompt(board);
return 0;
}
/************************************************************************
* saves board onto a new file
************************************************************************/
int saveBoard(int board[][9])
{
ofstream fout;
char newFile[256];
cout << "What file would you like to write your board to: ";
cin >> newFile;
fout.open(newFile);
fout.close();
return 0;
}
/************************************************************************
* prompts user, calls functions to interact with array
************************************************************************/
char getPrompt(int board[][9])
{
char prompt;
cout << "> "; //change
switch (prompt)
{
case '?':
remindInstructions(board);
break;
case 'D':
displayBoard(board);
break;
case 'E':
editBoard(board);
break;
case 'S':
showPossibleValues(board);
break;
case 'Q':
saveBoard(board);
break;
}
}
/************************************************************************
* Main will drive the program
************************************************************************/
int main()
{
char fileName[256];
getFileName(fileName);
int board[9][9];
readFile(fileName, board);
showInstructions(board);
return 0;
}