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

Matlab program, need help (Write sepreate scripts to answer all of the following

ID: 3858017 • Letter: M

Question

Matlab program, need help (Write sepreate scripts to answer all of the following questions.

Tic-tac-toe. Write a program to play tic-tac-toe. For now, the interface will be minimal: you will 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. c. Write a function that detects if any valid moves remain. Show 3 test runs. d. Write a function that supplies a computer guess. It can be random or sophisticated - your 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)

function [] = tictactoeGame()
% Setup UI
close all
figure('Name','Tic Tac Toe');
plot(-1. -1)
axis([0 3 0 3])
set(gca,'x_Tick',0:3)
set(gca,'y_Tick',0:3)
set(gca,'xLabel','')
set(gca,'yLabel','')
xlabel('Player: X')
grid on
shg
  
isHuman = 1; % tracking curr user
_state = [[-1 -1 -1]
[-1 -1 -1]
[-1 -1 -1]];
winner = -1;
  
% reading user or computer inputs
while winner == -1
next = playRound(isHuman, _state);
if next == -1 % if chosen wrong choice, then ask again to fill proper choice
title('Please provide correct details');
else
_state = next;
title('');
isHuman = mod(isHuman + 1,2); % roll up for next player
if isHuman == 1
xlabel('Player: X');
else
xlabel('Player: O');
end
winner = won(_state); % check for winning chances
end
end
  
if winner == 0 % O player won
warndlg('O wins');
title('O wins');
xlabel('');
elseif winner == 1 % X player won
warndlg('X wins');
title('X wins');
xlabel('');
else % game tie
warndlg('Tie');
title('Tie');
xlabel('');
end
end

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

b)

% checking any one won or not
function won = won(_state)
% Horizontal check
if (_state(1,1) == _state(1,2) && _state(1,1) == _state(1,3) && _state(1,1) ~= -1)
won = _state(1,1);
elseif (_state(2,1) == _state(2,2) && _state(2,1) == _state(2,3) && _state(2,1) ~= -1)
won = _state(2,1);
elseif (_state(3,1) == _state(3,2) && _state(3,1) == _state(3,3) && _state(3,1) ~= -1)
won = _state(3,1);
% Vertical check
elseif (_state(1,1) == _state(2,1) && _state(1,1) == _state(3,1) && _state(3,1) ~= -1)
won = _state(1,1);
elseif (_state(1,2) == _state(2,2) && _state(1,2) == _state(3,2) && _state(1,2) ~= -1)
won = _state(1,2);
elseif (_state(1,3) == _state(2,3) && _state(1,3) == _state(3,3) && _state(1,3) ~= -1)
won = _state(1,3);
% Diagonal check
elseif (_state(1,1) == _state(2,2) && _state(1,1) == _state(3,3) && _state(1,1) ~= -1)
won = _state(1,1);
elseif (_state(1,3) == _state(2,2) && _state(1,3) == _state(3,1) && _state(2,2) ~= -1)
won = _state(1,3);
% check for tie
elseif ~ismember(_state, -1)
won = 2;
else
won = -1;
end
end

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

c)

function validMovesRemaining(_state)

if(_state(1,1) == -1 || _state(1,2) == -1 || _state(1,3) == -1 || _state(2,1) == -1 || _state(2,2) == -1 || _state(2,3) == -1 || _state(3,1) == -1 || _state(3,2) == -1 || _state(3,3) == -1)

return 1;

else

return -1;

end

end

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

d)

function _state = playRound(isHuman, _state)

if(isHuman == 1)

[x y] = ginput(1); % getting the mouse positions

else

x = ceil(rand*3);

y = ceil(rand*3);

end

[col row] = getActualPositions(x, y);

row = 2 - row; % fetching actual row

if _state(col+1, row+1) ~= -1 % if slot already got filled

_state = -1;

else

_state(col+1, row+1) = isHuman; % state accepted and fill game board

if isHuman

drawXChoice(col, 2 - row);

else

drawOChoice(col, 2 - row);

end

end

end

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

e)

isHuman = 1; % tracking curr user
_state = [[-1 -1 -1]
[-1 -1 -1]
[-1 -1 -1]];
winner = -1;
  
