📄 car-n-goat.c
字号:
/* Demo of car and goat game, using random number generator. * * Written by Cyril Hu (cyrilhu@gmail.com), public domain. */#include<ctype.h>#include<stdio.h>#include<time.h>#include<stdlib.h>int main(void){ int i, other, open, max, ch, x[3]; do { puts("Choose one door, 1,2,3?"); scanf("%d", &ch); ch--; } while(ch != 1 && ch != 2 && ch != 0); srand(time(NULL)); do { x[0] = rand(); x[1] = rand(); x[2] = rand(); } while(x[1]==x[2] || x[0]==x[1] || x[0]==x[2]); for(max=i=0; i<3; i++) { if(x[max] < x[i]) max = i; } for(;;) { open = (int)( 3.0 * ( rand() / (RAND_MAX + 1.0) ) ); other = (int)( 3.0 * ( rand() / (RAND_MAX + 1.0) ) ); if(other != ch && other != open && open != ch && open != max) { printf("Door %d is open, a goat behind it\n", open+1); break; } } do { printf("Now, Door %d or Door %d ?\n", ch+1, other+1); scanf("%d", &i); i--; } while(i != ch && i != other); if(i != max) puts("You lose, a goat behind this door"); else puts("You win, a car behind this door"); return EXIT_SUCCESS;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -