欢迎光临散文网 会员登陆 & 注册

jass基础-回评-随机数问题

2023-08-07 02:33 作者:庞各庄大棚  | 我要投稿

#include "../../jass/BlizzardAPI.j"

library demo initializer test


    // 1.数组定义了 0~10,11个数,用随机取其中n个,且n小于11大于0,如何保证每次取的n个数不同/唯一?,

    //   是否有办法在取值后临时排除被取的数,不加入后续取值


    globals

        // 思路1: 记录已经随到的数据,每次随之前,先检查是否重复,如果重复就再随一次,直到随到了不重复的

        // 思路2: 建一个新的数组,每次随完,都对新数组进行重新赋值


        integer array baseArr

        integer array ranArr

        integer baseCount = 6

        integer ranCount = 0

    endglobals


    // function isRepeated takes integer index returns boolean

    //     local integer i = 0

    //     loop

    //         exitwhen i >= ranCount

    //         if index == ranArr[i] then

    //             return true

    //         endif

    //         set i = i + 1

    //     endloop

    //     return false

    // endfunction


    // function ranInt takes nothing returns integer

    //     local integer index = GetRandomInt(0, baseCount - 1)

    //     local integer result

    //     if ranCount >= baseCount then

    //         return - 999

    //     endif

    //     if isRepeated(index) then

    //         set result = ranInt()

    //     else

    //         set ranArr[ranCount] = index

    //         set ranCount = ranCount + 1

    //         set result = baseArr[index]

    //     endif

    //     return result

    // endfunction

    function ranInt takes nothing returns integer

        local integer index = GetRandomInt(0, ranCount - 1)

        local integer result

        if ranCount == 0 then

            return - 999

        endif

        set result = ranArr[index]

        if index != ranCount - 1 then

            set ranArr[index] = ranArr[ranCount - 1]

        endif

        set ranCount = ranCount - 1

        return result

    endfunction


    function doSomething takes nothing returns nothing

        local integer i = ranInt()

        if i == - 999 then

            call BJDebugMsg("没有可用的数字")

        else

            call BJDebugMsg(I2S(i))

        endif

    endfunction


    function initRanArr takes nothing returns nothing

        local integer i = 0

        loop

            exitwhen i >= baseCount

            set ranArr[i] = baseArr[i]

            set i = i + 1

        endloop

    endfunction


    function initBaseArr takes nothing returns nothing

        set baseArr[0] = 1

        set baseArr[1] = 165

        set baseArr[2] = 21

        set baseArr[3] = 56

        set baseArr[4] = 8

        set baseArr[5] = 17

        call initRanArr()

        set ranCount = baseCount

    endfunction


    function triggerInit takes nothing returns nothing

        local trigger t = CreateTrigger()

        call TriggerRegisterPlayerChatEvent(t, Player(0), "1", true)

        call TriggerAddAction(t, function doSomething)

        set t = null

    endfunction


    function gameInit takes nothing returns nothing

        local unit u = CreateUnit(Player(0), 'hfoo', - 100, - 100, 0)

        call FogEnable(false)

        call FogMaskEnable(false)

    endfunction


    function test takes nothing returns nothing

        call gameInit()

        call triggerInit()

        call initBaseArr()

    endfunction

endlibrary

jass基础-回评-随机数问题的评论 (共 条)

分享到微博请遵守国家法律