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

Need help with the following matlab problem. Please don\'t copy and paste some c

ID: 3857947 • Letter: N

Question

Need help with the following matlab problem. Please don't copy and paste some codes from the internet or give me some codes in other languages. ANSWER THE QUESTION IN YOU OWN WORK.

Please answer all of the following questions below, and aanswer them step by step, (a), (b), (c), (d), (e)....

I will rate the answer , Thank you.

2. Tic-tac-toe. Write a program to play tic-tac-toe. For now, the interface will be minimal: you wil ask the user for a number (a square) between 1 and 9, and pick one in return. You must make a legal move, and detect when one of you wins. Your gamestate data structure (the way you internally represent the board) should be a nine-element array with empty squares as zero, X (human player) spaces are marked with +1, O (computer) with -1. a. Write a function that asks the user for a move and makes sure it is legal. The input and output should be the gamestate. Show 3 test runs. b. Write a function that determines if either the computer or player has won (return 1 for player, 0 for no winner, -1 for computer). Show 3 test runs. Write a function that detects if any valid moves remain. Show 3 test runs. Write a function that supplies a computer guess. It can be random or sophisticated - your c. d. choice. Show 3 test runs. e. (optional) Write a script that initializes the board and uses a while loop to string together your functions until the game has been won or drawn. Play!

Explanation / Answer

a.


  
gamestate1 = [[0 0 0]
[0 0 0]
[0 0 0]];

gamestate2 = [[1 1 1]
[0 0 0]
[0 0 0]];

gamestate3 = [[-1 -1 -1]
[1 1 1]
[1 1 1]];


function state =play(state)

valid=false;
while (valid==false)
row = input("enter valid row ");
col=input("enter valid column");

if(row>=1 && row<=3 && col >=1 && col<=3)

if(state(row,col)==0)
state(row,col)=1;
valid=true;
end

end

end

end

disp(play(gamestate1))
disp(play(gamestate2))
disp(play(gamestate3))

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

b.


  
gamestate1 = [[0 0 0]
[0 0 0]
[0 0 0]];

gamestate2 = [[1 1 1]
[0 0 0]
[0 0 0]];

gamestate3 = [[-1 -1 -1]
[0 0 0]
[0 0 0]];

  
% The won function calculates if the current game state is in a winning
% state.
function won = won(state)
% Horizontal
  
if (state(1,1) == state(1,2) && state(1,1) == state(1,3) && state(1,1) ~= 0)
won = state(1,1);
elseif (state(2,1) == state(2,2) && state(2,1) == state(2,3) && state(2,1) ~= 0)
won = state(2,1);
elseif (state(3,1) == state(3,2) && state(3,1) == state(3,3) && state(3,1) ~= 0)
won = state(3,1);
% Vertical
elseif (state(1,1) == state(2,1) && state(1,1) == state(3,1) && state(3,1) ~= 0)
won = state(1,1);
elseif (state(1,2) == state(2,2) && state(1,2) == state(3,2) && state(1,2) ~= 0)
won = state(1,2);
elseif (state(1,3) == state(2,3) && state(1,3) == state(3,3) && state(1,3) ~= 0)
won = state(1,3);
% Diagonal
elseif (state(1,1) == state(2,2) && state(1,1) == state(3,3) && state(1,1) ~= 0)
won = state(1,1);
elseif (state(1,3) == state(2,2) && state(1,3) == state(3,1) && state(2,2) ~= 0)
won = state(1,3);
else
won=0;
  
end
end

disp(won(gamestate1))
disp(won(gamestate2))
disp(won(gamestate3))

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

c.


  
gamestate1 = [[0 0 0]
[0 0 0]
[0 0 0]];

gamestate2 = [[1 1 1]
[0 0 0]
[0 0 0]];

gamestate3 = [[-1 -1 -1]
[1 1 1]
[1 1 1]];


%returns 1 for true
%-1 for false
function out =validMoveRemaining(state)

out=-1;

for i = 1:size(state,1)
for j = 1:size(state,2)
if(state(i,j)==0)
out=1;
return
end
end
end   
  


end

disp(validMoveRemaining(gamestate1))
disp(validMoveRemaining(gamestate2))
disp(validMoveRemaining(gamestate3))

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

d.


  
gamestate1 = [[0 0 0]
[0 0 0]
[0 0 0]];

gamestate2 = [[1 1 1]
[0 0 0]
[0 0 0]];

gamestate3 = [[-1 -1 -1]
[1 1 1]
[1 1 1]];


function state =computerGuess(state)

valid=false;
while (valid==false)
A = 1; B = 3;
row = (A-1) + (B-(A-1))*rand(1);
row = floor(row) + 1;

col = (A-1) + (B-(A-1))*rand(1);
col = floor(col) + 1;


if(row>=1 && row<=3 && col >=1 && col<=3)

if(state(row,col)==0)
state(row,col)=-1;
valid=true;
end

end

end

end

disp(computerGuess(gamestate1))
disp(computerGuess(gamestate2))
disp(computerGuess(gamestate3))

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