📄 7_1view.cpp
字号:
// 7_1View.cpp : implementation of the CMy7_1View class
//
#include "stdafx.h"
#include "7_1.h"
#include "7_1Doc.h"
#include "7_1View.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMy7_1View
IMPLEMENT_DYNCREATE(CMy7_1View, CView)
BEGIN_MESSAGE_MAP(CMy7_1View, CView)
//{{AFX_MSG_MAP(CMy7_1View)
ON_COMMAND(ID_START, OnStart)
ON_COMMAND(ID_CPMPUTER, OnCpmputer)
ON_COMMAND(ID_PLAYER, OnPlayer)
ON_WM_SETCURSOR()
ON_WM_LBUTTONUP()
ON_COMMAND(ID_NET, OnNet)
ON_WM_TIMER()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CMy7_1View construction/destruction
CMy7_1View::CMy7_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;
//表示不是网络游戏
play=1;
}
CMy7_1View::~CMy7_1View()
{
}
BOOL CMy7_1View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMy7_1View drawing
void CMy7_1View::OnDraw(CDC* pDC)
{
CMy7_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);
}
}
/////////////////////////////////////////////////////////////////////////////
// CMy7_1View printing
BOOL CMy7_1View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMy7_1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMy7_1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMy7_1View diagnostics
#ifdef _DEBUG
void CMy7_1View::AssertValid() const
{
CView::AssertValid();
}
void CMy7_1View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMy7_1Doc* CMy7_1View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy7_1Doc)));
return (CMy7_1Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMy7_1View message handlers
void CMy7_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;
play=1;
Invalidate();
}
void CMy7_1View::OnCpmputer()
{
// TODO: Add your command handler code here
vscomputer=1;
OnStart();
}
void CMy7_1View::OnPlayer()
{
// TODO: Add your command handler code here
vscomputer=2;
OnStart();
}
BOOL CMy7_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 CMy7_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;
}
}
}
//
if(vscomputer==3)
{
//是否在棋盘内
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)
{
//如果是服务器
//因为服务器是黑棋
if(play==2)
//返回
return;
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;
//如果是客户端
if(play==3)
{
//字符串数组
char x[5];
char y[5];
//分别把刚下棋的位置的数组参数改为字符类型
itoa(px,x,10);
itoa(py,y,10);
//字符串数组
char AnMsg[100];
//清空
strcpy(AnMsg,"");
//接上x字符串
strcat(AnMsg,x);
//接上一个空格
//这是为了分开两个字符串
strcat(AnMsg," ");
//接上y字符串
strcat(AnMsg,y);
//接上空格
//这是为了接收时容易判断下棋位置
strcat(AnMsg," ");
UpdateData();
CMainFrame *pFrame=(CMainFrame *)AfxGetApp()->m_pMainWnd;
UpdateData(FALSE);
if(pFrame->m_pSocket)
{
char pMsg[100];
sprintf(pMsg,"%s",AnMsg);
pFrame->SendMsg(pFrame->m_pSocket,pMsg);
}
return;
}
}
else if(wzq[px][py]==0)
{
if(play==3)
return;
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;
if(play==2)
{
char x[5];
char y[5];
itoa(px,x,10);
itoa(py,y,10);
char AnMsg[100];
strcpy(AnMsg,"");
strcat(AnMsg,x);
strcat(AnMsg," ");
strcat(AnMsg,y);
strcat(AnMsg," ");
CMainFrame *pFrame=(CMainFrame *)AfxGetApp()->m_pMainWnd;
char pMsg[100];
sprintf(pMsg,"%s",AnMsg);
CMy7_1App *App=( CMy7_1App * )AfxGetApp();
pFrame->SendMsg(App->pRequestSave,pMsg);
return;
}
}
}
}
CView::OnLButtonUp(nFlags, point);
}
void CMy7_1View::over(CPoint point)
{
//获取鼠标指向数组位置
int x=(point.x-30)/20;
int y=(point.y-30)/20;
//计算开始判断的坐标 xx,yy
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("白棋胜!");
//重新开始
//不是联机游戏
if(play==1)
{
OnStart();
return;
}
//联机游戏
else
{
//停止计时器
KillTimer(1);
//重新开始
OnNet2();
//返回
return;
}
}
//五个都是黑棋
if(a==-5)
{
AfxMessageBox("黑棋胜!");
if(play==1)
{
OnStart();
return;
}
else
{
KillTimer(1);
OnNet2();
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("白棋胜!");
if(play==1)
{
OnStart();
return;
}
else
{
KillTimer(1);
OnNet2();
return;
}
}
if(a==-5)
{
AfxMessageBox("黑棋胜!");
if(play==1)
{
OnStart();
return;
}
else
{
KillTimer(1);
OnNet2();
return;
}
}
}
}
//向右下角
//判断起点位置
if(x<y)
{
if(xx==0)
yy=y-x;
}
else
{
if(yy==0)
xx=x-y;
}
//参数over=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("白棋胜!");
if(play==1)
{
OnStart();
return;
}
else
{
KillTimer(1);
OnNet2();
return;
}
}
if(a==-5)
{
AfxMessageBox("黑棋胜!");
if(play==1)
{
OnStart();
return;
}
else
{
KillTimer(1);
OnNet2();
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("白棋胜!");
if(play==1)
{
KillTimer(1);
OnStart();
return;
}
else
{
OnNet2();
return;
}
}
if(a==-5)
{
AfxMessageBox("黑棋胜!");
if(play==1)
{
OnStart();
return;
}
else
{
KillTimer(1);
OnNet2();
return;
}
}
}
//到了边界
else
over=1;
}
xx-=1;
yy+=1;
}while(over==0);
}
//黑棋下
void CMy7_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 CMy7_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++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -