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

Matlab GUI Problem: For the Matlab script below (A TIC_TAC_TOE program), if I wa

ID: 3866157 • Letter: M

Question

Matlab GUI Problem:

For the Matlab script below (A TIC_TAC_TOE program), if I want to add textboxes on the GUI, so that the player could type in the name and display on the credit box(shows win, lose, tie). Then how could I modify the code below to make this work.

function buttons(action)

if isempty(gcbo)

hFig = openfig('ttt.fig');

image(imread('table.jpg'));

set(gca,'XTickLabel','','YTickLabel','')

action = 'Restart';

else

hFig = gcbf;

action = get(gcbo,'tag');

end

gameState = get(hFig,'userdata');

switch (action)

case 'single'

disp('single');

gameState.gameBoard = [0,0,0;

   0,0,0;

   0,0,0];

gameState.gameMode = '1player';

case 'double'

disp('double');

gameState.gameBoard = [0,0,0;

   0,0,0;

   0,0,0];

gameState.gameMode = '2players';

case 'pushbuttonEasy'

gameState.difficulty = 'easy';

case 'pushbuttonModerate'

gameState.difficulty = 'moderate';

  

case {'pushbutton1','pushbutton2','pushbutton3',...

'pushbutton4','pushbutton5','pushbutton6',...

'pushbutton7','pushbutton8','pushbutton9'}

gameState.pushButton = action;

if strcmp(gameState.gameMode, '2players')

if (gameState.player == 1)

gameState = tttDoublePlayer(gameState);

set(gcbo, 'string', '1');

set(gcbo, 'enable', 'off')

elseif (gameState.player == 2)

gameState = tttDoublePlayer(gameState);

set(gcbo, 'string', '2');

set(gcbo, 'enable', 'off')

  

end

elseif (strcmp(gameState.gameMode, '1player') && strcmp(gameState.difficulty, 'easy'))

if (gameState.player == 1)

gameState = tttSingleEasy(gameState);

set(gcbo, 'string', '1');

set(gcbo, 'enable', 'off')

end

end

gameState = tttwinnerLoserTie(gameState);

case 'Exit'

close(hFig);

case 'Restart'

gameState = tictactoeNewGame(gameState);

b = findobj('tag','pushbutton1');

set(b, 'enable', 'on')

set(b, 'string', '');

b = findobj('tag','pushbutton2');

set(b, 'enable', 'on')

set(b, 'string', '');

b = findobj('tag','pushbutton3');

set(b, 'enable', 'on')

set(b, 'string', '');

b = findobj('tag','pushbutton4');

set(b, 'enable', 'on')

set(b, 'string', '');

b = findobj('tag','pushbutton5');

set(b, 'enable', 'on')

set(b, 'string', '');

b = findobj('tag','pushbutton6');

set(b, 'enable', 'on')

set(b, 'string', '');

b = findobj('tag','pushbutton7');

set(b, 'enable', 'on')

set(b, 'string', '');

b = findobj('tag','pushbutton8');

set(b, 'enable', 'on')

set(b, 'string', '');

b = findobj('tag','pushbutton9');

set(b, 'enable', 'on')

set(b, 'string', '');

b = findobj('tag','textBox');

set(b, 'string', '');

end % END OF SWITCH STATEMENT

if (gameState.winner == 17)

if (strcmp(gameState.gameMode, '1player') && strcmp(gameState.difficulty, 'easy') && (gameState.validMoves > 0))

if (gameState.player == -1)

gameState = tttSingleEasy(gameState);

if (gameState.compMoveNum1 == 1)

b = findobj('tag','pushbutton1');

set(b, 'string', '2');

set(b, 'enable', 'off')

elseif (gameState.compMoveNum1 == 2)

b = findobj('tag','pushbutton2');

set(b, 'string', '2');

set(b, 'enable', 'off')

elseif (gameState.compMoveNum1 == 3)

b = findobj('tag','pushbutton3');

set(b, 'string', '2');

set(b, 'enable', 'off')

   elseif (gameState.compMoveNum1 == 4)

b = findobj('tag','pushbutton4');

set(b, 'string', '2');

set(b, 'enable', 'off')

   elseif (gameState.compMoveNum1 == 5)

b = findobj('tag','pushbutton5');

set(b, 'string', '2');

set(b, 'enable', 'off')

   elseif (gameState.compMoveNum1 == 6)

b = findobj('tag','pushbutton6');

set(b, 'string', '2');

