You are on page 1of 5

Project 4: Sudoku

Write a program to allow the user to play Sudoku. For details on the rules of the game, see: http://en.wikipedia.org/wiki/Sudoku 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 ? in the field, 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 6 8 0 0 0 5 4 9 2 0 0 7 0 5 0 0 3 3 0 0 0 4 0 0 0 2 0 3 0 6 2 9 0 1 0 0 0 1 5 0 3 7 0 0 0 2 0 4 7 1 0 3 0 1 0 0 0 3 0 0 0 7 5 0 0 2 0 4 0 0 1 9 8 2 0 0 0 3 6 4

Note that 0 corresponds to an unknown value. The following is an example run of the program. Please see the following program for an example of how this works:
/home/cs124/prj4

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:

Procedural Programming in C++

Section 4: Advanced Topics

Project 4: Sudoku

Page 333

Section 4

1 2 3 4 5 6 7 8 9

A B C D E F G H I 7 2 3| |1 5 9 6 |3 2| 8 8 | 1 | 2 -----+-----+----7 |6 5 4| 2 4|2 7|3 5 |9 3 1| 4 -----+-----+----5 | 7 | 3 4 |1 3| 6 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 users command. If the user types an invalid command, then the following message will be displayed and the options will be redisplayed:
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

Section 4

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.

Page 334

Project 4: Sudoku

Section 4: Advanced Topics

Procedural Programming in C++

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.

Extra Credit
You can earn extra credit with the following:

"Robust" error checking. Every possible input or board configuration is handled. For example, the user could type e5 or 5E to specify a square. Also, the board is validated on open to ensure it is a valid board This means ensuring that there is no more than one instance of each value in a given row, column, or inside square. 10% Differentiate read-only values (the ones provided by the board that the user cannot change) and read/write values (the ones the user entered). Read-only text will be rendered in green. The following example outputs green text for the word "green"
cout << "This text is \E[22;32m green \E[0m but not the rest.\n";

To get 10% credit, you must preserve the number through a file save. Submit extra credit attempts to Project Extra instead of Project 4 and use cs124/extraCredit for the Test-Bed.

Alternative to the Final


You can earn 100% on the final exam if you do the above and add an option to solve any board. There is no test-bed for this option; please submit those attempts to Project Final. To get 85% on the final exam, you need to solve a simple board. To get 100%, you need to solve a much harder board. The boards to solve, solutions, and corresponding grades are: Board to solve
/home/cs124/prjFinal85.txt /home/cs124/prjFinal90.txt /home/cs124/prjFinal100.txt

Solution to board
/home/cs124/prjFinal85Solution.txt /home/cs124/prjFinal90Solution.txt /home/cs124/prjFinal100Solution.txt

Grade 85% 90% 100%

Procedural Programming in C++

Section 4: Advanced Topics

Project 4: Sudoku

Page 335

Section 4

Assignment: Design Document


The first part of the project is the design document. This consists of three parts:

1. Write the pseudocode for the function

computeValues. This function will take as parameters coordinates (row and column) for a given square on the board and calculate the possible values for the square. To do this, computeValues must be aware of all the rules of Sudoku. Make sure to include both the logic for the rules of the game (only one of each number on a row, column, and inside square), but also to display the values. 2. Write the pseudocode for the function interact. This function will prompt the user for his option (ex: D for Display the board or S for Show the possible values for a square) and call the appropriate function to carry out the users request. Note that the program will continue to play the game until the user has indicated he is finished (with the Q option). 3. Create a Structure Chart describing the entire Sudoku program

The grading criteria are: Exceptional 100%


computeValues

Good 90%
All the rules are correctly represented in the pseudocode All possible values will be listed for a given square on the board The function will allow the user to interact with the game

Acceptable 70%
A bug exists or not all the rules are enforced

Developing 50%

Missing 0%
No rule logic in the
computeValues

Rules are enforced 30%


computeValues:

The most efficient solution has been found

List possible values 10%


interact

Design is elegant and well thought out

20%

The most elegant solution has been found.

Structure Chart 40%

Design is elegant and well thought out

Design will solve the problem

Pseudocode insufficient quality to help in the design process A bug exists in the Pseudocode not algorithm or does detailed enough not solve all the to tell if the problems problem has been solved. The design fails to Pseudocode do one of the insufficient following: quality to help in Select an option the design process Execute the option specified Continue looping until the user is finished The Structure Structure chart Chart is not showing the incomplete or has connection a serious defect between the making the functions, name solution of the functions, unworkable. or the data passed between the functions

function

No display logic in the


computeValues

function No pseudocode for the interact function

Section 4
Page 336 |

No structure chart

You are required to attach the above rubric to your design document. Please self-grade.

Project 4: Sudoku

Section 4: Advanced Topics

Procedural Programming in C++

Assignment: Code
Please:

The Test-Bed is:


testBed cs124/project4 project4.cpp

Submit it with Project 4, Sudoku in the program header.

The grading criteria are: Exceptional 100% Modularization 20%


All modules at "functional cohesion" and loose coupling Errors in the file are perfectly handled Program is robust to user errors Code is elegant and well thought out All prompts, displays, and messages pass test bed Great variable names, no errors, great comments

Good 90%
One minor cohesion or coupling error

Acceptable 70%
One bug calling a function or unnecessary data passed between functions A board can be read from a file or written to a file correctly One edit is correctly handled

Developing 50%
Multiple functions exist in the project

Missing 0%
Only one function used

Files 20% Edits 10% Rules 20% Display 20% Style 10%

Able to roundtrip data through the file All edits are perfectly handled Able to enforce all the rules of the game

Some data is read to a file or read from a file Program is interactive Attempts were made to enforce game rules Data from the board is rendered on screen Misleading variable names or gross style errors

No functioning file reading code

No UI

No rule enforcement No display code

Procedural Programming in C++

Section 4: Advanced Topics

Project 4: Sudoku

Page 337

Section 4

Zero style checker errors

Looks correct on screen but there are cosmetic test bed errors A few style checker errors

Little effort was spent on style

You might also like