How do I call a subfunction (local function)?
1 次查看(过去 30 天)
显示 更早的评论
How do I call a subfunction (local function)?
I am supposed to make a game of tictactoe using a sunfunction (local function). I keep getting errors on the line that I am trying to call the subfunction (the line that has "???"). Can someone please help me with making the correct code to call my subfunction, "Initialize_Board."
function Ticktacktoe()
clc();
% The board size must be square, but it can be any size > 1
Board_size = 3;
???
% Begin playing the game until someone wins or the board is full
Whose_turn = 'X';
Winner = ' ';
while Winner == ' '
% Display the current tic-tac-toe board
disp(Board);
% Get the location to put the next X or O
fprintf('Enter %c''s row : ', Whose_turn);
Row = input('');
fprintf('Enter %c''s column: ', Whose_turn);
Column = input('');
% Place the X or O on the Board; the times 2 skips over the borders
Board(Row*2,Column*2) = Whose_turn;
% Check to see if this created a sequence -- i.e., is there a winner?
if sum( sum( Board == Whose_turn ) == Board_size) > 0 % a column is full of the same symbols
Winner = Whose_turn;
elseif sum( sum( Board' == Whose_turn ) == Board_size) > 0 % a row is full of the same symbols
Winner = Whose_turn;
elseif sum( diag(Board,0) == Whose_turn ) == Board_size % the diagonal has all the same symbols
Winner = Whose_turn;
else % check the upper-right to lower left diagonal for all of the same symbols
Total_Xs = 0;
for row = 2:2:Board_size*2
if Board(row,(Board_size*2-row+2) ) == Whose_turn
Total_Xs = Total_Xs + 1;
end
end
if Total_Xs == Board_size
Winner = Whose_turn;
end
end
% Check to see if all the blanks are gone and the game is over
if sum( sum( Board == ' ') ) == 0
Winner = 'C'; % "Cat" got the game
end
% Switch players turn
if Whose_turn == 'X'
Whose_turn = 'O';
else
Whose_turn = 'X';
end
end % while Winner == ' '
% Display the final board layout so we can see the last move
disp(Board);
% Display who won
if Winner == 'X'
fprintf('X won - congratulations!\n');
elseif Winner == 'O'
fprintf('O won - congratulations!\n');
else % it must be a 'cat' game
fprintf('Cat won - sorry.\n');
end
end % function tictacktoe
function Initialize_Board()
% INPUTS: Board_Size
% OUTPUTS: Board
%
% REFERENCES: None
%---------------------------------------------------------------
clc();
% Allocate a character array for the board description.
% The board contains the square boundaries and a blank space
% for the X's and O's to be played into.
Board = char(2*Board_size + 1, 2*Board_size + 1);
% Initialize the board.
Row = 1;
for j = 1:Board_size
Board(Row, 1) = '+';
Board(Row+1,1) = '|';
Column = 2;
for k = 1:Board_size
Board(Row,Column ) = '-';
Board(Row,Column+1) = '+';
Board(Row+1,Column ) = ' ';
Board(Row+1,Column+1) = '|';
Column = Column + 2;
end
Row = Row + 2;
end
% Fill in the bottom row of the board
Board(Row, 1) = '+';
Column = 2;
for k = 1:Board_size
Board(Row,Column ) = '-';
Board(Row,Column+1) = '+';
Column = Column + 2;
end
end % function Initialize_Board
0 个评论
采纳的回答
Mehmed Saad
2020-4-27
编辑:Mehmed Saad
2020-4-27
pass Board argument out from function and pass in Board_size
Board= Initialize_Board(Board_size);
function part
function Board= Initialize_Board(Board_size)
Line-7
Initialize_Board;
remove end from line 72 (function tictacktoe) and put it at the end of the code (after Initialize_Board())
This will allow Initialize_Board function to use tictacktoe workspace
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!