china chess win32

发表于:2007-07-01来源:作者:点击数: 标签:
#include #include "resource.h" LRESULT CALLBACKWndProc(HWND, UINT, WPARAM, LPARAM); void drawstar1(HDC hdc,int x,int y); void drawstar2(HDC hdc,int x,int y); void drawstar(HDC hdc,int x,int y); void drawboard(HDC hdc); void drawpallate(HDC


#include
#include "resource.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void drawstar1(HDC hdc,int x,int y);
void drawstar2(HDC hdc,int x,int y);
void drawstar(HDC hdc,int x,int y);
void drawboard(HDC hdc);
void drawpallate(HDC hdc);
void finishchesser(HDC hdc);
BOOL  isCanGo(int master,POINT  from, POINT  to);
BOOL isxy(POINT  mouse);
BOOL  IsNormal(  int  master, POINT to );
int   l,ll;
HDC   hdc ;
  POINT     lmouse;
int larry[12][11]=
 {
  {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
  {  0,  1,  2,  3,  4,   5, 4,   3, 2,  1,  0},
  {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
  {  0,  0, 6,   0,  0,  0,   0,  0,  6, 0,  0},
  {  0, 7,  0,   7,  0,  7,   0,  7,  0, 7,  0},
  {  0,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0},

  {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},
  {  0,  14, 0,  14, 0,  14, 0,  14, 0, 14,  0},
  {  0,  0,  13,  0,  0,  0,  0,  0, 13, 0,  0},
  {  0,  0,  0,   0,  0,  0,  0,  0, 0, 0,  0},
  {  0,  8,  9,  10,  11, 12, 11, 10, 9,  8, 0},
  {  0,  0,  0,   0,  0,  0,  0,  0,  0,  0,  0}
 };//棋盘十行九列
HINSTANCE hInst;
HWND      hwnd;
 
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
    static  TCHAR szAppName[] = TEXT ("SINGLECHESS") ;
 HWND  hwnd ;
 MSG    msg ;
 WNDCLASS  wndclass ;
 wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  wndclass.lpfnWndProc = WndProc ;
  wndclass.cbClsExtra  = 0 ;
  wndclass.cbWndExtra  = 0 ;
  wndclass.hInstance = hInstance ;
  wndclass.hIcon   = LoadIcon (NULL, IDI_APPLICATION) ;
 wndclass.hCursor  = LoadCursor (NULL, IDC_ARROW) ;
 wndclass.hbrBackground  = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
 wndclass.lpszMenuName   =  szAppName ;
 wndclass.lpszClassName  = szAppName ;
 
 if (!RegisterClass (&wndclass)) 
     {
         MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
               szAppName, MB_ICONERROR) ;
   
    return 0 ;
     }
 hwnd = CreateWindow (szAppName, TEXT ("中国民航飞行学院洛阳分院刘建国编2002.3 "),
   WS_OVERLAPPEDWINDOW,
   CW_USEDEFAULT, CW_USEDEFAULT,
   CW_USEDEFAULT, CW_USEDEFAULT,
   NULL, NULL, hInstance, NULL) ;
 ShowWindow (hwnd, iCmdShow) ;
 UpdateWindow (hwnd) ;
 while (GetMessage (&msg, NULL, 0, 0))
 {
    TranslateMessage (&msg) ;
    DispatchMessage (&msg) ;
    }
 return msg.wParam ;
}
 
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     PAINTSTRUCT ps ;
     static POINT  to,from, mouse ;
    
     static   int  cxClient, cyClient ;
