代码目的:
测试crap的获胜概率。
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
# define SIZE 21
int game();
int judge(int sum);
void outcome(int total[][SIZE]);
int main()
{ int status,sum,temp;//status 0-end 1-continue
int total[2][SIZE]={0}; //i for times j for
srand(time(NULL));
//start
int test;
printf("%s","请输入测试次数:\n");
scanf("%d",&test);
for(int i = 0 ;i < test; i++){
sum=game();
status=judge(sum);
temp=sum;
switch(status){
case 0:
total[0][0]++;
break;
case 1:
total[1][0]++;
break;
case 2:
for (int j = 1; ;j++ ){
int target;
target = game();
if(target == temp){
if(j>=SIZE){
total[0][SIZE-1]++;
}else{
total[0][j-1]++;
}
break;
}
if(target==7){
if(j>=SIZE){
total[1][SIZE-1]++;
}else{
total[1][j-1]++;
}
break;
}
}
}
}
outcome(total[][SIZE]);
return 0;
}
void outcome(int total[][SIZE]){
printf("%s","测试结果如下为:\n");
printf("%6s","");
for(int i =1;i<SIZE;i++){
printf("%3d",i);
}
for(int j=0;j<SIZE;j++){
switch(j){
case 0:
printf("%6s","\nWIN");
break;
case 1:
printf("%6d","\nLOSE");
}
for(int k=0;k<SIZE;k++){
printf("%3d",total[j][k]);
}
}
}
int game( ){
int roll_1,roll_2,sum;
roll_1= rand() % 6 + 1;
roll_2= rand() % 6 + 1;
sum = roll_1 + roll_2;
// printf("%3d %7d\n",roll_1,roll_2);
return sum;
}
int judge(int sum){
switch(sum){
case 7:
case 11:
// printf("%s","Player Win!\n");
return 0;
case 2:
case 3:
case 12:
// printf("%s","Host Win!\n");
return 1;
default:
return 2;
}
}
报错如下:
64 16 [Error] expected primary-expression before ']' token