Reset pushbuttons by another pushbutton

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.

Respuestas (1)

Voss
Voss el 12 de Dic. de 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 comentarios

thank you for advice.
Now there are no "Unrecognized function or variable 'winner'." error.
You said "not sure what box1_Callback has to do with clearing the board.".
I didnt intended to delete sth in box1_callback function.
it is just "New game button's job."
Now i am making newgame button.
But, there are some other error about restart.
after press new game button, when i press box 1 to 9, i cant see any O,X mark.
function Newgame_Callback(hObject, eventdata, handles)
% hObject handle to Newgame (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.box1, 'String', ' ');
set(handles.box2, 'String', ' ');
set(handles.box3, 'String', ' ');
set(handles.box4, 'String', ' ');
set(handles.box5, 'String', ' ');
set(handles.box6, 'String', ' ');
set(handles.box7, 'String', ' ');
set(handles.box8, 'String', ' ');
set(handles.box9, 'String', ' ');
handles.box=[0 0 0 ; 0 0 0 ; 0 0 0];
handles.plr = 1;
winner=whowins(handles.plr,handles.box);
As you know, box1~9 is button for tictactoe. and handles.box is for plr.
i think
set(handles.box2, 'String', ' ');
has a problem. so i tried "set(handles.box, 'enable', 0/1 ), ", and ' ' to '0'.
but i can't see any marks.
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.
민성 김
민성 김 el 13 de Dic. de 2022
Oh amazing! Thanks a lot....
Voss
Voss el 13 de Dic. de 2022
You're welcome! Any other questions, let me know. Otherwise, please "Accept This Answer". Thanks!

Iniciar sesión para comentar.

Categorías

Más información sobre Strategy & Logic en Centro de ayuda y File Exchange.

Preguntada:

el 12 de Dic. de 2022

Comentada:

el 13 de Dic. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by