static     int  i,j,k;
     static int lbutton=0;
     static int master;
     static int master1; 
     switch (message)
     { 
    case WM_CREATE:
          hInst = ((LPCREATESTRUCT) lParam)->hInstance ; 
          return 0 ;
     case WM_SIZE:
          cxClient = LOWORD (lParam) ;
          cyClient = HIWORD (lParam) ;
    ll=(min(cxClient,cyClient))/33;
          l=3*ll;
          return 0 ;   
     case WM_LBUTTONDOWN: 
                switch(lbutton)  
    {
    case 0:
                                  mouse.x =  LOWORD (lParam) ;
                                  mouse.y =  HIWORD (lParam) ;
          if(mouse.x<=2*ll||mouse.x>=10*l||mouse.y<=2*ll||mouse.y>=11*l){ lbutton=0;return 0;}
          if(isxy(mouse)==0 ){lbutton=0;  return 0; }
                         from.x=lmouse.x;
                                  from.y=lmouse.y;
                         master=larry[from.y][from.x];
          if(!master){lbutton=0;  return 0; }
////变量观察窗口
 //// char   lszBuffer [40] ;
 //// wsprintf (lszBuffer,   "  %d  " ,  master ) ;
 /////MessageBox(NULL,"master",lszBuffer ,MB_OK);
                                  lbutton=1;
                    break; 
    case 1:
     
                      lmouse.x =  LOWORD (lParam) ;
                               lmouse.y =  HIWORD (lParam) ;
                   if(lmouse.x<=2*ll||lmouse.x>=10*l||lmouse.y<=2*ll||lmouse.y>=11*l){lbutton=0;return 0;}
          if(isxy(lmouse)==0) { lbutton=0;  return 0;}
                        to.x=lmouse.x;
                                 to.y=lmouse.y;
                        master1=larry[to.y][to.x];
         if((from.x==to.x)&&(from.y==to.y)){lbutton=0;   return 0;}
         if(((master>=1)&&(master<=7))&&((master1>=1)&&(master1<=7))||
                                   ((master>=8)&&(master<=14))&&((master1>=8)&&(master1<=14))){lbutton=0;   return 0;}
//////变量观察窗口
 /////char  llszBuffer [40] ;
 /////wsprintf (llszBuffer,   "  %d  " ,  master1 ) ;
 /////MessageBox(NULL,"master1",llszBuffer ,MB_OK);
         if(isCanGo(master,from,to)==FALSE )  {lbutton=0;   return 0; }
 //// MessageBox(NULL,"masterkkkk",llszBuffer ,MB_OK);
         if(IsNormal(master, to)==FALSE) {lbutton=0;   return 0;}
        larry[to.y][to.x]=master;
                                larry[from.y][from.x]=0;
//////变量观察窗口        
 /////char szBuffer [40] ;
 /////wsprintf (szBuffer,   "  %d  , %d " ,  master , master1 ) ;
 /////MessageBox(NULL,"ok",szBuffer ,MB_OK);
 /////ReleaseDC (hwnd, hdc) ;
                                 InvalidateRect (hwnd, NULL, TRUE) ;
         lbutton=0;
                       break;
    }
               return 0; 
         
     case WM_PAINT: 
                   hdc = BeginPaint (hwnd, &ps) ; 
                   drawboard( hdc);
                   drawstar(hdc,3*ll,9*ll);
                   drawstar(hdc,24*ll,9*ll);
                   drawstar(hdc,3*ll,12*ll);
                   drawstar(hdc,15*ll,12*ll);
                   drawstar(hdc,21*ll,12*ll);
                   drawstar2(hdc,ll,12*ll);
                   drawstar1(hdc,27*ll,12*ll); 
                   drawstar(hdc,6*ll,24*ll);
                   drawstar(hdc,21*ll,24*ll);
                   drawstar2(hdc,3*ll,21*ll);
                   drawstar(hdc,9*ll,21*ll);
                   drawstar(hdc,15*ll,21*ll);
                   drawstar(hdc,21*ll,21*ll);
                   drawstar1(hdc,27*ll,21*ll);
                   drawpallate(hdc);
                   finishchesser(hdc);
          if(master1==5)MessageBox(NULL,"      恭贺你红方胜   ","中国民航飞行学院洛阳分院刘建国编",MB_OK);
                   if(master1==12)MessageBox(NULL,"     恭贺你 黑方胜   ","中国民航飞行学院洛阳分院刘建国编 ",MB_OK);
                   lbutton=0; 
                   EndPaint (hwnd, &ps) ;
     
          return 0 ;
     case WM_COMMAND:
           switch (LOWORD (wParam))
          {
         
          case IDM_FILE_EXIT:
              DeleteDC (hdc) ;
               SendMessage (hwnd, WM_CLOSE, 0, 0) ;
               return 0 ;
          }
          break ;
     case WM_DESTROY:
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}

 void finishchesser(HDC  hdc )
 {  
         static   HBITMAP  hBitmap[14];
            BITMAP bitmap;
            HDC  hdcMem;
            int  cx ,cy ;
            int k,i,j;
              for(i=1;i<11;i++)
              for(j=1;j<=10;j++)
     {
                       if(larry[i][j]!=0)
        {
                        k=larry[i][j];
         hBitmap[k] = LoadBitmap (hInst, MAKEINTRESOURCE(k) ) ;
                              GetObject (hBitmap[k], sizeof (BITMAP), &bitmap) ;
                              cx = bitmap.bmWidth ;
                              cy = bitmap.bmHeight ;
                              hdcMem = CreateCompatibleDC (hdc) ;
                              SelectObject (hdcMem, hBitmap[k]) ;
                              StretchBlt(hdc ,j*3*ll-ll, i*3*ll-ll,  2*ll,  2*ll,
                              hdcMem,0,0, cx, cy,  SRCCOPY) ;
                              DeleteDC (hdcMem) ;
                              DeleteObject (hBitmap[k]) ;
        }
     }
 }
