对于改变随机数范围大小出现频率的操作
来源:互联网 发布:卫宁软件 编辑:程序博客网 时间:2024/04/30 14:01
最近在研究此书,花费几天时间,略有收获,所以写下此编文章,受个人能力限制,这个只是我的粗陋之见。
首先贴下此书源码下载地址,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(); } } } }
0 0
- 对于改变随机数范围大小出现频率的操作
- OpenCV改变图像大小的操作
- OpenCV改变图像大小的操作
- OpenCV改变图像大小的操作
- 对于求字符串中出现频率最高字符的算法实现
- 不改变button大小,扩大点击范围
- 整型的范围大小
- 15电气 齐振昊 实验名称:在VB中对于随机数ab的比较大小
- 实验名称:在VB中对于随机数ab的比较大小
- 取指定范围的随机数
- 产生某个范围的随机数
- 生成一定范围的随机数
- 生成指定范围的随机数
- 生成指定范围的随机数
- 生成指定范围的随机数
- 产生某一范围的随机数
- 得到指定范围的随机数
- 生成指定范围的随机数
- ZOJ 2480-Simplest Task in Windows
- CentOS 7 RedHat5 安装 munin2 详解
- How to slipstream SQL Server 2008 R2 and a SQL Server 2008 R2 Service Pack 1 (SP1)
- 第一次C++,计算两个数和与差
- iOS 链式编程/函数式编程写法
- 对于改变随机数范围大小出现频率的操作
- 深入浅出Symfony2 - 结合MongoDB开发LBS应用
- Tomcat6标准日志,按天输出日志,并禁止catalina.out文件[tomcat中catalina.out日志时间久变得很大]
- 第一次上机实验1—1 求和及差
- Subversion error: Repository moved permanently to please relocate 解决方法
- 第1次上机实验
- @RequestBody, @ResponseBody 注解详解
- UHF epc控制字计算
- C++实验1-两个整数的和与差