gpt4 book ai didi

c++ - 如何给游戏添加概率?

转载 作者:太空宇宙 更新时间:2023-11-04 11:25:48 25 4
gpt4 key购买 nike

该程序运行良好,但就像真正的赌场老板一样。我希望在不增加更多可能性的情况下,支出尽可能少。我想要这样做的方法是为每个数字添加概率。有 8 种可能性(2、3、4、5、6、7、8、9),我希望 7 种可能性最少。所以我想将概率设置为 2-6 为 75%(每个 15%),8-9 为 20%(每个 10%)和 7 为 5%。有人能帮我吗?

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <ctime>

using namespace std;
int Random(int low, int high);
int Result(int a, int b, int c, int chips, int bet);
int main()
{
srand( time(0) );
int low1(2), low2(2), low3(2);
int high1(9), high2(9), high3(9);
int menu=0;
int bet =0;
bool quit=0;
cout << "Player's chips: $1000" << endl;
int chips=1000;
while (!quit)
{
cout << "1) Play slot. 2) Exit.";
cin >> menu;
switch (menu)
{
case 1:
{
cout << "Enter your bet: ";
cin >> bet;
if (bet<=0 || bet>chips)
{
cout << "You did not enter a valid bet." << endl;
menu=1;
}
else
{
int a = Random(low1,high1);
int b = Random(low2,high2);
int c = Random(low3,high3);
cout << a << " " << b << " " << c << endl;
chips = Result(a,b,c,chips,bet);
cout << "Player's chips: $" << Result(a,b,c,chips,bet) << endl;
}
break;
}
case 2:
{
cout << "Exiting..." << endl;
quit=1;
break;
}
default:
{
cout << "Not a valid menu !"<<endl;
}
}
}
if (chips <=0)
{
cout << "You have $0 ! Game Over !" << endl;
quit=1;
}
}

int Random(int low, int high)
{
int random;
random = low + rand() % ((high+1)-low);
return random;
}

int Result(int a, int b, int c, int chips, int bet)
{
if ((a==7 && b==7 && c==7))
{
cout << "You won 10 times your bet !($" << (bet*10) << ")" << endl;
chips=chips+(bet*10);
}
if ((a==b==c) && !(a==7 && b==7 && c==7))
{
cout << "You won 5 times your bet !($" << (bet*5) << ")" << endl;
chips=chips+(bet*5);
}
if ((a==b || a==c || b==c) && !(a==b==c) && !(a==7 && b==7 && c==7))
{
chips=chips+(bet*3);
cout << "You won 3 times your bet ! ($" << (bet*3) << ")" << endl;
}
else
{
cout << "You lost your bet ! ($-" << bet << ")" << endl;
chips=chips-bet;
}
return chips;
}

最佳答案

引入一个值数组 [0.05, 0.15, 0.25, 0.4, 0.55, 0.7, 0.85] 对应于值 7, 8, 9, 2, 3, 4, 5. 6 如果生成的随机数 > 0.85。

然后得到一个从0到1的随机数r。

if r <=0.05 then it is 7
else if r <= 0.15 then it is 8
else if r <= 0.1 then it is 9
...
else if r <= 0.85 then it is 5
else it is 6

如果你愿意,你可以弄清楚如何将它写成一个循环。

关于c++ - 如何给游戏添加概率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26745063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com