c语言游戏源码_C语言学习教程:搬山游戏开发源码

C语言学习教程:搬山游戏开发源码

9dd50da21508bb14f71cd5a18cb17131.png
#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语言资料’几个字就可以免费领取了。(注意私信我关键字不要写错,可以复制)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值