五子棋(API编写)

来源:互联网 发布:淘宝网买电脑可靠吗 编辑:程序博客网 时间:2024/05/17 17:56

#include<windows.h>
#include<iostream>
#include<TCHAR.h>
#include<stdio.h>
#include<afxres.h>
using namespace std;
TCHAR ch1[]=TEXT("我的五子棋程序");
static int shuzu[20][20];
bool tagx=false;//

static int ix,iy;//用来确定选择的是哪个格子;

bool is_win()//此函数用来判断是否赢了
{
    bool tag=false;
 static int a;
 a=shuzu[ix][iy];
    static int i,j;

 static int count;
 count=0;

   for(i=ix;i>=ix-4&&i>=0;--i)//检查左右
   {
      if(shuzu[i][iy]==a)
    ++count;
   else
    break;
   }

 if(count>=5)
  return true;

   for(i=ix;i<=ix+4&&i<20;++i)
   {
    if(shuzu[i][iy]==a)//注意这里ix,iy这个点算了2次
     ++count;
    else
     break;
   }

 if(count>5)
 return true;

 count=0;//计数复0;
   
  for(j=iy;j>=iy-4&&j>=0;--j)//检查上下
   {
      if(shuzu[ix][j]==a)
    ++count;
   else
    break;
   }

 if(count>=5)
 return true;

   for(j=iy;j<=iy+4&&j<20;++j)
   {
    if(shuzu[ix][j]==a)
     ++count;
    else
     break;
   }

 if(count>5)
 return true;
 
 count=0;//计数复0
  for(i=ix,j=iy;i>=ix-4&&j>=iy-4&&i>=0&&j>=0;--i,--j)//检查右斜线
  {
   if(shuzu[i][j]==a)
    ++count;
   else
    break;
  }
  if(count>=5)
   return true;

  for(i=ix,j=iy;i<=ix+4&&j<=iy+4&&i<20&&j<20;++i,++j)
  {

    if(shuzu[i][j]==a)
    ++count;
   else
    break;
  }
  if(count>5)
   return true;

  count=0;//计数复0
  for(i=ix,j=iy;i>=ix-4&&i>=0&&j<=iy+4&&j<20;--i,++j)//检查左斜线
  {
   if(shuzu[i][j]==a)
    ++count;
   else
    break;
  }
  if(count>=5)
   return true;

  for(i=ix,j=iy;i<=ix+4&&j>=iy-4&&i<20&&j>=0;++i,--j)
  {
   if(shuzu[i][j]==a)
    ++count;
   else
    break;
  }

  if(count>5)
   return true;

    return tag;

}

