MATLAB Examples

function Congklak_custom

%--------------------------------------------------------------------------
%Congklak_custom
%Version 1.00
%Created by Stepen
%Created 23 April 2012
%Last modifed 27 April 2012
%--------------------------------------------------------------------------
%Congklak_custom starts GUI game of Congklak (Indonesian's traditional
%board game) with customisable number of house and beans.
%--------------------------------------------------------------------------
%How to play Congklak:
%Each player takes turn to play seeds/bean from his/her house (house is a
%small holes on both player's side of the board). Playing seed is done by
%selecting a house on player's side, taking all beans in it, and moving it
%forward in counterclockwise direction by putting one seed on all houses
%that it passes through except the opponent storehouse (store house is a
%big hole on the left and the right side of the board, each representing
%player's score). If the last seed falls on a non-empty house, player
%continues its turn by taking all seeds in it and moving it forward again.
%If the last seed falls on the player's storehouse, player can continue his
%turn by playing new seed from any houses on player's side. If the last
%seed falls on an empty house on player's side, player takes all seed in
%player's house (whose last seed falls in it) and in the opposing house on
%opponent side to player's storeroom. This is called shooting and this ends
%player's turn. If the last seed falls on an empty house on opponent's
%side, player ends turns without shooting. Playing seed is done repeatedly
%by both player in turns until all seeds in all houses are taken. In the
%end of the game, player with most seeds in his storehouse wins the game.
%--------------------------------------------------------------------------

%CodeStart-----------------------------------------------------------------
%Resetting MATLAB environment
    clear
    clc
%Generating startup window
    global n_house n_bean
    begin=0;
    ScreenSize=get(0,'ScreenSize');
    startupwindow=figure('Name','Congklak Start Up',...
                         'WindowStyle','modal',...
                         'NumberTitle','Off',...
                         'Menubar','none',...
                         'Resize','off',...
                         'Units','pixels',...
                         'Position',[0.5*(ScreenSize(3)-300),...
                                     0.5*(ScreenSize(4)-100),...
                                     300,100],...
                         'DeleteFcn',@quitcongklakcustomfcn);
    uicontrol('Parent',startupwindow,...
              'Style','text',...
              'String','Number of houses on each side',...
              'HorizontalAlignment','left',...
              'BackgroundColor',[0.8,0.8,0.8],...
              'Units','normalized',...
              'Position',[0.15,0.715,0.55,0.15])
    housecountpopup=uicontrol('Parent',startupwindow,...
                              'Style','popup',...
                              'String',{4:10},...
                              'Value',4,...
                              'Units','normalized',...
                              'Position',[0.7,0.8,0.15,0.1]);
    uicontrol('Parent',startupwindow,...
              'Style','text',...
              'String','Number of beans in each house',...
              'HorizontalAlignment','left',...
              'BackgroundColor',[0.8,0.8,0.8],...
              'Units','normalized',...
              'Position',[0.15,0.49,0.55,0.15])
    beancountpopup=uicontrol('Parent',startupwindow,...
                             'Style','popup',...
                             'String',{1:20},...
                             'Value',7,...
                             'Units','normalized',...
                             'Position',[0.7,0.575,0.15,0.1]);
    uicontrol('Parent',startupwindow,...
              'Style','pushbutton',...
              'String','Run Congklak',...
              'Units','normalized',...
              'Position',[0.2,0.1,0.25,0.2],...
              'Callback',@startcongklakcustomfcn);
    uicontrol('Parent',startupwindow,...
              'Style','pushbutton',...
              'String','Quit',...
              'Units','normalized',...
              'Position',[0.55,0.1,0.25,0.2],...
              'Callback',@quitcongklakcustomfcn);
    while begin==0
        pause(0.1)
    end
    if begin==2
        return
    end
%Declaring global variable
    hand=0;
    handholeposition=NaN;
    board=zeros(1,(2*n_house)+2);
    playstat=0;
    playerinputstat=0;
    difficultylevel=1;
    initialseed=n_bean;
    animationtype=2;
    animationdelay=0.125;
%Generating user interface
    close(startupwindow)
    ScreenSize=get(0,'ScreenSize');
    mainwindow=figure('Name','Congklak',...
                      'NumberTitle','Off',...
                      'Menubar','none',...
                      'Resize','off',...
                      'Units','pixels',...
                      'Position',[0.5*(ScreenSize(3)-384),...
                                  0.5*(ScreenSize(4)-400),...
                                  384,400],...
                      'WindowKeyPressFcn',@keypressfcn,...
                      'WindowKeyReleaseFcn',@keyreleasefcn,...
                      'DeleteFcn',@closegamefcn);
    boardimage=axes('Parent',mainwindow,...
                    'Units','normalized',...
                    'Position',[0.025,0.45,0.95,0.4]);
	axis(boardimage,'off')
    rectangle('Parent',boardimage,...
              'Position',[0,0,1,1],...
              'Curvature',1,...
              'FaceColor',[0.5,0.25,0.125])
	chole=zeros(1,n_house);
    phole=zeros(1,n_house);
    takebutton=zeros(1,10);
    buttonarea=0.7/n_house;
    for count=1:n_house
        chole(count)=uicontrol('Parent',mainwindow,...
                               'Style','text',...
                               'String','0',...
                               'FontSize',12,...
                               'BackgroundColor',[1,0.75,0.75],...
                               'Units','normalized',...
                               'Position',[0.85+(0.1*buttonarea)-...
                                           (count*buttonarea),...
                                           0.775,0.8*buttonarea,0.05]);
        phole(count)=uicontrol('Parent',mainwindow,...
                               'Style','text',...
                               'String','0',...
                               'FontSize',12,...
                               'BackgroundColor',[0.75,0.75,1],...
                               'Units','normalized',...
                               'Position',[0.15+(0.1*buttonarea)+...
                                           ((count-1)*buttonarea),...
                                           0.475,0.8*buttonarea,0.05]);
        takebutton(count)=uicontrol('Parent',mainwindow,...
                                    'Style','pushbutton',...
                                    'Enable','off',...
                                    'Units','normalized',...
                                    'Position',[0.15+((count-1)*...
                                                      buttonarea),...
                                                0.35,buttonarea,0.09]);
    end
    for count=n_house+1:10
        takebutton(count)=uicontrol('Parent',mainwindow,...
                                    'Style','pushbutton',...
                                    'Visible','off',...
                                    'Units','normalized',...
                                    'Position',[0.15+((count-1)*...
                                                      buttonarea),...
                                                0.35,buttonarea,0.09]);
    end
    set(takebutton(1),'Callback',@takehole1fcn)
    set(takebutton(2),'Callback',@takehole2fcn)
    set(takebutton(3),'Callback',@takehole3fcn)
    set(takebutton(4),'Callback',@takehole4fcn)
    set(takebutton(5),'Callback',@takehole5fcn)
    set(takebutton(6),'Callback',@takehole6fcn)
    set(takebutton(7),'Callback',@takehole7fcn)
    set(takebutton(8),'Callback',@takehole8fcn)
    set(takebutton(9),'Callback',@takehole9fcn)
    set(takebutton(10),'Callback',@takehole10fcn)
    pscorehole=uicontrol('Parent',mainwindow,...
                         'Style','text',...
                         'String','0',...
                         'FontSize',18,...
                         'BackgroundColor',[0.5,0.5,1],...
                         'Units','normalized',...
                         'Position',[0.85,0.6125,...
                                     0.1,0.075]);
    cscorehole=uicontrol('Parent',mainwindow,...
                         'Style','text',...
                         'String','0',...
                         'FontSize',18,...
                         'BackgroundColor',[1,0.5,0.5],...
                         'Units','normalized',...
                         'Position',[0.05,0.6125,...
                                     0.1,0.075]);
	handbox=uicontrol('Parent',mainwindow,...
                      'Style','text',...
                      'String','0',...
                      'FontSize',12,...
                      'BackgroundColor','y',...
                      'Units','normalized',...
                      'Position',[0.46,0.625,0.08,0.05]);
    uicontrol('Parent',mainwindow,...
              'Style','pushbutton',...
              'String','Start Game',...
              'Units','normalized',...
              'Position',[0.15,0.15,0.2,0.05],...
              'Callback',@startgamefcn);
	levelpopup=uicontrol('Parent',mainwindow,...
                         'Style','popup',...
                         'String',{'Easy';'Normal';'Hard'},...
                         'HorizontalAlignment','center',...
                         'Units','normalized',...
                         'Position',[0.4,0.15,0.2,0.05],...
                         'Callback',@setlevelfcn);
    uicontrol('Parent',mainwindow,...
              'Style','pushbutton',...
              'String','Close Game',...
              'Units','normalized',...
              'Position',[0.65,0.15,0.2,0.05],...
              'Callback',@closegamefcn);
    instructionbox=uicontrol('Parent',mainwindow,...
                             'Style','text',...
                             'String',['Click Start Game button to',...
                                       ' begin the game...'],...
                             'Units','normalized',...
                             'Position',[0.1,0.05,0.8,0.04]);
%Initiating user interface
    drawboard(board,hand,handholeposition)
%Declaring CallbackFunction
    %Start of startcongklakcustomfcn
    function startcongklakcustomfcn(~,~)
        begin=1;
        n_house=get(housecountpopup,'Value')+3;
        n_bean=get(beancountpopup,'Value');
    end
    %End of startcongklakcustomfcn
    %Start of quitcongklakcustomfcn
    function quitcongklakcustomfcn(~,~)
        begin=2;
        delete(startupwindow)
    end
    %End of quitcongklakcustomfcn
    %Start of playseed
    function [board,turn]=playseed(board,turn,selectedhole,animationstat)
        %Changing hand color
        if strcmpi(turn,'p')
            set(handbox,'BackgroundColor',[0.9,0.9,1])
        elseif strcmpi(turn,'c')
            set(handbox,'BackgroundColor',[1,0.9,0.9])
        end
        %Performing normal seedplay
        turnend=0;
        while turnend==0
            %Taking seeds from selected hole
            if (animationstat~=0)&&(animationtype==1)
                drawboard(board,hand,selectedhole,0)
                pause(0.5*animationdelay)
                if playstat==0
                    return
                end
            elseif (animationstat~=0)&&(animationtype==2)
                for counter=1:10
                    drawboard(board,hand,selectedhole,counter/10)
                    pause(0.025*animationdelay)
                    if playstat==0
                        return
                    end
                end
            end
            hand=board(selectedhole);
            board(selectedhole)=0;
            if (animationstat~=0)&&(animationtype==1)
                drawboard(board,hand,selectedhole,0)
                pause(0.5*animationdelay)
                if playstat==0
                    return
                end
            elseif (animationstat~=0)&&(animationtype==2)
                for counter=10:-1:1
                    drawboard(board,hand,selectedhole,counter/10)
                    pause(0.0333*animationdelay)
                    if playstat==0
                        return
                    end
                end
            end
            %Moving seeds forward
            lasthole=selectedhole;
            while hand>0
                lasthole=lasthole+1;
                if lasthole>2*(n_house+1)
                    lasthole=lasthole-(2*(n_house+1));
                end
                if (lasthole==n_house+1)
                    if strcmpi(turn,'p')
                        if (animationstat~=0)&&(animationtype==1)
                            drawboard(board,hand,lasthole,0)
                            pause(0.5*animationdelay)
                            if playstat==0
                                return
                            end
                        elseif (animationstat~=0)&&(animationtype==2)
                            for counter=0:0.1:1
                                drawboard(board,hand,lasthole-1+counter,0)
                                pause(0.0333*animationdelay)
                                if playstat==0
                                    return
                                end
                            end
                            for counter=0:0.1:1
                                drawboard(board,hand,lasthole,counter)
                                pause(0.0333*animationdelay)
                                if playstat==0
                                    return
                                end
                            end
                        end
                        hand=hand-1;
                        board(lasthole)=board(lasthole)+1;
                        if (animationstat~=0)&&(animationtype==1)
                            drawboard(board,hand,lasthole,0)
                            pause(0.5*animationdelay)
                            if playstat==0
                                return
                            end
                        elseif (animationstat~=0)&&(animationtype==2)
                            for counter=1:-0.1:0
                                drawboard(board,hand,lasthole,counter)
                                pause(0.0333*animationdelay)
                                if playstat==0
                                    return
                                end
                            end
                        end
                    else
                        if (animationstat~=0)&&(animationtype==1)
                            drawboard(board,hand,lasthole,0)
                            pause(0.5*animationdelay)
                            if playstat==0
                                return
                            end
                        elseif (animationstat~=0)&&(animationtype==2)
                            for counter=0:0.1:1
                                drawboard(board,hand,lasthole-1+counter,0)
                                pause(0.0333*animationdelay)
                                if playstat==0
                                    return
                                end
                            end
                        end
                    end
                elseif (lasthole==(2*(n_house+1)))
                    if strcmpi(turn,'c')
                        if (animationstat~=0)&&(animationtype==1)
                            drawboard(board,hand,lasthole,0)
                            pause(0.5*animationdelay)
                            if playstat==0
                                return
                            end
                        elseif (animationstat~=0)&&(animationtype==2)
                            for counter=0:0.1:1
                                drawboard(board,hand,lasthole-1+counter,0)
                                pause(0.0333*animationdelay)
                                if playstat==0
                                    return
                                end
                            end
                            for counter=0:0.1:1
                                drawboard(board,hand,lasthole,counter)
                                pause(0.0333*animationdelay)
                                if playstat==0
                                    return
                                end
                            end
                        end
                        hand=hand-1;
                        board(lasthole)=board(lasthole)+1;
                        if (animationstat~=0)&&(animationtype==1)
                            drawboard(board,hand,lasthole,0)
                            pause(0.5*animationdelay)
                            if playstat==0
                                return
                            end
                        elseif (animationstat~=0)&&(animationtype==2)
                            for counter=1:-0.1:0
                                drawboard(board,hand,lasthole,counter)
                                pause(0.0333*animationdelay)
                                if playstat==0
                                    return
                                end
                            end
                        end
                    else
                        if (animationstat~=0)&&(animationtype==1)
                            drawboard(board,hand,lasthole,0)
                            pause(0.5*animationdelay)
                            if playstat==0
                                return
                            end
                        elseif (animationstat~=0)&&(animationtype==2)
                            for counter=0:0.1:1
                                drawboard(board,hand,lasthole-1+counter,0)
                                pause(0.0333*animationdelay)
                                if playstat==0
                                    return
                                end
                            end
                        end
                    end
                else
                    if (animationstat~=0)&&(animationtype==1)
                        drawboard(board,hand,lasthole,0)
                        pause(0.5*animationdelay)
                        if playstat==0
                            return
                        end
                    elseif (animationstat~=0)&&(animationtype==2)
                        for counter=0:0.1:1
                            drawboard(board,hand,lasthole-1+counter,0)
                            pause(0.0333*animationdelay)
                            if playstat==0
                                return
                            end
                        end
                        for counter=0:0.1:1
                            drawboard(board,hand,lasthole,counter)
                            pause(0.0333*animationdelay)
                            if playstat==0
                                return
                            end
                        end
                    end
                    hand=hand-1;
                    board(lasthole)=board(lasthole)+1;
                    if (animationstat~=0)&&(animationtype==1)
                        drawboard(board,hand,lasthole,0)
                        pause(0.5*animationdelay)
                        if playstat==0
                            return
                        end
                    elseif (animationstat~=0)&&(animationtype==2)
                        for counter=1:-0.1:0
                            drawboard(board,hand,lasthole,counter)
                            pause(0.0333*animationdelay)
                            if playstat==0
                                return
                            end
                        end
                    end
                end
                if playstat==0
                    return
                end
            end
            %Anticipating continuous turn
            if (board(lasthole)==1)||...
               (lasthole==n_house+1)||(lasthole==2*(n_house+1))
                turnend=1;
            else
                selectedhole=lasthole;
            end
        end
        %Anticipating hole shooting and replaying
        if strcmpi(turn,'p')
            if (lasthole>0)&&(lasthole<n_house+1)
                if animationstat~=0
                    set(instructionbox,...
                        'String','Shooting COM opposing house...')
                    pause(1)
                end
                board=shoothole(board,turn,lasthole,animationstat);
            end
            if lasthole~=n_house+1
                turn=switchturn(turn);
            else
                if animationstat~=0
                    set(instructionbox,...
                        'String',['Your turn ended in your house.',...
                                  ' It is your turn again!'])
                    pause(1);
                end
            end
        elseif strcmpi(turn,'c')
            if (lasthole>n_house+1)&&(lasthole<2*(n_house+1))
                if animationstat~=0
                    set(instructionbox,...
                        'String','COM is shooting your house...')
                    pause(1)
                end
                board=shoothole(board,turn,lasthole,animationstat);
            end
            if lasthole~=2*(n_house+1)
                turn=switchturn(turn);
            else
                if animationstat~=0
                    set(instructionbox,...
                        'String',['COM turn ended in its house.',...
                                  ' It is COM turn again!'])
                    pause(1);
                end
            end
        end
    end
    %End of playseed
    %Start of shoothole
    function board=shoothole(board,turn,shootinghole,animationstat)
        %Finding seed amount in the shooted hole
        temphand=0;
        shootedhole=(2*(n_house+1))-shootinghole;
        seedcount=board(shootedhole);
        %Taking all in the shootinghole
        if (animationstat~=0)&&(animationtype==1)
            drawboard(board,temphand,shootinghole,0)
            pause(0.25*animationdelay)
            if playstat==0
                return
            end
        elseif (animationstat~=0)&&(animationtype==2)
            for counter=0:0.1:1
                drawboard(board,temphand,shootinghole,counter)
                pause(0.025*animationdelay)
                if playstat==0
                    return
                end
            end
        end
        board(shootinghole)=0;
        temphand=1;
        if (animationstat~=0)&&(animationtype==1)
            drawboard(board,temphand,shootinghole,0)
            pause(0.25*animationdelay)
            if playstat==0
                return
            end
        elseif (animationstat~=0)&&(animationtype==2)
            for counter=1:-0.1:0
                drawboard(board,temphand,shootinghole,counter)
                pause(0.025*animationdelay)
                if playstat==0
                    return
                end
            end
        end
        %Taking all seeds in the shootedhole
        if (animationstat~=0)&&(animationtype==1)
            drawboard(board,temphand,shootedhole,0)
            pause(0.25*animationdelay)
            if playstat==0
                return
            end
        elseif (animationstat~=0)&&(animationtype==2)
            for counter=0:0.1:1
                drawboard(board,temphand,shootedhole,counter)
                pause(0.025*animationdelay)
                if playstat==0
                    return
                end
            end
        end
        board(shootedhole)=0;
        temphand=1+seedcount;
        if (animationstat~=0)&&(animationtype==1)
            drawboard(board,temphand,shootedhole,0)
            pause(0.25*animationdelay)
            if playstat==0
                return
            end
        elseif (animationstat~=0)&&(animationtype==2)
            for counter=1:-0.1:0
                drawboard(board,temphand,shootedhole,counter)
                pause(0.025*animationdelay)
                if playstat==0
                    return
                end
            end
        end
        %Putting all taken seeds in the scorehole
        if strcmpi(turn,'p')
            target=n_house+1;
        elseif strcmpi(turn,'c')
            target=2*(n_house+1);
        end
        if (animationstat~=0)&&(animationtype==1)
            drawboard(board,temphand,target,0)
            pause(0.25*animationdelay)
            if playstat==0
                return
            end
        elseif (animationstat~=0)&&(animationtype==2)
            for counter=0:0.1:1
                drawboard(board,temphand,target,counter)
                pause(0.025*animationdelay)
                if playstat==0
                    return
                end
            end
        end
        board(target)=board(target)+temphand;
        temphand=0;
        if (animationstat~=0)&&(animationtype==1)
            drawboard(board,temphand,target,0)
            pause(0.25*animationdelay)
            if playstat==0
                return
            end
        elseif (animationstat~=0)&&(animationtype==2)
            for counter=1:-0.1:0
                drawboard(board,temphand,target,counter)
                pause(0.025*animationdelay)
                if playstat==0
                    return
                end
            end
        end
    end
    %End of shoothole
    %Start of switchturn
    function turn=switchturn(turn)
        if strcmpi(turn,'p')
            turn='c';
        elseif strcmpi(turn,'c')
            turn='p';
        end
    end
    %End of switch turn
    %Start of playermove
    function selectedhole=playermove(board)
        %Checking available holes for selection and modifying push button
        for hole=1:n_house
            if board(hole)~=0
                set(takebutton(hole),'Enable','on')
            else
                set(takebutton(hole),'Enable','off')
            end
        end
        %Waiting for player selection input
        playerinputstat=0;
        while playerinputstat==0
            pause(0.01)
        end
        %Assigning player selection
        if isnan(playerinputstat)
            selectedhole=NaN;
            return
        else
            selectedhole=playerinputstat;
            playerinputstat=0;
            set(takebutton,'Enable','off')
            set(instructionbox,'String',['Playing your turn,',...
                                         ' please wait...'])
        end
    end
    %End of playermove
    %Start of commmove_easy
    function selectedhole=commove_easy(board)
        %Checking available holes for selection
        validhole=n_house+1+find(board(n_house+2:(2*n_house)+1)~=0);
        %Choosing the best hole
        decision=ceil(rand*numel(validhole));
        selectedhole=validhole(decision);
    end
    %End of commove_easy
    %Start of commove_normal
    function selectedhole=commove_normal(board)
        %Checking available holes for selection
        validhole=n_house+1+find(board(n_house+2:(2*n_house)+1)~=0);
        %Finding selection that allows continuous turn (with 25% chance)
        chance=rand;
        if chance>0.75
            for hole=numel(validhole):-1:1
                [~,turn]=playseed(board,'c',validhole(hole),0);
                if strcmpi(turn,'c')
                    %disp('Possible continuous turn found!')
                    selectedhole=validhole(hole);
                    return
                end
            end
        end
        %Choosing randomly when ther is no selection allows continuous turn
        if ~exist('selectedhole','var')
            %disp('Possible continuous turn not found!')
            decision=ceil(rand*numel(validhole));
            selectedhole=validhole(decision);
        end
    end
    %End of commove_normal
    %Start of comove_hard
    function selectedhole=commove_hard(board)
        %Checking available holes for selection
        validhole=n_house+1+find(board(n_house+2:(2*n_house)+1)~=0);
        %Finding selection that allows continuous turn
        for hole=numel(validhole):-1:1
            [~,turn]=playseed(board,'c',validhole(hole),0);
            if strcmpi(turn,'c')
                %disp('Possible continuous turn found!')
                selectedhole=validhole(hole);
                return
            end
        end
        %Choosing randomly when ther is no selection allows continuous turn
        if ~exist('selectedhole','var')
            %disp('Possible continuous turn not found!')
            decision=ceil(rand*numel(validhole));
            selectedhole=validhole(decision);
        end
    end
    %End of commove_hard
    %Start of drawboard
    function drawboard(board,hand,handholeposition,handheightposition)
        %Writing seed values in all hole
        for counter=1:n_house
            set(phole(counter),'String',num2str(board(counter)))
            set(chole(counter),'String',num2str(board(counter+n_house+1)))
        end
        %Writing seed values in scorehole
        set(pscorehole,'String',num2str(board(n_house+1)))
        set(cscorehole,'String',num2str(board(2*(n_house+1))))
        %Writing seed values in handbox
        set(handbox,'String',num2str(hand))
        %Placing handbox
        if isnan(handholeposition)
            set(handbox,'String',num2str(0))
            set(handbox,'Position',[0.46,0.625,0.08,0.05])
        elseif (handholeposition>=0)&&(handholeposition<1)
            tempval=[0.15+(0.1*buttonarea),...
                     0.625-(0.075*(handholeposition)),...
                     0.8*buttonarea,0.05];
            set(handbox,'Position',tempval-...
                                   [0.075*handheightposition,0,0,0])
        elseif (handholeposition>=1)&&(handholeposition<=n_house)
            tempval=[0.15+(0.1*buttonarea)+...
                          (buttonarea*(handholeposition-1)),...
                     0.55,0.8*buttonarea,0.05];
            set(handbox,'Position',tempval-...
                                   [0,0.075*handheightposition,0,0])
        elseif (handholeposition>n_house)&&(handholeposition<n_house+2)
            tempval=[0.85-(0.9*buttonarea),...
                     0.55+(0.075*(handholeposition-n_house)),...
                     0.8*buttonarea,0.05];
            set(handbox,'Position',tempval+...
                                   [0.075*handheightposition,0,0,0])
        elseif (handholeposition>n_house+1)&&...
               (handholeposition<=2*n_house+1)
            tempval=[0.85-(0.9*buttonarea)-...
                          ((handholeposition-n_house-2)*buttonarea),...
                     0.7,0.8*buttonarea,0.05];
            set(handbox,'Position',tempval+...
                                   [0,0.075*handheightposition,0,0])
        elseif (handholeposition>2*n_house+1)||...
               (handholeposition<2*n_house+3)
            tempval=[0.15+(0.1*buttonarea),...
                     0.7-(0.075*(handholeposition-(2*n_house)-1)),...
                     0.8*buttonarea,0.05];
            set(handbox,'Position',tempval-...
                                   [0.075*handheightposition,0,0,0])
        end
    end
    %End of drawboard
%Declaring LocalFunction
    %Start of setlevelfcn
    function setlevelfcn(~,~)
        difficultylevel=get(levelpopup,'Value');
    end
    %End of setlevelfcn
    %Start of stargamefcn
    function startgamefcn(~,~)
        %Locking levelpopup and selectionbutton
        set(levelpopup,'Enable','off')
        set(takebutton,'Enable','off')
        %Resetting board
        board=initialseed*ones(2*(n_house+1),1);
        board(n_house+1)=0;
        board(2*(n_house+1))=0;
        %Resetting board
        drawboard(board,hand,handholeposition,handholeposition)
        %Randomly choose player for first turn
        decision=rand;
        if decision<=0.5
            turn='c';
        elseif decision>0.5
            turn='p';
        end
        %Playing the game until all seeds are taken into score hole
        playstat=1;
        while (playstat==1)&&...
              (sum(board([1:n_house,n_house+2:(2*n_house)+1])==0)~=...
                                                                 2*n_house)
            if strcmpi(turn,'p')
                if sum(board(1:n_house)==0)~=n_house
                    set(instructionbox,...
                        'String',['Your turn, choose a hole to play',...
                                  ' your turn!'])
                    selectedhole=playermove(board);
                    if isnan(selectedhole)
                        return
                    end
                    [board,turn]=playseed(board,turn,selectedhole,1);
                    if playstat==0
                        return
                    end
                else
                    set(instructionbox,...
                        'String',['You got no available moves.',...
                                  ' It is COM turn again!'])
                    pause(1)
                    turn=switchturn(turn);
                end
            elseif strcmpi(turn,'c')
                if sum(board(n_house+2:(2*n_house)+1)==0)~=n_house
                    set(instructionbox,...
                        'String','COM turn, please wait...')
                    pause(1)
                    if difficultylevel==1
                        selectedhole=commove_easy(board);
                    elseif difficultylevel==2
                        selectedhole=commove_normal(board);
                    elseif difficultylevel==3
                        selectedhole=commove_hard(board);
                    end
                    [board,turn]=playseed(board,turn,selectedhole,1);
                    if playstat==0
                        return
                    end
                else
                    set(instructionbox,...
                        'String',['No available moves for COM.',...
                                  ' It is your turn again!'])
                    pause(1)
                    turn=switchturn(turn);
                end
            end
        end
        %Announcing winner
        if board(n_house+1)>board(2*(n_house+1))
            set(instructionbox,...
                'String',['Congratulations! You win! Press start to',...
                          ' play again!'])
        elseif board(n_house+1)<board(2*(n_house+1))
            set(instructionbox,...
                'String','Sorry, you lose! Press start to retry!')
        elseif board(n_house+1)==board(2*(n_house+1))
            set(instructionbox,...
                'String','It is a draw! Press start to retry!')
        end
        %Resetting user interface
        hand=0;
        handholeposition=NaN;
        set(handbox,'BackgroundColor','y')
        drawboard(board,hand,handholeposition)
        set(levelpopup,'Enable','on')
    end
    %End of startgamefcn
    %Start of keypressfcn
    function keypressfcn(~,event)
        switch event.Key
            case 'f'
                animationtype=1;
        end
    end
    %End of keypressfcn
    %Start of keyreleasefcn
    function keyreleasefcn(~,event)
        switch event.Key
            case 'f'
                animationtype=2;
        end
    end
    %End of keyreleasefcn
    %Start of takehole1fcn
    function takehole1fcn(~,~)
        playerinputstat=1;
    end
    %End of takehole1fcn
    %Start of takehole2fcn
    function takehole2fcn(~,~)
        playerinputstat=2;
    end
    %End of takehole2fcn
    %Start of takehole3fcn
    function takehole3fcn(~,~)
        playerinputstat=3;
    end
    %End of takehole3fcn
    %Start of takehole4fcn
    function takehole4fcn(~,~)
        playerinputstat=4;
    end
    %End of takehole4fcn
    %Start of takehole5fcn
    function takehole5fcn(~,~)
        playerinputstat=5;
    end
    %End of takehole5fcn
    %Start of takehole6fcn
    function takehole6fcn(~,~)
        playerinputstat=6;
    end
    %End of takehole6fcn
    %Start of takehole7fcn
    function takehole7fcn(~,~)
        playerinputstat=7;
    end
    %End of takehole7fcn
    %Start of takehole8fcn
    function takehole8fcn(~,~)
        playerinputstat=8;
    end
    %End of takehole8fcn
    %Start of takehole9fcn
    function takehole9fcn(~,~)
        playerinputstat=9;
    end
    %End of takehole9fcn
    %Start of takehole10fcn
    function takehole10fcn(~,~)
        playerinputstat=10;
    end
    %End of takehole10fcn
    %Start of closegamefcn
    function closegamefcn(~,~)
        set(instructionbox,'String','Closing the game, please wait...')
        playstat=0;
        playerinputstat=NaN;
        pause(1)
        delete(mainwindow)
    end
    %End of closegame
%CodeEnd-------------------------------------------------------------------

end