Reset pushbuttons by another pushbutton
Show older comments
Hi. i am making Tic-tac-toe with gui.
when i play game and try to play new game, i want to make pushbutton that clear all O,X.
but there is a error message "Unrecognized function or variable 'winner'."
i have no idea to solve this error.
i'd glad if you help.
handles.plr=1;
handles.box=[0 0 0;0 0 0;0 0 0];
These are defined at OpeningFcn.
function box1_Callback(hObject, eventdata, handles)
% hObject handle to box1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if handles.plr==1
plrmark='X';
elseif handles.plr==2
plrmark='O';
end
%if not already marked
if handles.box(1,1)==0
set(hObject,'String',plrmark)
handles.box(1,1)=handles.plr;
%update player value
winner=whowins(handles.plr,handles.box);
if handles.plr==1
handles.plr=2;
else handles.plr=1;
end
end
%when nobody wins winner=0 other wise exit program with results
if (winner ~= 0)
if winner==1
set(handles.text,'String', 'Player 1 Win');
set(hObject,'String', '');
elseif winner==2
set(handles.text,'String', 'Player 2 Win');
set(hObject,'String', '');
elseif winner==-1
set(handles.text,'String', 'Draw');
end
end
guidata(hObject,handles);
this is each box's code.
function [winner] = whowins (plr, box)
winner=0;
% logic for winner
if box(1,1)==plr && box(1,2)==plr && box(1,3)==plr
winner=box(1,1);
return;
elseif box(2,1)==plr && box(2,2)==plr && box(2,3)==plr
winner=box(2,1);
return;
elseif box(3,1)==plr && box(3,2)==plr && box(3,3)==plr
winner=box(3,1);
return;
elseif box(1,1)==plr && box(2,1)==plr && box(3,1)==plr
winner=box(1,1);
return;
elseif box(1,2)==plr && box(2,2)==plr && box(3,2)==plr
winner=box(1,2);
return;
elseif box(1,3)==plr && box(2,3)==plr && box(3,3)==plr
winner=box(1,3);
return;
elseif box(1,1)==plr && box(2,2)==plr && box(3,3)==plr
winner=box(1,1);
return;
elseif box(1,3)==plr && box(2,2)==plr && box(3,1)==plr
winner=box(1,3);
return;
% logic for a draw that is winner=3
% all boxes should be full
elseif (box(1)~=0 && box(2)~=0 && box(3)~=0 && box(4)~=0 && box(5)~=0 && box(6)~=0 && box(7)~=0 && box(8)~=0 && box(9)~=0)
winner=-1;
end
end
and this is whowins Fcn.
Answers (1)
Voss
on 12 Dec 2022
function box1_Callback(hObject, eventdata, handles)
% ...
% winner is not defined here
% ...
if handles.box(1,1)==0
% ...
winner=whowins(handles.plr,handles.box);
% ...
end
if (winner ~= 0) % error here (presumably)
% ...
end
% ...
end
Notice that if handles.box(1,1) is not 0, then that first "if" block is not entered and the variable "winner" is not set. That's the cause of the error (presumably - I can't say for sure because I don't know what line the error happens on).
Also, I'm not sure what box1_Callback has to do with clearing the board. box1_Callback is the callback for one (or all) of the uicontrols (presumably) comprising the board. The undefined "winner" error would happen when a player selects a button that is already "occupied", in which case the correct thing to do is to disregard the selection. This can be achieved by returning early from the callback function:
function box1_Callback(hObject, eventdata, handles)
if handles.box(1,1) ~= 0
% box is already selected -> do nothing
return
end
% the rest is basically as you have it, except you
% don't need to check if handles.box(1,1) == 0 below,
% because you know handles.box(1,1) == 0 already:
if handles.plr==1
plrmark='X';
elseif handles.plr==2
plrmark='O';
end
set(hObject,'String',plrmark)
handles.box(1,1)=handles.plr;
%update player value
winner=whowins(handles.plr,handles.box);
if handles.plr==1
handles.plr=2;
else
handles.plr=1;
end
% ...
% etc.
% ...
end
4 Comments
민성 김
on 13 Dec 2022
Voss
on 13 Dec 2022
Do you have
guidata(hObject,handles)
at the end of Newgame_Callback?
This is necessary so that handles.box and handles.plr are updated in the handles structure, so that other functions use the updated values.
민성 김
on 13 Dec 2022
Voss
on 13 Dec 2022
You're welcome! Any other questions, let me know. Otherwise, please "Accept This Answer". Thanks!
Categories
Find more on Strategy & Logic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!