?? 3_1view.cpp
字號:
// 3_1View.cpp : implementation of the CMy3_1View class
//
#include "stdafx.h"
#include "3_1.h"
#include "3_1Doc.h"
#include "3_1View.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMy3_1View
IMPLEMENT_DYNCREATE(CMy3_1View, CView)
BEGIN_MESSAGE_MAP(CMy3_1View, CView)
//{{AFX_MSG_MAP(CMy3_1View)
ON_WM_LBUTTONUP()
ON_WM_SETCURSOR()
ON_COMMAND(ID_START, OnStart)
ON_COMMAND(ID_PLAYER, OnPlayer)
ON_COMMAND(ID_CPMPUTER, OnCpmputer)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMy3_1View construction/destruction
CMy3_1View::CMy3_1View()
{
// TODO: add construction code here
hcursorblack=AfxGetApp()->LoadCursor(IDC_CURSOR1);
hcursorwhite=AfxGetApp()->LoadCursor(IDC_CURSOR2);
m_bmwhite.LoadBitmap(IDB_WHITE);
m_bmblack.LoadBitmap(IDB_BLACK);
for(int i=0;i<19;i++)
for(int j=0;j<19;j++)
wzq[i][j]=0;
colorwhite=true;
//默認為人對機
vscomputer=1;
}
CMy3_1View::~CMy3_1View()
{
}
BOOL CMy3_1View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMy3_1View drawing
void CMy3_1View::OnDraw(CDC* pDC)
{
CMy3_1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//畫背景
CBrush mybrush1;
mybrush1.CreateSolidBrush(RGB(192,192,192));
CRect myrect1(0,0,1200,800);
pDC->FillRect(myrect1,&mybrush1);
CBrush mybrush;
mybrush.CreateSolidBrush(RGB(0,0,0));
for(int a=0;a<3;a++)
for(int b=0;b<3;b++)
{
CRect myrect(97+a*120,97+b*120,103+a*120,103+b*120);
pDC->FillRect(myrect,&mybrush);
}
//畫黑框線
CPen mypen;
CPen*myoldPen;
mypen.CreatePen(PS_SOLID,1,RGB(0,0,0));
myoldPen=pDC->SelectObject(&mypen);
for(int i=0;i<19;i++)
{
pDC->MoveTo(40,40+i*20);
pDC->LineTo(400,40+i*20);
pDC->MoveTo(40+i*20,40);
pDC->LineTo(40+i*20,400);
}
CDC Dc;
if(Dc.CreateCompatibleDC(pDC)==FALSE)
AfxMessageBox("Can't create DC");
for(int n=0;n<19;n++)
for(int m=0;m<19;m++)
if(wzq[n][m]==1)
{
Dc.SelectObject(m_bmwhite);
pDC->BitBlt(n*20+32,m*20+32,160,160,&Dc,0,0,SRCCOPY);
}
else if(wzq[n][m]==-1)
{
Dc.SelectObject(m_bmblack);
pDC->BitBlt(n*20+32,m*20+32,160,160,&Dc,0,0,SRCCOPY);
}
}
/////////////////////////////////////////////////////////////////////////////
// CMy3_1View printing
BOOL CMy3_1View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMy3_1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMy3_1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMy3_1View diagnostics
#ifdef _DEBUG
void CMy3_1View::AssertValid() const
{
CView::AssertValid();
}
void CMy3_1View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMy3_1Doc* CMy3_1View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy3_1Doc)));
return (CMy3_1Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMy3_1View message handlers
void CMy3_1View::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC *pDC=GetDC();
CDC Dc;
if(Dc.CreateCompatibleDC(pDC)==FALSE)
AfxMessageBox("Can't create DC");
//顯示棋子
/////////////////////人對機
if(vscomputer==1)
{
if(point.x>30&&point.x<410&&point.y>30&&point.y<410)
{
int px=(point.x-30)/20;
int py=(point.y-30)/20;
if(colorwhite&&wzq[px][py]==0)
{
Dc.SelectObject(m_bmwhite);
pDC->BitBlt(px*20+32,py*20+32,160,160,&Dc,0,0,SRCCOPY);
wzq[px][py]=1;
over(point);
colorwhite=false;
//保存白棋位置
vspoint=point;
//計算機下棋
computerdown();
}
}
}
//人對人
if(vscomputer==2)
{
if(point.x>30&&point.x<410&&point.y>30&&point.y<410)
{
int px=(point.x-30)/20;
int py=(point.y-30)/20;
if(colorwhite&&wzq[px][py]==0)
{
Dc.SelectObject(m_bmwhite);
pDC->BitBlt(px*20+32,py*20+32,160,160,&Dc,0,0,SRCCOPY);
wzq[px][py]=1;
over(point);
colorwhite=false;
}
else if(wzq[px][py]==0)
{
Dc.SelectObject(m_bmblack);
pDC->BitBlt(px*20+32,py*20+32,160,160,&Dc,0,0,SRCCOPY);
wzq[px][py]=-1;
over(point);
colorwhite=true;
}
}
}
CView::OnLButtonUp(nFlags, point);
}
BOOL CMy3_1View::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: Add your message handler code here and/or call default
if(nHitTest==HTCLIENT)
{
if(colorwhite)
{
CMainFrame*pFrm=(CMainFrame*)AfxGetApp()->m_pMainWnd;
CStatusBar*pStatus=&pFrm->m_wndStatusBar;
if(pStatus)
{
pStatus->GetStatusBarCtrl().SetIcon(0,AfxGetApp()->LoadIcon(IDI_WHITE));
pStatus->SetPaneText(0,"白棋下");
}
SetCursor(hcursorwhite);
}
else
{
CMainFrame*pFrm=(CMainFrame*)AfxGetApp()->m_pMainWnd;
CStatusBar*pStatus=&pFrm->m_wndStatusBar;
if(pStatus)
{
//顯示圖像
pStatus->GetStatusBarCtrl().SetIcon(0,AfxGetApp()->LoadIcon(IDI_BLACK));
//顯示文字
pStatus->SetPaneText(0,"黑棋下");
}
SetCursor(hcursorblack);
}
return 1;
}
return CView::OnSetCursor(pWnd, nHitTest, message);
}
void CMy3_1View::over(CPoint point)
{
//獲取鼠標指向數組位置
int x=(point.x-30)/20;
int y=(point.y-30)/20;
//開始判斷的坐標
int xx,yy;
if(x<4)
xx=0;
else
xx=x-4;
if(y<4)
yy=0;
else
yy=y-4;
int i,j,a;
//橫向判斷
for(i=xx;i<15;i++)
{
a=0;
for(j=i;j<i+5;j++)
{
a=a+wzq[j][y];
if(a==5)
{
AfxMessageBox("白棋勝!");OnStart();return;
}
if(a==-5)
{
AfxMessageBox("黑棋勝!");OnStart();return;
}
}
}
//豎向判斷
for(i=yy;i<15;i++)
{
a=0;
for(j=i;j<i+5;j++)
{
a=a+wzq[x][j];
if(a==5)
{
AfxMessageBox("白棋勝!");OnStart();return;
}
if(a==-5)
{
AfxMessageBox("黑棋勝!");OnStart();return;
}
}
}
//向右下角
//判斷起點位置
if(x<y)
{
if(xx==0)
yy=y-x;
}
else
{
if(yy==0)
xx=x-y;
}
//參數1時退出循環
int over=0;
do
{
a=0;
for(i=0;i<5;i++)
{
if((xx+i)<19||(yy+i)<19)
{
a=a+wzq[xx+i][yy+i];
if(a==5)
{
AfxMessageBox("白棋勝!");OnStart();return;
}
if(a==-5)
{
AfxMessageBox("黑棋勝!");OnStart();return;
}
}
//到了邊界
else
over=1;
}
xx+=1;
yy+=1;
}while(over==0);
//向左下角
if(y>(18-x))
{
if(x>13)
{
yy=y-(18-x);
xx=18;
}
else
{
yy=y-4;
xx=x+4;
}
}
else
{
if(y<5)
{
xx=x+y;
yy=0;
}
else
{
yy=y-4;
xx=x+4;
}
}
over=0;
do
{
a=0;
for(i=0;i<5;i++)
{
if((xx-i)>=0||(yy+i)<19)
{
a=a+wzq[xx-i][yy+i];
if(a==5)
{
AfxMessageBox("白棋勝!");OnStart();return;
}
if(a==-5)
{
AfxMessageBox("黑棋勝!");OnStart();return;
}
}
//到了邊界
else
over=1;
}
xx-=1;
yy+=1;
}while(over==0);
}
void CMy3_1View::OnStart()
{
// TODO: Add your command handler code here
for(int i=0;i<19;i++)
for(int j=0;j<19;j++)
wzq[i][j]=0;
colorwhite=true;
Invalidate();
}
//黑棋下
void CMy3_1View::putdown(CPoint point)
{
CDC *pDC=GetDC();
CDC Dc;
if(Dc.CreateCompatibleDC(pDC)==FALSE)
AfxMessageBox("Can't create DC");
Dc.SelectObject(m_bmblack);
pDC->BitBlt(point.x*20+32,point.y*20+32,160,160,&Dc,0,0,SRCCOPY);
wzq[point.x][point.y]=-1;
//變數組坐標為棋盤坐標
CPoint overpoint;
overpoint.x=point.x*20+30;
overpoint.y=point.y*20+30;
over(overpoint);
colorwhite=true;
}
//輪到計算機下棋
void CMy3_1View::computerdown()
{
//把各種情形賦值為如下
bpointcan4=(-1,-1);
wpointcan4=(-1,-1);
bpointcan3=(-1,-1);
wpointcan3=(-1,-1);
bpointcan2=(-1,-1);
wpointcan2=(-1,-1);
bpointcan1=(-1,-1);
//搜索最好的落棋點
for(int i=0;i<19;i++)
for(int j=0;j<19;j++)
bestputdown(i,j);
//判斷放在哪里
//棋多的位置優先
//黑白一樣多時黑先
if(bpointcan4.x!=-1)
{
putdown(bpointcan4);
return;
}
else if(wpointcan4.x!=-1)
{
putdown(wpointcan4);
return;
}
else if(bpointcan3.x!=-1)
{
putdown(bpointcan3);
return;
}
else if(wpointcan3.x!=-1)
{
putdown(wpointcan3);
return;
}
else if(bpointcan2.x!=-1)
{
putdown(bpointcan2);
return;
}
else if(wpointcan2.x!=-1)
{
putdown(wpointcan2);
return;
}
else
{
putdown(bpointcan1);
return;
}
}
//檢查四個方向,各算出五個棋子的和并賦值
void CMy3_1View::bestputdown(int i,int j)
{
int num[4];
CPoint numbig;
int a,k;
/////////////////////////////// num[0] -->
a=0;
if(i<15)
for(k=0;k<5;k++)
a=a+wzq[i+k][j];
num[0]=abs(a);
////////////////////////////// num[1] "|"
a=0;
if(j<15)
for(k=0;k<5;k++)
a=a+wzq[i][j+k];
num[1]=abs(a);
/////////////////////////////// num[2] "\"
a=0;
if(i<15&&j<15)
for(k=0;k<5;k++)
a=a+wzq[i+k][j+k];
num[2]=abs(a);
////////////////////////////// num[3] "/"
a=0;
if((i>4)&&(j<15))
for(k=0;k<5;k++)
a=a+wzq[i-k][j+k];
num[3]=abs(a);
//比較哪個方向同色棋最多
numbig=maxnum(num[0],num[1],num[2],num[3]);
//在得到最大值和方向上尋找落棋點
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -