Error using linecallback function with ButtonDownFcn.

조회 수: 1 (최근 30일)
Nathan Ross
Nathan Ross 2021년 3월 15일
댓글: Walter Roberson 2021년 3월 15일
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