### Matlab代写 | CSCE 155N Sudoku Validation

这是一个基础的Matlab数独游戏作业，难度初级，对Matlab的学习和理解有一定帮助。

Objectives

The objectives of this homework assignment:

1. Gain exposure to MATLAB syntax, programming styles and matrix operations.

2. Gain exposure to console I/O in MATLAB.

3. Understand the usage of functions and the design of algorithms.

4. Become familiar with different matrix and vector operations and several built-in

functions.

5. Become familiar with code documentation and execution.

Problem Description

This project involves validating the final state of 3 different games using the GamesTest.m file.

A player can choose one of the 3 games and verify its validity. The 3 games involved in this

project are 1. Sudoku, 2. Tic-Tac-Toe and 3. Chess.

The final states or a board configuration is provided as input to the component and the program

needs to check the validity of the board configuration and return a logical value indicating the

specified condition.

1. Sudoku Validation.

The input to this component is a 4×4 matrix representing a completed 4×4 Sudoku. The

input is provided in the variable ‘matrix’. The programmer needs to verify the validity of

the configuration and store the logical answer in the ‘valid’ variable. For example, If the

board configuration is valid (i.e. satisfies the rules of Sudoku) then logical value of 1 is

stored in the valid variable.

The rules for a given matrix to be a valid solution are:

a. Every row of the matrix must consist of numbers from 1 to 4. (The given input will

consist of 4 rows)

b. Every column of the matrix must consist of numbers from 1 to 4. (The given input

will consist of 4 columns)

c. Every sub-matrix of size 2×2 must consist of numbers from 1 to 4. (The given input

will consist of 4 2×2 sub-matrices).

If a given matrix satisfies all of the above conditions, then the variable ‘valid’ is set to

logical value 1. Otherwise, it is set to logical value 0.

Note: The input values will be provided at random by the test function and the

webgrader. You should modify the sudokuValidator.m file only.

Hints:

Think about:

1. How can you verify if an array contains numbers 1 to 4 in any order? (sort function in

MATLAB rearranges the elements in an array in ascending order).

2. How can you compare two arrays?

3. How can you combine results from multiple comparisons (results from each row,

column and sub-matrices)? (logical operators like or, and)

4. How can you access each row, column in a matrix?

5. How can you access a sub-matrix in a matrix?

6. Some helpful functions are sort, ‘==’, ‘|’, ‘&’, ‘all’ and indexing for matrices.

7. Sort ([1 3 4 2]) == [1 2 3 4] gives a logical array of all 1’s. So, how can you use this?

This component can be tested by selecting choice #1 after executing the GamesTest.m

file.

Examples:

2 3 1 4

4 1 3 2

3 2 4 1

1 4 2 3

The matrix shown above is a valid solution because all the rules are satisfied. You need

to verify the validity of each row, column and sub-matrix to obtain the final validation.

1 2 3 4

4 3 2 1

2 3 1 4

1 4 2 3

The matrix shown above is invalid because it violates the 2nd rule.

2. Tic-Tac-Toe Validation:

The input to this component is a 3×3 matrix representing a tic-tac-toe board. The matrix

will be filled with both 0’s and 1’s. Here 0’s represents circles (Ó) and 1’s represents (X)

on an actual board. The programmer needs to verify whether the game ended or not and

store the logical value in the ‘hasaWinner’ variable in the file tic_tac_toeValidator.m. For

example, if the board configuration is hasaWinner (i.e. a person wins) then logical value

of 1 is stored in the ‘hasaWinner’ variable.

The rules for a given matrix to be complete are:

a. Any one of the rows or columns or a diagonal must contain all 0’s or all 1’s. (The

given input will consist of 3 rows, 3 columns and 2 diagonals).

If a given matrix satisfies any of the above conditions then the variable ‘hasaWinner’ is

set to logical value 1. Otherwise, it is set to logical value 0.

Note: The input values will be provided at random by the test function and the

webgrader. You should modify the tic-tac-toeValidator.m file only.

Hints:

Think about:

1. How can you access a row of a matrix?

2. How can you access a column of a matrix?

3. How can you access a diagonal of a matrix? (diag function in matlab)

4. How can you access the 2nd diagonal of a matrix (which is in the opposite direction)?

(fliplr or flipud are some functions in matlab which can be used to flip a matrix)

5. Which logical operator should you use to combine multiple results from multiple

rows and columns and diagonals? (‘|’, ‘&’)

This component can be tested by selecting choice #2 after executing the GamesTest.m

file.

Examples:

1 0 0

0 1 0

1 1 0

The matrix shown above has a winner because the last column has all 0’s.

0 1 1

1 1 0

1 0 0

The matrix shown above also has a winner because the reverse diagonal has all 1’s.

0 1 0

1 0 1

1 0 1

The matrix shown above does not have a winner because it doesn’t satisfy the rule.

3. Do the Queens attack each other?

The input to this component will be the coordinates of 2 queens on a chess board. The

coordinates will be provided as an array of 2 elements representing the x and y

coordinates on a chess board (x,y ranging from 1 to 8). The programmer needs to verify

whether the two queens can attack each other or not and store the result in the ‘attack

variable’. For example, If the queens can attack each other, the value of logical 1 is stored

in the attack variable. Pos1 and pos2 are the input provided to the chessValidator.m file.

For example, a vector [2,4] represents that a queen piece is present in the 2nd row, 4th

column position on the chess board.

The conditions where 2 queens can attack each other are:

a. If they are in the same horizontal row on a chess board. (Which element of a

coordinate represents the row?)

b. If they are in the same vertical column on a chess board. (Which element of a

coordinate represents the column?)

c. If they are on any diagonal of a chess board. (How can you know if two coordinate

values are diagonal to each other?)

If the given coordinates satisfy any of the above conditions then the variable ‘attack is set

to logical value 1. Otherwise, it is set to logical value 0.

Note: The input values will be provided at random by the test function and the

webgrader. You should modify the chessValidator.m file only.

Hints:

Think about:

1. How can you access the elements from a vector?

2. How can you compare the x coordinates of 2 positions?

3. How can you verify if two queens lie on a diagonal?

4. How can you combine results from the multiple conditions? (Which logical operator

should you use?)

This component can be tested by selecting choice #3 after executing the GamesTest.m

file.

Examples:

Pos1 = [2 4]

Pos2 = [3 5]

The queens at these positions can attack each other because they lie on a diagonal.

Pos1 = [1 3]

Pos2 = [6 5]

The queens at these positions cannot attack each other.