這是一個園長學習VFP(Visual Foxpro)的Blog歡迎同好一起來參與

星期四, 3月 18, 2010

VFP設計大樂透簽號程式


Green’s LOTTO VFP8版下載
Green’s LOTTO VFP9版下載

image
這是什麼啊? 沒錯,這是我多年前無聊時寫來玩的
我同事有拿去玩,聽說中獎率還蠻高的,只是他沒分我1/3就是了

程式的設計原理很簡單,就是亂數函式:Rand()

我們先來看看這個函式的作用(VFP 說明)



返回一個 0 到 1 之間的隨機數。

RAND([nSeedValue])


返回值


數值型



參數

nSeedValue
指定種子數值,它決定 rand( ) 函數返回的數值序列。

在第一次發出 rand( ) 函數時用種子數 nSeedValue,然後再使用不帶 nSeedValue 參數的 rand( ) 函數,將得到一個隨機數序列。如果第三次發出 rand( ) 函數時使用同樣的種子數值 nSeedValue,那麼 RAND( )返回同樣的隨機數序列。



如果第一次發出 RAND( ) 時使用的 nSeedValue 參數是負數,那麼將使用來自系統時鐘的種子值。若要獲得隨機程度最大的數字序列,可以最初用一個負的參數發出 rand( ) 函數,然後再不帶參數發出 rand( ) 函數。



如果省略了 nSeedValue 參數,rand( ) 函數使用默認的種子數值 100001。




示例


第一個示例使用 rand( ) 函數創建了包含 10 條隨機記錄的表,然後使用 min( ) 和 max( ) 函數來顯示表中的最大值和最小值。



第二個示例顯示 1 到 10 之間的一個隨機數。



CLOSE DATABASES
CREATE TABLE Random (cValue N(3))
FOR nItem = 1 TO 10 && 添加 10 條記錄,
APPEND BLANK
REPLACE cValue WITH 1 + 100 * RAND() && 插入隨機值
ENDFOR

CLEAR
LIST && 顯示值
gnMaximum = 1 && 初始化最小值
gnMinimum = 100 && 初始化最大值
SCAN
gnMinimum = MIN(gnMinimum, cValue)
gnMaximum = MAX(gnMaximum, cValue)
ENDSCAN
? '最小值是: ', gnMinimum && 顯示最小值
? '最大值是: ', gnMaximum && 顯示最大值

CLEAR
gnLower = 1
gnUpper = 10

? INT((gnUpper - gnLower + 1) * RAND() + gnLower)


請參閱


EXP() | PI() | MIN() | MAX()




從說明來看,其實它就是可以得到一組0~1之間的亂數,各位別小看這亂數,它可以玩很多東西的,這暫且不說

大家在簽樂透時不是有電腦選號嗎,其實它也是用亂數跑的,廢話少說~我們自己也來做一個吧~~



程式列表如下:




Lotto.prg








myform=CREATEOBJECT('lotto')

myform.show


READ EVENTS



*

