fivechee.c

来自「redboy for gba 是BPNS为GBA编写的一个小软件。软件的邹形是B」· C语言 代码 · 共 1,116 行 · 第 1/2 页

C
1,116
字号
      return set;
   }

   if (comp[3][0].xp) //我方冲三(攻)
   {
      for (i=1;i<=comp[3][0].xp;i++)//主动攻
      {
         //if (i>3-(int)(step/2+0.5)) break;//??两次未成,是否还行?
         xsp=comp[3][i].xp;
         ysp=comp[3][i].yp;
         nowp=15*xsp+ysp;
         *(plate+nowp)=1;
         *(man+nowp)=2;
         chee_push(xsp,ysp,coms,mans,stack,plate);
         temp=fix_set(xsp,ysp,plate,coms);
         *(plate+temp.xp*15+temp.yp)=2;//限定敌方
         *(man+15*temp.xp+temp.yp)=1;
         chee_push(temp.xp,temp.yp,coms,mans,stack,plate);
         result=comset(plate,man,next,coms,mans,stack);
         *(plate+temp.xp*15+temp.yp)=0;
         *(man+temp.xp*15+temp.yp)=0;//恢复数据
         chee_pop(temp.xp,temp.yp,coms,mans,stack,plate);
         *(plate+nowp)=0;
         *(man+nowp)=0;
         chee_pop(xsp,ysp,coms,mans,stack,plate);
         if (result.xp>=200) //敌方陷入
         {
            set.xp=xsp+200;
            set.yp=ysp;
            return set;
         }
         else if(result.yp<200 && safe_xp>200)//安全点
         {
            safe_xp=xsp;
            safe_yp=ysp;
         }
      }
   }
   if (manp[1][0].xp)//敌方活三(堵)
   {
      //j=0;
      if (manp[1][0].xp==1)
      {
         set.xp=manp[1][1].xp;
         set.yp=manp[1][1].yp;
         return set;
      }
      if (manp[1][0].xp>2)//注定挡不了了
      {
         if (safe_xp>200)
         {
            set.xp=manp[1][1].xp;
            set.yp=manp[1][1].yp+200;
            return set;
         }
         else
         {
            set.xp=safe_xp;
            set.yp=safe_yp;
            return set;
         }
      }
      for (i=1;i<=manp[1][0].xp;i++)
      {
         xsp=manp[1][i].xp;
         ysp=manp[1][i].yp;
         nowp=15*xsp+ysp;
         *(plate+nowp)=1;
         *(man+nowp)=2;
         chee_push(xsp,ysp,coms,mans,stack,plate);
         result=comset(man,plate,next,mans,coms,stack);
         *(plate+nowp)=0;
         *(man+nowp)=0;
         chee_pop(xsp,ysp,coms,mans,stack,plate);
         if (result.xp<200)//成功解围
         {
            set.xp=xsp;
            set.yp=ysp;
            return set;
         }
      }
      if (safe_xp<200)//试图解除危险
      {
         set.xp=safe_xp;
         set.yp=safe_yp;
         return set;
      }
      set.xp=manp[1][1].xp;
      set.yp=manp[1][1].yp+200;
      return set;
   }
   if (comp[2][0].xp) //双二陷阱(攻)
   {
      if (manp[3][0].xp==0) // 敌方无反抗能力
      {
         set.xp=200+comp[2][1].xp;
         set.yp=comp[2][1].yp;
         return set;
      }
      for(i=1;i<=comp[2][0].xp;i++)
      {
         if (i>3) break;//?????
         xsp=comp[2][i].xp;
         ysp=comp[2][i].yp;
         nowp=15*xsp+ysp;
         *(plate+nowp)=1;
         *(man+nowp)=2;//设置陷阱
         chee_push(xsp,ysp,coms,mans,stack,plate);
         result=comset(man,plate,next,mans,coms,stack);//敌方下子
         *(plate+nowp)=0;
         *(man+nowp)=0;
         chee_pop(xsp,ysp,coms,mans,stack,plate);
         if (result.xp<200) //设陷成功!
         {
            set.xp=200+xsp;
            set.yp=ysp;
            return set;
         }
      }
   }
   if (manp[2][0].xp)//敌方双二(防)
   {
      if (manp[2][0].xp==1)
      {
         set.xp=manp[2][1].xp;
         set.yp=manp[2][1].yp;
         return set;
      }
      for (i=1;i<=manp[2][0].xp;i++)
      {
         if (i>3) break;//????
         xsp=manp[2][i].xp;
         ysp=manp[2][i].yp;
         nowp=15*xsp+ysp;
         *(plate+nowp)=1;
         *(man+nowp)=2;
         chee_push(xsp,ysp,coms,mans,stack,plate);
         result=comset(man,plate,next,mans,coms,stack);
         *(plate+nowp)=0;
         *(man+nowp)=0;
         chee_pop(xsp,ysp,coms,mans,stack,plate);
         if (result.xp<200)
         {
            set.xp=xsp;
            set.yp=ysp;
            return set;
         }//成功解围
      }
      if (safe_xp<200)//试图解除危险
      {
         set.xp=safe_xp;
         set.yp=safe_yp;
         return set;
      }
      set.xp=manp[2][1].xp;
      set.yp=manp[2][1].yp+200;
      return set;
   }
   if (manp[3][0].xp)//敌方冲三(防)
   {
      for (i=1;i<=manp[3][0].xp;i++)
      {
         if (i>3) break;//??????
         xsp=manp[3][i].xp;
         ysp=manp[3][i].yp;
         nowp=15*xsp+ysp;
         *(plate+nowp)=2;
         *(man+nowp)=1;
         chee_push(xsp,ysp,coms,mans,stack,plate);
         result=comset(man,plate,next,mans,coms,stack);
         *(plate+nowp)=0;
         *(man+nowp)=0;
         chee_pop(xsp,ysp,coms,mans,stack,plate);
         if (result.yp>=200)//能捡便宜
         {
            set.xp=xsp+200;
            set.yp=ysp;
            return set;
         }
         else if(result.xp<200 && safe_xp>200)
         {
            safe_xp=xsp;
            safe_yp=ysp;
         }
      }
   }
   if (comp[4][0].xp && step<4) //我方活二(攻)
   {
      for (i=1;i<=comp[4][0].xp;i++)
      {
         if (i>3) break;//???
         xsp=comp[4][i].xp;
         ysp=comp[4][i].yp;
         nowp=15*xsp+ysp;
         *(plate+nowp)=1;
         *(man+nowp)=2;
         chee_push(xsp,ysp,coms,mans,stack,plate);
         result=comset(man,plate,next,mans,coms,stack);
         *(plate+nowp)=0;
         *(man+nowp)=0;
         chee_pop(xsp,ysp,coms,mans,stack,plate);
         if (result.yp>=200)//敌方致命!
         {
            set.xp=xsp+200;
            set.yp=ysp;
            return set;
         }
         if (result.xp<200 && safe_xp>200)//该点安全
         {
            safe_xp=xsp;
            safe_yp=ysp;
         }
      }
   }
   if (manp[4][0].xp&&step<4) //敌手活二
   {
      for (i=1;i<=manp[4][0].xp;i++)
      {
         if (i>2) break;//????
         xsp=manp[4][i].xp;
         ysp=manp[4][i].yp;
         nowp=15*xsp+ysp;
         *(plate+nowp)=1;
         *(man+nowp)=2;
         chee_push(xsp,ysp,coms,mans,stack,plate);
         result=comset(man,plate,next,mans,coms,stack);
         *(plate+nowp)=0;
         *(man+nowp)=0;
         chee_pop(xsp,ysp,coms,mans,stack,plate);
         if(result.yp>=200)//有便宜啦!
         {
            set.xp=xsp+200;
            set.yp=ysp;
            return set;
         }
         else if (result.xp<200 && safe_xp>200)
         {
            safe_xp=xsp;
            safe_yp=ysp;
         }//安全点
      }
   }
   if (safe_xp<250)//有安全点
   {
      set.xp=safe_xp;
      set.yp=safe_yp;
      return set;
   }
   if (step>0) goto nocheck;
   if (manp[3][0].xp && comp[4][0].xp && !comp[3][0].xp)
   {
      xp=comp[4][1].xp;
      yp=comp[4][1].yp;
   }
   else if(comp[3][0].xp && comp[4][0].xp && rnd(2))
   {
      xp=comp[4][1].xp;
      yp=comp[4][1].yp;
   }
   else if(comp[3][0].xp && manp[4][0].xp && rnd(2) && rnd(2) && !comp[4][0].xp)
   {
      xp=manp[4][1].xp;
      yp=manp[4][1].yp;
   }
