Từ 1 tới 2 trên tổng số 2 kết quả

Đề tài: Lập trình xử lý 8 con hậu?

  1. #1
    Ngày gia nhập
    10 2007
    Bài viết
    0

    Question Lập trình xử lý 8 con hậu?

    em code bài 8 hậu, mà chưa ra được hướng, mấy anh coi, rùi hướng dẫn giúp em nha cảm ơn nhiều
    code:
    C Code:
    1. # include <graphics.h>
    2. # include <conio.h>
    3. # include <stdio.h>
    4. # include <stdlib.h>
    5. # include <math.h>
    6.  
    7.  
    8. void khoitao(int a[][8])
    9. {
    10.     int i,j;
    11.     for(i=0;i<8;i++)
    12.         for(j=0;j<8;j++)
    13.             a[i][j]=0;
    14. }
    15. void dathau(int a[][8],int x,int y)
    16. {
    17.     int i,j,i1,j1,f1,f2;
    18.     f1=f2=0;
    19.     for(j=0;j<8;j++)
    20.         a[x][j]=1;
    21.     for (i=0;i<8;i++)
    22.         a[i][y]=1;
    23.     i=i1=x;
    24.     j=j1=y;
    25.     while (f1!=1 || f2!=1)
    26.     {
    27.         if(i>0 && j>0)
    28.             a[--i][--j]=1;
    29.         else
    30.             f1=1;
    31.         if (i1<8 && j1<8)
    32.             a[++i1][++j1]=1;
    33.         else
    34.             f2=1;
    35.     }
    36.     i=i1=x;
    37.     j=j1=y;
    38.     f1=f2=0;
    39.     while (f1!=1 || f2!=1)
    40.     {
    41.         if(i>0 && j<8)
    42.             a[--i][++j]=1;
    43.         else
    44.             f1=1;
    45.         if (i1<8 && j1>0)
    46.             a[++i1][--j1]=1;
    47.         else
    48.             f2=1;
    49.     }
    50.     a[x][y]=2;
    51. }
    52. void gohau(int a[][8],int x)
    53. {
    54.     int i,j,i1,j1,f1,f2,y;
    55.     for (j=0;j<8;j++)
    56.         if(a[x][j]==2)
    57.             y=j;
    58.     f1=f2=0;
    59.     for(j=0;j<8;j++)
    60.     {
    61.         if (a[x][j]==-1) j=j+1;
    62.         a[x][j]=0;
    63.     }
    64.     for (i=0;i<8;i++)
    65.     {   if (a[i][y]==-1) i=i+1;
    66.         a[i][y]=0;
    67.     }
    68.     i=i1=x;
    69.     j=j1=y;
    70.     while (f1!=1 || f2!=1)
    71.     {
    72.         if(i>0 && j>0)
    73.         {   if (a[i][j]==-1)
    74.                 {i--;j--;}
    75.             a[--i][--j]=0;
    76.         }
    77.         else
    78.             f1=1;
    79.         if (i1<8 && j<8)
    80.         {   if (a[i][j]==-1)
    81.                 {i1++;j1++;}
    82.             a[++i1][++j1]=0;
    83.         }
    84.         else
    85.             f2=1;
    86.     }
    87.     i=i1=x;
    88.     j=j1=y;
    89.     f1=f2=0;
    90.     while (f1!=1 || f2!=1)
    91.     {
    92.         if(i>0 && j<8)
    93.         {   if (a[i][j]==-1)
    94.                 {i--;j++;}
    95.             a[--i][++j]=0;
    96.         }
    97.         else
    98.             f1=1;
    99.         if (i1<8 && j>0)
    100.         {
    101.             if (a[i][j]==-1)
    102.                 {i1++;j1--;}
    103.             a[++i1][--j1]=0;
    104.         }
    105.         else
    106.             f2=1;
    107.     }
    108.     a[x][y]=-1;
    109.     for(i=0;i<8;i++)
    110.         for(j=0;j<8;j++)
    111.             if(a[i][j]==2)
    112.                 dathau(a,i,j);
    113.  
    114. }
    115.  
    116. void xuat(int a[][8])
    117. {
    118.     int i,j;
    119.     printf("\n");
    120.     for(i=0;i<8;i++)
    121.     {
    122.         for(j=0;j<8;j++)
    123.             printf("%d\t",a[i][j]);
    124.         printf("\n");
    125.     }
    126.  
    127. }
    128. int sapxephau(int a[8][8],int hau,int i)
    129. {
    130.     int j,kt;
    131.     kt=0;
    132.     xuat(a);
    133.  
    134.     if (hau==8) return 9;
    135.  
    136.     if (i==3) i++;
    137.  
    138.     for(j=0;j<8;j++)
    139.     {
    140.         if(a[i][j]==0)
    141.         {
    142.             dathau(a,i,j);
    143.             hau=hau+1;
    144.             kt=sapxephau(a,hau,i+1);
    145.             if (kt==1)
    146.             {
    147.                 hau=hau-1;
    148.                 gohau(a,i);
    149.                                 xuat(a);
    150.             }
    151.         }
    152.     }
    153.  
    154.     for(j=0;j<8;j++)
    155.        {
    156.         if (a[i][j]==2)
    157.             return 0;
    158.         else
    159.             return 1;
    160.        }
    161. }
    162.  
    163. void main()
    164. {
    165.  clrscr();
    166.  int a[8][8];
    167.  khoitao(a);
    168.  dathau(a,3,4);
    169.  sapxephau(a,1,0);
    170.  xuat(a);
    171.  getch();
    172. }
    GIÚP EM VỚI NHA !!!
    THANKS NHIỀU
    Đã được chỉnh sửa lần cuối bởi iamvtn : 16-11-2007 lúc 01:18 PM. Lý do: Không để thẻ code vào trong code

  2. #2
    Ngày gia nhập
    07 2007
    Nơi ở
    Sơn La
    Bài viết
    133

    Bài này bạn nên làm bằng quay lui(sử dụng đệ quy) chứ làm vậy không ổn lắm.Với lại khi truyền đối số vào các hàm phải truyền địa chỉ (tham chiếu) như vậy mới làm thay đổi được vị trí của ô cờ đã hay chưa đặt hậu.
    Để mình xem kỹ hơn đã rồi trao đổi tiếp.
    Bạn có thể tham khảo bài của mình.(Bạn tạo tệp project rồi add 2 cái vô).
    Mình định cho Mouse vô, với nhiều kích thước bàn cờ.Nhưng chưa có thời gian nên vẫn để vậy.

    C Code:
    1. #include <iostream.h>
    2. #include <stdlib.h>
    3. #include <string.h>
    4. #include <conio.h>
    5. #include <stdio.h>
    6. #include <dos.h>
    7. #include <math.h>
    8. #include <alloc.h>
    9. #define o_free 0
    10. #include <graphics.h>
    11. #define SIZE 50
    12.  
    13. void khung();
    14. void ban_co();
    15. int Pane[8][8];//ban co 8*8
    16. int sc,state=0;
    17. struct tao_do{
    18.  int x[8],y[8];
    19. };
    20. struct save{
    21.  tao_do Hau;
    22.  save *next;
    23. };
    24.  save *dau,*temp,*cuoi;
    25. void luu(){
    26.           sc++;
    27.           if(dau==NULL)
    28.             {
    29.         dau=(struct save*)malloc(sizeof(save));
    30.         cuoi=dau;
    31.         cuoi->Hau=temp->Hau;
    32.         cuoi->next=NULL;
    33.             }else{
    34.          cuoi=dau;
    35.          while(cuoi->next!=NULL) cuoi=cuoi->next;
    36.          cuoi->next=(struct save*)malloc(sizeof(save));
    37.          cuoi=cuoi->next;
    38.          cuoi->Hau=temp->Hau;
    39.          cuoi->next=NULL;
    40.              }//eoE
    41.      return;
    42.   }
    43. void khoi_tao(){
    44.   for(int i=0;i<8;i++)
    45.      for(int j=0;j<8;j++)
    46.       Pane[i][j]=o_free;
    47.       sc=0;
    48.       temp=(struct save*)malloc(sizeof(save));
    49.       dau=NULL;
    50.       return ;
    51. }
    52.  
    53. void xoa(int hn){
    54.     for(int i=0;i<8;i++)
    55.      for(int j=0;j<8;j++)
    56.       if(Pane[i][j]==hn+1) Pane[i][j]=o_free;
    57.      return ;
    58. }
    59.  
    60. void write_result(){
    61.   char *ch;
    62.   int J=1;
    63.   setcolor(RED);
    64.   outtextxy(100,10,itoa(sc,ch,10));
    65.   setcolor(WHITE);
    66.   rectangle(96, 6,102+(strlen(itoa(sc,ch,10)))*8,20);
    67.   rectangle(96,22,102+(strlen(itoa(sc,ch,10)))*8,34);
    68.   cuoi=dau;
    69.   while(cuoi!=NULL){
    70.       setfillstyle(SOLID_FILL,YELLOW);
    71.       floodfill(97,25,WHITE);
    72.       setcolor(CYAN);
    73.       if( J <10 ){
    74.       outtextxy(100,24,"0");
    75.       outtextxy(110,24,itoa(J,ch,10));
    76.       }else outtextxy(100,24,itoa(J,ch,10));
    77.       ban_co();
    78.       setcolor(YELLOW);
    79.       for(int i=0;i<8;i++)
    80.       circle((cuoi->Hau.x[i]-1)*SIZE+10+SIZE/2,getmaxy()-(cuoi->Hau.y[i]-1)*SIZE-10-SIZE/2,14);
    81.       cuoi=cuoi->next;
    82.       ++J;
    83.       getch();
    84.     }
    85.   return ;
    86. }
    87.  
    88. void da_di(int hn,int hd){
    89.     for(int i=0;i<8;i++)
    90.      for(int j=0;j<8;j++)
    91.       {
    92.         if(abs(j-hn)==abs(i-hd)&& Pane[i][j]==o_free) Pane[i][j]=hn+1;
    93.         if(i==hd && Pane[i][j]==o_free)               Pane[i][j]=hn+1;
    94.         if(j==hn && Pane[i][j]==o_free)               Pane[i][j]=hn+1;
    95.       }
    96.      return ;
    97. }
    98.  
    99. void run(int tt)
    100. {
    101.   for(int i=0;i<8;i++)
    102.   {
    103.     if(tt==8) { luu();break;}
    104.      else{          
    105.       if(Pane[i][tt]==o_free)
    106.                  {
    107.             temp->Hau.x[tt]=tt+1;
    108.              temp->Hau.y[tt]=i+1;
    109.               da_di(tt,i);
    110.              run(tt+1);
    111.             xoa(tt);
    112.           }
    113.             }//eoe
    114.     }//eoi  1
    115. }
    116.  
    117. main(){
    118.   int mh=DETECT,kieu;
    119.     initgraph(&mh,&kieu,"c:\\tc\\bgi");
    120. khung();
    121. khoi_tao();
    122. run(0);
    123. write_result();
    124. closegraph();
    125. return 0;
    126. }

    đó là tệp chính, còn tệp sau là tệp đồ họa(vì mình làm bên đồ họa nên ở đây mình up luôn và không có thời gian sửa lại)
    C Code:
    1. #include "conio.h"
    2. #include "stdio.h"
    3. #include "stdlib.h"
    4. #include "graphics.h"
    5.  
    6. #define  CL_khung RED
    7. #define  MAXX getmaxx()
    8. #define  MAXY getmaxy()
    9. #define  SIZE 50
    10.  
    11. void Fill(int i,int j)//to o ban co
    12.  {
    13.     if((i+j)%2==0) {
    14.              setfillstyle(SOLID_FILL,MAGENTA);
    15.               floodfill(j*SIZE+35,MAXY-i*SIZE-35,WHITE);
    16.                }else{
    17.                  setfillstyle(SOLID_FILL,CYAN);
    18.                   floodfill(j*SIZE+35,MAXY-i*SIZE-35,WHITE);
    19.                 }//E of E
    20.  }
    21.  
    22. extern void ban_co()    //ve ban co
    23.  {
    24.     int i,j;
    25.     setcolor(WHITE);
    26.     for(i=0;i<8;i++)
    27.       for( j=0;j<8;j++)
    28.        {
    29.          rectangle(j*SIZE+10,MAXY-i*SIZE-10,(j+1)*SIZE+10,MAXY-(i+1)*SIZE-10);
    30.           Fill(i,j);//fill pane
    31.        }
    32.  }
    33.  
    34. extern void khung()//ve khung
    35.  {
    36.   //khung va nen
    37.   setcolor(CL_khung);
    38.   rectangle(0,0,MAXX,MAXY);
    39.   setfillstyle(SOLID_FILL,BLACK+BLUE);
    40.   floodfill(1,1,CL_khung);
    41.   //bong do 3D cho ban co
    42.   setfillstyle(1,BLACK+DARKGRAY);
    43.   bar(8*SIZE+10,MAXY-5,8*SIZE+15,MAXY-8*SIZE-5);//doc
    44.   bar(15,MAXY-5,8*SIZE+10,MAXY-10);//ngang
    45.   ban_co();
    46.  }
    Trao đổi kiến thức sẽ giúp ta tiếp cận nhanh với kiến thức.

Quyền hạn của bạn

  • Bạn không thể gửi đề tài mới
  • Bạn không thể gửi bài trả lời
  • Bạn không thể gửi các đính kèm
  • Bạn không thể chỉnh sửa bài viết của bạn