C语言问题,求助。掷骰子。

①掷骰子游戏:
两人玩骰子,游戏规则如下:
1) 两人轮流掷骰子,每次掷两个,每人最多掷10次。
2) 将每人每次的分值累加计分
3) 当两个骰子点数都为6时,计8分;当两个点数相等且不为两个6时,计7分;当两个点数不一样时,计其中点数较小的骰子的点数。
4) 结束条件:当双方都掷10次或经过5次后一方累计分数多出另一方的30%及以上。最后显示双方分数并判定优胜者。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int func(){
int a,b;
a=rand()%6+1;
b=rand()%6+1;
if(a==b&&a==6){
return 8;
}else if(a==b){
return 7;
}else{
return (a<b?a:b);
}
}
void main(){
int sumA,sumB,i;
sumA=sumB=0;
srand(time(0));
for(i=0;i<10;i++){
sumA+=func();
sumB+=func();
if(i==4){
if(sumA>1.3*sumB){
printf("A:%d\nB:%d\n5局A获胜\n",sumA,sumB);
return;
}else if(sumB>1.3*sumA){
printf("A:%d\nB:%d\n5局B获胜\n",sumA,sumB);
return;
}
}
}
if(sumA>sumB){
printf("A:%d\nB:%d\n10局A获胜\n",sumA,sumB);
}else if(sumB>sumA){
printf("A:%d\nB:%d\n10局B获胜\n",sumA,sumB);
}else{
printf("A:%d\nB:%d\n平局\n",sumA,sumB);
}
return;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-06-20
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
void main()
{
int i,j,y[10];
int m=2; // 如果是 2 粒骰子 (你可以改1,2,3,4,5,6。。。10)
srand((unsigned)time(NULL));
for (j=0;j<10;j++){ // 掷 10 次
for (i=0;i<m;i++){
y[i] = rand() % 6+ 1;
printf("%d ",y[i]);
}
printf("\n");
}

}
第2个回答  2015-09-15
try, and try again. Eac
h obstacle I will consider
as a mere detour to my g
oal and a challenge to my
profession. I will persist an
d develop my skills as the
mariner develops hi
相似回答