This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable,
visit the help center。
7年前关闭。
我正在为我的C编程课程执行一项学习任务,要求我执行以下操作:
任务2.掷骰子。
在掷骰子游戏中,“通过线”下注进行如下。用两个六面骰子
掷骰子的第一卷骰子称为“滚出骰子”。投注
当出局掷骰为7或11时立即获胜,而当出局掷骰为
2、3或12。如果将4、5、6、8、9或10滚动到输出卷上,则该数字成为“点”。玩家不断掷骰子,直到掷出7或点。如果该点先被掷出,则玩家赢得下注。如果玩家先掷出7,则玩家
输了
编写一个程序,使用上述规则玩掷骰子游戏,以便
模拟没有人工输入的游戏。该程序不要求下注,而是
应该只是计算玩家是赢还是输。创建一个模拟的函数
滚动两个骰子并返回总和。添加一个循环,以便该程序播放10,000
游戏。
添加计数器来计算玩家获胜的次数以及获胜的次数
玩家输了。在10,000场比赛结束时,计算获胜的概率,即
获胜/(获胜+损失)并输出此值。从长远来看,谁会赢
最多的掷骰子游戏,是您还是房子?
到目前为止,这是我写的内容:
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
// Craps Program
// Written by Kane Charles
// Lab 2 - Task 2
// 7 or 11 indicates instant win
// 2, 3 or 12 indicates instant los
// 4, 5, 6, 8, 9, 10 on first roll becomes "the point"
// keep rolling dice until either 7 or "the point is rolled"
// if "the point" is rolled the player wins
// if 7 is rolled then the player loses
int wins = 0, losses = 0;
int r, i;
int N = 1, M = 12;
int randomgenerator();
main(void){
/* initialize random seed: */
srand (time(NULL));
/* generate random number 10,000 times: */
for(i=0; i < 10000 ; i++){
int r = randomgenerator();
if (r == 7 || r == 11) {
wins++;
}
else if (r == 2 || r == 3 || r == 12) {
losses++;
}
else if (r == 4 || r == 5 || r == 6 || r == 8 || r == 9 || r == 10) {
int point = r;
int temproll;
int *tr = &temproll;
do
{
*tr = randomgenerator();
}while (temproll != 7 || temproll != point);
if (temproll == 7) {
losses++;
}
else if (temproll == point) {
wins++;
}
}
}
printf("Wins\n");
printf("%d",&wins);
printf("\nLosses\n");
printf("%d",&losses);
}
int randomgenerator(){
r = M + rand() / (RAND_MAX / (N - M + 1) + 1);
return r;
}
该程序始终以以下结尾:
Wins:
0.000000
Losses:
0.000000
而应该显示(总共10000个)程序赢了多少次,输了多少次。有人能帮我这个忙吗?我怀疑我可能需要使用指针而不是
wins++
和
losses++
,以便将分数全局保存。
请记住,我是很多新手,因此将不胜感激。
例如,我在if
中看到您的main
语句的一个主要问题:
if (r = 7 || 11)
您使用的是
=
分配而不是
==
相等。下一个问题是您似乎对
||
的工作方式有误解。看来您想检查
r
是
7
还是
11
实际上是:
if( (r == 7) || (r == 11) )
您还可以在此处的
temproll
循环中创建本地
do
:
int temproll;
do
{
int temproll = randomgenerator();
^^^^^^^^^^^^
}while (temproll != 7 || point);
这意味着一旦离开,您在
do
循环中生成的值就会丢失,随后的
if
语句将检查未定义的值。据我所知,
while
可能是这样的:
while (temproll != 7 && temproll != r );
这表示
temproll
不是
7
也不是
r
。
此外,最终需要打印出的赢利和亏损必须得到修复:
printf("%lf",&wins);
printf("%lf",&losses);
lf
格式用于
double
,并且您使用的是
wins
的地址而不是值,它应该是:
printf("%d",wins);
printf("%d",losses);