C语言学习教程:搬山游戏开发源码
#includevoid main(){ int n,k,x,y,cc,pc,g; clrscr(); puts("*******************************************************"); puts("* This is a Mountain Moveing Game. *"); puts("* There are n mountains, two persons move them in *"); puts("* turn. Each one each time moves 1 to k mountains, the*"); puts("* one who takes the last stick will lose the game. *"); puts("*******************************************************"); printf(" >> --------------- Game Begin ---------------------"); pc=cc=0; g=1; for(;;) { printf(" >> No.%2d game ",g++); printf(" >> ---------------------------------------"); printf(" >> How many mountains are there? "); scanf("%d",&n); if(!n) break; printf(" >> How many mountains are allowed to each time? "); do{ scanf("%d",&k); if(k>n||k<1) printf(" >> Repeat again!"); }while(k>n||k<1); do{ printf(" >> How many mountains do you wish move ? "); scanf("%d",&x); if(x<1||x>k||x>n) /*判断搬山数是否符合要求*/ { printf(" >> Illegal,again please!"); continue; } n-=x; printf(" >> There are %d mountains left now.",n); if(!n) { printf(" >> ---- I win. You are failure.----------");cc++; } else { y=(n-1)%(k+1); /*求出最佳搬山数*/ if(!y) y=1; n-=y; printf(" >> Copmputer move %d mountains away.",y); if(n) printf(" >> There are %d mountains left now.",n); else { printf(" >> ---- I am failure. You win.-----------"); pc++; } } }while(n); } printf(" >> Games in total have been played %d.",cc+pc); printf(" >> You score is win %d,lose %d.",pc,cc); printf(" >> My score is win %d,lose %d.",cc,pc); printf(" >> --------------- Game Over! ---------------------"); printf(" Press any key to quit..."); getch();}
ps:需要电子书籍、相关学习视频教程、项目源码、开发工具等学习资料可以关注我头条号,然后私信我‘C语言资料’几个字就可以免费领取了。(注意私信我关键字不要写错,可以复制)