set(b, 'enable', 'off')

   elseif (gameState.compMoveNum1 == 7)

b = findobj('tag','pushbutton7');

set(b, 'string', '2');

set(b, 'enable', 'off')

   elseif (gameState.compMoveNum1 == 8)

b = findobj('tag','pushbutton8');

set(b, 'string', '2');

set(b, 'enable', 'off')

   elseif (gameState.compMoveNum1 == 9)

b = findobj('tag','pushbutton9');

set(b, 'string', '2');

set(b, 'enable', 'off')

end

end

end

end

gameState = tttwinnerLoserTie(gameState);

disp(gameState.gameBoard)

gameState.p1W = 0;

gameState.p1L = 0;

gameState.p2W = 0;

gameState.p2L = 0;

gameState.pT = 0;

if (gameState.winner == 1)

box = findobj('tag','textBox')

set(box, 'string' ,'Player 1 is the winner');

if strcmp(gameState.gameMode, '2players')

gameState.p1W = gameState.p1W + 1;

P1W = num2str(gameState.p1W);

gameState.p2L = gameState.p2L + 1;

P2L = num2str(gameState.p2L);

showbox = findobj('tag','P1W')

set(showbox, 'string' , P1W);

showbox = findobj('tag','P2L')

set(showbox, 'string', P2L);

end

elseif (gameState.winner == -1)

box = findobj('tag','textBox')

set(box, 'string', 'Player 2 is the winner');

elseif (gameState.winner == 2)

box = findobj('tag','textBox')

set(box, 'string', 'Player 2 is the winner');

if strcmp(gameState.gameMode, '2players')

gameState.p2W = gameState.p2W + 1;

P2W = num2str(gameState.p2W);

gameState.p1L = gameState.p1L + 1;

P1L = num2str(gameState.p1L);

showbox = findobj('tag','P2W')

set(showbox, 'string' , P2W);

showbox = findobj('tag','P1L')

set(showbox, 'string', P1L);

end

elseif (gameState.winner == 0)

gameState.pT = gameState.pT +1;

PT = num2str(gameState.pT);

box = findobj('tag','textBox')

set(box, 'string', 'Tie Game. No winner.');

showbox = findobj('tag','TieBox');

set(showbox, 'string', PT);

  

end

set(hFig,'userdata',gameState )

return

end

Explanation / Answer


function buttons(action)
if isempty(gcbo)
hFig = openfig('ttt.fig');
image(imread('table.jpg'));
set(gca,'XTickLabel','','YTickLabel','')
action = 'Restart';
else
hFig = gcbf;
action = get(gcbo,'tag');
end
prompt = {'Enter player1 name:','Enter player2 name:'};
dlg_title = 'Input';
num_lines = 1;
defaultans = {'20','hsv'};
playerNames = inputdlg(prompt,dlg_title,num_lines,defaultans);


gameState = get(hFig,'userdata');
switch (action)
case 'single'
disp('single');
gameState.gameBoard = [0,0,0;
0,0,0;
0,0,0];
gameState.gameMode = '1player';
case 'double'
disp('double');
gameState.gameBoard = [0,0,0;
0,0,0;
0,0,0];
gameState.gameMode = '2players';
case 'pushbuttonEasy'
gameState.difficulty = 'easy';
case 'pushbuttonModerate'
gameState.difficulty = 'moderate';
  
case {'pushbutton1','pushbutton2','pushbutton3',...
'pushbutton4','pushbutton5','pushbutton6',...
'pushbutton7','pushbutton8','pushbutton9'}
gameState.pushButton = action;
if strcmp(gameState.gameMode, '2players')
if (gameState.player == 1)
gameState = tttDoublePlayer(gameState);
set(gcbo, 'string', '1');
set(gcbo, 'enable', 'off')
elseif (gameState.player == 2)
gameState = tttDoublePlayer(gameState);
set(gcbo, 'string', '2');
set(gcbo, 'enable', 'off')
  
