0% found this document useful (0 votes)
22 views5 pages

FGH

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)
22 views5 pages

FGH

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/ 5

#include <iostream>

#include <string>
#include <fstream>
#include <cctype>
using namespace std;

char getPrompt(int board[][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[][9])
{
ifstream fin;
fin.open(fileName);
if (fin.fail())
cout << "Error reading file " << fileName << endl;

for (int x = 0; x < 9; x++)


{
for (int y = 0; y < 9; y++)
{
fin >> board[x][y];
}
}
fin.close();
}

/************************************************************************
* displays array saved from the file
************************************************************************/
void displayBoard(int board[][9])
{
cout << " A B C D E F G H I\n";

for (int x = 0; x < 9; x++)


{
cout << x + 1 << " ";

for (int y = 0; y < 9; y++)


{
if (y == 3 || y == 6)
cout << '|';
else
cout << ' ';

if (board[x][y] == 0)
cout << ' ';
else
cout << board[x][y];
}

if (x == 2 || x == 5)
cout << "\n -----+-----+-----";

cout << endl;


}

cout << endl;

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";

cout << endl;

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";

cout << endl;


cout << endl;

getPrompt(board);
}

/************************************************************************
* edits an empty coordinate
************************************************************************/
void editBoard(int board[][9])
{
int boardRow;
char boardCol;

cout << "What are the coordinates of the square: ";


cin >> boardCol >> boardRow;
boardCol = toupper(boardCol);
int inputCol = boardCol - 'A';
int inputRow = boardRow - 1;

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";

cout << endl;

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);

for (int a = 0; a < 9; a++)


{
for (int b = 0; b < 10; b++)
{
if (b == board[inputRow][a])
valueList[b] = false;
}
}

for (int c = 0; c < 9; c++)


{
for (int b = 0; b < 10; b++)
{
if (b == board[c][inputCol])
valueList[b] = false;
}
}

int smallRow = inputRow / 3 * 3;


int smallCol = inputCol / 3 * 3;

for (int d = 0; d < 3; d++)


{
for (int e = 0; e < 3; e++)
{
for (int b = 0; b < 10; b++)
{
if (b == board[smallRow + e][smallCol + d])
valueList[b] = false;
}
}
}

cout << "Possible Values: ";

for (int f = 1; f < 10; f++)


{
if (valueList[f])
cout << f << ' ';
}

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);

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


{
for (int j = 0; j < 9; j++)
{
fout << board[i][j] << ' ';
}
fout << endl;
}

fout.close();

cout << "Board written successfully\n";

return 0;
}

/************************************************************************
* prompts user, calls functions to interact with array
************************************************************************/
char getPrompt(int board[][9])
{
char prompt;
cout << "> "; //change

cin >> prompt;


prompt = toupper(prompt);

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;
}

You might also like