五子棋(API編寫)

來源:互聯網
上載者:User

#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);
}

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.