Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Tic-Tac-Toe Validation: The input to this component is a 3x3 matrix represent

ID: 3750324 • Letter: 2

Question

2. Tic-Tac-Toe Validation: The input to this component is a 3x3 matrix representing a tic-tac-toe board. The matrix will be filled with both O's and 1's. Here 0's represents circles (Ó) and 1's epresents (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 toe Validator.m. For example, if the board configuration is hasaWinner (i.e. a person wins) then logical value of 1 is stored in the 'hasa The rules for a given matrix to be complete are: a. Any one of the rows or columns or a diagonal must contain all O's or all 1's. (The Winner' variable. 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

Explanation / Answer

Screenshot

-------------------------------------------------

Program

%random 3*3 matrix geneartion
A =[rand(1,3)>.3;rand(1,3)>.3;rand(1,3)>.3]
%First element of the matrix
val=A(1,1);
%Row wise check
for i=1:3
for j=1:3
if(val==A(i,j))
hasnext=1;
else
hasnext=0;
break;
end
end
end
%Check winn conditon
if(hasnext==1)
disp('You Won')
exit(0)
%column wise check
else
for i=1:3
for j=1:3
if(val==A(j,i))
hasnext=1;
else
hasnext=0;
break;
end
end
end
end
%Win check
if(hasnext==1)
disp('You Won')
exit(0)
%Diagonal check
else
val1=A(1,3);
if(val==A(2,2) & val==A(3,3))
hasnext=1;
elseif(val1==A(2,2) & val1==A(3,1))
hasnext=1;
else
hasnext=0;
end
end
%Winner check
if(hasnext==1)
disp('You Won')
exit(0)
else
disp('You Lost')
exit(0)
end

-------------------------------------------------

Output

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote