function hangman()
handles               = create_gui();
handles               = init_game(handles);
guidata(handles.hfig, handles);





%-------------------------------------------------------------------------------
%
%-------------------------------------------------------------------------------
function handles = init_game(handles)
word                  = 'gomatlab';
handles.word          = word;
handles.versuch       = 0;
handles.maxAnzVersuch = 9;
handles.guessedletter = false(size(word));

if isfield(handles, 'hedit')
   delete(handles.hedit)
end
%
% Felder für Buchstaben erzeugen
hedit        = zeros(size(word));
for k = 1:length(word)
   x = 20*k;
   y = 320;
   w = 20;
   h = 20;
   %
   % handles in ein Array schreiben
   hedit(k) = uicontrol('Style', 'edit',...
      'String', '',...
      'Position', [x y w h]);
   
end
handles.hedit = hedit;
cla(handles.axes_hangman);





%-------------------------------------------------------------------------------
%
%-------------------------------------------------------------------------------
function handles = create_gui()
handles.hfig = figure();
figpos       = get(handles.hfig, 'Position');
%
% Buttons mit Buchstaben erzeugen:
ABC = 'A':'Z';
hbutton = zeros(size(ABC));
for k = 1:length(ABC)
   x = 20*k;
   y = 250;
   w = 20;
   h = 20;
   if k > round(length(ABC)/2)
      y = 230;
      x = 20 * (mod(k-1, round(length(ABC)/2 )) + 1);
   end
   
   hbutton(k) = uicontrol('Style', 'pushbutton',...
      'String'  ,  ABC(k),...
      'Position',  [x y w h],...
      'Callback',  @letter_callback);
   
end
handles.hbutton = hbutton;
x = 290;
y = 10;
w = figpos(3) - x - 10;
h = figpos(4) - y - 10;
handles.axes_hangman = axes(...
   'Units'   , 'pixels'  ,...
   'Position', [x,y,w,h] ,...
   'XLim'    , [0 100]   ,...
   'YLim'    , [0 200]   ,...
   'XTick'   , []        ,...
   'YTick'   , []        );
box on;
hold on;


%-------------------------------------------------------------------------------
%
%-------------------------------------------------------------------------------
function letter_callback(hobj, event)
handles = guidata(hobj);

word    = handles.word;

% Buchstaben auslesen
letter  = get(hobj, 'String');

% vergleichen ob die Buchstabe in Suchword vorkommt 
pos = lower(word) == lower(letter);


% ablegenen in speicher, um später rauszufinden ob das Word erraten wurde
handles.guessedletter = handles.guessedletter | pos;

% wenn Buchstabe in Word vorkommt
if any(pos)
   % dann anzeigen
   set(handles.hedit(pos), 'String', letter);
else
   % sonst die Versuch hochzählen, hangman malen
   handles.versuch = handles.versuch + 1;
   update_hangman(handles)
end

% checken ob gewonnen oder verloren wurde
if handles.versuch >= handles.maxAnzVersuch || all(handles.guessedletter)
   res = 'You won';
   if handles.versuch >= handles.maxAnzVersuch
      res = 'You lost';
   end
   
   button = questdlg([res , ', do you want start new game?'],...
      'Continue Operation','Restart','Close','Restart');
   
   % 
   if strcmp(button,'Restart')
       handles = init_game(handles);
   elseif strcmp(button,'Close')
      
      close(handles.hfig);
      return
   end% if pressed button Yes or No
   
end




guidata(handles.hfig, handles);

%------------------------------------------------------------------
%
%------------------------------------------------------------------
function update_hangman(handles)


switch(handles.versuch)
   case 1
      plot(handles.axes_hangman, [10 10],[0 160])
   case 2
      plot(handles.axes_hangman, [10 50],[160 160])
   case 3
      plot(handles.axes_hangman, [50 50],[160 120])
   case 4
      rectangle(...
         'Position'  , [40, 100, 20, 20], ...
         'Curvature' , [1,1]            , ...
         'Parent'    , handles.axes_hangman)
   case 5
      plot(handles.axes_hangman, [50 50],[100 50])
   case 6
      plot(handles.axes_hangman, [30 50], [50 80])
   case 7
      plot(handles.axes_hangman, [50 70], [80 50])
   case 8
      plot(handles.axes_hangman, [30 50], [20 50])
   case 9
      plot(handles.axes_hangman, [50 70], [50 20])
end