CS140 -- Lab 5/6 (Spring 2021)


Inspiration

Sudoku was a daily coffee break ritual when I was in graduate school in computer science and, historically, it has been used in the 140/302 sequence to stress recursion and/or brute force solutions. Here, I want you to implement two very basic strategies that will reinforce 2D vectors, bit vectors, and help move us more into a data structures/problem solving/algorithm focus in the second half of the semester.

Rules

A traditional Sudoku puzzle is either a 4x4, 9x9 or 16x16 grid. In the case of a 9x9 numeric puzzle, a solved puzzle has the following three properties:

Since you will have the puzzles, feel free (and encouraged!) to debug and compare your computational solution to what you may come up with using a pen and paper. The easy puzzle we will use for testing is below.

A Sudoku puzzle (From Will Shortz, Sudoku: Easy to Hard, St. Martin's Griffin, 2005).

Rubric

This lab will rely on solving two "core" Sudoku puzzles that are easy and less easy. Each puzzle roughly corresponds to the strategy we'll discuss in the lab write up below.

Due 3/3

Please upload onto canvas the following files:

  1. A simple text file (report.txt) with a brief time log spent on the assignment, and at least one item that you have a question on or ** if you are already solving the easy puzzle by 3/3 at ** one thing you are proud of. 1 pt per hour logged outside of your assigned lab periods (4 max, 1 per course credit)
  2. A tar file containing your code so we can run "make test-easy" and take a quick look where all of you are (aka a checkpoint)
If your submission solves the easy puzzle using one or both methods described below, you will get all four effort points plus an "early submission" bonus of 2 extra credit points no matter how many hours are logged above.

Recursive solutions will not be accepted! Code exists from other instances of CS140 @ UTK that can easily be co-opted -- but it will also be obvious to us as the lab specifically covers strategies to solve Sudoku as a human would vs. "brute forcing" it. You are welcome to look at recursive solutions if you were curious but they will not help you get a passing grade on this assignment. We'll revisit this in April when we discuss recursion this semester and compare it to the solution(s) from this lab.

Part 1: Implementing a simple but incomplete solver (start in lab but you will not finish in 50 mins)

You will not be required to use/make a custom C++ object unless you feel the need to.

A good goal for the first week is to read in a Sudoku puzzle from a file, start implementing the functions, and solve the "easy" puzzle that basically requires a mostly working (about 50%) scanning/elimination method described below.

This will require what amounts to a 3D vector of ints ** or ** a 2D vector of a class called "Cell" that has inside of it a vector of possibilities (just like the C4Board from Lab 3 had an array of columns).

Specifically, you should store in the extra vector which values are possible and eliminate possible values based on the current row/column/minigrid. If only a single value is left, fill the square with that value and restart the process.

Part 2: Singleton

To solve the medium and harder puzzles you need to also implement the "singleton" algorithm described here. For example, suppose that a puzzle has the row:

| 1 | A | 5 | 9 | 6 | 7 | 2 | B | C |

and that we know that A can only be 3, 4 or 8, B can only be 3 or 4, and C can only be 3 or 4 (based on other information). The value 8 is a singleton because it only appears in one cell, and therefore cell A must be an 8.

These two algorithms (single possibility and singleton) will be sufficient to solve all three puzzles used for grading. Note, however, that bugs in the code may still solve the medium but not the first sample hard puzzle. For the purposes of this lab solving a hard puzzle will result in a small amount of extra credit.

There are a few online sites with sample Sudoku puzzles and a online solver you can use for testing such as:

http://www.sudokuwiki.org/sudoku.htm

Part 3: Putting it all together

The final output out your solver should be the same as the input, with all 0s correctly replaced. Please send it to the screen using cout for grading.

BONUS: Your code will also be run the hard difficulty problem. If it produces the correct result without using recursion/brute force, you will receive 2 extra credit points (on top of any early submission bonus above)

Testing your code prior to submission

To faciliate testing, you were previously asked to clone the course Github repository as follows:

git clone https://github.com/semrich/CS140-21.git cs140

For this assignment, update this clone by using the following:

git pull

We'll discuss this in class but note that your client must be named "solution.cpp" and compilable using make. Given the flexibility in this assignment please include all source file(s) in your tar and edit/change a makefile if needed


Submission

Since a class is not required, you may have between 1-3 files. If you have a single .cpp file, use the following command:

tar -cvf lab5-6.tar solution.cpp
otherwise use this command/naming scheme:

tar -cvf lab5-6.tar Sudoku.h Sudoku.cpp main.cpp

Note: Although submission will be faciliated by Canvas, we will compile and test on EECS lab machines!

If you develop your solution elsewhere please make sure it works on the lab computers prior to the deadline.