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

Matlab: Make the following changes to the tictactoc.m file (see picture). 1) Mak

ID: 3856063 • Letter: M

Question

Matlab:

Make the following changes to the tictactoc.m file (see picture).

1) Make sure the game properly displays a full game board when the game ends in a draw.

2) Add numbers to the unused spaces, to make choosing an unoccupied space easier. ( Hint: self.board )

3) Bonus: Add a line that goes through a winning set of choices.

Code in text format:

classdef tictactoe < handle

%TICTACTOE Summary of this class goes here

% Detailed explanation goes here

  

properties

player1

player2

board

winner

end

  

methods

function self = tictactoe() % constructor

self.player1 = [];

self.player2 = [];

self.board = 1:9;

self.winner = [];

end

end

  

methods

function play(self)

while isempty(self.winner)

self.gamestate

if length(self.player1) == length(self.player2)

pick = input('Player 1: Pick an available space ');

self.player1 = [self.player1 pick]; % add pick to player list

else

pick = input('Player 2: Pick an available space ');

self.player2 = [self.player2 pick]; % add pick to player list

end

self.board(self.board == pick) = []; % remove pick from board

self.checkwin

end

end

function gamestate(self)

locations = [repmat(0.5:2.5,[1 3]); 2.5 2.5 2.5 1.5 1.5 1.5 0.5 0.5 0.5].';

exes = locations(self.player1,:);

ohs = locations(self.player2,:);

numbers = locations(self.board,:);

close all

hold on

scatter(exes(:,1),exes(:,2),10000,'rx','LineWidth',5)

scatter(ohs(:,1),ohs(:,2),6000,'bo','LineWidth',4)

axis([0 3 0 3])

axis off

plot([0 3],[1 1],'k','LineWidth',3)

plot([0 3],[2 2],'k','LineWidth',3)

plot([1 1],[0 3],'k','LineWidth',3)

plot([2 2],[0 3],'k','LineWidth',3)

end

function checkwin(self)

wins = [1 2 3; 4 5 6; 7 8 9; 1 4 7; 2 5 8; 3 6 9; 1 5 9; 3 5 7];

for i = 1:length(wins)

if issame(intersect(self.player1,wins(i,:)),wins(i,:))

self.winner = 'Player 1';

elseif issame(intersect(self.player2,wins(i,:)),wins(i,:))

self.winner = 'Player 2';

end

end

if ~isempty(self.winner)

disp([self.winner ' is the winner!'])

self.gamestate

end

if isempty(self.board) && isempty(self.winner)

self.winner = 'Draw';

disp('The game is a draw!')

end

end

end

  

end

classdef tictactoe

Explanation / Answer

classdef tictactoe < handle

%TICTACTOE Summary of this class goes here

% Detailed explanation goes here

  

properties

player1

player2

board

winner

end

  

methods

function self = tictactoe() % constructor

self.player1 = [];

self.player2 = [];

self.board = 1:9;

self.winner = [];

end

end

  

methods

function play(self)

while isempty(self.winner)

self.gamestate

if length(self.player1) == length(self.player2)

pick = input('Player 1: Pick an available space ');

self.player1 = [self.player1 pick]; % add pick to player list

else

pick = input('Player 2: Pick an available space ');

self.player2 = [self.player2 pick]; % add pick to player list

end

self.board(self.board == pick) = []; % remove pick from board

self.checkwin

end

end

function gamestate(self)

locations = [repmat(0.5:2.5,[1 3]); 2.5 2.5 2.5 1.5 1.5 1.5 0.5 0.5 0.5].';

exes = locations(self.player1,:);

ohs = locations(self.player2,:);

numbers = locations(self.board,:);

close all

hold on

scatter(exes(:,1),exes(:,2),10000,'rx','LineWidth',5)

scatter(ohs(:,1),ohs(:,2),6000,'bo','LineWidth',4)

axis([0 3 0 3])

axis off

plot([0 3],[1 1],'k','LineWidth',3)

plot([0 3],[2 2],'k','LineWidth',3)

plot([1 1],[0 3],'k','LineWidth',3)

plot([2 2],[0 3],'k','LineWidth',3)

end

function checkwin(self)

wins = [1 2 3; 4 5 6; 7 8 9; 1 4 7; 2 5 8; 3 6 9; 1 5 9; 3 5 7];

for i = 1:length(wins)

if issame(intersect(self.player1,wins(i,:)),wins(i,:))

self.winner = 'Player 1';

elseif issame(intersect(self.player2,wins(i,:)),wins(i,:))

self.winner = 'Player 2';

end

end

if ~isempty(self.winner)

disp([self.winner ' is the winner!'])

self.gamestate

end

if isempty(self.board) && isempty(self.winner)

self.winner = 'Draw';

disp('The game is a draw!')

end

end

end

  

end