1. Homepage
  2. Programming
  3. Project: Single-player Sudoku

Project: Single-player Sudoku

Engage in a Conversation
Single-player SudukuC

 1 Introduction CourseNana.COM

Sudoku is a number-placement logic-based puzzle. The objective is to fil a 9×9 grid with digits ,1 2, .,9. Each row, column, and nine 3 x 3 neighboring subgrids forming the 9 × 9 grid must c o m p o s e all digits 1 to 9 without repetition. A Sudoku puzzle is a grid partially filled with clues. Figure 1a and 1b shows a puzzle with 24 clues and its solution. In this project, the given puzzles are assumed to be proper, i.e., puzzles that have one unique solution. CourseNana.COM

5 634 7 CourseNana.COM

5 18 3 117 CourseNana.COM

69 439 CourseNana.COM

2 CourseNana.COM

563 CourseNana.COM

3 2 76 CourseNana.COM

5 9 8 CourseNana.COM

(b) The only valid solution for Figure 1a. CourseNana.COM

13 12 6 4| [5 CourseNana.COM

(a) Sudoku puzzle with 24 clues. CourseNana.COM

Figure 1: A Sudoku Puzzle CourseNana.COM

In this project, you are going to make a single-player Sudoku Game in C programming language with additional features. CourseNana.COM

You are required to complete the given source file main.c without modifying any existing code or introducing new libraries. Marks will be deducted for each modification. CourseNana.COM

2 Project Part :1 the Barebone (60%) CourseNana.COM

In Project Part 1, your program should hold a 9×9 Sudoku game with the following rules: CourseNana.COM

1. Start with a 9×9 game board filled with clues (a puzzle is given). CourseNana.COM

2. In a finished puzzle, each row, column, and the 9 neighboring 3×3 grids must contain num- CourseNana.COM

bers 1,. CourseNana.COM

. , 9 without repetition. CourseNana.COM

3. The player wins the game fi Condition 2 is met for the current game board. CourseNana.COM

• In Easy mode, the player has an unlimited number of trialsand is told whether the guess is correct. CourseNana.COM

• In Hard mode, the player loses when it has inputted a number violating Condition 2 for 3times or causing a board locking situation (Project Part 2). CourseNana.COM

The program will: CourseNana.COM

1. allow the player to select Easy or Hard mode. CourseNana.COM

2. read the puzzle & solution from source: CourseNana.COM

• The puzzle is assumed to have only a unique solution (you do not need to check they have a unique solution). CourseNana.COM

• The solution is assumed to be valid for the corresponding puzzle. CourseNana.COM

3. display the Sudoku puzzle. CourseNana.COM

4. read the player’s input and update the game board accordingly: CourseNana.COM

• The player will select a cell by inputting (row, column). • The selected cell will display an ‘X’. CourseNana.COM

5. tell the player if it inputs a wrong number (comparing to the solution) in Easy mode. In Hard mode, the program: CourseNana.COM

• will store the number of chances left (starting at 3). CourseNana.COM

• will only check if the number collides with other numbers in that row, column, and subgrid. If so, it consumes one chances, CourseNana.COM

• does not support edits (and eventually loses) if an incorrect number is inputted. CourseNana.COM

6. help fill a cell in the Easy mode. CourseNana.COM

7. check if the player wins or (in Hard mode) loses. CourseNana.COM

You must start with the template code provided, and only modify the designated areas. CourseNana.COM

2 CourseNana.COM


CourseNana.COM

3 Program Flow CourseNana.COM

The flowchart of the game running in easy mode is provided in Figure 2. Easy Mode Start CourseNana.COM

   4 Detailed Design CourseNana.COM

Figure 2: A flowchart depicting the easy mode. CourseNana.COM

The player’s inputs are underscored in the sample outputs. 4.1 Header Files and Libraries CourseNana.COM

No new header file or library should be added. You should not modify the function parameters. CourseNana.COM

4.2 Reading Game Mode and Puzzle (12%) CourseNana.COM

The main() function starts with game mode selection. In this stage, the player first enters the difficulty level: CourseNana.COM

• If the choice is 0, the game runs in Easy mode. CourseNana.COM

• If the choice is 1, the game runs in Hard mode. CourseNana.COM

• Else1, the program prints "Invalid Input." and starts over the game mode selection. CourseNana.COM

The program reads from the source the puzzle and solution. The puzzle and solution are initialized as two 2D arrays: CourseNana.COM

• myPuzzle is a 9⇥9 integer array storing the clues of the given puzzle. CourseNana.COM

• mySolution is a 9⇥9 integer array storing the puzzle’s solution (the completed puzzle). CourseNana.COM

Format of Puzzle and Solution CourseNana.COM

                                                                                                                                                                                                                                             Code 1: The board starts counting from top-left (0, 0) to bottom-right (8, 8) CourseNana.COM

A puzzle and its solution are two 2D integer arrays of 81 integers corresponding to the 9 ⇥ 9 cells CourseNana.COM

arranged from left to right, top to bottom. CourseNana.COM

• Integer 1 to 9 represents a clue and a 0 represents an empty cell to be filled by the player. • For example (the puzzle in Code 1): CourseNana.COM

• In both game modes, the same puzzle and solution are used. CourseNana.COM

You need to complete initGameBoard() to initialize the board with the selected puzzle. CourseNana.COM

1If the player inputs a character/string, scanf() would act “strangely” (why?). You may assume the player only inputs integer(s) separated by space and end with newline ‘\n’. CourseNana.COM

4.3 Display Sudoku Puzzle (16%) CourseNana.COM

The program will initialize a 9 ⇥ 9 grid (or the board) following the format (calling initGame- Board()), using the puzzle stored in the source. Then, the board will be printed by invoking printGameBoard() with the following format (Code 1): CourseNana.COM

4.4 CourseNana.COM

• The board is displayed with row and column numbers. CourseNana.COM

• The given clues are displayed. CourseNana.COM

• The 3 ⇥ 3 subgrids are separated by borders. CourseNana.COM

• The unfilled cells (storing 0) are displayed with the space character ‘ ’. CourseNana.COM

• The selected cell that stores a specific integer (if exists) is displayed with the character ‘X’. CourseNana.COM

The coordinate of a cell is (x, y) where x is the row number and y is the column number. Selecting a cell and inputs (12%) CourseNana.COM

After a puzzle is selected and displayed, the player inputs row and column number to select a cell. The selected cell is displayed with a character ‘X’. CourseNana.COM

Code 2: The player selected cell (1,1) displays an ‘X’. The program will check if the input is valid1, if it is invalid, it prompts CourseNana.COM

The program will keep asking for the player’s selection if the selected cell is occupied or the input format is invalid (e.g., out of range): CourseNana.COM

• In case of occupied cell, "Occupied." is printed. CourseNana.COM

• In case of invalid output, "Out of bound. Input Again." is printed. CourseNana.COM

You need to complete the program segment (in main()) to accomplish the cell selection task. 5 CourseNana.COM

4.5 Checking the Number (16%) CourseNana.COM

Having the cell selected (row and column index), the program then calls a function inputBoard() to read an input from the player and updates the grid: CourseNana.COM

1. The line “Input a number [or H: hint]: ” will be displayed and the program will read a character CourseNana.COM

input by the player. (You may assume that the player only inputs one character followed by ‘\n’.) CourseNana.COM

2. The program should check if the input is valid: either ‘1’ to ‘9’ or ‘H’ for hint. It goes back to Cell Selection (Section 4.4) for any invalid input: CourseNana.COM

• if the input is not ‘1’ to ‘9’ or ‘H’, the program outputs "Invalid Input." CourseNana.COM

• if ’H’ is inputted and the game is in Hard mode, the program outputs "No hint in Hard mode.". CourseNana.COM

3. The function updates the game board if ‘1’ to ‘9’ or ‘H’ is inputted. CourseNana.COM

4. The function returns 1 if a hint is used; -1 if the input is invalid; and 0 if the character is in ‘1’ to ‘9’. You should make use of the return value for the control flow. CourseNana.COM

In Easy mode, the inputted number will be checked against the solution (with if-else statement in main()). The program will tell the player and goes back to cell selection if it has inputted a wrong number. The program will fill in the solution for the player after a correct number or ‘H’ is inputted. It will also count the number of hint used. CourseNana.COM

Sample Outputs CourseNana.COM

Assuming the player selected (1,1), e.g., continues from Code 2, and 7 is inputted to the cell: CourseNana.COM

and when the correct answer 3 (or ‘H’) is inputted to the cell (1,1): CourseNana.COM

For an invalid input entered by the player after it has chosen cell (1, 1):
CourseNana.COM

(Programmer’s) Hard Mode CourseNana.COM

In Hard mode, the program will not check the input number against the solution. Instead, it only prompts whether there is a collision with the numbers in the same row, column, or that subgrid (via checkSolutionDetail()). The player is given 3 chances for inputting such number. It is possible that the player’s input does not cause a collision, but is in fact an incorrect solution2. CourseNana.COM

Depending on the three cases of collision, the program prints the following in order: CourseNana.COM

• "Check again the numbers in row x." if there is a repeated number at the same row, CourseNana.COM

• "Check again the numbers in column y." if there is a repeated number at the same column, CourseNana.COM

• "Check again the numbers in the subgrid where (x,y) is at." if there is a repeated number at the same subgrid, CourseNana.COM

where the underlined x and y are the row and column index for the selected cell. The program deducts the chances if and only if the inputted number causes a collision. CourseNana.COM

2Since we do not allow the player to edit filled cells, the player will eventually lose the game for a single wrong guess. CourseNana.COM

When 3 wrong guesses are inputted, the player loses the game. The program prints "You lose." Here is a sample output:
CourseNana.COM

4.6 Check Finished Puzzle (4%) CourseNana.COM

After the player fills a cell, the program will call checkFinish() to check if the puzzle is finished. If the puzzle is unfinished, the program will start again from displaying the board, i.e., goes back to “Display the Board (4.3)” in the flowchart (Figure 2). CourseNana.COM

If the player has finished the puzzle, the program prints: CourseNana.COM

• "Congratulations! You have finished a puzzle in easy mode and used 5 hints." for a player winning the easy mode with the number of hints used. CourseNana.COM

• "Congratulations! You have finished a puzzle in hard mode with 3 chances left." for a player winning the hard mode with the number of chances left. CourseNana.COM

5 Additional Features (40%) CourseNana.COM

In Project Part 2, your task is to enrich your Sudoku Game with the following features. Make a copy of your course code for Part 1. You need to submit another source code for Part 2. CourseNana.COM

1. Read puzzle from file (15%). CourseNana.COM

• The program reads from one text file, puzzle.txt, storing a puzzle and its solution. (The file is assumed to have valid format.) CourseNana.COM

– The format of the text file is as follows: CourseNana.COM

– Each line (ends with ‘\n’) contains 9 integers separated by space corresponding to a row in the 9 ⇥ 9 grid. CourseNana.COM

– The first 9 lines are the clues for the puzzle. CourseNana.COM

– Line 10 is an “empty” line. CourseNana.COM

– The next 9 lines are the finished puzzle (solution). CourseNana.COM

• The program reads puzzle.txt after the player has chosen the game mode. (This modi- fication does not change the program outputs.) CourseNana.COM

2. Save and load one unfinished puzzle since hard puzzles often take time to finish. (15%) CourseNana.COM

• During cell input after cell selection (see Section 4.5), the player can input ‘S’ to save the current game mode, puzzle, and solution to saveGame.txt. The program will exit after saving the game: CourseNana.COM

– The selected cell needs not to be saved. CourseNana.COM

– You need to determine what to save to saveGame.txt. CourseNana.COM

– You should modify inputBoard() to return another value representing game saving. CourseNana.COM

• Complete savePuzzle() that saves the game board and solution to saveGame.txt – It prints “Game Saved.” and exits the program. CourseNana.COM

• The program asks the player whether it shall load the saved game saveGame.txt before the game mode selection. CourseNana.COM

– Your program will print “Load the saved game (y/n)?” and reads a character from the player. CourseNana.COM

– The player inputs3 ‘y’ to load a saved game (or ‘n’ to not load the saved game). 3You may assume the player inputs one character and need to apply input check. CourseNana.COM

– The program proceeds to game mode selection, and reads from puzzle.txt if the input is ‘n’ (read from saveGame.txt if ‘y’). CourseNana.COM

• Complete loadPuzzle() that takes as input the game board to load the puzzle in the file to the game board. CourseNana.COM

3. A locked board checker added to Hard mode. (10%) CourseNana.COM

• In Hard mode, the program checks if the board is locked, i.e., there exists a cell in which no valid number can be inputted after a cell is filled. You need to complete the function isLockBoard() which outputs 1 for a locked board: CourseNana.COM

– The function checks for each non-empty cell that the set of possible answers is not empty. CourseNana.COM

– Inside isLockBoard(), it utilizes a function checkFillable() that finds the set of possible answers for the cell (given the coordinate (x,y)) in the game board. CourseNana.COM

– checkFillable() returns 0 if the set of possible answer is empty, i.e., cell (x, y) is a (locked) cell that cannot be filled, and returns 1 otherwise. CourseNana.COM

– isLockBoard() returns 1 if there exists a locked cell, and 0 if the board is alive. CourseNana.COM

• After filling each cell, the program should call isLockBoard() to determine if the player loses the game. It outputs “Cell (x, y) is locked.”, “Board is locked.”, and the losing message if the board contains a locked cell. CourseNana.COM

Sample Run CourseNana.COM

5 sample runs are provided (3 for Project Part 1 and 2 for the locked block checker). 5 sample input files are also provided. You are recommended to design your own test cases to debug the program. CourseNana.COM

7 Describe Your Approach CourseNana.COM

Please also write in comments to describe your approach/code for the following: • Board checking for checkSolutionDetail(). CourseNana.COM

(a) How do you check collision in row and column? CourseNana.COM

(b) How do you check collision for the 3⇥3 subgrid for the selected cell? CourseNana.COM

• Implementation of the save/load function. CourseNana.COM

(a) How do you save the puzzle as a file, e.g., the format? CourseNana.COM

(b) Where (and how) do you add the load option? CourseNana.COM

(c) Can you use loadPuzzle() to read puzzle from other text files? Describe how or why not. CourseNana.COM

• Locked Block Checker. CourseNana.COM

(a) How do you find the set of possible numbers for an empty cell in checkFillable()? (b) How do you make use of the function checkFillable()? CourseNana.COM

• Any extra features you have implemented. CourseNana.COM

Marks will be deducted for each item missed in the report. CourseNana.COM


Get in Touch with Our Experts

WeChat WeChat
Whatsapp WhatsApp
Single-player Suduku代写,C代写,Single-player Suduku代编,C代编,Single-player Suduku代考,C代考,Single-player Sudukuhelp,Chelp,Single-player Suduku作业代写,C作业代写,Single-player Suduku编程代写,C编程代写,Single-player Sudukuprogramming help,Cprogramming help,Single-player Sudukuassignment help,Cassignment help,Single-player Sudukusolution,Csolution,