Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- /**********************************************************************
- * This function asks the user for a filename with a sudoku board on it.
- ***********************************************************************/
- void getFileName(char fileName[])
- {
- cout << "Where is your board located? ";
- cin >> fileName;
- }
- /**********************************************************************
- * This function reads in the sudoku board.
- ***********************************************************************/
- int readFile(char fileName[], char game[][9])
- {
- int i = 0;
- ifstream input;
- input.open(fileName);
- if (input.fail())
- return -1;
- for (int i = 0; i < 9; i++)
- for (int j = 0; j < 9; j++)
- input >> game[i][j];
- input.close();
- return i;
- }
- void display(char game[][9])
- {
- cout << " A B C D E F G H I\n";
- for (int i = 0; i < 9; i++)
- {
- cout << i + 1 << " ";
- for (int j = 0; j < 9; j++)
- if (game[i][j] == '0')
- {
- cout << " ";
- if (j == 2 || j == 5)
- cout << "|";
- else if (j == 8)
- cout << "";
- else
- cout << " ";
- }
- else if (j == 2 || j == 5)
- cout << game[i][j] << "|";
- else if (j == 8)
- cout << game[i][j];
- else
- cout << game[i][j] << " ";
- if (i == 2 || i == 5)
- cout << "\n -----+-----+-----" << endl;
- else
- cout << endl;
- }
- }
- bool checkSquare(int row, int col, char board[][9], int value)
- {
- for (int i = 0; i < 9; i++)
- if (board[row][i] == value)
- return false;
- for (int i = 0; i < 9; i++)
- if (board[i][col] == value)
- return false;
- int startX = row/3 * 3;
- int startY = col/3 * 3;
- for (int i = startX; i < startX + 3; i++)
- for (int j = startY; j < startY + 3; j++)
- if (board[i][j] == value)
- return false;
- return true;
- }
- void editSquare(char game[][9])
- {
- bool a = 0;
- int x;
- int y;
- char input[3];
- char value;
- cout << "What are the coordinates of the square: ";
- cin >> input;
- x = input[1] - '1';
- y = input[0] - 'A';
- if (game[x][y] != '0')
- {
- cout << "ERROR: Square '" << input << "' is filled\n";
- // cout << endl;
- }
- else
- {
- cout << "What is the value at '" << input << "': ";
- cin >> value;
- // cout0 << endl;
- int i = value;
- if (checkSquare(y, x, game, i))
- game[x][y] = value;
- else
- {
- while(bool a = 0)
- cout << "ERROR: Value '" << value << "' in square '" << input[0] << input[1] << "' is invalid\n";
- bool a = 1;
- }
- }
- cout << endl;
- }
- void showValues(char board[][9])
- {
- int x;
- int y;
- int count = 0;
- char input[3];
- cout << "What are the coordinates of the square: ";
- cin >> input;
- x = input[1] - '1';
- y = input[0] - 'A';
- if (board[x][y] != '0')
- cout << "Error at coord " << y + 'A' << x + '1' << "is already filled.\n";
- else
- {
- cout << "Possible values are: ";
- for (int i = 0; i < 9; i++)
- if (checkSquare(x, y, board, i + '0'))
- {
- if (count == 0)
- cout << i;
- else
- cout << ", " << i;
- count++;
- }
- cout << endl;
- }
- }
- void gameLoop(char data[][9])
- {
- cout << endl;
- char input;
- do
- {
- cout << "> ";
- cin >> input;
- switch (input)
- {
- 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";
- cout << endl << endl;
- break;
- case 'D':
- display(data);
- cout << endl;
- break;
- case 'E':
- editSquare(data);
- break;
- case 'Q':
- break;
- case 'S':
- showValues(data);
- break;
- default:
- cout << "Incorrect input\n";
- break;
- }
- }
- while (input != 'Q');
- }
- void getFileName2(char fileName[])
- {
- cout << "What file would you like to write your board to: ";
- cin >> fileName;
- }
- int writeFile(char fileName[], char game[][9])
- {
- int i = 0;
- ofstream output;
- output.open(fileName);
- if (output.fail())
- return -1;
- for (int i = 0; i < 9; i++)
- for (int j = 0; j < 9; j++)
- output << game[i][j];
- output.close();
- return i;
- }
- int main()
- {
- char fileName[256];
- char data[9][9];
- char input;
- getFileName(fileName);
- int size = readFile(fileName, data);
- if (size == -1)
- cout << "Bad file " << fileName << endl;
- else
- {
- 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";
- cout << endl;
- display(data);
- }
- gameLoop(data);
- getFileName2(fileName);
- writeFile(fileName, data);
- cout << "Board written successfully\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement