1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
/***********************************************************************
* Program:
* project 12, Sudoku
* Sister Unsicker, CS124
* Author:
* Lanie Molinar
* Summary:
*
*
* Estimated: 10.0 hrs
* Actual: 0.0 hrs
* Please describe briefly what was the most difficult part.
************************************************************************/
#include <iostream>
#include <fstream>
using namespace std;
void getFileName(char fileName[256])
{
cout << "Where is your board located? ";
cin >> fileName;
}
void readFile(char fileName[256], int board[9][9])
{
ifstream fin(fileName);
if (fin.fail())
{
cout << "Error reading file \""
<< fileName
<< "\"\n";
return;
}
for (int r = 0; r < 9; r++)
for (int c = 0; c < 9; c++)
fin >> board[r][c];
fin.close();
}
void displayOptions()
{
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";
}
void displayBoard(int board[9][9])
{
cout << "\n A B C D E F G H I\n";
for (int r = 0; r < 9; r++)
{
cout << r + 1
<< " ";
for (int c = 0; c < 9; c++)
{
char value = (board[r][c] == 0) ? ' ' : '0' + board[r][c];
if (c == 8)
{
cout << " "
<< value
<< endl;
}
else if (c != 3 && c != 6)
{
cout << " "
<< value;
}
else
cout << value;
if(c == 2 || c == 5)
cout << "|";
}
if (r == 2 || r == 5)
cout << " -----+-----+-----\n";
}
}
int computeValues(int board[9][9], int r, int c)
{
bool possible;
return possible;
}
void editSquare(int board[9][9], int r, int c)
{
char column = c + 65;
computeValues(board, r, c);
if (board[r][c] != 0)
{
cout << "ERROR: Square '"
<< column
<< r
<< "' is filled\n";
return;
}
cout << "What is the value at '"
<< column
<< r
<< "': ";
int value
cin >> board[r][c];
}
void getCoordinates(int &r, int &c)
{
cout << "What are the coordinates of the square: ";
char column;
cin >> column >> r;
switch (column)
{
case 'a':
case 'A':
c = 0;
break;
case 'b':
case 'B':
c = 1;
break;
case 'c':
case 'C':
c = 2;
break;
case 'd':
case 'D':
c = 3;
break;
case 'e':
case 'E':
c = 4;
break;
case 'f':
case 'F':
c = 5;
break;
case 'g':
case 'G':
c = 6;
break;
case 'h':
case 'H':
c = 7;
break;
case 'i':
case 'I':
c = 8;
}
}
void displayPossible(int board[9][9], int r, int c)
{
computeValues(board, r, c);
}
void writeBoard(int board[9][9], char fileName[256])
{
cout << "What file would you like to write your board to: ";
cin >> fileName;
ofstream fout(fileName);
for (int r = 0; r < 9; r++)
{
for (int c = 0; c < 9; c++)
{
fout << board[r][c] << " ";
}
cout << endl;
}
if (fout.fail())
{
cout << "Error writing to file \""
<< fileName
<< "\"\n";
}
fout.close();
cout << "Board written successfully\n";
}
void interact(int board[9][9])
{
char fileName[256];
int r;
int c;
bool possible;
char letter;
do
{
cout << "\n> ";
cin >> letter;
switch (letter)
{
case '?':
displayOptions();
break;
case 'd':
case 'D':
displayBoard(board);
break;
case 'e':
case 'E':
getCoordinates(r, c);
editSquare(board, r, c);
break;
case 's':
case 'S':
getCoordinates(r, c);
possible = computeValues(board, r, c);
}
}
while (letter != 'q' && letter != 'Q');
}
/**********************************************************************
* The main function tells a program where to start.
***********************************************************************/
int main()
{
char fileName[256];
int board[9][9];
getFileName(fileName);
readFile(fileName, board);
displayOptions();
displayBoard(board);
interact(board);
writeBoard(board, fileName);
return 0;
}
|