600.108 Intro Programming Lab, Week 7, Fall 2017

Getting Started: Hopefully you will remember who your partner is after missing a week! Get together again this week, and start in opposite driver/navigator roles. Start with a quiz and short review as usual. Also feel free to ask your lab leaders any questions you may have about your midterm.

This week you will be working with arrays - 1 and 2-dimensional, and also continuing to create multiple subroutines to break a problem into smaller pieces. Part of your task will be to design a subroutine breakdown. Then you will incrementally implement methods that we have specified for you in order to put a solution together in a well-structured way. Remember that your solution to each step must be reviewed by a lab leader before moving to the next one.

The Problem: Write a program that is a Sudoku solution checker!

Specifically, the program must read a file containing numbers in a 9x9 grid (9 integers per row, 9 rows), and then check to see if the numbers are a valid solution to a sudoku puzzle. A valid solution contains each of the numbers 1 through 9 in every row, column and 3x3 sub-grid. Get user input for the filename, and display the valid or invalid result on the screen. If the grid is invalid, give at least one reason why (row number, column number or cube number). (In the posted examples, we give all reasons.) When displaying invalid rows, columns or cubes, refer to them with the numbers 1-9 (not 0-8) because that will make more sense to the user. Assume the numbers in the input file are separated by spaces. You can find a bunch of puzzles on various websites. A nice one that includes solutions (which you can use to test your program) is www.dailysudoku.com

We have posted two sample input files: sudoku1.txt and sudoku2.txt. In addition to the input, the results of checking each grid are in each file as well. (Just ignore those lines when reading in the files.)

Step 1) Problem Analysis & Design - Think about how you will need to solve this problem. Basically, you have to do the same kind of checking for each row, column and sub-cube. You should do a top-down design that has several methods to help with processing, and a main program to control things. Try to avoid using global variables. Read through all the phases below for some ideas on how to break things down. Sketch the structure of this solution by writing java headers for all the methods you will use, include dummy return values where needed to make it compile. Don't write javadoc comments yet, since you might change the structure of these initial methods somewhat as lab proceeds.

--- Switch driver/nagivator roles ---

Step 2) Coding: Create a main method which should handle the input processing details. Make it so that it will get the name of an input file from the user, create the puzzle array, read from the file to initialize the puzzle, and then display the puzzle on the screen. The last two operations should call other methods to do the work. Write the bodies of these methods. Add javadoc comments. Compile, checkstyle, run and test.

--- Switch driver/nagivator roles ---

Step 3) Coding: Next, create a method which has a one dimensional array of 9 integers as a parameter and checks whether it contains each value 1 through 9 exactly once. Now write a method to check all rows of your puzzle by calling this check method on each row in the puzzle, displaying the numbers of any invalid ones. Now from main, call the method which checks all rows on your puzzle array. Add javadoc comments to your new methods, compile, checkstyle, run and test.

--- Switch driver/nagivator roles ---

Step 4) More Coding: Now figure out how to use the static array check method to deal with a column instead. Once you have that worked out, add another method to use it on every column. Call this new method from main on your puzzle array. (Hint: copy each column into a 1 dimensional array.) Add javadoc comments for this new method, compile, checkstyle, run and test.

--- Switch driver/nagivator roles ---

Step 5) Final Coding: Lastly, figure out how to similarly process each subcube. You might want to refer back to assignment 2 Part C (Formulas.java) or see below for some help with subcube numbering and processing. If you have enough time time, combine the calls to check the rows, columns and subcubes from main into one validate method. Replace the three separate method calls in main with one validate call. Add javadoc comments for any new methods, compile, checkstyle, run and test.

--- Switch driver/nagivator roles ---

Step 6) CLEAN-UP: Zip all the files together from today's lab and email to both students. Then each student must submit their work on Blackboard for the week, noting their partner's name and JHED login in the submission textbox. You must then delete your files off the lab computer and logout before leaving! Failure to do so constitutes an ethics violation.

      Cube Numbers in puzzle:
            x x x  x x x  x x x          
            x 0 x  x 1 x  x 2 x 
            x x x  x x x  x x x 
 
            x x x  x x x  x x x 
            x 3 x  x 4 x  x 5 x 
            x x x  x x x  x x x 
 
            x x x  x x x  x x x 
            x 6 x  x 7 x  x 8 x 
            x x x  x x x  x x x     
 
      Starting rows:
            0 for cubes 0, 1, 2 
            3 for cubes 3, 4, 5 
            6 for cubes 6, 7, 8 

      Starting col:   
            0 for cubes 0, 3, 6 
            3 for cubes 1, 4, 7 
            6 for cubes 2, 5, 8