// JingZiQiView.cpp : implementation of the CJingZiQiView class
//
#include "stdafx.h"
#include "JingZiQi.h"
#include "JingZiQiDoc.h"
#include "JingZiQiView.h"
#include "MainFrm.h"
//#define TCOLOR RGB(128,255,128)
//#define BCOLOR RGB(40,48,180)
#define TCOLOR RGB(0,248,0)
#define TCOLOR2 RGB(255,255,255)
#define BCOLOR RGB(80,64,79)
//#define BCOLOR RGB(63,32,31)
//#define BCOLOR RGB(78,49,49)
//#define BGROUND RGB(153,104,64)
//#define BGROUND RGB(47,32,63)
//#define BGROUND RGB(100,73,95)
//#define BGROUND RGB(63,32,31)
//#define BGROUND RGB(112,88,79)
//#define BGROUND RGB(112,72,64)
//#define BGROUND RGB(79,64,64)
//#define BGROUND RGB(89,56,55)
//#define BGROUND RGB(63,30,0)
//#define BGROUND RGB(207,144,32)
#define BGROUND RGB(209,146,34)
//RGB(189,91,1)
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CJingZiQiView
IMPLEMENT_DYNCREATE(CJingZiQiView, CView)
BEGIN_MESSAGE_MAP(CJingZiQiView, CView)
//{{AFX_MSG_MAP(CJingZiQiView)
ON_WM_SETCURSOR()
ON_WM_LBUTTONUP()
ON_COMMAND(ID_Start, OnStart)
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()
/////////////////////////////////////////////////////////////////////////////
// CJingZiQiView construction/destruction
CJingZiQiView::CJingZiQiView()
{
// TODO: add construction code here
//CurBlack=::LoadCursor(AfxGetInstanceHandle(),
//MAKEINTRESOURCE(IDC_CURSOR1));
CurBlack=AfxGetApp()->LoadCursor(IDC_CURSOR2);
m_bmRed.LoadBitmap(IDB_BITMAP15);//(IDB_BITMAP12);
m_bmBlack.LoadBitmap(IDB_BITMAP14);//(IDB_BITMAP9);
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
QP[i][j]=0;
pre_qp[i][j]=0;
}
man_turn=1;
cmptfirst=false;
Level=1;
mode=1;
IsServer=false;
over=0;
requ_reply=true;
}
CJingZiQiView::~CJingZiQiView()
{
}
BOOL CJingZiQiView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CJingZiQiView drawing
void CJingZiQiView::OnDraw(CDC* pDC)
{
CJingZiQiDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//画背景
CBrush mybrush;
mybrush.CreateSolidBrush(BGROUND);
CRect myrect(0,0,1200,800);
pDC->FillRect(myrect,&mybrush);
mybrush.DeleteObject();
//画棋盘
CPen *oldpen,mypen;
mypen.CreatePen(PS_SOLID,1,RGB(255,255,255));
oldpen=pDC->SelectObject(&mypen);
for(int i=0;i<4;i++)
{
pDC->MoveTo(60,60+i*60);
pDC->LineTo(240,60+i*60);
pDC->MoveTo(60+i*60,60);
pDC->LineTo(60+60*i,240);
}
pDC->SelectObject(oldpen);
//画棋子
CDC DC;
if(DC.CreateCompatibleDC(pDC)==FALSE)
AfxMessageBox("Fail to create DC!");
for(i=0;i<3;i++)
for(int j=0;j<3;j++)
if(QP[i][j]==1)
{
DC.SelectObject(m_bmBlack);
pDC->BitBlt(64+i*60,64+j*60,60,60,&DC,0,0,SRCCOPY);
}
else if(QP[i][j]==-1)
{
DC.SelectObject(m_bmRed);
pDC->BitBlt(64+i*60,64+j*60,60,60,&DC,0,0,SRCCOPY);
}
ShowState();
}
/////////////////////////////////////////////////////////////////////////////
// CJingZiQiView printing
BOOL CJingZiQiView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CJingZiQiView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CJingZiQiView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CJingZiQiView diagnostics
#ifdef _DEBUG
void CJingZiQiView::AssertValid() const
{
CView::AssertValid();
}
void CJingZiQiView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CJingZiQiDoc* CJingZiQiView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CJingZiQiDoc)));
return (CJingZiQiDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CJingZiQiView message handlers
BOOL CJingZiQiView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: Add your message handler code here and/or call default
if(nHitTest==HTCLIENT)
{
if(mode==1)
{
if(man_turn)
{
SetCursor(CurBlack);
// SetCursor(LoadCursor(NULL,IDC_CROSS));//显示十字形光标
return 1;
}
}
else if(mode==2)
{
if(man_turn&&(!IsServer))
{
SetCursor(CurBlack);
return 1;
}
else if(IsServer&&(!man_turn))
{
SetCursor(CurBlack);
return 1;
}
}
}
return CView::OnSetCursor(pWnd, nHitTest, message);
}
//DEL void CJingZiQiView::OnMouseMove(UINT nFlags, CPoint point)
//DEL {
//DEL // TODO: Add your message handler code here and/or call default
//DEL /* CRect rect(100,100,500,500);//定一一个改变光标的范围
//DEL
//DEL if(rect.PtInRect(point))//鼠标当前位置在矩形范围内
//DEL
//DEL {
//DEL
//DEL SetCursor(LoadCursor(NULL,IDC_CROSS));//显示十字形光标
//DEL
//DEL }*/
//DEL
//DEL
//DEL CView::OnMouseMove(nFlags, point);
//DEL }
void CJingZiQiView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
int x,y,px,py;
x=point.x;
y=point.y;
CDC *pDC=GetDC();
CDC DC;
if(DC.CreateCompatibleDC(pDC)==FALSE)
MessageBox("fail to create DC!");
//人机对弈
if(mode==1&&!over)
{
KillTimer(1);
if((x>60)&&(x<240)&&(y>60)&&(y<240))
{
px=(x-60)/60;
py=(y-60)/60;
if(man_turn&&(QP[px][py]==0))
{
for(int k=0;k<3;k++)
for(int l=0;l<3;l++)
pre_qp[k][l]=QP[k][l];
DC.SelectObject(m_bmBlack);
pDC->BitBlt(64+60*px,64+60*py,60,60,&DC,0,0,SRCCOPY);
QP[px][py]=1;
man_turn=0;
if(!result())
{
computerdown();
result();
}
}
}
}
//联机游戏
else if(mode==2)
{
CMainFrame *pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;
if((x>60)&&(x<240)&&(y>60)&&(y<240))
{
px=(x-60)/60;
py=(y-60)/60;
if(!over)
{
if((!IsServer)&&man_turn&&(QP[px][py]==0)) //客户端(man_turn=1)
{
// if(IsServer)
// return;
for(int k=0;k<3;k++)
for(int l=0;l<3;l++)
pre_qp[k][l]=QP[k][l];
DC.SelectObject(m_bmBlack);
pDC->BitBlt(64+60*px,64+60*py,60,60,&DC,0,0,SRCCOPY);
QP[px][py]=1;
man_turn=0;
SendPosInfo(px,py,pFrame->m_pClient);
result();
}
else if(IsServer&&(man_turn==0)&&(QP[px][py]==0)) //服务器端(man_turn=0)
{
//if(!IsServer)
// return;
DC.SelectObject(m_bmRed);
pDC->BitBlt(64+60*px,64+60*py,60,60,&DC,0,0,SRCCOPY);
QP[px][py]=-1;
man_turn=1;
SendPosInfo(px,py,pFrame->m_pConnect);
result();
}
}
}
}
CView::OnLButtonUp(nFlags, point);
}
void CJingZiQiView::computerdown()
{
CDC *pDC=GetDC();
CDC DC;
if(DC.CreateCompatibleDC(pDC)==FALSE)
MessageBox("fail to create DC!");
int i,j,k1,k2,temp1,temp2,flag;
struct datatype
{
int data;
int locatedata;
}datas[8];
flag=1;
for (i=0;i<3;i++)
for (j=0;j<3;j++)
{
if (QP[i][j]==0)
flag=0;
}
if(flag==1)
{
return;
}
for (i=0;i<8;i++)
{
datas[i].locatedata=i;
}
for(i=0;i<3;i++)
{
datas[i].data=0;
for (j=0;j<3;j++)
datas[i].data=datas[i].data+QP[i][j];
}
for(j=0;j<3;j++)
{
datas[j+3].data=0;
for (i=0;i<3;i++)
datas[j+3].data=datas[j+3].data+QP[i][j];