回 帖 发 新 帖 刷新版面

主题:根据 Ilikefox 老师的《小游戏puzzle》改编

*根据 Ilikefox 老师的 [原创]也来国庆献礼《小游戏puzzle》改编,贴上,请 Ilikefox 老师指点。

*[原创]也来国庆献礼《小游戏puzzle》在:
http://www.programfan.com/club/post-252277.html

_screen.visible=.f.
lujin=addbs(left(sys(16),rat('\',sys(16))-1))
SET defa to (lujin)
oPuzzle=CREATEOBJECT("puzzleform")
oPuzzle.show
on shutdown oPuzzle.destroy
READ events
set default to
on shutdown
_screen.visible=.t.
retu

DEFINE CLASS puzzleform AS form
    showwindow=2
    Height = 230
    DoCreate = .T.
    AutoCenter = .T.
    BorderStyle = 2
    Caption = "Puzzle"
    MaxButton = .F.
    Name = "frmPuzzle"

    ADD OBJECT panel1 AS panel WITH ;
        Top = 12, ;
        Left = 12, ;
        TabIndex = 2, ;
        Name = "Panel1"

    ADD OBJECT cmdshuffle AS CommandButton WITH ;
        Top = 192, ;
        Left = 12, ;
        Height = 25, ;
        Width = 54, ;
        Caption = "\<S 开新局", ;
        forecolor=rgb(0,0,100),;
        Name = "cmdShuffle"

    ADD OBJECT cmdbaocun AS CommandButton WITH ;
        Top = 192, ;
        Left = 70, ;
        Height = 25, ;
        Width = 54, ;
        forecolor=rgb(0,0,100),;
        enabled=.f.,;
        Caption = " \<B 保存"

    ADD OBJECT cmdqujiu AS CommandButton WITH ;
        Top = 192, ;
        Left = 126, ;
        Height = 25, ;
        Width = 54, ;
        forecolor=rgb(0,0,100),;
        Caption = "\<J 取残局", ;
        enabled=.f.

    proc cmdbaocun.click
        oPanel=thisform.panel1
        oPanel.LayoutArray=0  
        with thisform
            jiu=''
            oPanel.shuffle
            FOR nI=1 TO 15
                   oPanel.LayoutArray(nI)=.Panel1.controls(nI).position
                jiu1=oPanel.LayoutArray(nI)
                if jiu1>=10
                    jiu1=chr(jiu1+87)
                else
                    jiu1=allt(str(jiu1))
                endif
                jiu=jiu+jiu1
            ENDFOR

            delete file lujin+'jiuwj.mem'
            save to lujin+'jiuwj.mem' ALL LIKE jiu
            .cmdqujiu.enabled=.t.
        endwith
        oPanel.handalfocus    
        if vartype(oPuzzle.Panel1.lab)='O'
            this.enabled=.f.
        endif

    proc cmdqujiu.click
        LOCAL nValue, nPos, oPanel
        if file(lujin+'jiuwj.mem')
            RESTORE FROM lujin+'jiuwj.mem' ADDITIVE
        else
            retu
        endif

        oPanel=thisform.panel1
        oPanel.LayoutArray=0  

        for nValue=1 to 15
            cVal=allt(subs(jiu,nValue,1))
            if cVal>'9'
                cVal=allt(str(asc(cVal)-87))
            endif
            do WHILE .t.
                nPos=val(cVal)
                if oPanel.LayoutArray(nPos)=0
                    oPanel.LayoutArray(nPos)=nValue
                    exit
                endif
            enddo
        endfor

        oPanel.handalfocus    
        oPanel.shuffle
        if vartype(oPuzzle.Panel1.lab)='O'
            oPuzzle.Panel1.RemoveObject('lab')
        endif

    PROCEDURE QueryUnload
        CLEAR EVENTS

    PROCEDURE cmdshuffle.Click
        LOCAL nValue, nPos, oPanel
        =RAND(-1)
        oPanel=thisform.panel1
        oPanel.LayoutArray=0  
        FOR nValue=1 TO 15
            DO WHILE .t.
                nPos=INT(16*RAND()+1)
                IF oPanel.LayoutArray(nPos)=0       &&空位
                    oPanel.LayoutArray(nPos)=nValue &&赋值
                    EXIT
                ENDIF
            ENDDO
        ENDFOR
        oPanel.handalfocus    
        oPanel.shuffle
        if vartype(oPuzzle.Panel1.lab)='O'
            oPuzzle.Panel1.RemoveObject('lab')
        endif

    proc init
        with this
            .width=.panel1.left*2+.panel1.width
            if file(lujin+'jiuwj.mem')
                .cmdqujiu.enabled=.t.
            endif
        endwith

ENDDEFINE

DEFINE CLASS panel AS container
    DIMENSION layoutarray[16]
    Width =40*4+4
    Height = 164
    SpecialEffect = 1
    layoutstr = ""
    Name = "panel"
    backcolor=rgb(255,220,220)

    PROCEDURE shuffle
        WITH this
            FOR nI=1 TO 16
                nValue=.LayoutArray(nI)
                IF nValue>0
                    .controls(nValue).Position=nI
                ENDIF
            ENDFOR
        ENDWITH

    PROCEDURE movetile
        Lparameters oTile
        Local nPos,nRow,nCol
        nPos=oTile.Position
        nRow=Ceiling(nPos/4)
        nCol=nPos-(nRow-1)*4
        With This
            For nDegree=0 To 270 Step 90
                nNextRow=nRow+Sin(Dtor(nDegree))
                nNextCol=nCol+Cos(Dtor(nDegree))
                nNextPos=(nNextRow-1)*4+nNextCol
                If nNextRow*nNextCol<1 Or nNextRow>4 Or nNextCol>4
                    Loop
                Endif
                If .LayoutArray(nNextPos)=0                    &&空位则 :
                    .LayoutArray(nNextPos)=.LayoutArray(nPos)  && 1 填入LayoutArray
                    .LayoutArray(nPos)=0                       && 2 原位赋0
                    oTile.Position=nNextPos                    && 3 到新位
                    .getcurrentlayoutstr                       && 3 结果转换成16进制串
                    .checkwinningcondition                     && 4 是否成功
                    Exit
                Endif
            Endfor
        Endwith

    PROCEDURE getcurrentlayoutstr
        oPanel=thisform.panel1
        With This
            .LayoutStr=""
            For nI=1 To 16   &&结果转换成16进制串
                .LayoutStr=.LayoutStr+RIGHT(TRANSFORM(.LayoutArray(nI),"@0"),1)
            Endfor
        Endwith

    PROCEDURE checkwinningcondition  &&是否成功
        IF this.layoutstr="123456789ABCDEF0"
            this.addobject('lab','label')
            with this.lab
                .fontsize=10
                .FontBold=.t.
                .forecolor=rgb(0,100,0)
                .top=136
                .left=this.width-38
                .visible=.t.
                .backstyle=0
                .name='lab'
                .caption='成功'
            endwith
            thisform.cmdbaocun.enabled=.f.
        else
            thisform.cmdbaocun.enabled=.t.
        ENDIF

    PROCEDURE handalfocus   
        this.cmdFocusHolder.setfocus

    PROCEDURE Init
        Local nI
        With This
            .LayoutArray=0
            For nI=1 To 15
                .LayoutArray(nI)=nI
            Endfor
            For nI=1 To 15                              &&添加15个checkbox
                cTileNum=Transform(nI)
                .AddObject("Tile"+cTileNum,"Tile")
                .controls(nI).Caption=cTileNum
                .controls(nI).Visible=.T.
                .controls(nI).fontsize=10
                .controls(nI).FontBold=.t.
                .controls(nI).forecolor=rgb(0,0,180)
*                .controls(nI).picture='Myzfd.ico'
*                .controls(nI).PicturePosition=12
            ENDFOR
            .AddObject("cmdFocusHolder","CommandButton")
            .handalfocus    
            .shuffle
        Endwith

ENDDEFINE

DEFINE CLASS tile AS checkbox
    Height = 40
    Width = 40
    Style = 1       &&checkbox类似一个命令按钮显示
    position = 0    &&!
    Name = "tile"

    PROCEDURE position_assign
        LPARAMETERS vNewVal
        LOCAL nRow,nCol
        WITH THIS
            .Position =vNewVal          && m.vNewVal 新位置号
            nRow=CEILING(vNewVal/4)     && 新行
            nCol=vNewVal-4*(nRow-1)     && 新列
            .top=2+.height*(nRow-1)     && 新top
            .left=2+.width*(nCol-1)     && 新left
        ENDWITH

    PROCEDURE Click
        if vartype(oPuzzle.Panel1.lab)='O'
            oPuzzle.Panel1.RemoveObject('lab')
        endif
        This.Value=0
        this.parent.handalFocus
        This.Parent.MoveTile(This)
ENDDEFINE

回复列表 (共2个回复)

沙发

不错不错,把俺偷懒的部分填上了,用了mem文件。注意第二个按钮按下应该弹起。
VFP的强项是操作表,所以俺一般用自由表保存状态,不过用自己的扩展名使它看起来不象个表,而不用MEM文件;这一点微软是榜样,它的许多文件其实都是表(库文件、表单文件、报表文件、菜单文件等等)。
读别人程序比自己写程序要难,特别是没有任何注释,楼主还是有功底的。

板凳


修改了一下,请老师指点!

我来回复

您尚未登录,请登录后再回复。点此登录或注册