Error using linecallback function with ButtonDownFcn.

1 次查看(过去 30 天)
I have been trying to create a tic-tac-toe game , but am running into a problem as to how I should enable clicking on each cell of the board to enter the respective "x" or "o". I have created a function to try and implement this but can not figure out where I went wrong.
This is the attempted function:
function lineCallback(~,~)
~ = text(column,-rows,'x';'horizontalalignment';'center';'fontsize';20)
end
This is the main file so far:
TTCboard = zeros(3,3);
% for creating the standard tic-tac-toe board
figure
plot([.5 3.5],[-1.5 -1.5], 'g','linewidth',1);% creates top horizontal line in board
hold on
plot([2.5 2.5],[-.5 -3.5], 'g','linewidth',1)% creates right-most vertical line
plot([.5 3.5],[-2.5 -2.5], 'g','linewidth',1)% creates bottom horizontal line
plot([1.5 1.5],[-.5 -3.5], 'g','linewidth',1)% creates left-most vertical line
axis off % keeps the X & y-axis off, creating a better looking natural board
hold off% ensures later commands are added to existing board
%Start of player one's input
move = [row ,column], 'ButtonDownFcn', @lineCallback;
row = move(1);
column= move(2);
board(row,column) = 1; % states that the inputted tile value is equal to one
Any tips on what to Fix?

回答(1 个)

Walter Roberson
Walter Roberson 2021-3-15
编辑:Walter Roberson 2021-3-15
~ = text(column,-rows,'x';'horizontalalignment';'center';'fontsize';20)
would have to be
[~] = text(column, -rows, 'x', 'horizontalalignment', 'center', 'fontsize', 20);
but it would make more sense to use
text(column, -rows, 'x', 'horizontalalignment', 'center', 'fontsize', 20);
Also
move = [row ,column], 'ButtonDownFcn', @lineCallback;
is not going to do what you want. You have not defined row or column at that point. If you had defined them, then you would assign to move and then display the result of the assignment (because of the comma after the assignment), and then you would create the character vetor 'ButtonDownFcn' and display it (because of the comma after it), and thne you would create a handle to lineCallback function and would then discard the handle (because of the semi-colon.)
What are you thinking that a callback is getting attached to?
Also, in your callback, neither column nor rows appears to be defined. column you might potentially get from the main script, if you were to turn it into a nested function, but rows does not appear anywhere else in your code.
  3 个评论
Nathan Ross
Nathan Ross 2021-3-15
any suggestions on how to define row and column in the scope of the tic tac toe board?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by