function varargout = mckv(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @mckv_OpeningFcn, ...
                   'gui_OutputFcn',  @mckv_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end

function pshow=COVERTP(nowmap)
ph1=imread('pic\h1.bmp');
pn1=imread('pic\n1.bmp');
pt1=imread('pic\t1.bmp');
pt2=imread('pic\t2.bmp');
pt3=imread('pic\t3.bmp');
pt4=imread('pic\t4.bmp');
pt5=imread('pic\t5.bmp');
pshow=[];
for count1=1:15;
    pstmp=[];
    for count2=1:15;
        count3=count2+(count1-1)*15;
        switch nowmap(count3)
            case 1
                pstmp=[pstmp;pt1];
            case 2
                pstmp=[pstmp;pt2];
            case 3
                pstmp=[pstmp;pt3];
            case 4
                pstmp=[pstmp;pt4];
            case 5
                pstmp=[pstmp;pt5];
            case 6
                pstmp=[pstmp;ph1];
            case 7
            	pstmp=[pstmp;pn1];
            otherwise
                pstmp=[pstmp;pt1];
        end
    end
    pshow=[pshow,pstmp];
end

function moveing2=MOVING(nextlocal,next2local)
global nowmap nowlocal;
    moveing2=0;  
    if nextlocal(3)==1
        nowmap(nextlocal(1),nextlocal(2))=7;
        moveing2=1;
    elseif nextlocal(3)==3
        nowmap(nextlocal(1),nextlocal(2))=6;
        moveing2=1;
    elseif (nextlocal(3)==4) & (next2local(3)==1)
        nowmap(next2local(1),next2local(2))=4;
        nowmap(nextlocal(1),nextlocal(2))=7;
        moveing2=1;
    elseif (nextlocal(3)==4) & (next2local(3)==3)
        nowmap(next2local(1),next2local(2))=5;
        nowmap(nextlocal(1),nextlocal(2))=7;
        moveing2=1;
    elseif (nextlocal(3)==5) & (next2local(3)==1)
        nowmap(next2local(1),next2local(2))=4;
        nowmap(nextlocal(1),nextlocal(2))=6;
        moveing2=1;
    elseif (nextlocal(3)==5) & (next2local(3)==3)
        nowmap(next2local(1),next2local(2))=5;
        nowmap(nextlocal(1),nextlocal(2))=6;
        moveing2=1;
    end
    if moveing2==1
        if nowlocal(3)==7
            nowmap(nowlocal(1),nowlocal(2))=1;
        elseif nowlocal(3)==6
            nowmap(nowlocal(1),nowlocal(2))=3;
        end
        nowlocal(1)=nextlocal(1);
        nowlocal(2)=nextlocal(2);
        nowlocal(3)=nowmap(nextlocal(1),nextlocal(2));
    end 
    
function mckv_OpeningFcn(hObject, eventdata, handles, varargin)
global winname;
Fname=inputdlg('input your name','input dialog');
set(handles.txtFname,'string',Fname);
winname=double(Fname{1,1})
handles.cnt=0;
set(handles.txtFnumber,'string',handles.cnt);
handles.highb=0;
st1=imread('pic\t1.bmp');
imshow(st1);
handles.output = hObject;
guidata(hObject, handles);

function varargout = mckv_OutputFcn(hObject, eventdata, handles) 
varargout{1} = handles.output;

function btnOpen_Callback(hObject, eventdata, handles)
global nowmap nowlocal selstage selsave;
[sel1 sel2]=uigetfile;
selstage=[sel2 sel1];
nowstg=fopen(selstage,'r');
nowmap=fscanf(nowstg,'%d',[15 15]);
nowmap=nowmap';
nowlocal=fscanf(nowstg,'%d',[3]);
nowlocal=nowlocal';
fclose(nowstg);
handles.cnt=0;
set(handles.txtFnumber,'string',handles.cnt);
selsave=['save\' sel1 'sav'];
nowsave=fopen(selsave,'r');
if nowsave==-1 
    dlmwrite(selsave,[99999 double('小棠')],'\t');
else    
    fclose(nowsave);
end
nb = textread(selsave, '%d', 'delimiter', '\t');
nb=nb';
set(handles.txtKname,'String',char(nb([2:length(nb)])));
handles.highb=nb(1);
set(handles.txtKnumber,'String',handles.highb);
guidata(hObject, handles);
imshow(COVERTP(nowmap));
axes(handles.map);

function btnReset_Callback(hObject, eventdata, handles)
global nowmap nowlocal selstage selsave;
nowstg=fopen(selstage,'r');
nowmap=fscanf(nowstg,'%d',[15 15]);
nowmap=nowmap';
nowlocal=fscanf(nowstg,'%d',[3]);
nowlocal=nowlocal';
fclose(nowstg);
handles.cnt=0;
set(handles.txtFnumber,'string',handles.cnt);
guidata(hObject, handles);
nowsave=fopen(selsave,'r');
if nowsave==-1 
    dlmwrite(selsave,[99999 double('小棠')],'\t');
else    
    fclose(nowsave);
end
nb = textread(selsave, '%d', 'delimiter', '\t');
nb=nb';
set(handles.txtKname,'String',char(nb([2:length(nb)])));
handles.highb=nb(1);
set(handles.txtKnumber,'String',handles.highb);
guidata(hObject, handles);
imshow(COVERTP(nowmap));
axes(handles.map);

function btnUp_Callback(hObject, eventdata, handles)
global nowmap nowlocal selsave winname;
if nowlocal(1)~=1
    nextlocal=[0 0 0];
    next2local=[0 0 0];
    if nowlocal(1)>2
        next2local=[nowlocal(1)-2 nowlocal(2) nowmap(nowlocal(1)-2,nowlocal(2))];
    end
    nextlocal=[nowlocal(1)-1 nowlocal(2) nowmap(nowlocal(1)-1,nowlocal(2))];
    if MOVING(nextlocal,next2local)==1
        handles.cnt=handles.cnt+1; 
        set(handles.txtFnumber,'string',handles.cnt);
        imshow(COVERTP(nowmap));
        if size(find(nowmap==4))==[0 1]
            if handles.highb > handles.cnt
                msgbox('new','過關');
                handles.highb=handles.cnt;
                set(handles.txtKnumber,'string',handles.highb);
                dlmwrite(selsave,[handles.cnt winname],'\t');
            else
                msgbox('過關了','過關');
            end
        end
        guidata(hObject, handles);
        axes(handles.map); 
    end
end

function btnLeft_Callback(hObject, eventdata, handles)
global nowmap nowlocal selsave winname;
if nowlocal(2)~=1
    nextlocal=[0 0 0];
    next2local=[0 0 0];
    if nowlocal(2)>2
        next2local=[nowlocal(1) nowlocal(2)-2 nowmap(nowlocal(1),nowlocal(2)-2)];
    end
    nextlocal=[nowlocal(1) nowlocal(2)-1 nowmap(nowlocal(1),nowlocal(2)-1)];
       if MOVING(nextlocal,next2local)==1 
        handles.cnt=handles.cnt+1;
        set(handles.txtFnumber,'string',handles.cnt);
        imshow(COVERTP(nowmap));
        if size(find(nowmap==4))==[0 1]
            if handles.highb > handles.cnt
                msgbox('new','過關');
                handles.highb=handles.cnt;
                set(handles.txtKnumber,'string',handles.highb);
                dlmwrite(selsave,[handles.cnt winname],'\t');
            else
                msgbox('過關了','過關');
            end
        end
        guidata(hObject, handles);
        axes(handles.map);  
       end
end

function btnRight_Callback(hObject, eventdata, handles)
global nowmap nowlocal selsave winname;
if nowlocal(2)~=15
    nextlocal=[0 0 0];
    next2local=[0 0 0];
    if nowlocal(2)<14
        next2local=[nowlocal(1) nowlocal(2)+2 nowmap(nowlocal(1),nowlocal(2)+2)];
    end
    nextlocal=[nowlocal(1) nowlocal(2)+1 nowmap(nowlocal(1),nowlocal(2)+1)];
    if MOVING(nextlocal,next2local)==1
        handles.cnt=handles.cnt+1;
        set(handles.txtFnumber,'string',handles.cnt);
        imshow(COVERTP(nowmap));
        if size(find(nowmap==4))==[0 1]
            if handles.highb > handles.cnt
                msgbox('new','過關');
                handles.highb=handles.cnt;
                set(handles.txtKnumber,'string',handles.highb);
                dlmwrite(selsave,[handles.cnt winname],'\t');
            else
                msgbox('過關了','過關');
            end
        end
        guidata(hObject, handles);
        axes(handles.map);  
    end
end

function btnDown_Callback(hObject, eventdata, handles)
global nowmap nowlocal selsave winname;
if nowlocal(1)~=15
    nextlocal=[0 0 0];
    next2local=[0 0 0];
    if nowlocal(1)<14
        next2local=[nowlocal(1)+2 nowlocal(2) nowmap(nowlocal(1)+2,nowlocal(2))];
    end
    nextlocal=[nowlocal(1)+1 nowlocal(2) nowmap(nowlocal(1)+1,nowlocal(2))];
    if MOVING(nextlocal,next2local)==1 
        handles.cnt=handles.cnt+1; 
        set(handles.txtFnumber,'string',handles.cnt); 
        imshow(COVERTP(nowmap));
        if size(find(nowmap==4))==[0 1]
            if handles.highb > handles.cnt
                msgbox('new','過關');
                handles.highb=handles.cnt;
                set(handles.txtKnumber,'string',handles.highb);
                dlmwrite(selsave,[handles.cnt winname],'\t');
            else
                msgbox('過關了','過關');
            end
        end
        guidata(hObject, handles);
        axes(handles.map);   
    end
end


function pushbutton7_Callback(hObject, eventdata, handles)
msgbox({'遊戲說明：';'';'點選右邊的「OPEN」按鈕';...
'選擇stage資料夾下的任一檔案開啟就可以了';...
'(預設附檔名為*.m會看不到，請改第二排為全部檔案)　　.';...
'「Reset」按鈕可以從來';...
'而上下左右按鈕則是控制人物的移動';...
'將箱子全部蓋住紅點即可過關。';'~~更詳細說明可看\help下之readme檔~~';'';''},'說明');