% reading user or computer inputs
while winner == -1
next = playRound(isHuman, _state);
if next == -1 % if chosen wrong choice, then ask again to fill proper choice
title('Please provide correct details');
else
_state = next;
title('');
isHuman = mod(isHuman + 1,2); % roll up for next player
if isHuman == 1
xlabel('Player: X');
else
xlabel('Player: O');
end
winner = won(_state); % check for winning chances
end
end

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

Combining all scripts...

function [] = tictactoeGame()

% Setup UI

close all

figure('Name','Tic Tac Toe');

plot(-1. -1)

axis([0 3 0 3])

set(gca,'x_Tick',0:3)

set(gca,'y_Tick',0:3)

set(gca,'xLabel','')

set(gca,'yLabel','')

xlabel('Player: X')

grid on

shg

  

isHuman = 1; % tracking curr user

_state = [[-1 -1 -1]

[-1 -1 -1]

[-1 -1 -1]];

winner = -1;

  

% reading user or computer inputs

while winner == -1

next = playRound(isHuman, _state);

if next == -1 % if chosen wrong choice, then ask again to fill proper choice

title('Please provide correct details');

else

_state = next;

title('');

isHuman = mod(isHuman + 1,2); % roll up for next player

if isHuman == 1

xlabel('Player: X');

else

xlabel('Player: O');

end

winner = won(_state); % check for winning chances

end

end

  

if winner == 0 % O player won

warndlg('O wins');

title('O wins');

xlabel('');

elseif winner == 1 % X player won

warndlg('X wins');

title('X wins');

xlabel('');

else % game tie

warndlg('Tie');

title('Tie');

xlabel('');

end

end

function _state = playRound(isHuman, _state)

if(isHuman == 1)

[x y] = ginput(1); % getting the mouse positions

else

x = ceil(rand*3);

y = ceil(rand*3);

end

[col row] = getActualPositions(x, y);

row = 2 - row; % fetching actual row

if _state(col+1, row+1) ~= -1 % if slot already got filled

_state = -1;

else

_state(col+1, row+1) = isHuman; % state accepted and fill game board

if isHuman

drawXChoice(col, 2 - row);

else

drawOChoice(col, 2 - row);

end

end

end

% checking any one won or not

function won = won(_state)

% Horizontal check

if (_state(1,1) == _state(1,2) && _state(1,1) == _state(1,3) && _state(1,1) ~= -1)

won = _state(1,1);

elseif (_state(2,1) == _state(2,2) && _state(2,1) == _state(2,3) && _state(2,1) ~= -1)

won = _state(2,1);

elseif (_state(3,1) == _state(3,2) && _state(3,1) == _state(3,3) && _state(3,1) ~= -1)

won = _state(3,1);

% Vertical check

elseif (_state(1,1) == _state(2,1) && _state(1,1) == _state(3,1) && _state(3,1) ~= -1)

won = _state(1,1);

elseif (_state(1,2) == _state(2,2) && _state(1,2) == _state(3,2) && _state(1,2) ~= -1)

won = _state(1,2);

elseif (_state(1,3) == _state(2,3) && _state(1,3) == _state(3,3) && _state(1,3) ~= -1)

won = _state(1,3);

% Diagonal check

elseif (_state(1,1) == _state(2,2) && _state(1,1) == _state(3,3) && _state(1,1) ~= -1)

won = _state(1,1);

elseif (_state(1,3) == _state(2,2) && _state(1,3) == _state(3,1) && _state(2,2) ~= -1)

won = _state(1,3);

% check for tie

elseif ~ismember(_state, -1)

won = 2;

else

won = -1;

end

end

% Returns actual positions of mouse

function [col row] = getActualPositions(x, y)

col = floor(x);

row = floor(y);

if col > 2 % if out of box

col = 2;

end

  

if row > 2

row = 2;

end

end

function drawXChoice(col, row)

hold on

x = 0:1;

pos = 0:1;

neg = 1-x;

plot(x+col, pos+row)

plot(x+col, neg+row)

hold off

end

function drawOChoice(col, row)

hold on

t = 0:0.1:2*pi;

x = cos(t)/2+0.5;

y = sin(t)/2+0.5;

plot(x+col, y+row)

hold off

end

function validMovesRemaining(_state)

if(_state(1,1) == -1 || _state(1,2) == -1 || _state(1,3) == -1 || _state(2,1) == -1 || _state(2,2) == -1 || _state(2,3) == -1 || _state(3,1) == -1 || _state(3,2) == -1 || _state(3,3) == -1)

return 1;

else

return -1;

end

end