Error using linecallback function with ButtonDownFcn.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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?
0 Kommentare
Antworten (1)
Walter Roberson
am 15 Mär. 2021
Bearbeitet: Walter Roberson
am 15 Mär. 2021
~ = 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 Kommentare
Siehe auch
Kategorien
Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!