| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 534 人关注过本帖
标题:《求助》新到不行的新手——三个传教士和三个恶魔过河的问题
只看楼主 加入收藏
foloway
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-4-4
收藏
 问题点数:0 回复次数:0 
《求助》新到不行的新手——三个传教士和三个恶魔过河的问题
好吧。这是一个很旧的问题。本人很早以前也看过。但今天叫我用C语言编的时候。我是不行了。
由于小弟刚刚入门。学的很少。只学了基本。也就是请各位大咖只用 WHILE ,IF,FOR,SCANF,PRINTF这几个修改一下。。跪求了。这是作业算分的。。
小弟卡在两个点。一是怎样在玩家输错(输错是指将人数变成负数的情况)之后还能再次输入并继续游戏。(完全无奈)。。 ╮(╯▽╰)╭
                二是怎样在玩家失败后按6重新开始和按7退出。小弟尝试了一下。却并没有成功。。。总是跳出循环。

看的下去的人请帮帮我吧

#include<stdio.h>
 int main()
 {
  printf( "三个传教士和三个吃人恶魔过河 \n");
int a,b,c,d,x,y,z;
    a=3;b=3;c=0;d=0;x=2;z=6;
   printf("  %d传教士 %d恶魔 \n"
          "----------- 船 \n"
          "  ~ ~ ~ \n"
          "  ~ ~ ~ \n"
          "----------- \n"
          "  %d传教士 %d恶魔 \n ",a,b,c,d );
   printf("Menu: [1] 恶 [2]传  [3] 两个恶 [4] 两个传 [5] 一传一魔 \n"
          "请按一个选择  再按enter");
   scanf("%d",&y);
   while(a<=3&&b<=3&&c<=3&&d<=3&&z==6)
     {  /*  北岸开始。北岸到南岸                 */
        if(x%2==0){
                  if(y==1){
                            b=b-1; d=d+1;x=x+1;
                           }
                  else if(y==2){
                           a=a-1; c=c+1;x=x+1;
                           }
                  else if(y==3){
                           b=b-2; d=d+2;x=x+1;
                           }
                  else if(y==4){
                           a=a-2; c=c+2;x=x+1;
                           }
                  else if(y==5){
                           a=a-1;c=c+1;
                           b=b-1;d=d+1;x=x+1;
                           }
               if(a<b&&a!=0&&a>=0&&b>=0&&c>=0&&d>=0){ /* 如果,北岸,传教士人数少于恶魔  */
                     printf(" 0传教士 %d恶魔 \n"
                        "-----------  \n"
                         "  ~ ~ ~ \n"
                         "  ~ ~ ~ \n"
                         "----------- 船\n"
                         "  %d传教士 %d恶魔 \n ",b,c,d );

                     printf("失败, %d 个传教士在北岸被吃 \n",a);
                     printf("按6重新开始游戏,按7退出游戏");
                      scanf("%d",&z);if(z==6){a=3;b=3;c=0;d=0;x=2;};
                                            }
              else if(c<d&&c!=0&&a>=0&&b>=0&&c>=0&&d>=0){/* 如果,南岸,传教士人数少于恶魔 */
                     printf(" %d传教士 %d恶魔 \n"
                        "-----------  \n"
                         "  ~ ~ ~ \n"
                         "  ~ ~ ~ \n"
                         "----------- 船\n"
                         "  0传教士 %d恶魔 \n ",a,b,c,d );

                     printf("失败, %d 个传教士在南岸被吃 \n",c);
                     printf("按6重新开始游戏,按7退出游戏");
                      scanf("%d",&z);if(z==6){a=3;b=3;c=0;d=0;x=2;};
                     }

              else if(a>=0&&b>=0&&c>=0&&d>=0){printf(
                        "  %d传教士 %d恶魔 \n"
                        "-----------  \n"
                         "  ~ ~ ~ \n"
                         "  ~ ~ ~ \n"
                         "----------- 船\n"
                         "  %d传教士 %d恶魔 \n ",a,b,c,d );
                        if(a==0&&b==0){x=x-2;
                                      printf("恭喜你成功在%d次过河后讲传教士和恶魔传到对岸"
                                              "最优秀的成绩是 11 次.",x);
                     printf("按6重新开始游戏,按7退出游戏");
                      scanf("%d",&z);if(z==6){a=3;b=3;c=0;d=0;x=2;};
                                             }
                        else{ printf("Menu: [1] 恶 [2]传  [3] 两个恶 [4] 两个传 [5] 一传一魔 \n"
                                     "请按一个选择  再按enter");
                              scanf("%d",&y);                                }
                     }
            }
 /*  当船从南岸回来时,至少要有一个人在船上                                                        */
        else if(x%2==1){
                  if(y==1){
                          d=d-1; b=b+1;x=x+1;
                          }
                  else if(y==2){
                           c=c-1; a=a+1;x=x+1;
                           }
                  else if(y==3){
                           d=d-2; b=b+2;x=x+1;
                           }
                  else if(y==4){
                           c=c-2; a=a+2;x=x+1;
                           }
                  else if(y==5){
                           c=c-1;a=a+1;
                           d=d-1;b=b+1;x=x+1;
                           }


              if(a<b&&a!=0&&a>=0&&b>=0&&c>=0&&d>=0){/* 如果,北岸,传教士人数少于恶魔   */
                     printf(" 0传教士 %d恶魔 \n"
                        "-----------  \n"
                         "  ~ ~ ~ \n"
                         "  ~ ~ ~ \n"
                         "----------- 船\n"
                         "  %d传教士 %d恶魔 \n ",b,c,d );
                     printf("失败, %d 个传教士在北岸被吃",a);
                     printf("按6重新开始游戏,按7退出游戏");
                     scanf("%d",&z);if(z==6){a=3;b=3;c=0;d=0;x=2;};
                     }
              else if(c<d&&c!=0&&a>=0&&b>=0&&c>=0&&d>=0){/* 如果,南岸,传教士人数少于恶魔*/
                     printf(" %d传教士 %d恶魔 \n"
                        "-----------  \n"
                         "  ~ ~ ~ \n"
                         "  ~ ~ ~ \n"
                         "----------- 船\n"
                         "  0传教士 %d恶魔 \n ",a,b,c,d );
                     printf("失败, %d 个传教士在南岸被吃",c);
                     printf("按6重新开始游戏,按7退出游戏");
                     scanf("%d",&z);if(z==6){a=3;b=3;c=0;d=0;x=2;};
                     }
               else if(a>=0&&b>=0&&c>=0&&d>=0){printf(
                         "  %d传教士 %d恶魔 \n"
                         "-----------船  \n"
                         "  ~ ~ ~ \n"
                         "  ~ ~ ~ \n"
                         "----------- \n"
                         "  %d传教士 %d恶魔 \n ",a,b,c,d );
                 printf("Menu: [1] 恶 [2]传  [3] 两个恶 [4] 两个传 [5] 一传一魔 \n"
                         "请按一个选择  再按enter");
                 scanf("%d",&y);
                   }
        }
    if(x>22){printf("失败。你超出了20次过河的次限");}
   }
   printf("谢谢");
 return 0;
 }


希望各位高手能搭把手救救我。

[ 本帖最后由 foloway 于 2015-4-4 08:07 编辑 ]
搜索更多相关主题的帖子: 游戏 C语言 传教士 include 
2015-04-04 08:03
快速回复:《求助》新到不行的新手——三个传教士和三个恶魔过河的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019074 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved