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

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!

Answers (1)

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 Comments

function button1_Callback(hObject, eventdata, handles)
% hObject handle to button1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if handles.player == 1
playermark='x';
elseif handles.player == 2
playermark='o';
end
if handles.grid(1,1) == 0
set(hObject,'String',playermark)
handles.grid(1,1) = handles.player;
winner = whowins(handles.player,handles.grid);
if handles.player == 1
handles.player = 2;
else
handles.player = 1;
end
end
if winner ~=0
if winner == 1
msgbox('player x wins!');
elseif winner == 2
msgbox('player o wins!');
elseif winner == -1
msgbox('its a draw! try again');
end
end
handles.button1.Enable = 'Off';
guidata(hObject,handles);
%%this is basically the code under each pushbutton callback
% --- Executes on button press in button10.
function button10_Callback(hObject, eventdata, handles)
% hObject handle to button10 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
disp(hObject.String)
handles = guihandles(hObject);
handles.label.text = 'player 1';
handles.button1.Enable = 'On';
handles.button2.Enable = 'On';
handles.button3.Enable = 'On';
handles.button4.Enable = 'On';
handles.button5.Enable = 'On';
handles.button6.Enable = 'On';
handles.button7.Enable = 'On';
handles.button8.Enable = 'On';
handles.button9.Enable = 'On';
handles.player_1 = 1;
handles.mat = -1*ones(3,3);
%%and this is under my new game call back button
@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.
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

Sign in to comment.

Categories

Find more on Board games in Help Center and File Exchange

Asked:

on 19 Nov 2018

Commented:

on 19 Nov 2018

Community Treasure Hunt

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

Start Hunting!