对于改变随机数范围大小出现频率的操作

  1. 以下内容参照《unity3d 人工智能编程》--Aung Sithu Kyaw,Clifford Peters ,Thet Naing Swe编著</span>  

最近在研究此书,花费几天时间,略有收获,所以写下此编文章,受个人能力限制,这个只是我的粗陋之见。

首先贴下此书源码下载地址,http://hzbook.com/Books/8580.html,请大家尊重知识产权,如果有需要自行购买(非广告)。



using UnityEngine;
using System.Collections;

public class SlotMachineWeighted : MonoBehaviour
{
    //滚动间隔
    public float spinDuration = 2.0f;
    //数字
    public int numberOfSym = 10;
    //组件
    public GameObject betResult;
    //标志位
    private bool isStartSpin = false;
    private bool isFirstReelSpinned = false;
    private bool isSecondReelSpinned = false;
    private bool isThirdReelSpinned = false;
    //赌金
    private int betAmount = 100;
    //作弊设置
    private int creditBalance = 1000;
    private ArrayList weightedReelPoll = new ArrayList();
    private int zeroProbability = 30;
    //滚轮
    private int firstReelResult = 0;
    private int secondReelResult = 0;
    private int thirdReelResult = 0;
    //计时器
    private float elapsedTime = 0.0f;

    // Use this for initialization
    void Start()
    {
        //初始化
        betResult.GetComponent<GUIText>().text = "";

        for (int i = 0; i < zeroProbability; i++)
        {
            weightedReelPoll.Add(0);
        }
        //剩余可能值
        int remainingValuesProb = (100 - zeroProbability) / 9;

        //初始化数组
        for (int j = 1; j < 10; j++)
        {
            for (int k = 0; k < remainingValuesProb; k++)
            {
                weightedReelPoll.Add(j);
            }
        }
    }

    void OnGUI()
    {
        //显示设置
        GUI.Label(new Rect(150, 40, 100, 20), "Your bet: ");
        betAmount = int.Parse(GUI.TextField(new Rect(220, 40, 50, 20), betAmount.ToString(), 25));

        GUI.Label(new Rect(300, 40, 100, 20), "Credits: " + creditBalance.ToString());

        if (GUI.Button(new Rect(200, 300, 150, 40), "Pull Lever"))
        {
            betResult.GetComponent<GUIText>().text = "";
            isStartSpin = true;
        }
    }

    void checkBet()
    {
        if (firstReelResult == secondReelResult && secondReelResult == thirdReelResult)
        {
            //最大胜率,赢得50倍
            betResult.GetComponent<GUIText>().text = "JACKPOT!";
            creditBalance += betAmount * 50;
        }
        else if (firstReelResult == 0 && thirdReelResult == 0)
        {
            //输掉一半,伪胜利
            betResult.GetComponent<GUIText>().text = "YOU WIN " + (betAmount / 2).ToString();
            creditBalance -= (betAmount / 2);
        }
        else if (firstReelResult == secondReelResult)
        {
            //吸引玩家,接近胜利的状态
            betResult.GetComponent<GUIText>().text = "AWW... ALMOST JACKPOT!";
        }
        else if (firstReelResult == thirdReelResult)
        {
            //赢得2倍奖金
            betResult.GetComponent<GUIText>().text = "YOU WIN " + (betAmount * 2).ToString();
            creditBalance -= (betAmount * 2);
        }
        else
        {
            //失败
            betResult.GetComponent<GUIText>().text = "YOU LOSE!";
            creditBalance -= betAmount;
        }
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (isStartSpin)
        {
            //计时
            elapsedTime += Time.deltaTime;
            //取数
            int randomSpinResult = Random.Range(0, numberOfSym);
            if (!isFirstReelSpinned)
            {
                //第一位数
                GameObject.Find("firstReel").GetComponent<GUIText>().text = randomSpinResult.ToString();
                if (elapsedTime >= spinDuration)
                {
                    //生成第一个数
                    int weightedRandom = Random.Range(0, weightedReelPoll.Count);
                    GameObject.Find("firstReel").GetComponent<GUIText>().text = weightedReelPoll[weightedRandom].ToString();
                    firstReelResult = (int)weightedReelPoll[weightedRandom];
                    isFirstReelSpinned = true;
                    elapsedTime = 0;
                }
            }
            else if (!isSecondReelSpinned)
            {
                //获得第二个数字
                GameObject.Find("secondReel").GetComponent<GUIText>().text = randomSpinResult.ToString();
                if (elapsedTime >= spinDuration)
                {
                    secondReelResult = randomSpinResult;
                    isSecondReelSpinned = true;
                    elapsedTime = 0;
                }
            }
            else if (!isThirdReelSpinned)
            {
                GameObject.Find("thirdReel").GetComponent<GUIText>().text = randomSpinResult.ToString();
                if (elapsedTime >= spinDuration)              
                    //判断生成的三个和第二个数是否相同
                    if ((firstReelResult == secondReelResult) &&
                        //第一个数和其他两个数不同
                        randomSpinResult != firstReelResult)
                    {
                        //the first two reels have resulted the same symbol
                        //but unfortunately the third reel missed
                        //so instead of giving a random number we'll return a symbol which is one less than the other 2
                        //作弊的操作,
                        randomSpinResult = firstReelResult - 1;
                        if (randomSpinResult < firstReelResult) randomSpinResult = firstReelResult - 1;
                        if (randomSpinResult > firstReelResult) randomSpinResult = firstReelResult + 1;
                        if (randomSpinResult < 0) randomSpinResult = 0;
                        if (randomSpinResult > 9) randomSpinResult = 9;
                        //重新赋值第三个数
                        GameObject.Find("thirdReel").GetComponent<GUIText>().text = randomSpinResult.ToString();
                        thirdReelResult = randomSpinResult;
                    }
                    else
                    {
                        //直接赋值,不需要作弊
                        int weightedRandom = Random.Range(0, weightedReelPoll.Count);
                        GameObject.Find("thirdReel").GetComponent<GUIText>().text = weightedReelPoll[weightedRandom].ToString();
                        thirdReelResult = (int)weightedReelPoll[weightedRandom];
                    }
                //更新标志位
                    isStartSpin = false;
                    elapsedTime = 0;
                    isFirstReelSpinned = false;
                    isSecondReelSpinned = false;

                    checkBet();
                }
            }
        }
    }



你可能感兴趣的:(算法,C#,人工智能,unity3d)