Пример #1
0
/* move chess 就等於吃棋子 */
move_chess(char y, char x, char yy, char xx, char *mark, char *turn)
{
 chess[y][x]=chess[yy][xx];
 chess[yy][xx]=0;
 show_chess(y, x, chess[y][x], 0);
 show_chess(yy, xx, 0, 0);
 *mark=0;
 *turn=nexturn(*turn);
}
Пример #2
0
/**
 * 算法6.16,八皇后问题求解
 * 进入本函数时,在n*n棋盘前i-1行已放置了互不攻击的i-1个棋子
 * 现从第i行起继续为后续棋子选择合适位置
 * 当i>n时,求得一个合法布局,输出之
 */
void Queen(int i, int n)   //i从1开始
{
	if (i > n)
		show_chess(n);  //输出棋盘当前布局
	else
	{
		for (int j = 1; j <= n; j++)
		{
			Place(i, j);    //在第i行第j列放置一个棋子
			if (illigal(i))    //若当前布局合法
				Queen(i + 1, n);
			Remove(i, j);   //移走第i行第j列的棋子
		}
	}
}
Пример #3
0
int
dark(int fd, user_info *uin, int t)
                                      {
 char x=0,y=0,turn=0,xx,yy,mark=0,color=-1;
 char data[100],shown[2]={0,0},result=0,peace=0;
 time_t rest,init=time(0);
 int key,datac;
//       screenline *screen; //for save the scre
 
 data[0]=0;
 start(uin);
 add_io(fd, 0);
 t=t?1:0;
 
 if(!t) { /* 兩邊棋盤資料同步 */
  for(yy=0; yy<8; yy++)
   strncat(data, chess[yy], 4);
  do {
   datac=send(fd, data, strlen(data), 0);
  } while(datac<0);
 }
 else {
  do {
   key=igetkey();
   if (key == I_OTHERDATA) {
     datac= recv(fd, data, sizeof(data), 0);
     if(datac<=0) {add_io(0, 0); return;}
     for(yy=0; yy<8; yy++)
      strncpy(chess[yy], data+yy*4, 4);
   }
  } while(key != I_OTHERDATA);
 }
 move(18,0); clrtoeol();
 
 do {
  printtt(19, 56, "%s",t==turn?"自己":"對方"); 
  refresh();
  rest=time(0);
  
  do {
    if(t==turn) {
      show_cursor(y, x, 1);
      move(18, 0);
    }
    key=time(0)-init;
    datac=ttt+rest-time(0);

    if(datac<1 && turn==t) {
      change_record(1);
      add_io(0,0); bell();
      pressanykey("時間到囉!!..這局輸囉.. :~(");
      return 0;
    }
    else if(datac<1) {
      change_record(0);
      add_io(0,0); bell();
      pressanykey("對方可能睡著啦~ so算你贏了!! ^^Y");
      return 0;
    }
    
    move(22,34); 
    prints("%2d:%2d:%2d",key/3600,(key%3600)/60,key%60);
    printtt(21,47,"4%d;1m  %d:%2d",datac<30?1:4,datac/60,datac%60);
    move(b_lines,0); clrtoeol();
    prints(" 說明  [←↑↓→/jikl]移動 \
[Enter/Space]確定 [w]傳訊 [p]和棋 [q]\
投降 [h]Help ");
    refresh();
    key=igetkey();
    peace=0;

     if(key==I_OTHERDATA) {
       bell();
       datac= recv(fd, data, sizeof(data), 0);
       if(datac<=0) {add_io(0, 0); return;}
       
       if(*data=='P') {
         getdata(18, 0, "對方提出和棋的要求..答應和棋嗎?? (y/n) [n]", data, 4, LCECHO, 0);
         move(18,0); clrtoeol();
         do {
          datac=send(fd, data, 1, 0);
         } while(datac<0);
         if(*data=='y') {
           change_record(2);
           add_io(0, 0);
           pressanykey("雙方握手言和 :)");
           return 0;
         }
         key=0;
       }
       else if(*data=='Q') {
         change_record(0);
         add_io(0, 0);
         pressanykey("耶耶耶..對方投降了.. ^^Y");
         return 0;
       }
       else {
         yy=data[0]-1; xx=data[1]-1;
         y=data[2]-1; x=data[3]-1;

         if(darked(chess[y][x])) { /* 對方翻棋 */
           if(color<0) {
             color=nexturn(red(chess[y][x]));
             show_color(color);
           }
           show_chess(y, x, chess[y][x], 0);
           chess[y][x]--;
           turn=nexturn(turn);
         }
         else {
           if(chess[y][x]) 
             result=show_eaten(chess[y][x],shown[t]++,0);
           move_chess(y,x,yy,xx,&mark,&turn);
         }
       }
     }

   switch(key) {
     case 'h':
      {
       extern screenline* big_picture;
       screenline* screen0 = calloc(t_lines, sizeof(screenline));
       memcpy(screen0, big_picture, t_lines * sizeof(screenline));
       more("game/dark.help",YEA); //* 改成自己的路徑
       memcpy(big_picture, screen0, t_lines * sizeof(screenline));
       free(screen0);
       redoscr();
      }
       break;
     case 'w':
      {
       extern screenline* big_picture;
       screenline* screen0 = calloc(2, sizeof(screenline));
       memcpy(screen0, big_picture, 2 * sizeof(screenline));
       my_write(uin->pid, "傳訊息: ");
       memcpy(big_picture, screen0, 2 * sizeof(screenline));
       free(screen0);
       redoscr();
      }
       break;
     case 'p':
       if(t==turn) {
         int keyy;
         if(peace) break;
         bell();
         getdata(18, 0, "真的要和棋嗎?? (y/n) [n]", data, 4, LCECHO, 0);
         move(18, 0); clrtoeol(); 
         if(*data!='y') break;
         prints("詢問對手是否同意和棋..請稍後... :)"); refresh();
         peace=1;
         do {
          datac=send(fd, "P", 1, 0);
         } while(datac<0);

         do {
           keyy=igetkey();
           if(keyy==I_OTHERDATA) {
             datac= recv(fd, data, sizeof(data), 0);
             if(datac<=0) {add_io(0, 0); return 0;}
             move(18, 0); clrtoeol(); 
             if(*data=='y') {
               change_record(2);
               add_io(0, 0);
               pressanykey("雙方握手言和 :)");
               return 0;
             }
             else 
               pressanykey("對方不同意和棋.. :~");
           }
         } while (keyy!=I_OTHERDATA);
       }
       break;
     case 'q':
       if(t==turn) {
         bell();
         getdata(18, 0, "真的要投降嗎?? (y/n) [n]", data, 4, LCECHO, 0);
         move(18,0); clrtoeol();
         if(*data!='y') break;
         do {
          datac=send(fd, "Q", 1, 0);
         } while(datac<0);
         change_record(1);
         add_io(0, 0);
         pressanykey("啊啊啊..我不行了..投降~ ^^;");
         return 0;
       }
       break;
     default:
      if(t!=turn) key=0;
      break;
   }

   if(t!=turn) continue;
   switch(key) {
    case ' ':
    case 13:
     if(!mark && darked(chess[y][x])) { /* 翻棋 */
       if(color<0) {
        color=red(chess[y][x]);
        show_color(color);
       }
       show_chess(y, x, chess[y][x], 0);
       chess[y][x]--;
       turn=nexturn(turn);
     }
     
     else if(mark) { /* 已 mark 的處理 */
       if(!chess[y][x] && nexto(x-xx,y-yy)) /* 移動 */
        move_chess(y,x,yy,xx,&mark,&turn);
       else if(!mychess(y,x,color) && !darked(chess[y][x])) { /* 吃對方的棋子 */
         if(nexto2(x-xx,y-yy) && mark==6) { /* mark 起來的棋子不在隔壁(炮) */
          char i,tmp=0;
          if(x==xx)
           for(i=min(y,yy)+1; i<max(y,yy); i++)
            tmp+=(chess[i][x]!=0);  
          else
           for(i=min(x,xx)+1; i<max(x,xx); i++)
            tmp+=(chess[y][i]!=0);  
          if(tmp==1 && chess[y][x]) {
            result=show_eaten(chess[y][x],shown[nexturn(t)]++,1);
            move_chess(y,x,yy,xx,&mark,&turn);
          }
          else cancel_mark(yy,xx,&mark,&key);
         }
         else { /* mark 起來的棋子在隔壁 */
           if( nexto(x-xx,y-yy) &&
              ((mark==7 && rank(chess[y][x])==1) || /* 小兵立大功 */
               ( mark<=rank(chess[y][x]) &&
                 rank(chess[y][x])-mark!=6  ))  ) { /* 將軍怕小兵 */
             result=show_eaten(chess[y][x],shown[nexturn(t)]++,1);
             move_chess(y,x,yy,xx,&mark,&turn);
           }
           else cancel_mark(yy,xx,&mark,&key);
         }
       }
       else cancel_mark(yy,xx,&mark,&key); /* 不能移不能吃 */
     }
     
     /* mark 起來 */     
     else if(!mark && chess[y][x] && mychess(y,x,color)) { /* mark 起來 */
       show_chess(y, x, chess[y][x], 1);
       mark=rank(chess[y][x]);
       xx=x; yy=y;
       key=0;
     }
     else key=0;
     break;
     
    case 'u':
     if(mark) cancel_mark(yy, xx, &mark, &key);
     break;
    case KEY_LEFT:
    case 'j':
     show_cursor(y, x, 0);
     y=y==0?7:y-1;
     break;
    case KEY_RIGHT:
    case 'l':
     show_cursor(y, x, 0);
     y=y==7?0:y+1;
     break;
    case KEY_UP:
    case 'i':
     show_cursor(y, x, 0);
     x=x==0?3:x-1;
     break;
    case KEY_DOWN:
    case 'k':
     show_cursor(y, x, 0);
     x=x==3?0:x+1;
     break;
   } 
   
  } while(key!=' ' && key!=13 && key!=I_OTHERDATA);
  
  show_cursor(y, x, 0);
  
  if(t==turn) continue; /* turn已經動過..so是相反的值 */
  data[0]=yy+1; data[1]=xx+1; 
  data[2]=y+1; data[3]=x+1;
  do {
    datac=send(fd, data, 4, 0);
  } while(datac<0);
  
 } while(!result);
 add_io(0, 0);  
 if(!turn) 
 {
   FILE *fs;
   init=time(0)-init;
   if(fs=fopen("log/dark.log","a+")) //* 可改成自己想要的路徑
   {
     fprintf(fs,"%s win %s %d:%d\n",currutmp->userid,uin->userid,init/60,init%60);
     fclose(fs); 
   }
 }
 change_record(1);
 pressanykey("恭禧你%s了 ^^Y",t==turn?"輸":"贏");
}
Пример #4
0
cancel_mark(char yy, char xx, char *mark, int *key)
{
 show_chess(yy, xx, chess[yy][xx], 0);
 *mark=0; *key=0;
}