end
elseif (strcmp(gameState.gameMode, '1player') && strcmp(gameState.difficulty, 'easy'))
if (gameState.player == 1)
gameState = tttSingleEasy(gameState);
set(gcbo, 'string', '1');
set(gcbo, 'enable', 'off')
end
end
gameState = tttwinnerLoserTie(gameState);
case 'Exit'
close(hFig);
case 'Restart'
gameState = tictactoeNewGame(gameState);
b = findobj('tag','pushbutton1');
set(b, 'enable', 'on')
set(b, 'string', '');
b = findobj('tag','pushbutton2');
set(b, 'enable', 'on')
set(b, 'string', '');
b = findobj('tag','pushbutton3');
set(b, 'enable', 'on')
set(b, 'string', '');
b = findobj('tag','pushbutton4');
set(b, 'enable', 'on')
set(b, 'string', '');
b = findobj('tag','pushbutton5');
set(b, 'enable', 'on')
set(b, 'string', '');
b = findobj('tag','pushbutton6');
set(b, 'enable', 'on')
set(b, 'string', '');
b = findobj('tag','pushbutton7');
set(b, 'enable', 'on')
set(b, 'string', '');
b = findobj('tag','pushbutton8');
set(b, 'enable', 'on')
set(b, 'string', '');
b = findobj('tag','pushbutton9');
set(b, 'enable', 'on')
set(b, 'string', '');
b = findobj('tag','textBox');
set(b, 'string', '');
end % END OF SWITCH STATEMENT
if (gameState.winner == 17)
if (strcmp(gameState.gameMode, '1player') && strcmp(gameState.difficulty, 'easy') && (gameState.validMoves > 0))
if (gameState.player == -1)
gameState = tttSingleEasy(gameState);
if (gameState.compMoveNum1 == 1)
b = findobj('tag','pushbutton1');
set(b, 'string', '2');
set(b, 'enable', 'off')
elseif (gameState.compMoveNum1 == 2)
b = findobj('tag','pushbutton2');
set(b, 'string', '2');
set(b, 'enable', 'off')
elseif (gameState.compMoveNum1 == 3)
b = findobj('tag','pushbutton3');
set(b, 'string', '2');
set(b, 'enable', 'off')
elseif (gameState.compMoveNum1 == 4)
b = findobj('tag','pushbutton4');
set(b, 'string', '2');
set(b, 'enable', 'off')
elseif (gameState.compMoveNum1 == 5)
b = findobj('tag','pushbutton5');
set(b, 'string', '2');
set(b, 'enable', 'off')
elseif (gameState.compMoveNum1 == 6)
b = findobj('tag','pushbutton6');
set(b, 'string', '2');
set(b, 'enable', 'off')
elseif (gameState.compMoveNum1 == 7)
b = findobj('tag','pushbutton7');
set(b, 'string', '2');
set(b, 'enable', 'off')
elseif (gameState.compMoveNum1 == 8)
b = findobj('tag','pushbutton8');
set(b, 'string', '2');
set(b, 'enable', 'off')
elseif (gameState.compMoveNum1 == 9)
b = findobj('tag','pushbutton9');
set(b, 'string', '2');
set(b, 'enable', 'off')
end
end
end
end
gameState = tttwinnerLoserTie(gameState);
disp(gameState.gameBoard)
gameState.p1W = 0;
gameState.p1L = 0;
gameState.p2W = 0;
gameState.p2L = 0;
gameState.pT = 0;
if (gameState.winner == 1)
box = findobj('tag','textBox')
set(box, 'string' ,playerNames(1)+' is the winner');
if strcmp(gameState.gameMode, '2players')
gameState.p1W = gameState.p1W + 1;
P1W = num2str(gameState.p1W);
gameState.p2L = gameState.p2L + 1;
P2L = num2str(gameState.p2L);
showbox = findobj('tag','P1W')
set(showbox, 'string' , P1W);
showbox = findobj('tag','P2L')
set(showbox, 'string', P2L);
end
elseif (gameState.winner == -1)
box = findobj('tag','textBox')
set(box, 'string', playerNames(2)+' is the winner');
elseif (gameState.winner == 2)
box = findobj('tag','textBox')
set(box, 'string', playerNames(2)+' is the winner');
if strcmp(gameState.gameMode, '2players')
gameState.p2W = gameState.p2W + 1;
P2W = num2str(gameState.p2W);
gameState.p1L = gameState.p1L + 1;
P1L = num2str(gameState.p1L);
showbox = findobj('tag','P2W')
set(showbox, 'string' , P2W);
showbox = findobj('tag','P1L')
set(showbox, 'string', P1L);
end
elseif (gameState.winner == 0)
gameState.pT = gameState.pT +1;
PT = num2str(gameState.pT);
box = findobj('tag','textBox')
set(box, 'string', 'Tie Game. No winner.');
showbox = findobj('tag','TieBox');
set(showbox, 'string', PT);
  
end
set(hFig,'userdata',gameState )
return
end