DEFINE CLASS lotto AS form



    Height = 334

    Width = 437


    ShowWindow = 2


    DoCreate = .T.


    AutoCenter = .T.


    Picture = "lotto.jpg"


    Caption = "Lotto Lucky Ver1.0 by greencwi@gmail.com"


    ControlBox = .F.


    Closable = .F.


    Icon = "lotto.ico"


    KeyPreview = .T.


    Name = "LOTTO"



    ADD OBJECT commandbutton1 AS commandbutton WITH ;

        Top = 276, ;


        Left = 216, ;


        Height = 27, ;


        Width = 48, ;


        FontName = "標楷體", ;


        FontSize = 10, ;


        Caption = "開始", ;


        Name = "commandbutton1"



    ADD OBJECT label1 AS label WITH ;

        AutoSize = .T., ;


        FontBold = .F., ;


        FontName = "標楷體", ;


        FontSize = 12, ;


        Caption = "幸運號碼:", ;


        Height = 21, ;


        Left = 12, ;


        Top = 276, ;


        Width = 74, ;


        ForeColor = RGB(255,255,255), ;


        Name = "label1"



    ADD OBJECT n1 AS textbox WITH ;

        FontBold = .T., ;


        FontOutline = .F., ;


        FontShadow = .T., ;


        FontSize = 12, ;


        Alignment = 2, ;


        BorderStyle = 0, ;


        Height = 23, ;


        Left = 36, ;


        SpecialEffect = 1, ;


        Top = 12, ;


        Width = 48, ;


        Style = 0, ;


        DisabledBackColor = RGB(255,255,255), ;


        DisabledForeColor = RGB(128,0,64), ;


        BorderColor = RGB(255,255,255), ;


        Name = "n1"



    ADD OBJECT n2 AS textbox WITH ;

        FontBold = .T., ;


        FontOutline = .F., ;


        FontShadow = .T., ;


        FontSize = 12, ;


        Alignment = 2, ;


        BorderStyle = 0, ;


        Height = 23, ;


        Left = 96, ;


        SpecialEffect = 1, ;


        Top = 12, ;


        Width = 48, ;


        Style = 0, ;


        DisabledBackColor = RGB(255,255,255), ;


        DisabledForeColor = RGB(128,0,64), ;


        BorderColor = RGB(255,255,255), ;


        Name = "n2"



    ADD OBJECT n3 AS textbox WITH ;

        FontBold = .T., ;


        FontOutline = .F., ;


        FontShadow = .T., ;


        FontSize = 12, ;


        Alignment = 2, ;


        BorderStyle = 0, ;


        Height = 23, ;


        Left = 156, ;


        SpecialEffect = 1, ;


        Top = 12, ;


        Width = 48, ;


        Style = 0, ;


        DisabledBackColor = RGB(255,255,255), ;


        DisabledForeColor = RGB(128,0,64), ;


        BorderColor = RGB(255,255,255), ;


        Name = "n3"



    ADD OBJECT n4 AS textbox WITH ;

        FontBold = .T., ;


        FontOutline = .F., ;


        FontShadow = .T., ;


        FontSize = 12, ;


        Alignment = 2, ;


        BorderStyle = 0, ;


        Height = 23, ;


        Left = 216, ;


        SpecialEffect = 1, ;


        Top = 12, ;


        Width = 48, ;


        Style = 0, ;


        DisabledBackColor = RGB(255,255,255), ;


        DisabledForeColor = RGB(128,0,64), ;


        BorderColor = RGB(255,255,255), ;


        Name = "n4"



    ADD OBJECT n5 AS textbox WITH ;

        FontBold = .T., ;


        FontOutline = .F., ;


        FontShadow = .T., ;


        FontSize = 12, ;


        Alignment = 2, ;


        BorderStyle = 0, ;


        Height = 23, ;


        Left = 276, ;


        SpecialEffect = 1, ;


        Top = 12, ;


        Width = 48, ;


        Style = 0, ;


        DisabledBackColor = RGB(255,255,255), ;


        DisabledForeColor = RGB(128,0,64), ;


        BorderColor = RGB(255,255,255), ;


        Name = "n5"



    ADD OBJECT n6 AS textbox WITH ;

        FontBold = .T., ;


        FontOutline = .F., ;


        FontShadow = .T., ;


        FontSize = 12, ;


        Alignment = 2, ;


        BorderStyle = 0, ;


        Height = 23, ;


        Left = 336, ;


        SpecialEffect = 1, ;


        Top = 12, ;


        Width = 48, ;


        Style = 0, ;


        DisabledBackColor = RGB(255,255,255), ;


        DisabledForeColor = RGB(128,0,64), ;


        BorderColor = RGB(255,255,255), ;


        Name = "n6"



    ADD OBJECT grid1 AS grid WITH ;

        ColumnCount = 6, ;


        HeaderHeight = 0, ;


        Height = 165, ;


        Left = 36, ;


        Panel = 1, ;


        RecordSource = "ltmp", ;


        ScrollBars = 2, ;


        Top = 48, ;


        Width = 367, ;


        Name = "grid1", ;


        Column1.ControlSource = "", ;


        Column1.Width = 46, ;


        Column1.Name = "Column1", ;


        Column2.ControlSource = "", ;


        Column2.Width = 61, ;


        Column2.Name = "Column2", ;


        Column3.ControlSource = "", ;


        Column3.Width = 58, ;


        Column3.Name = "Column3", ;


        Column4.ControlSource = "", ;


        Column4.Width = 59, ;


        Column4.Name = "Column4", ;


        Column5.ControlSource = "", ;


        Column5.Width = 58, ;


        Column5.Name = "Column5", ;


        Column6.ControlSource = "", ;


        Column6.Width = 60, ;


        Column6.Name = "Column6"



    ADD OBJECT lotto.grid1.column1.header1 AS header WITH ;

        Caption = "Header1", ;


        Name = "Header1"



    ADD OBJECT lotto.grid1.column1.text1 AS textbox WITH ;

        BorderStyle = 0, ;


        Margin = 0, ;


        ForeColor = RGB(0,0,0), ;


        BackColor = RGB(255,255,255), ;


        Name = "Text1"



    ADD OBJECT lotto.grid1.column2.header1 AS header WITH ;

        Caption = "Header1", ;


        Name = "Header1"



    ADD OBJECT lotto.grid1.column2.text1 AS textbox WITH ;

        BorderStyle = 0, ;


        Margin = 0, ;


        ForeColor = RGB(0,0,0), ;


        BackColor = RGB(255,255,255), ;


        Name = "Text1"



    ADD OBJECT lotto.grid1.column3.header1 AS header WITH ;

        Caption = "Header1", ;


        Name = "Header1"



    ADD OBJECT lotto.grid1.column3.text1 AS textbox WITH ;

        BorderStyle = 0, ;


        Margin = 0, ;


        ForeColor = RGB(0,0,0), ;


        BackColor = RGB(255,255,255), ;


        Name = "Text1"



    ADD OBJECT lotto.grid1.column4.header1 AS header WITH ;

        Caption = "Header1", ;


        Name = "Header1"



    ADD OBJECT lotto.grid1.column4.text1 AS textbox WITH ;

        BorderStyle = 0, ;


        Margin = 0, ;


        ForeColor = RGB(0,0,0), ;


        BackColor = RGB(255,255,255), ;


        Name = "Text1"



    ADD OBJECT lotto.grid1.column5.header1 AS header WITH ;

        Caption = "Header1", ;


        Name = "Header1"



    ADD OBJECT lotto.grid1.column5.text1 AS textbox WITH ;

        BorderStyle = 0, ;


        Margin = 0, ;


        ForeColor = RGB(0,0,0), ;


        BackColor = RGB(255,255,255), ;


        Name = "Text1"



    ADD OBJECT lotto.grid1.column6.header1 AS header WITH ;

        Caption = "Header1", ;


        Name = "Header1"



    ADD OBJECT lotto.grid1.column6.text1 AS textbox WITH ;

        BorderStyle = 0, ;


        Margin = 0, ;


        ForeColor = RGB(0,0,0), ;


        BackColor = RGB(255,255,255), ;


        Name = "Text1"



    ADD OBJECT label2 AS label WITH ;

        AutoSize = .T., ;


        FontBold = .F., ;


        FontName = "標楷體", ;


        FontSize = 12, ;


        Caption = "產生組數:", ;


        Height = 21, ;


        Left = 12, ;


        Top = 252, ;


        Width = 74, ;


        ForeColor = RGB(255,255,255), ;


        Name = "label2"



    ADD OBJECT label3 AS label WITH ;

        AutoSize = .T., ;


        FontBold = .F., ;


        FontName = "標楷體", ;


        FontSize = 12, ;


        Caption = "樂透號數:", ;


        Height = 21, ;


        Left = 12, ;


        Top = 228, ;


        Width = 74, ;


        ForeColor = RGB(255,255,255), ;


        Name = "label3"



    ADD OBJECT label4 AS label WITH ;

        AutoSize = .T., ;


        FontName = "標楷體", ;


        FontSize = 12, ;


        Caption = "玩法:", ;


        Height = 21, ;


        Left = 204, ;


        Top = 228, ;


        Width = 42, ;


        ForeColor = RGB(255,255,255), ;


        Name = "label4"



    ADD OBJECT l_type AS combobox WITH ;

        FontSize = 10, ;


        RowSourceType = 1, ;


        RowSource = "1.出現最多號碼,2.隨機", ;


        Height = 24, ;


        Left = 240, ;


        Style = 2, ;


        Top = 228, ;


        Width = 156, ;


        Name = "l_type"



    ADD OBJECT l_num AS textbox WITH ;

        FontSize = 10, ;


        Alignment = 1, ;


        Value = 49, ;


        Height = 23, ;


        InputMask = "9999999999", ;


        Left = 84, ;


        MaxLength = 10, ;


        Top = 228, ;


        Width = 100, ;


        Name = "l_num"



    ADD OBJECT l_outnum AS textbox WITH ;

        FontSize = 10, ;


        Alignment = 1, ;


        Value = 1, ;


        Height = 23, ;


        InputMask = "9999999999", ;


        Left = 84, ;


        MaxLength = 10, ;


        Top = 252, ;


        Width = 100, ;


        Name = "l_outnum"



    ADD OBJECT l_lucky AS textbox WITH ;

        FontSize = 10, ;


        Alignment = 1, ;


        Value = 1, ;


        Height = 23, ;


        InputMask = "9999999999", ;


        Left = 84, ;


        MaxLength = 10, ;


        Top = 276, ;


        Width = 100, ;


        Name = "l_lucky"



    ADD OBJECT l_disp AS label WITH ;

        FontBold = .T., ;


        FontSize = 14, ;


        BackStyle = 0, ;


        Caption = "", ;


        Height = 26, ;


        Left = 388, ;


        Top = 11, ;


        Width = 44, ;


        ForeColor = RGB(255,255,128), ;


        Name = "l_disp"



    ADD OBJECT l_print AS commandbutton WITH ;

        Top = 276, ;


        Left = 288, ;


        Height = 27, ;


        Width = 48, ;


        FontName = "標楷體", ;


        FontSize = 10, ;


        Caption = "列印", ;


        Name = "l_print"



    ADD OBJECT label5 AS label WITH ;

        AutoSize = .T., ;


        FontBold = .F., ;


        FontName = "標楷體", ;


        FontSize = 12, ;


        Caption = "訊息", ;


        Height = 21, ;


        Left = 12, ;


        Top = 312, ;


        Width = 34, ;


        ForeColor = RGB(255,255,255), ;


        Name = "label5"



    ADD OBJECT cmsg AS textbox WITH ;

        FontName = "標楷體", ;


        FontSize = 10, ;


        Alignment = 0, ;


        Value = "祝您中大獎,中了以後分我1/3,謝謝!! 請按開始執行.....", ;


        Enabled = .F., ;


        Height = 23, ;


        Left = 48, ;


        Top = 307, ;


        Width = 372, ;


        DisabledBackColor = RGB(255,255,128), ;


        DisabledForeColor = RGB(255,0,0), ;


        Name = "cMsg"



    PROCEDURE load

        CREATE CURSOR ltmp (n1 n(2,0),n2 n(2,0),n3 n(2,0),n4 n(2,0),n5 n(2,0),n6 n(2,0))


    ENDPROC



    PROCEDURE btn_defaexit.Click

        quit


        *DODEFAULT()


    ENDPROC



    PROCEDURE commandbutton1.Click

        *


        Local wl_lucky


        wl_lucky=Thisform.l_lucky.Value


        wl_num=Thisform.l_num.Value


        wl_outnum=Thisform.l_outnum.Value


        IF wl_lucky<=0 OR wl_outnum<=0


            =messagebox("輸入的組數,或幸運號碼錯誤!",0,"錯誤",3000)


            RETURN .F.


        endif


        *


        Select ltmp


        ZAP


        SET ESCAPE ON


        is_run=.T.


        ON ESCAPE is_run=.F.


        Do Case


            Case Thisform.l_type.ListIndex=1    &&取最大數


                For r=1 To wl_outnum


                    thisform.cmsg.Value="若要停止執行請按[ESC],目前執行..."+ALLTRIM(STR(r))+"/"+ALLTRIM(STR(wl_outnum))


                    DIMENSION la[wl_num]


                    FOR u=1 TO wl_num


                        la[u]=0


                    NEXT u


        *


                    For j=1 To 6


                        nj='n'+Alltrim(Str(j))


                        *產生號碼


                        Do While .T.


                            IF !is_run


                                is_run=.t.


                                *=messagebox("輸入的組數,或幸運號碼錯誤!",0,"錯誤",3000)


                                *return


                                thisform.cmsg.Value="請選擇....."


                                ans=MESSAGEBOX("您確定要停止出號嗎?",4,"您按了ESC鍵!")


                                thisform.cmsg.Value=""


                                IF ans=6


                                    thisform.cmsg.Value="祝您中大獎,中了以後分我1/3,謝謝!! 請按開始執行....."


                                    RETURN


                                endif


                            endif


                            For i = 1 To wl_lucky


                                vv=Int(Rand()*wl_num)+1


                                la[vv]=la[vv]+1


                                Thisform.&nj..Value=vv


                            Next i


                            wget=0


                            wlmax=0


                            *取最大


                            FOR cc=1 TO wl_num


                                IF la[cc]>wget


                                    wget=la[cc]


                                    wlmax=cc


                                endif


                            NEXT cc


                            thisform.l_disp.Caption=ALLTRIM(STR(wlmax))


                            *檢查是否重覆


                            is_dup=.F.


                            For k=1 To j-1


                                nk='n'+Alltrim(Str(k))


                                If wlmax=Thisform.&nk..Value


                                    is_dup=.T.


                                Endif


                            Next k


                            *


                            If !is_dup


                                *Thisform.&nj..Value=wlmax


                                Exit


                            Endif


                        ENDDO


                        *


                    Next j


                *    If wl_outnum>1


                        Select ltmp


                        Append Blank


                        DIMENSION sortnum[6]


                        FOR rr=1 TO 6


                            nrr='n'+Alltrim(Str(rr))


                            sortnum[rr]=Thisform.&nrr..Value


                        NEXT rr


                        =ASORT(sortnum)


                        For rr=1 To 6


                            nrr='n'+Alltrim(Str(rr))


                            Replace &nrr With sortnum[rr]


                        Next rr


                *    Endif


                Next r


                Select ltmp


                Go Top


                Thisform.Refresh()


                *******************


            Case Thisform.l_type.ListIndex=2    &&隨機


                For r=1 To wl_outnum


                    thisform.cmsg.Value="若要停止執行請按[ESC],目前執行..."+ALLTRIM(STR(r))+"/"+ALLTRIM(STR(wl_outnum))


                    For j=1 To 6


                        nj='n'+Alltrim(Str(j))


                        Do While .T.


                            IF !is_run


                                is_run=.t.


                                *=messagebox("輸入的組數,或幸運號碼錯誤!",0,"錯誤",3000)


                                *return


                                thisform.cmsg.Value="請選擇....."


                                ans=MESSAGEBOX("您確定要停止出號嗎?",4,"您按了ESC鍵!")


                                thisform.cmsg.Value=""


                                IF ans=6


                                    thisform.cmsg.Value="祝您中大獎,中了以後分我1/3,謝謝!! 請按開始執行....."


                                    RETURN


                                endif


                            endif


                            *


                            For i = 1 To wl_lucky


                                Thisform.&nj..Value=Int(Rand()*wl_num)+1


                            Next i


                            *檢查是否重覆


                            is_dup=.F.


                            For k=1 To j-1


                                nk='n'+Alltrim(Str(k))


                                If Thisform.&nj..Value==Thisform.&nk..Value


                                    is_dup=.T.


                                Endif


                            Next k


                            If !is_dup


                                Exit


                            Endif


                        Enddo


                    Next j


                    *If wl_outnum>1


                        Select ltmp


                        Append Blank


                        DIMENSION sortnum[6]


                        FOR rr=1 TO 6


                            nrr='n'+Alltrim(Str(rr))


                            sortnum[rr]=Thisform.&nrr..Value


                        NEXT rr


                        =ASORT(sortnum)


                        For rr=1 To 6


                            nrr='n'+Alltrim(Str(rr))


                            Replace &nrr With sortnum[rr]


                        Next rr


                    *Endif


                Next r


                Select ltmp


                Go Top


                Thisform.Refresh()


        Endcase


        ON ESCAPE


        thisform.cmsg.Value="祝您中大獎,中了以後分我1/3,謝謝!! 請按開始執行....."


    ENDPROC



    PROCEDURE l_type.Init

        this.ListIndex=1


    ENDPROC



    PROCEDURE l_print.Click

        Select ltmp


        Go Top


        If !Eof()


            Report Form Report\lotto To Printer Prompt


        Endif


    ENDPROC



ENDDEFINE

*


*-- EndDefine: lotto


**************************************************


沒有留言: