Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The red shows the errors Im getting. I posted my code below. The EXP is the expected. I need help on getting the expected. Thank

The red shows the errors Im getting. I posted my code below. The EXP is the expected. I need help on getting the expected. Thank you!

These are the instructions:

The program will prompt the user for the filename of the game he or she is currently working on and display the board on the screen. The user will then be allowed to interact with the game by selecting which square he or she wishes to change. While the program will not solve the game for the user, it will ensure that the user has not selected an invalid number. If the user types 'S' in the prompt, then the program will show the user the possible valid numbers for a given square. When the user is finished, then the program will save the board to a given filename and exit.

Interface Design

Consider a game saved as myGame.txt:

7 2 3 0 0 0 1 5 9 6 0 0 3 0 2 0 0 8 8 0 0 0 1 0 0 0 2 0 7 0 6 5 4 0 2 0 0 0 4 2 0 7 3 0 0 0 5 0 9 3 1 0 4 0 5 0 0 0 7 0 0 0 3 4 0 0 1 0 3 0 0 6 9 3 2 0 0 0 7 1 4 

Note that '0' corresponds to an unknown value. The following is an example run of the program.

Program starts

An example of input is underlined.

Where is your board located? myGame.txt

With the filename specified, the program will display a menu of options:

Options: ? Show these instructions D Display the board E Edit one square S Show the possible values for a square Q Save and quit 

After this, the board as read from the file will be displayed:

 A B C D E F G H I 1 7 2 3| |1 5 9 2 6 |3 2| 8 3 8 | 1 | 2 -----+-----+----- 4 7 |6 5 4| 2 5 4|2 7|3 6 5 |9 3 1| 4 -----+-----+----- 7 5 | 7 | 3 8 4 |1 3| 6 9 9 3 2| |7 1 4 

Here, the user will be prompted for a command (the main prompt).

 Z

Please note that you will need a newline, a carat ('>'), and a space before the prompt.

The next action of the program will depend on the user's command. If the user types an invalid command, then the following message will be displayed:

ERROR: Invalid command

Show Instructions

If the user types '?', then the menu of options will be displayed again. These are the same instructions that are displayed when the program is first run.

Display the Board

If the user types 'D', then the board will be redrawn. This is the same as the drawing of the board when the program is first run.

Save and Quit

If the user types 'Q', then he or she will be prompted for the filename:

What file would you like to write your board to: newGame.txt

The program will display the following message:

Board written successfully

Then the program will terminate.

Edit One Square

If the user types 'E', then the program will prompt him for the coordinates and the value for the square to be edited:

What are the coordinates of the square: A1

If the value is invalid or if the square is already filled, then the program will display one of the following message and return to the main prompt:

ERROR: Square 'zz' is invalid
ERROR: Square 'A1' is filled

With a valid coordinate, then the program next prompts the user for the value:

What is the value at 'A1': 9

If the user types a value that is outside the accepted range (1 ? value ? 9) or does not follow the rules of Sudoku, then a message appears and the program returns to the main prompt:

ERROR: Value '9' in square 'A1' is invalid

Show Possible Values

If the user types 'S', then the program will prompt him for the coordinates and display the possible values:

What are the coordinates of the square: A1

The same parsing logic applies here as for the Edit One Square case. Once the user has selected a valid coordinate, then the program will display the possible values:

The possible values for 'A1' are: 1, 5, 8, 9

After the message appears, the program returns to the main prompt.

image text in transcribed

#include #include #include #include

using namespace std;

#define ROWS 9 #define COLS 9

void getFilename(string & filename); // a bool readFile(string & filename, int board[][COLS]); // b void display(int board[][COLS]); // c void displayOptions(); // d void interact(int board[][COLS]); // e void getSaveFilename(string & filename); // f bool writeFile(string & filename, int board[][COLS]); // g void editSquare(int board[][COLS]); // h void getCoordinates(string & coords); // i bool isValid(int r, int c, string & coords, int board[][COLS]); // j void showPossibleValues(int board[][COLS]); void getPossibleValues(int r, int c, int board[][COLS], int possilbe[]); // k

