View the step-by-step solution to:

For your last assignment in IPC144 this semester, you are to write the code for a C program that will programmatically generate and print (as plain

For your last assignment in IPC144 this semester, you are to write
the code for a C program that will programmatically generate and
print (as plain text) random boards from the popular game Sudoku.

What is Sudoku?
Sudoku is a puzzle with a grid containing nine 3x3 blocks.
Each block is divided into its own matrix of nine cells, so there
are a total of 81 cells (numbers) in the entire puzzle.
For more information, you may visit the Wikipedia page:
http://en.wikipedia.org/wiki/Sudoku

The rules for solving Sudoku puzzles are very simple:
Each row, column and block must contain one of the numbers from 1 to 9.
No number may appear more than once in any row, column, or block, so when
you generate game boards you will have to follow these requirements.

Below are examples of a randomly generated and fully complete game board
as well as a game board that is ready for printing.


complete board printable board
+-----+-----+-----+ +-----+-----+-----+
|5 3 4|6 7 8|9 1 2| |5 3 | 7 |9 |
|6 7 2|1 9 5|3 4 8| |6 |1 9 5| 4 8|
|1 9 8|3 4 2|5 6 7| | 9 8| | 6 |
+-----+-----+-----+ +-----+-----+-----+
|8 5 9|7 6 1|4 2 3| |8 | 6 | 2 3|
|4 2 6|8 5 3|7 9 1| | |8 3| 1|
|7 1 3|9 2 4|8 5 6| |7 1 3| 2 | 6|
+-----+-----+-----+ +-----+-----+-----+
|9 6 1|5 3 7|2 8 4| | 6 1| 3 |2 4|
|2 8 7|4 1 9|6 3 5| |2 |4 | |
|3 4 5|2 8 6|1 7 9| | 4 |2 8 |1 9|
+-----+-----+-----+ +-----+-----+-----+

You will use an 81 element array to store all of the numbers
in the game board. So, given the completed board above, your array
would contain the following values:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ... 77 78 79 80
+-+-+-+-+-+-+-+-+-+-+--+--+--+--+--+--+--+--+--+ +--+--+--+--+
|5|3|4|6|7|8|9|1|2|6|7 |2 |1 |9 |5 |3 |4 |8 |1 | |6 |1 |7 |9 |
+-+-+-+-+-+-+-+-+-+-+--+--+--+--+--+--+--+--+--+ +--+--+--+--+

NOTE:
The array indices for the top left 3x3 block are:
0, 1, 2, 9, 10, 11, 18, 19, 20

The array indices for the top middle 3x3 block are:
3, 4, 5, 12, 13, 14, 21, 22, 23

The array indices for the top right 3x3 block are:
6, 7, 8, 15, 16, 17, 24, 25, 26

Notice how the values of each group of 3 increase by 9 on
each subsequent row!

The array indices for the first column are:
0, 9, 18, 27, 36, 45, 54, 63, 72

The array indices for the second column:
1, 10, 19, 28, 37, 46, 55, 64, 73



As part of your solution, you are required to write the following 3
functions:

void genSudokuNumbers(int grid[ ])
This function fills the array 'grid' with 81 numbers (generated randomly)
according to the Sudoku game rules (see above).
Recall, to generate random numbers in C, you must use the following
functions (prototyped in the <time.h> and <stdlib.h> libraries):
srand(time(NULL)); /* to be called only ONCE */
rand( ) % n + 1; /* generates a random number from 1 to n inclusive */
UPDATE (March 24, 2010): You are no longer required to code this function
it has been coded for you!
Please see the assign3_primer.c file link in the assignments or announcements link.

void genSudokuBoard(int grid[ ], int display[ ])
This function uses the 81 element 'grid' array (containing a fully
completed list of valid Sudoku values) and randomly copies 4 numbers
from each 3x3 block into the corresponding positions in the 81
element 'display' array. The random numbers chosen will be by array
position (index), so for example 4 random array elements from the
top left 3x3 block might be:
entire 3x3 block: 0, 1, 2, 9, 10, 11, 18, 19, 20
4 random indexes: 2, 10, 11, 19

void displaySudokuBoard(int display[ ])
This function uses the 81 element 'display' array (containing a partial
list of valid Sudoku values; 4 values per 3x3 block) and then displays
the entire Sudoku board. You are also required to display the border
consisting of '-' and '+' symbols (see below for an example).

PLAY IPC144 SUDOKU
+-----+-----+-----+
|5 3 | 7 |9 |
|6 |1 9 5| 4 8|
| 9 8| | 6 |
+-----+-----+-----+
|8 | 6 | 2 3|
| |8 3| 1|
|7 1 3| 2 | 6|
+-----+-----+-----+
| 6 1| 3 |2 4|
|2 |4 | |
| 4 |2 8 |1 9|
+-----+-----+-----+

void getRandNum(int low, int high, int *pn)
This function generates a random number from 'low' to 'high' and
then updates the pointer 'pn' with that value.


You may use (if you wish) the already coded checkDups( ) function
(coded below) as part of your solution.

/* given an array of 'n' elements, this function determines if the value 'num'
is already present within the array 'nums' and if so, sends back a true value (1)
or false (0) otherwise */

int checkDups(int nums[ ], int num, int size) {
int i, rv = 0; /* 0 is false in C */
for(i=0; i<size; i++) {
if(n[i] == num) {
rv = 1;
}
}
return rv;
}

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question