抢30

这是中国民间的一个游戏。两人从1开始轮流报数,每人每次可报一个数或两个连续的数,谁先报到30,谁就为胜方。

*问题分析与算法设计
本题与上题类似,算法也类似,所不同的是,本谁先走第一步是可选的。若计算机走第一步,那么计算机一定是赢家。若人先走一步,那么计算机只好等待人犯错误,如果人先走第一步且不犯错误,那么人就会取胜;否则计算机会抓住人的一次错误使自己成为胜利者。

*程序说明与注释
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int input(int t);
int copu(int s);
int main()
{
int tol=0;
printf("\n* * * * * * * *catch thirty* * * * * * * \n");
printf("Game Begin\n");
randomize(); /*初始化随机数发生器*/
if(random(2)==1) /*取随机数决定机器和人谁先走第一步*/
tol=input(tol); /*若为1,则余元走第一步*/
while(tol!=30) /*游戏结束条件*/
if((tol=copu(tol))==30) /*计算机取一个数,若为30则机器胜利*/
printf("I lose! \n");
else
if((tol=input(tol))==30) /*人取一个数,若为30则人胜利*/
printf("I lose! \n");
printf(" * * * * * * * *Game Over * * * * * * * *\n");
}
int input(int t)
{
int a;
do{
printf("Please count:");
scanf("%d",&a);
if(a>2||a<1||t+a>30)
printf("Error input,again!");
else
printf("You count:%d\n",t+a);
}while(a>2||a<1||t+a>30);
return t+a; /*返回当前的已经取走的数累加和*/
}
int copu(int s)
{
int c;
printf("Computer count:");
if((s+1)%3==0) /*若剩余的数的模为1,则取1*/
printf(" %d\n",++s);
else if((s+2)%3==0)
{
s+=2; /*若剩余的数的模为2,则取2*/
printf(" %d\n",s);
}
else
{
c=random(2)+1; /*否则随机取1或2*/
s+=c;
printf(" %d\n",s);
}
return s;
}

*思考题
巧夺偶数。桌子上有25颗棋子,游戏双方轮流取子,每人每次最少取走一颗棋子,最多可取走3颗棋子。双方照这样取下去,直到取光所有的棋子。于是双方手中必然一方为偶数,一方为奇数,偶数方为胜者。请编程实现人机游戏。

转载于:https://www.cnblogs.com/junzhongxu/archive/2008/07/18/1245693.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值