For this assignment, let the Tic Tac Toe game board be stored as a 3 × 3 charact
ID: 3873057 • Letter: F
Question
For this assignment, let the Tic Tac Toe game board be stored as a 3 × 3 character array called ‘board.’ For example, at a given point the board might look like the one shown below:
>> board = [ ’ - ’ , ’O ’ , ’X ’; ’ - ’ ,’ - ’ , ’O ’; ’X ’ , ’O ’ , ’ - ’]
board =
3 x3 char array
’ - OX ’
’ --O ’
’XO - ’
Write a Matlab script that displays the current game board to the command prompt. Your code should display whatever happens to be stored in the variable ‘board’ at the time the code is run. In other words, it should not simply display the particular game board shown above.
See sample output below:
>> board = [ ’O ’ , ’X ’ , ’ - ’; ’X ’ ,’O ’ , ’ - ’; ’ -’ , ’ - ’ , ’ - ’ ];
>> problem2
OX -
XO -
---
>>
Explanation / Answer
Make the board variable global. Do code like this;
>> global board ;
>> board = [ ’O ’ , ’X ’ , ’ - ’; ’X ’ ,’O ’ , ’ - ’; ’ -’ , ’ - ’ , ’ - ’ ];
here the code for your problem2.m script file
%-----problem2.m--------------------
function problem2
global board ;
display(board);
%------------------------------
Now modifile the board
>>board = [ '-' , 'O' , 'X'; 'X' ,'O' , 'X'; 'X' , 'O' , 'M'];
>> problem2
board =
-OX
XOX
XOM
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.