qishi.c

来自「C实现的MUD,对大家基本入门网络游戏很有帮助!」· C语言 代码 · 共 657 行 · 第 1/2 页

C
657
字号
     if(j>0) {
        color=bd[i*bsize+j-1];
        if(color==WQ_BLANK) qi_flag=1;
          else if(color==mycolor) {
          alist[aindex]=i*bsize+j-1; aindex++;
        }
     }
     if(j<bsize-1) {
        color = bd[i*bsize+j+1];
        if(color==WQ_BLANK) qi_flag=1;
          else if(color==mycolor) {
          alist[aindex]=i*bsize+j+1; aindex++;
        }
     }
   }

   while(bindex>0) { bd[blist[bindex-1]] = mycolor; bindex--; }

   return(!qi_flag);

}

/* This routine removes a block of stones which contains (x,y).
  It returns the number of stones removed. */

int remove_block(int * bd, int x, int y)
{
   int i,j;
   int color;
   int * alist=allocate(bsize*bsize);
   int * blist=allocate(bsize*bsize);
   int aindex=0;
   int bindex=0;
   int num;

   color=bd[x*bsize+y]; // should be either WQ_BLACK or WQ_WHITE
   alist[0]=x*bsize+y;   aindex++;
        while(aindex>0) {
                i=alist[aindex-1]/bsize;
                j=alist[aindex-1]%bsize;
                aindex--;
                blist[bindex]=i*bsize+j;   bindex++;
                bd[i*bsize+j]=WQ_AUX_COLOR; 
                if(i>0 && color==bd[(i-1)*bsize+j]) {
                        alist[aindex]=(i-1)*bsize+j; aindex++;
                }
                if(i<bsize-1 && color==bd[(i+1)*bsize+j]) {
                        alist[aindex]=(i+1)*bsize+j; aindex++;
                }
                if(j>0 && color==bd[i*bsize+j-1]) {
                        alist[aindex]=i*bsize+j-1; aindex++;
                }
                if(j<bsize-1 && color==bd[i*bsize+j+1]) {
        alist[aindex]=i*bsize+j+1; aindex++;
     }
   }

   num=bindex;
   while(bindex>0) { bd[blist[bindex-1]]=WQ_BLANK; bindex--; }
   return(num);
}
     

/* Function 'eat' returns the number of stones eaten by move
 (x,y), according to the data of bd[19][19] with move (x,y) set.
 eat modifies bd[19][19] by removing stones eaten. 
 eat also stores the position of the last stone eaten in
 *x_eat_ptr, * y_eat_ptr.
*/
 
int eat(int * bd, int x, int y, int * x_eat_ptr, int * y_eat_ptr)
{       
   int mycolor, opcolor;
   int num=0;

   mycolor=bd[x*bsize+y];
   if(mycolor==WQ_BLACK) opcolor=WQ_WHITE;
     else opcolor=WQ_BLACK;
   if(x>0 && bd[(x-1)*bsize+y]==opcolor && no_qi(bd,x-1,y) ) {
     num += remove_block(bd,x-1,y); 
     x_eat_ptr[0]=x-1; y_eat_ptr[0]=y;
   }
   if(x<bsize-1 && bd[(x+1)*bsize+y]==opcolor && no_qi(bd,x+1,y) ) {  
     num += remove_block(bd,x+1,y);
     x_eat_ptr[0]=x+1; y_eat_ptr[0]=y;
   }
   if(y>0 && bd[x*bsize+y-1]==opcolor && no_qi(bd,x,y-1) ) { 
     num += remove_block(bd,x,y-1);
     x_eat_ptr[0]=x; y_eat_ptr[0]=y-1;
   }
   if(y<bsize-1 && bd[x*bsize+y+1]==opcolor && no_qi(bd,x,y+1) ) {
     num += remove_block(bd,x,y+1);
     x_eat_ptr[0]=x; y_eat_ptr[0]=y+1;
   }

   return(num);
}



/* Function 'weiqi_rule' decides if the move (x,y) is legal under the
 weiqi rule. Return value 1(legal) or 0(illegal). */
 
