Question: Please code this in MatLab. Solve a Sudoku game. Sudoku is a popular number placement puzzle game. The objective of the game is to fill

Please code this in MatLab. Solve a Sudoku game. Sudoku is a popular number placement puzzle game. The objective of the game is to fill a 4x4 grid with 4 sets of the numbers from 1 to 4 such that each number (from 1 to 4) occurs only once every row, column and 2x2 sub-block.In this exercise we will write a simple matlab function that receives a 4x4 matrix input representing an incomplete Sudoku game and returns the correctly solved board. There are several approaches for solving such a problem; we will implement a simple backtracking algorithm. The concept of backtracking involves guessing a certain solution which does not violate the game rules and proceeding until we reach a point where we cannot satisfy the rules. At this point we reverse our steps (backtrack) to the last known working solution and make a difference choice among the available possibilities until the entire game is solved. Download the file sudoku.mat from the class website and load it into your workspace. You will now have an example for an unsolved sudoku board in your workspace named unsolved board. Write a function with the declaration safe = checkSudoku (board, row, col, num) board is a 4x4 matrix which contains the Sudoku board to check. Unfilled board cells are indicated by a NaN value. row and col are index to the check if the number num (between 1 and 4) can be inserted without causing a violation. i.e. does num already exist in the given row, column or 2x2 sub-block. Note that we do not need to check the entire board's validity (although you could try to do so as practice) since we will be filling our board one element at a time. The function returns a logical true or false value in the output safe depending on the outcome of the check. There are several ways to accomplish this functionality. Possible functions you may want to use are sort, unique, is member, any, all. Note that NaN values are unequal to one another! Test your function with various inputs to see it is operating as expected!. For example: checkSudoku(unsolvedBoard,3,1,2) answer =0 Write a function with the declaration solvedBoard = solveSudoku(board). The input to this function will be an incomplete Sudoku board and the output will be the solved board. Below are hints and suggestions for steps to help you implement this function: i. Find the indices of all the missing values in the table and store them in the variable emptyInd (find, isnan) ii. Create a variable called ind which will indicate our current location within the emptyInd array iii. Write a loop that will traverse the emptyInd array. At each location check what number can be placed in that location using your function from part (b)(Which numbers do you need to check?). If its not valid, try a different number, if none work, replace it with a NaN, and go back to the previous empty index and try a different number than before. (Which loop type would be most suitable for this scenario?)(ind2sub, checkSudoku) d. Test your function with the given example unsolvedBoard and compare the result to the image shown above. e. Optional: Write a function displaySudoku(board) for displaying the Sudoku game board in a more human friendly form. See an example for such a display below. >> load sudoku >> displaySudoku(unsolvedBoard) Please code this in matlab and comment as you go. Assume that the file is loaded and just create the commands to accept the file as stated.
Please code this in MatLab. Solve a Sudoku game.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!