
#include "stdioh"
#include "stdlibh"
#include "timeh"
enum {CONTINUE,WON,LOST};
void main()
{
int status=CONTINUE;
int i=0,j=0,add,k;
srand((unsigned)time(NULL));
while(i==0)
i=rand()%6;
printf("1: %d\n",i);
while(j==0)
j=rand()&6;
printf("2: %d\n",j);
add=i+j;
if(add==11 || add==7)
status=WON;
if(add==2 || add==3 || add==12)
status=LOST;
if(status==WON)
printf("You Win!\n");
else if(status==LOST)
printf("YOU LOST!\n");
else
{
for(k=1;k<=7;k++)
{
i=0;
j=0;
printf("%d\n",k);
while(i==0)
i=rand()%6;
printf("1: %d\n",i);
while(j==0)
j=rand()%6;
printf("2: %d\n",j);
if((i+j)==add)
{
status=WON;
break;
}
}
if(status==WON)
printf("YOU WIN!\n");
else
printf("YOU LOST!\n");
}
getch();
}
public class Test{
public static void main(String args []){
int i1=0,i2=0,i3=0,i4=0,i5=0,i6=0;
for(int q=0;q<5000;q++){
int w=1+(int)(Mathrandom()6);
if(w==1){
i1++;
}else if(w==2){
i2++;
}else if(w==3){
i3++;
}else if(w==4){
i4++;
}else if(w==5){
i5++;
}else{
i6++;
}
}
Systemoutprintln("出现一点的次数是:"+i1);
Systemoutprintln("出现二点的次数是:"+i2);
Systemoutprintln("出现三点的次数是:"+i3);
Systemoutprintln("出现四点的次数是:"+i4);
Systemoutprintln("出现五点的次数是:"+i5);
Systemoutprintln("出现六点的次数是:"+i6);
}
}结果
7次。
扔骰子是一个概率问题,每扔出一面的概率都是1/6,所以在第7次之后就一定会出现重复的情况,但是在前7次当中可能会出现重复的情况。
概率问题是数学问题当中的一个计算,概率论,是研究随机现象数量规律的数学分支。
参考下面的代码
play 可能有问题,主要是没说清楚在保留牌的时候, 输入Ace 或者 "Ace Ace" 有什么区别,到底是输入一次 Ace 保留手上所有的 Ace 还是只保留一个,这个没说清楚。看例子,这两种用法都有,我按照输入了几个就保留几个来做的。
simulate 没问题,和中的结果完全一样
必须用 python 3
import randomimport collections
_dice_type = ['Ace', 'King', 'Queen', 'Jack', '10', '9']
_hand_mapping = collectionsOrderedDict([
('5kind', 'Five of a kind'),
('4kind', 'Four of a kind'),
('full', 'Full house'),
('straight', 'Straight'),
('3kind', 'Three of a kind'),
('2pair', 'Two pair'),
('1pair', 'One pair'),
('bust', 'Bust'),
])
def _check_hand(dices):
counter = collectionsCounter(dices)
if len(counter) == 1:
return '5kind'
sorted5 = countermost_common(5)
if sorted5[0][1] == 4:
return '4kind'
if sorted5[0][1] == 3:
if sorted5[1][1] == 2:
return 'full'
else:
return '3kind'
if sorted5[0][1] == 2:
if sorted5[1][1] == 2:
return '2pair'
else:
return '1pair'
if len(counter) == 5:
dtype = sorted5[0][0]
for x in sorted5:
if dtype != x[0]:
break
dtype += 1
else:
return 'straight'
return 'bust'
def play():
dices = []
retry = 0
while True:
remain = 5 - len(dices)
if remain <= 0:
break
dicesextend([randomrandint(0,5) for x in range(remain)])
print("The roll is: {}"format(
" "join([_dice_type[d] for d in sorted(dices)])
))
print("It is a {}"format(_hand_mapping[_check_hand(dices)]))
if retry > 1:
break
prompt = "Which dice do you want to keep for the {} roll "format(
"second" if retry == 0 else "third"
)
while True:
answer = input(prompt)lower()
if answer == 'all':
break
answer = [xcapitalize() for x in answersplit()]
if set(answer)issubset(set(_dice_type)):
break
print("That is not possible, try again!")
retry += 1
if answer == 'all':
print("Ok, done")
break
tmp = dices
dices = []
for x in tmp:
if _dice_type[x] in answer:
dicesappend(x)
answerremove(_dice_type[x])
def simulate(n, debug=False):
result = dictfromkeys(_hand_mappingkeys(), 0)
for _ in range(n):
dices = [randomrandint(0,5) for x in range(5)]
if debug:
print("DEBUG:", " "join([_dice_type[d] for d in sorted(dices)]))
result[_check_hand(dices)] += 1
for k, v in _hand_mappingitems():
cnt = result[k]
print("{:<16s}: {:2f}%"format(v, 100cnt/n))
编写程序?还是电子表格?
一、编写程序思路:
1千次循环开始{
随机数;
如果{
1:统计1
2:统计2
……
6:统计6
}
结束如果;
}结束循环
列出总的结果
二、电子表格思路:
Excel表格,则使用rand函数,在A1:K100里,产生0~5六个数。
然后,用countif函数统计。
我很懒。不写实际东东了……
微信电脑版提供了完整的聊天功能,但是目前好像还没有内置骰子功能。这可能是因为微信电脑版专注于提供直接从电脑上使用微信的功能,并不那么注重游戏和娱乐方面。但是,你也可以通过搜索微信桌面版的扩展程序来找到骰子工具,安装后就可以在聊天窗口里使用了。另外,如果你的电脑接入了数字盘,可以通过输入数字来模拟骰子扔的过程,虽然不够方便快捷,但仍可以实现你的需求。总体来说,微信电脑版不直接支持骰子,但你可以通过各种方式满足你的需求。
掷骰子比大小,用随机数就可以,但是庄家的规则是咋样的没交代清楚(这里假定庄家的优势在于点数一样大时,庄家赢):
#include "stdafxh"
#include <iostream>
#include <timeh>
using namespace std;
int main()
{
int Me, Computer,n=1,MC=1;
int meWin = 0, nGames = 0;
srand((unsigned int)time(NULL));
while (n)
{
cout << "开始掷骰子(输入1开始,输入0结束):" << endl;
cin >> n;
Me = rand() % 6 + 1;
Computer= rand() % 6 + 1;
nGames++;
if (MC)
{
cout << "你做庄:" << endl;
cout << "你的点数:" << Me << " 电脑的点数:" << Computer << endl;
if (Me >= Computer)
{
meWin++;
cout << "恭喜你赢了!" << endl;
}
else
{
MC = 0;
cout << "哦,你输了!" << endl;
}
}
else
{
cout << "电脑做庄:" << endl;
cout << "你的点数:" << Me << " 电脑的点数:" << Computer << endl;
if (Computer >= Me)
cout << "哦,你输了!" << endl;
else
{
meWin++;
MC = 1;
cout << "恭喜你赢了!" << endl;
}
}
}
cout << "你赢的次数:" << meWin << " 输的次数:" << nGames - meWin << " 胜率:";
float p = (float)meWin / nGames;
int q = p 100;
printf("%2d%% \n", q);
system("pause");
return 0;
}
以上就是关于求掷骰子游戏C语言代码(我初学C,代码简单为佳)全部的内容,包括:求掷骰子游戏C语言代码(我初学C,代码简单为佳)、.编写一个应用程序,用1~6之间的随机数来模拟掷骰子游戏,统计扔5000次后各点数出现的次数并输出、电脑随机骰子会在多少次后重复等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)