I need a 'reset' push button to reset all other push buttons

3 Ansichten (letzte 30 Tage)
delaina gonzales
delaina gonzales am 19 Nov. 2018
Kommentiert: Dennis am 19 Nov. 2018
im making a tic tac toe game with a GUI and my 'new game' button isnt working!
my gui is made up of 9 push buttons for X's and O's and one New Game push button
i disabled all the buttons once theyre pressed so i have them enabled once i press the new game button but the X's and O's are still there.
any help is appreciated!

Antworten (1)

Dennis
Dennis am 19 Nov. 2018
Without seeing your code it is hard to provide an answer. If the enabling works your probably did not set the string property of your buttons.
One possible solution might look like this:
handles.f=figure('Position',[200 200 800 800]);
for i=1:3
for k=1:3
handles.pb(i,k)=uicontrol('parent',handles.f,'style','pushbutton','Position',[i*200-200 k*200-200 200 200],'String',num2str(i));
end
end
set(handles.pb(:,:),'Callback',{@tictac_cb,handles});
handles.flag='X';
handles.newgame=uicontrol('style','pushbutton','position',[100 700 100 40],'String','New Game','Callback',{@newgame_cb,handles});
guidata(handles.f,handles)
function tictac_cb(hObj,~,handles)
handles=guidata(handles.f);
set(hObj,'String',handles.flag);
set(hObj,'Enable','Off')
if strcmp(handles.flag,'X')
handles.flag='O';
else
handles.flag='X';
end
guidata(handles.pb(1,1),handles)
end
function newgame_cb(~,~,handles)
set(handles.pb(:,:),'Enable','On');
set(handles.pb(:,:),'String','');
end
  3 Kommentare
Stephen23
Stephen23 am 19 Nov. 2018
Bearbeitet: Stephen23 am 19 Nov. 2018
@delaina gonzales: please format your code properly. Today I did this for you, but in future you can do it yourself: select the code text, then click the code button.
Dennis
Dennis am 19 Nov. 2018
You enable the buttons, but you do not clear their strings. This can be done by setting their string property to '':
handles.button1.String='';
I'd also like to point out that putting handles in an array (see my example above) helps when you want to change multiple buttons. But even with GUIDE you could do it like this:
for i=1:9
h=handles.(sprintf('button%d',i));
set(h,'enable','on');
set(h,'string','');
end

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Card games 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!

Translated by