LRESULT CALLBACK myownproc(HWND,UINT,WPARAM,LPARAM);//声明窗口处理程序
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpComLine,int nShowCmd)
{

 MSG msg;//定义自己的消息体
 WNDCLASS myownclass;//定义自己的窗口类别
 HWND hwnd;//窗口句柄

 //下面先设计自己的窗口类别
 myownclass.style =CS_HREDRAW |CS_VREDRAW;
 myownclass.lpszMenuName =NULL;
 myownclass.lpszClassName=ch1;//窗口类名
 myownclass.lpfnWndProc=myownproc;//窗口类的处理函数
 myownclass.hInstance =hInstance;//使用WinMain函数传入的第一个参数
 myownclass.hIcon =LoadIcon(NULL,IDI_APPLICATION);
 myownclass.hCursor =LoadCursor(NULL,IDC_ARROW);
 myownclass.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);//背景颜色设计为白色
 myownclass.cbClsExtra =0;//额外空间设置为0
 myownclass.cbWndExtra =0;//额外空间设置为0
 //下面注册窗口类

  /*  for(int s=0;s<20;s++)//初始化数组
 {
 for(int k=0;k<20;k++)
 shuzu[s][k]=0;
 }
*/

 if(!RegisterClass(&myownclass))
 {
  MessageBox(NULL,TEXT("注册窗口类失败"),ch1,0);
  return 0;
 }

    //下面建立一个窗口
    hwnd=CreateWindow(ch1,ch1,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
 
   //下面显示窗口
 ShowWindow(hwnd,nShowCmd);
 UpdateWindow(hwnd);

 //下面进行消息循环

 while(GetMessage(&msg,NULL,0,0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }


 return msg.wParam ;
}//WinMain函数结束

 


//窗口处理函数的定义
LRESULT CALLBACK myownproc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{  
 HDC hdc;//设备内容句柄
 PAINTSTRUCT ps;//绘图结构体的定义
 RECT rect;//定义一个矩形区域
 static HBITMAP hBitmap;//位图句柄
 static int x,y;//用来画格子

 static int cxClient,cyClient;//用来取得客户区的大小
    static bool tag1=false;//用来标志蓝方是否单击了格子
 static bool tag2=false;//用来标志红方是否单击了格子
 static bool tag3=false ;//用来轮流下子,false表示轮到蓝方下,否则轮到红方下,开始是蓝方先下

 static HPEN hpen;//画笔句柄1
 static HPEN hpen2;//画笔句柄2
 static HBRUSH hbrush;//画刷句柄
 static HBRUSH hbrush2;//画刷句柄2
 //自己处理的消息
 switch(message)
 {
 case WM_SIZE:
  cxClient=LOWORD(lParam);
  cyClient=HIWORD(lParam);
  return 0;


 case WM_LBUTTONDOWN:
  x=LOWORD(lParam);//鼠标单击位置
  y=HIWORD(lParam);
  ix=x/60;
  if(x%60>=30)
   ++ix;
        iy=y/60;
  if(y%60>=30)
   ++iy;
  if(tag3==false)
  {
  tag1=true;
        InvalidateRect(hwnd,NULL,FALSE);//使客户区无效,最后一个参数为false,可以使的原先的都不变。 
  }
  return 0;

 case WM_RBUTTONDOWN:
     x=LOWORD(lParam);//鼠标单击位置
  y=HIWORD(lParam);
  ix=x/60;
  if(x%60>=30)
   ++ix;
        iy=y/60;
  if(y%60>=30)
   ++iy;
  if(tag3==true)
  {
   tag2=true;
        InvalidateRect(hwnd,NULL,FALSE);//使客户区无效,最后一个参数为false,可以使的原先的都不变。
  }
  return 0;
  

 case WM_PAINT:
  hdc=BeginPaint(hwnd,&ps);//获得设备内容句柄
  GetClientRect(hwnd,&rect);//得到客户区大小

  
  for( x=0;x<rect.right ;x+=60)//画出竖线
  {
   MoveToEx(hdc,x,0,NULL);
   LineTo(hdc,x,rect.bottom );
  }
  for( y=0;y<rect.bottom ;y+=60)//画出横线
  {
   MoveToEx(hdc,0,y,NULL);
   LineTo(hdc,rect.right ,y);
  }

  if(tag1==true&&shuzu[ix][iy]==0)
  {
        hpen=CreatePen(PS_SOLID,3,RGB(0,0,255));//制作画笔
  SelectObject(hdc,hpen);//将画笔选进设备内容
  hbrush=CreateSolidBrush(RGB(0,0,255));
  SelectObject(hdc,hbrush);
  Ellipse(hdc,ix*60-30,iy*60-30,ix*60+30,iy*60+30);
     tag1=false;
  tag3=true;
        shuzu[ix][iy]=1;
  if(is_win())
  {
   tagx=true;
   MessageBox(NULL,"蓝方获胜",ch1,0);
  }
  }

 if(tag2==true&&shuzu[ix][iy]==0)
  {
        hpen2=CreatePen(PS_SOLID,3,RGB(255,0,0));//制作画笔
  SelectObject(hdc,hpen2);//将画笔选进设备内容
  hbrush2=CreateSolidBrush(RGB(255,0,0));
  SelectObject(hdc,hbrush2);
  Ellipse(hdc,ix*60-30,iy*60-30,ix*60+30,iy*60+30);
     tag2=false;
  tag3=false;
        shuzu[ix][iy]=2;
     if(is_win())
  {
   tagx=true;
  MessageBox(NULL,"红方获胜",ch1,0);
  }

  }
   
  EndPaint(hwnd,&ps);
  DeleteObject(hpen);
  DeleteObject(hbrush);
  DeleteObject(hpen2);
  DeleteObject(hbrush2);


  return 0;
 case WM_DESTROY:
  PostQuitMessage(0);
  return 0;

 }

    //剩余的消息由系统默认处理
 return DefWindowProc(hwnd,message,wParam,lParam);
}

 

 

 

 


 

原创粉丝点击