int weiqi_rule(int x, int y)
{
   int i,j;
   int n;
   int *board = allocate(bsize*bsize);
   int *x_eat_ptr=allocate(1);
   int *y_eat_ptr=allocate(1);

   if(x<0 || x>=bsize || y<0 || y>=bsize) return 0;
     // This case is avoided by exact matching in 
     // translate_position().
        if(game[x*bsize+y] != WQ_BLANK) 
     return(WQ_POS_OCCUPIED);
   
   for(i=0;i<bsize;i++) for(j=0;j<bsize;j++) 
     board[i*bsize+j] = game[i*bsize+j];
   if(turn == "black") board[x*bsize+y] = WQ_BLACK;
     else board[x*bsize+y] = WQ_WHITE;
   n = eat(board,x,y,x_eat_ptr,y_eat_ptr);
   if(n>1) {
     for(i=0;i<bsize;i++) for(j=0;j<bsize;j++) 
        game[i*bsize+j] = board[i*bsize+j];
     jie_flag = WQ_NO_JIE;
     return 1;
   }
   else if(n==1) {
     if(jie_flag==WQ_JIE_POSSIBLE && x==jie_x_ban && y==jie_y_ban)
        return(WQ_JIE_BANNED);
     for(i=0;i<bsize;i++) for(j=0;j<bsize;j++) 
        game[i*bsize+j] = board[i*bsize+j];
     jie_flag = WQ_JIE_POSSIBLE;
     jie_x_ban = x_eat_ptr[0]; 
     jie_y_ban = y_eat_ptr[0];
     return 1;
   }
   else { // (n==0)
     if(no_qi(board,x,y)) 
        return(WQ_NO_QI_BANNED);
     game[x*bsize+y] = board[x*bsize+y];
     jie_flag = WQ_NO_JIE;
     return 1;
   }
}

int wuzi_rule(int x, int y)
{
   int i,j, color;
   int count;
   if(game[x*bsize+y] != WQ_BLANK)
     return(WQ_POS_OCCUPIED);
   if(turn == "black") game[x*bsize+y] = WQ_BLACK;
     else game[x*bsize+y] = WQ_WHITE;
   color=game[x*bsize+y];

   for(count=1,i=x+1,j=y+1; i>=0 && i<bsize && j>=0 && j<bsize &&
     color==game[i*bsize+j]; i++,j++,count++);
   for(i=x-1,j=y-1; i>=0 && i<bsize && j>=0 && j<bsize &&
     color==game[i*bsize+j]; i--,j--,count++);
   if(count>=5) return(WQ_WINNING);

        for(count=1,i=x-1,j=y+1; i>=0 && i<bsize && j>=0 && j<bsize &&
                color==game[i*bsize+j]; i--,j++,count++);
        for(i=x+1,j=y-1; i>=0 && i<bsize && j>=0 && j<bsize &&
                color==game[i*bsize+j]; i++,j--,count++);
        if(count>=5) return(WQ_WINNING);

        for(count=1,i=x+1,j=y; i>=0 && i<bsize && j>=0 && j<bsize &&
                color==game[i*bsize+j]; i++,count++);
        for(i=x-1,j=y; i>=0 && i<bsize && j>=0 && j<bsize &&
                color==game[i*bsize+j]; i--,count++);
        if(count>=5) return(WQ_WINNING);

        for(count=1,i=x,j=y+1; i>=0 && i<bsize && j>=0 && j<bsize &&
                color==game[i*bsize+j]; j++,count++);
        for(i=x,j=y-1; i>=0 && i<bsize && j>=0 && j<bsize &&
                color==game[i*bsize+j]; j--,count++);
        if(count>=5) return(WQ_WINNING);

   return(1);
}


int translate_position(string s, int *x_ptr, int *y_ptr)
{
   int i, match_flag=0;
   string sl,sn;

   if (strlen(s)>3 || strlen(s)<2) return 0;
   sl=s[0..0];
   for(i=0;i<bsize;i++) {
     if(sl==Ucase[i] || sl==lcase[i]) {
        x_ptr[0] = i;
        match_flag=1;
     }
   }
   if(!match_flag) return 0;
   match_flag = 0;
   sn=s[1..strlen(s)-1];
   for(i=0;i<bsize;i++) {
     if(sn==nindex[i]) {
        y_ptr[0] = i;
        match_flag=1;
     }
   }
   if(match_flag) return 1;
   else return 0;
}
   
