Question: In this lab, you will write a program to play a game of Sudoku with the user. The goal of the game is to fill
In this lab, you will write a program to play a game of Sudoku with the user. The goal of the game is to fill in missing numbers in an 9x9 array. Each number in the set [1-9] must be found in every row, every column, and every 3x3 block in the array. For example, here is an example game, including initial game state and final game state. The user-provided solution is shown in red. Note that a Sudoku board could have multiple solutions! Thus, the solution provided with the initial puzzle may not be the only valid solution the user should provide.
The user should be able to set entries in the game board by providing input in the form row,column,value to solve a specific square, or enter -1 to exit the program.
When the user has filled in all blank spaces in the game board, the program should determine if the current board is a valid solution and inform the user accordingly. Note that a Sudoku board could have multiple solutions! The solution provided with the initial puzzle may not be the only valid solution the user could provide. Thus, you should not simply compare the user's input to the original solution. Instead, you need to test the user's solution against the Sudoku rules previously described.
Requirements: Makefile
You must create a Makefile for your program (with the name Makefile). I should be able to enter the directory containing your code and type "make" to trigger compilation. If the Makefile is broken, or if I need to type anything else (or dig into the source code to figure out how to compile your program), your program will be returned to you with no grade assigned.
Use the following GCC options in your Makefile to set the compiler to a very picky mode:
- -std=c99 (Use the more modern C99 standard for the C language)
- -Wall and -Wextra (Turn on all warnings and extra warnings. By viewing and fixing issues that generate warnings, you will produce better, safer, C code)
- Warning: Don't delete the existing -c flag flag in the Makefile example from the previous lab! (It is essential to Makefile operation)
Configure your Makefile so that the name of your program binary is sudoku
Makefile grading nodes:
- If your program produces any warning during compilation (with these options), 5 points will be deducted.
- If your program doesn't compile on my virtual machine (and the error is such that your code wouldn't compile on any of your classmate's machines either!), your program will be returned to you with no grade assigned.
Requirements: Memory Structures
Use a two-dimensional array of integers to store the current state of the puzzle. In this array, values of 1-9 represent user input, and a value of -1 represents a space with no current value. As the game is played, update this array based on user input.
You must use dynamic memory allocation to store this array. Remember to release dynamic memory before exiting! How can this be accomplished? In C, a two-dimensional array is simply an array of arrays, and you can allocate a single-dimensional array using malloc() or calloc(). This method is documented at the C FAQ site.
Tip: A demo program that uses dynamic memory allocation is provided for your reference. Note that it stores an array of integers, while you will want an array of characters. It can be compiled and run with the command: unix> gcc dynamic_2d_array.c -std=c99 -o dynamic_2d_array
Requirements: Other
- I will compile and test your program in a Linux virtual machine that meets the installation process followed at the beginning of the semester. Don't relay on anything that wouldn't be in this default installation!
- You must implement the program in C
- You cannot use global variables.
- You must use both header files (.h) and source files (.c) where appropriate
- You must create a C structure for a datatype called sudoku that is saved in a header file. The following information (at a minimum) should be contained in this structure
- Pointer to the array containing the game state
- Pointer to the array containing the game solution (if your implementation requires this)
- Any additional variables used to track the state of the game (Dimensions of the board? Number of spaces remaining to be filled?)
- You must use functions with struct sudoku parameters. The actual division of the program into functions is up to you.
- All source files must be commented, including your name and email address.
- Feel free to use fancier text editors or IDEs that auto-indent your code and provide other convenience features. Just note, however, that you still must have a Makefile that allows your project to be compiled by typing make at the command line! (i.e. I will not install any IDEs on my computer, and still should be able to compile your project).
- Geany (fancy code text editor) - install via sudo apt-get install geany
- CodeBlocks (cross-platform IDE) - install via sudo apt-get install codeblocks
Please use C and use the all the requirements. Please make the MakeFile
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