void drawstar1(HDC  hdc ,int x,int y)

          MoveToEx (hdc, x-ll, y-ll/3 , NULL) ;
          LineTo   (hdc, x-ll/3, y-ll/3 ) ;
          LineTo   (hdc,  x-ll/3, y-ll) ;
          MoveToEx (hdc,   x-ll/3,   y+ll/3 , NULL) ;
          LineTo   (hdc,  x-ll/3,  y+ll/3) ;
          LineTo   (hdc,  x-ll/3,  y+ll) ; 
}
void drawstar2(HDC  hdc ,int x,int y)
{
          MoveToEx (hdc, x+ll/3,       y-ll/3 , NULL) ;
          LineTo   (hdc,    x+ll/3,   y-ll/3 ) ;
         
          LineTo   (hdc,    x+2*ll/3,    y-ll/3) ;

          MoveToEx (hdc,   x+ll/3,   y+2*ll/3 , NULL) ;
          LineTo   (hdc,  x+ll/3,  y+ll/3) ;
         
          LineTo   (hdc,  x+2*ll/3,  y+ll/3) ;
}
void drawstar(HDC  hdc ,int x,int y)
{
 
         drawstar1(hdc,x, y);
         drawstar2(hdc,x, y);
 
}
void drawboard(HDC  hdc )
{
    int x,y;
      for (y = 1 ; y < 11 ; y++)
   {
          MoveToEx (hdc, 3*ll,3*ll*y, NULL) ;
          LineTo   (hdc, 3*ll*9, 3*ll*y) ;
   }
      for (x = 1 ; x < 10 ; x++)
   {
          MoveToEx (hdc, 3*ll*x, 3*ll, NULL) ;
          LineTo   (hdc, 3*ll*x, 15*ll) ;
   }

 
      for (x = 1 ; x < 10 ; x=x++)
   {
          MoveToEx (hdc, 3*ll*x, 18*ll, NULL) ;
          LineTo   (hdc, 3*ll*x, 30*ll) ;
   }
}
void drawpallate(HDC  hdc )
{
          MoveToEx (hdc, 12*ll,        3*ll, NULL) ;
          LineTo   (hdc, 18*ll,9*ll ) ;

          MoveToEx (hdc, 12*ll,9*ll , NULL) ;
          LineTo   (hdc, 18*ll, 3*ll) ;


          MoveToEx (hdc, 12*ll,        24*ll, NULL) ;
          LineTo   (hdc, 18*ll,30*ll ) ;

          MoveToEx (hdc, 12*ll,30*ll , NULL) ;
          LineTo   (hdc, 18*ll, 24*ll) ;
 
}
BOOL isxy(  POINT  mouse)
{
    
     int xx,yy;
  xx= (int)mouse.x/(int)(3*ll);
  yy= (int)mouse.y/(int)(3*ll);
       if( mouse.x<=xx*(3*ll)+ll) 
                if( mouse.y<=yy*(3*ll)+ll ) 
    {
              lmouse.x=xx;
                 lmouse.y=yy;
    }
               else
      {
       if( mouse.y       {
       lmouse.x=xx;
       lmouse.y=yy+1;
       }
      }
            else
   {
    if(mouse.x    
                if(mouse.y<=yy*(3*ll)+ll )
    {
       lmouse.x=xx+1;
          lmouse.y=yy;
    }
    else
    { 
     if(mouse.y     {
     lmouse.x=xx+1;
     lmouse.y=yy+1;
     }
    }
   }
///// wsprintf (zBuffer,   "  %d  , %d " , (int)lmouse.x ,( int)lmouse.y ) ;
//////  MessageBox(NULL," lmouse.x-lmouse.y)",zBuffer ,MB_OK);
 return 1;
}
BOOL isCanGo(int  master, POINT from, POINT to) 
{
   int xto=(int)to.x;
    int yto=(int)to.y;
    int xfrom=(int)from.x;
   int yfrom=(int)from.y;
    int i,j;
 switch(master) 
  {
  case 14:
   //兵不回头:
   if(yto>yfrom)return FALSE;
   //兵只走一步直线:
 
   if(yfrom-yto+abs(xto-xfrom)>1)return FALSE; 
   break;

  case 7:
   //卒不回头:
   if(yto < yfrom)return FALSE;
   //卒只走一步直线:
   if(yto-yfrom+abs(xto-xfrom)>1)return FALSE; 
   break;

  case 11:
  case 4:
   //士走斜线一步:
   if(abs(yfrom-yto)>1||abs(xto-xfrom)>1)return FALSE; 
   break;

  case 10:
  case 3:
   //相走田:
   if(abs(xfrom-xto)!=2||abs(yfrom-yto)!=2)return FALSE;
   //相心:
   if(larry[(yfrom+yto)/2][(xfrom+xto)/2]!=0)return FALSE;
   break;

  case 12:
  case 5:
   //将帅只走一步直线:
   if(abs(yfrom-yto)+abs(xto-xfrom)>1)return FALSE;
   break;

  case 8:
  case 1:
   //车只能走直线:
   if(yfrom!=yto&&xfrom!=xto)return FALSE; 
   //车经过的路线中不能有棋子: -----------
   if(yfrom==yto)
   {
    if(xfrom    {
     for(i=xfrom+1;i      if(larry [yfrom][i]!=0)return FALSE;
    }
    else
    {
     for(i=xto+1;i      if(larry [yfrom][i]!=0)return FALSE;
    }
   }
   else
   {
    if(yfrom    {
     for(j=yfrom+1;j      if(larry[j][xfrom]!=0)return FALSE;
    }
    else
    {
     for(j=yto+1;j      if(larry[j][xfrom]!=0)return FALSE;
    }
   }
   //以上是车---------------------------------
   break;

  case 13:
  case 6:
    //炮 
    if((yfrom!=yto)&&(xfrom!=xto))return FALSE; 
   if(larry[yto][xto]==0)
   {
///// MessageBox(NULL,"aaa","aaaThe Path Of File Is",MB_OK);
    if(yfrom==yto)
    {
     if(xfrom     {
      for(i=xfrom+1;i       if(larry[yfrom][i]!=0) return FALSE;
       
     }
     else
     {
      for(i=xto+1;i       if(larry[yfrom][i]!=0)return FALSE;
      
     }
    }
    else
    {
     if(yfrom     {
      for(j=yfrom+1;j       if( larry[j][xfrom] !=0) return FALSE;
      
     }
     else
     {
      for(j=yto+1;j       if(larry[j][xfrom] !=0)return FALSE;
     
     }
    }
   }
   //以上是炮不吃子
   //吃子时
   else 
   {
     int count=0;
    if(yfrom==yto)
    {
     if(xfrom     {
      for(i=xfrom+1;i       if(larry [yfrom][i]!=0)count++;
      if(count!=1)return FALSE;
     }
     else
     {
      for(i=xto+1;i       if(larry [yfrom][i]!=0)count++;
      if(count!=1)return FALSE;
     }
    }
    else
    {
     if(yfrom     {
      for(j=yfrom+1;j       if( larry[j][xfrom] !=0)count++;
      if(count!=1)return FALSE;
     }
     else
     {
      for(j=yto+1;j       if(larry[j][xfrom] !=0)count++;
      if(count!=1)return FALSE;
     }
    }
   }
   break;
  case 9:
  case 2:
   //马走日:
   if(!(((abs(xto-xfrom)==1)&&(abs(yto-yfrom)==2))||((abs(xto-xfrom)==2)&&((abs(yto-yfrom))==1))))  
   {
////变量观察窗口   
////////MessageBox(NULL,"lllllllla","The Path Of File Is",MB_OK);
    return FALSE;}
//////// MessageBox(NULL,"lllllllllb","The Path Of File Is",MB_OK);
   //找马脚:
   if(xto-xfrom==2) {i=xfrom+1;j=yfrom; }
   else if (xfrom-xto==2) {i=xfrom-1;j=yfrom;}
   else if (yto-yfrom==2) {i=xfrom;j=yfrom+1;}
   else if (yfrom-yto==2) {i=xfrom;j=yfrom-1;}
   //else return FALSE;
   //绊马脚:
   if(larry[j][i]!=0)return FALSE;
   break;
  default: 
   break;
  }
 return TRUE; 

BOOL  IsNormal(  int  master, POINT to )
{
  int x,y;
  x=(int)to.x;
  y=(int)to.y;
 switch(master )
 {
 case 12: 
  //帅不能在红方宫外:
  if( x>6|| x<4|| y<8)return FALSE;
  break;

 case 11: 
  //仕只能在宫内特定点:
  if(!(
   ( x==4&& y==10)||
   ( x==4&& y==8)||
   ( x==5&& y==9)||
   ( x==6&& y==10)||
   ( x==6&& y==8)
   ))return FALSE;
  break;
 case 10:
 if(y<5)return FALSE;
  //七个相位:
  if(!(
   ( x==1&& y==8)||
   ( x==3&& y==10)||
   ( x==3&& y==6)||
   ( x==5&& y==8)||
   ( x==7&& y==10)||
   ( x==7&& y==6)||
   ( x==9&& y==8)
   ))return FALSE;
  break;

 case 14:
  //兵不能在兵位后:
  if( y>7)return FALSE;
  //兵过河前不能左右移动:
  if( y>5&& x%2==0)return FALSE;
  break;

 case 5:
  //帅不能在红方宫外:
  if( x>6|| x<4|| y>3)return FALSE;
  break;

 case 4:
  //仕只能在宫内特定点:
  if(!(
   ( x==4&& y==1)||
   ( x==4&& y==3)||
   ( x==5&& y==2)||
   ( x==6&& y==1)||
   ( x==6&& y==3)
   ))return FALSE;
  break;

 case 3:
  //七个相位:
  if(y>6)return FALSE;
  if(!(
   ( x==1&& y==3)||
   ( x==3&& y==1)||
   ( x==3&& y==5)||
   ( x==5&& y==3)||
   ( x==7&& y==1)||
   ( x==7&& y==5)||
   ( x==9&& y==3)
   ))return FALSE;
  break;

 case 7:
  //兵不能在兵位后:
  if( y<4)return FALSE;
  //兵过河前不能左右移动:
  if( y<6&& x%2==0)return FALSE;
  break;

 default:
  break;
 }
 return TRUE;
}

 
   


原文转自:http://www.ltesting.net