102.搬山游戏

本文介绍了一个基于山移游戏的编程实现,通过用户和电脑的交互策略,探讨了游戏规则和算法设计。玩家和电脑轮流移动山脉,目标是找出最优策略以赢得比赛。程序包含输入验证和动态计算最佳步数,最后统计总游戏次数和胜负分数。
摘要由CSDN通过智能技术生成
#include<stdio.h>
void 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 ---------------------\n");
   pc=cc=0;
   g=1;
   for(;;)
   {
      printf(" >> No.%2d game \n",g++);
   printf(" >> ---------------------------------------\n");
   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!\n");
   }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!\n");
         continue;
      }
      n-=x;
      printf(" >> There are %d mountains left now.\n",n);
      if(!n)
      {
         printf(" >> ---- I win. You are failure.----------\n\n");cc++;
      }
      else
      {
         y=(n-1)%(k+1);      /*求出最佳搬山数*/
         if(!y) y=1;
         n-=y;
         printf(" >> Copmputer move %d mountains away.\n",y);
         if(n) printf(" >> There are %d mountains left now.\n",n);
         else
         {
	    printf(" >> ---- I am failure. You win.-----------\n\n");
            pc++;
         }
      }
   }while(n);

   }
   printf(" >> Games in total have been played %d.\n",cc+pc);
   printf(" >> You score is win %d,lose %d.\n",pc,cc);
   printf(" >> My score is win %d,lose %d.\n",cc,pc);
   printf(" >> --------------- Game Over! ---------------------\n");
   printf("\n Press any key to quit...");
   getch();
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程与实战

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值