nocheck:
   set.xp=xp;
   set.yp=yp;
   return set;
}

u8 is_win(chee_loc loc,u8 who,u8 *plate)
{
   p_type result=get_weight(loc.xp,loc.yp,who,plate);
   return (!result.type);
}

void save_step(chee_loc set,chee_loc *save,u8 steps)
{
   (save+steps-1)->xp=set.xp;
   (save+steps-1)->yp=set.yp;
   return;
}

void show_step(chee_loc *save,u8 steps,u8 first)
{
   u8 i,begin=0;
   if (steps>15) begin=steps-15;
   for (i=begin;i<steps;i++)
   {
      chee_box(15,i-begin,(i+first)%2?2:1);
      show_num (176,31+8*(i-begin),(save+i)->xp,2);
      show_num (184,31+8*(i-begin),(save+i)->yp,2);
   }
   return;
}

u8 chee_back(u8 *plate,u8 *plate_ve,chee_loc *save,u8 steps,u8 first,p_type coms[15][15],p_type mans[15][15],p_type stack[max_sp][2][8][4])
{
   u8 xp,yp,xsp,ysp;
   if (steps>1)
   {
      steps=steps-2;
      Q_box(168,30,192,148,RGB(24,15,13));
      show_step(save,steps,first);
      xp=(save+steps)->xp;
      yp=(save+steps)->yp;
      xsp=(save+steps+1)->xp;
      ysp=(save+steps+1)->yp;
      chee_box(xp,yp,0);
      chee_box(xsp,ysp,0);
      *(plate+xp*15+yp)=0;
      *(plate_ve+xp*15+yp)=0;
      chee_push(xp,yp,coms,mans,stack,plate);
      coms[xp][yp]=get_weight(xp,yp,1,plate);
      chee_sp--;
      *(plate+xsp*15+ysp)=0;
      *(plate_ve+xsp*15+ysp)=0;
      chee_push(xsp,ysp,coms,mans,stack,plate);
      mans[xsp][ysp]=get_weight(xsp,ysp,2,plate);
      chee_sp--;
      show_num(100,18,steps,3);
   }
   else arlt("出错啦","已经返回初始状态,不能梅棋!");
   return steps;
}
chee_loc man_set(u8 *plate,u8 *plate_ve,chee_loc *save,u8 *step,u8 first,p_type coms[15][15],p_type mans[15][15],p_type stack[max_sp][2][8][4])
{
   chee_loc set,comsets;
   u8 xp=(save+*step-1)->xp,yp=(save+*step-1)->yp;
   u8 xsp,ysp,i=0,done=0;
   u16 wait;
   if (!*step) {xp=7;yp=7;}
   xsp=xp;
   ysp=yp;
   while (TRUE)
   {
      for (i=0;i<10;i++)
      {
         for (wait=0;wait<1500;wait++) xp=xp;
         chee_box(xsp,ysp,i%2?0:(*(plate+15*xsp+ysp)?3:2));
      }
      CS_ReadKey();
      if (CS_IsKeyDown(KEY_UP))
      {
         done=1;
         ysp--;
         if (ysp>14) ysp=14;
      }
      else if(CS_IsKeyDown(KEY_DOWN))
      {
         done=1;
         ysp++;
         if (ysp>14) ysp=0;
      }
      else if(CS_IsKeyDown(KEY_LEFT))
      {
         done=1;
         xsp--;
         if (xsp>14) xsp=14;
      }
      else if(CS_IsKeyDown(KEY_RIGHT))
      {
         done=1;
         xsp++;
         if (xsp>14) xsp=0;
      }
      else if(CS_IsKeyDown(KEY_START))
      {
         comsets=comset(plate_ve,plate,0,coms,mans,stack);
         done=1;
         xsp=comsets.xp%100;
         ysp=comsets.yp%100;
      }
      else if(CS_IsKeyDown(KEY_A)) done=2;
      else if(CS_IsKeyDown(KEY_B)) done=3;
      else if(CS_IsKeyDown(KEY_R)) done=4;
      else if(CS_IsKeyDown(KEY_L)) done=5;
      switch (done)
      {
         case 1:
            done=0;
            chee_box(xp,yp,*(plate+15*xp+yp));
            xp=xsp;
            yp=ysp;
            show_num(120,18,xp*100+yp,4);
         break;
         case 2:
            if (*(plate+15*xsp+ysp))
            {
               arlt("出错啦!","该点已有棋子,请重新选放!");
               done=0;
            }
            else
            {
               chee_box(xsp,ysp,2);
               *(plate+xsp*15+ysp)=2;
               set.xp=xsp;
               set.yp=ysp;
               return set;
            }
         break;
         case 3:
            if (arlt("结束五子棋游戏","您真的要退出五子棋游戏吗?\n   [A:确定][B:取消]"))
            {
               set.xp=20;
               set.yp=20;
               return set;
            }
            done=0;
         break;
         case 4:
            *step=chee_back(plate,plate_ve,save,*step,first,coms,mans,stack);
            done=0;
         break;
         case 5:
            arlt("操作说明","←→ 移动 R键 梅棋\nA键 下棋 B键 退出");
            done=0;
         break;
         default:
      }
   }
}
void fivechee()
{
   u8 chee_plate[15*15],com_win,man_win;
   u8 plate_ve[15*15];
   u8 chee_test=0;//测试开关
   chee_loc save[15*15],man_sets,com_sets;
   p_type test;
   u8 steps=0;
   u8 *b_step=&steps;
   u8 i,j,k,think=14,flag=-1;
   u8 who_first=0;
   u8 manwin=0,comwin=0;
   p_type com[15][15];
   p_type man[15][15];
   p_type stack[max_sp][2][8][4];
   Q_box(44,10,195,152,RGB(24,15,13));
   Q_box(46,12,193,150,RGB(30,30,30));
   Q_box(47,13,192,149,RGB(24,15,13));
   chee_error=0;
   cwrite(50,16,RGB(30,30,30),"五子棋");
   r_line(47,28,193,28,RGB(30,30,30),1);
   chee_clear(chee_plate,plate_ve);
   cwrite(100,16,RGB(30,30,30),"∶请设置难度");
   chee_sethard();
   Q_box(99,15,181,27,RGB(24,15,13));
   who_first=arlt("请选择先手:","请您决定先手:\n[A键您先下] [B电脑先下]");
chee_start:
   steps=0;
   Q_box(168,30,192,148,RGB(24,15,13));
   chee_clear(chee_plate,plate_ve);
   Q_box(99,15,181,27,RGB(24,15,13));
   cwrite(100,16,RGB(30,30,30),"∶当前比分");
   chee_wins(manwin,comwin);
   Q_box(99,15,181,27,RGB(24,15,13));
   chee_clear(chee_plate,plate_ve);
   if (!who_first)
   {
      chee_plate[7*15+7]=1;
      plate_ve[7*15+7]=2;
      steps++;
      save[0].xp=7;
      save[0].yp=7;
      show_step(save,steps,who_first);
      chee_blink(7,7,1);
   }
   chee_sp=0;
   for (i=0;i<15;i++)
   for (j=0;j<15;j++)
   {
      com[i][j]=get_weight(i,j,1,chee_plate);
      man[i][j]=get_weight(i,j,2,chee_plate);
   }
   while (TRUE)
   {
      Q_box(140,16,191,26,RGB(24,15,13));
      cwrite(140,16,RGB(30,30,30),"请下棋…");
      man_sets=man_set(chee_plate,plate_ve,save,b_step,who_first,com,man,stack);
      if (man_sets.xp==20 && man_sets.yp==20) return;
      *(plate_ve+15*man_sets.xp+man_sets.yp)=1;
      *(chee_plate+15*man_sets.xp+man_sets.yp)=2;
      chee_push(man_sets.xp,man_sets.yp,com,man,stack,chee_plate);
      chee_sp--;
      man_win=is_win(man_sets,2,chee_plate);
      if (man_win)
      {
         show_win(man_sets,2,chee_plate);
         arlt("您赢咯!","电脑被你打败了!不要得意呀!");
         manwin++;
         who_first=!who_first;
         goto chee_start;
      }
      else
      {
         steps++;
         save_step(man_sets,save,steps);
         show_step(save,steps,who_first);
      }
      Q_box(140,16,191,26,RGB(24,15,13));
      cwrite(140,16,RGB(30,30,30),"思考中…");
      myobj_restart();
      com_sets=comset(chee_plate,plate_ve,0,com,man,stack);
      myobj_stop();
      com_sets.xp=com_sets.xp%100;
      com_sets.yp=com_sets.yp%100;
      if (chee_test)//测试开关
      {
         test=get_weight(com_sets.xp,com_sets.yp,1,chee_plate);
         show_num(1,100,test.weight,9);
         show_num(1,110,test.type,2);
         test=get_weight(com_sets.xp,com_sets.yp,2,chee_plate);
         show_num(1,120,test.type,2);
      }
      if (com_sets.xp<15 && com_sets.yp<15)
      {
         *(chee_plate+15*com_sets.xp+com_sets.yp)=1;
         *(plate_ve+15*com_sets.xp+com_sets.yp)=2;
         chee_push(com_sets.xp,com_sets.yp,com,man,stack,chee_plate);
         chee_sp--;//堆栈指针后退
         chee_box(com_sets.xp,com_sets.yp,1);
      }
      if (chee_error)
      {
         arlt("错误提示:","系统发现五子棋堆栈溢出,游戏被迫终止.");
         return;
      }
      if (!check_chee(chee_plate,who_first)) arlt("出错啦!","系统检测到,棋子有误!\n请退出游戏重新开始\n");
      Q_box(140,16,191,26,RGB(24,15,13));
      cwrite(140,16,RGB(30,30,30),"请开始…");
      if (com_sets.xp==20 && com_sets.yp==20)
      {
         arlt("游戏结束","呵呵,我和你打成平局了,你的技术不错呀!");
         goto chee_start;
         who_first=!who_first;
      }
      com_win=is_win(com_sets,1,chee_plate);
      steps++;
      save_step(com_sets,save,steps);
      show_step(save,steps,who_first);
      show_num(100,18,steps,3);
      if(chee_blink(com_sets.xp,com_sets.yp,1))
      {
         if(arlt("结束游戏","您真的要退出五子棋游戏吗?\n [A:退出]  [B:继续]")) return;
      }
      if (com_win)
      {
         show_win(com_sets,1,chee_plate);
         arlt("您败咯!","你被电脑打败了!多多努力呀!");
         comwin++;
         who_first=!who_first;
         goto chee_start;
      }
   }
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?