int do_undo()
{
    int *x_ptr=allocate(1);
    int *y_ptr=allocate(1);
    object me = this_player();
    object rm = environment(me);
    object player;
    if(!me->query_temp("weiqi_seat"))
        return notify_fail("你是旁观者,怎么好意思瞎指挥?\n");
    if(status==WQ_NOT_PLAYING) 
        return notify_fail("还没新开一局棋呐。\n");
   if (status!=WQ_PLAYING_WUZI)
     return notify_fail("目前只提供五子棋的悔棋功能。\n");
   if(!WQ_Started)
     return notify_fail("一步都没走,悔什么棋。\n");
   if (me->query_temp("weiqi_seat")!= turn)
     return notify_fail("要悔棋也得跟人家商量一下呀。\n");
   if (WQ_Undoed)
     return notify_fail("一次只能悔一步棋。\n");

   WQ_Undoed=1;
   translate_position(lastmove,x_ptr,y_ptr);
   if (status==WQ_PLAYING_WUZI)
     {
        game[x_ptr[0]*bsize+y_ptr[0]]=WQ_BLANK;
        if (lastlastmove=="")
          {
             WQ_Started=0;
             lastmove="";   
          }
        else
          {
             lastmove=lastlastmove;
          }
     }
   else
     {
     }
   if (turn=="black") 
     {
        turn="white";
        if (lastmove!="")
          tell_room(rm,"\n黑棋上一步走在了"+lastmove+"\n");
     }
   else { 
     turn="black";
     if (lastmove!="")
        tell_room(rm,"\n白棋上一步走在了"+lastmove+"\n");
        }
    tell_room(rm,show_game());
    player = pl[turn];
    if(turn=="black")
       tell_room(rm,"现在轮到黑方"+player->name()+"走棋。\n");
    else tell_room(rm,"现在轮到白方"+player->name()+"走棋。\n");

   return 1;
}
        
int do_play(string arg)
{
   int *x_ptr=allocate(1);
   int *y_ptr=allocate(1);
   object me = this_player();
   object rm = environment(me);
   object op;
   int rv;
   int wf=0; // winning flag

   if(!me->query_temp("weiqi_seat"))
     return notify_fail("你是旁观者,怎么好意思瞎指挥?\n");
   if(status==WQ_NOT_PLAYING) 
     return notify_fail("还没新开一局棋呐。\n");
   if(me->query_temp("weiqi_seat") != turn)
     return notify_fail("还没轮到你走棋。\n");
   if(!translate_position(arg,x_ptr,y_ptr))
     return notify_fail("你要下在哪里?\n");

   if(status==WQ_PLAYING_WUZI) {
     rv = wuzi_rule(x_ptr[0],y_ptr[0]);
     if(rv==WQ_POS_OCCUPIED)
        return notify_fail("这个位置上已经有子了!\n");
     if(rv==WQ_WINNING) wf=1;
   }
    else {
     rv=weiqi_rule(x_ptr[0],y_ptr[0]);
     if(rv==WQ_POS_OCCUPIED)
        return notify_fail("这个位置上已经有子了!\n");
     if(rv==WQ_JIE_BANNED)
        return notify_fail("现在还没轮到你提劫!\n");
     if(rv==WQ_NO_QI_BANNED)
        return notify_fail("这个位置是禁入点!\n");
   }
   WQ_Started=1;
   WQ_Undoed=0;
   lastlastmove=lastmove;
   lastmove=arg;
   if (turn=="black") {
     tell_room(rm, "\n黑棋上一步走在了"+arg+"\n");
               }
   else
     tell_room(rm, "\n白棋上一步走在了"+arg+"\n");
   if(wf) {
     tell_room(rm,show_game());
     if(turn=="black") message_vision("黑方$N胜。\n",me);
      else message_vision("白方$N胜。\n",me);
     status=WQ_NOT_PLAYING;
     return 1;
   }
   if(turn=="black") { turn = "white"; op = pl["white"]; }
     else { turn = "black"; op = pl["black"]; }
   tell_room(rm,show_game());
   if(turn=="black") message_vision("现在轮到黑方$N走棋。\n",op);
    else message_vision("现在轮到白方$N走棋。\n",op);
   return 1;
}

⌨️ 快捷键说明

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