/********************************************************************** * main * Starts program by retrieving the filename and game board data * Initializes user interaction and save game file procedure when the * program ends. ***********************************************************************/ int main() { string filename; int board[ROWS][COLS];

getFilename(filename); readFile(filename, board); interact(board); getSaveFilename(filename); if (writeFile(filename, board)) { cout

/********************************************************************** * get filename * Prompts the user for the name of the file where the board is saved ***********************************************************************/ void getFilename(string & filename) { cout > filename; cin.ignore(); }

/********************************************************************** * read file * stores the data from the file into a two dimensional array ***********************************************************************/ bool readFile(string & filename, int board[][COLS]) { ifstream fin(filename.c_str()); if (fin.fail()) { return false; } else { for (int r = 0; r > board[r][c]; } } return true; } fin.close(); }

/********************************************************************** * display * prints the game board with the loaded data from the given file ***********************************************************************/ void display(int board[][COLS]) { cout

} else if ((c + 1) % 9 == 0) { if (board[r][c] == 0) { cout

/********************************************************************** * interact * prompts the user for the game option desired and performs the * corresponding tasks. ***********************************************************************/ void interact(int board[][COLS]) { char option; displayOptions(); cout "; cin >> option; switch (option) { case '?': displayOptions(); cout

/********************************************************************** * display options * prints the game options and commands for the user ***********************************************************************/ void displayOptions() { cout

/********************************************************************** * get save filename * prompts the user for the location of the file the data will be * written to. ***********************************************************************/ void getSaveFilename(string & filename) { cout > filename; cin.ignore(); }

/********************************************************************** * write file * saves the game board data to a file in a nine by nine grid ***********************************************************************/ bool writeFile(string & filename, int board[][COLS]) { ofstream fout(filename.c_str()); if (fout.fail()) return false; for (int r = 0; r

/********************************************************************** * edit square * inserts a new value into an empty area of the game board ***********************************************************************/ void editSquare(int board[][COLS]) { string coords; int c = 0; int r = 0; getCoordinates(coords); c = toupper(coords[0]) - 'A'; r = (int)coords[1] - '1'; coords[0] = toupper(coords[0]); bool validInput = false; if (isValid(r, c, coords, board)) { int possible[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int value; cout > value; getPossibleValues(r, c, board, possible); for (int i = 0; i

/********************************************************************** * get coordinates * Prompts the user for the coordinates on the grid that they need ***********************************************************************/ void getCoordinates(string & coords) { cout > coords[0]; cin >> coords[1]; }

/********************************************************************** * is valid * checks if the coordinate being selected already contains a value ***********************************************************************/ bool isValid(int r, int c, string & coords, int board[][COLS]) {

if (board[r][c] != 0) { cout

/********************************************************************** * show possible values * prints the possible values for a specified coordinate of game board ***********************************************************************/ void showPossibleValues(int board[][COLS]) { string coords; getCoordinates(coords); int c = toupper(coords[0]) - 'A'; int r = (int)coords[1] - '1'; // make a list of possible inputs int possible[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; getPossibleValues(r, c, board, possible); coords[0] = toupper(coords[0]); // print all of the possible input values for the sqaure cout

/********************************************************************** * get possible values * calculates the possible values for a specified location ***********************************************************************/ void getPossibleValues(int r, int c, int board[][COLS], int possible[]) { // set left, right, top and bottom borders for the square coordinates int left = c / 3 * 3; int right = left + 2; int top = r / 3 * 3; int bottom = top + 2; // find already used values in the square for (int row = top; row Open a board from a file. The contents to the file is: 7 2 3 0 00 1 5 9 6 00 3 0 20 0 8 8 00 0 1000 2 0 70 6 5 4 0 2 0 0 0 4 2 0 7300 050 9 310 4 0 5 0 0 0 7 000 3 4 0 0 10 3 0 0 6 93 2 000 714 > Where is vour board located? /home/cs124/proiects/sudoku.txt > Options: ? Show these instructions D Display the board E Edit one square S Show the possible values for a square Q Save and Quit ABCDEFGHI > 1 7 2 3| > 2 6 3 8 1 5 9 8 1 > 4 7 |6 5 4|2 412 7 3 > 6 5 19 31| 4 7 5 8 99 3 2| 7 11 3 6 |7 1 4 The option'E' stands for Edit. We will prompt for a square to edit > What are the coordinates of the square: Error! We cannot edit a filled square A1 > ERROR: Square 'A1' is filled > What are the coordinates of the square: B2 Here B2 has a 0 in it so it can be edited > What is the value at 'B2': 9 Exp: Open a board from a file. The contents to the file is: 7 2 3 0 00 1 5 9 6 00 3 0 20 0 8 8 00 0 1000 2 0 70 6 5 4 0 2 0 0 0 4 2 0 7300 050 9 310 4 0 5 0 0 0 7 000 3 4 0 0 10 3 0 0 6 93 2 000 714 > Where is vour board located? /home/cs124/proiects/sudoku.txt > Options: ? Show these instructions D Display the board E Edit one square S Show the possible values for a square Q Save and Quit ABCDEFGHI > 1 7 2 3| > 2 6 3 8 1 5 9 8 1 > 4 7 |6 5 4|2 412 7 3 > 6 5 19 31| 4 7 5 8 99 3 2| 7 11 3 6 |7 1 4 The option'E' stands for Edit. We will prompt for a square to edit > What are the coordinates of the square: Error! We cannot edit a filled square A1 > ERROR: Square 'A1' is filled > What are the coordinates of the square: B2 Here B2 has a 0 in it so it can be edited > What is the value at 'B2': 9 Exp

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beyond Big Data Using Social MDM To Drive Deep Customer Insight

Authors: Martin Oberhofer, Eberhard Hechler

1st Edition

0133509796, 9780133509793

More Books

Students also viewed these Databases questions

Question

Explain all drawbacks of application procedure.

Answered: 1 week ago