#include <stdio.h>
#include <stdlib.h>
#define FACE 6
#define ROLLTIME 6000
int rand_range( int a, int b);
void show_array(int a[], int );
int roll_two();
int main(){
int a[FACE + 1], i, r;
for(i = 0; i <= FACE; i++){
a[i] = 0;
}
srand(time(NULL));
for(i = 0; i < ROLLTIME; i++){
r = rand_range(1,6);
a[ r ] ++ ;
}
show_array(a, FACE + 1);
int status = 0, mpoint = 0, roll_sum, times = 1;
roll_sum = roll_two();
mpoint = roll_sum;
switch(roll_sum ){
case 7:
case 11:
status = 1;
break;
case 2:
case 3:
case 12:
status = 2;
break;
default:
status = 0;
}
while( status == 0){
roll_sum = roll_two();
times ++;
if(roll_sum == mpoint){
status = 1;
break;
}else if( roll_sum == 7 ){
status = 2;
break;
}
}
if( status == 1){
printf("player win ,the point is %d , use %d times\n",roll_sum, times);
}else if( status == 2){
printf("player lose ,the point is %d , use %d times\n",roll_sum, times);
}
}
int rand_range( int a, int b ){
return rand() % b + a;
}
void show_array(int a[] ,int n){
int i;
for(i = 0;i < n;i++){
printf("%d : %d \n",i , a[i]);
}
}
int roll_two(){
int r1, r2;
r1 = rand_range(1,6);
r2 = rand_range(1,6);
return r1 + r2;
}
[C How To Program] 5.00 投色子游戏
最新推荐文章于 2024-04-15 23:35:18 发布