📄 ticktacktoeview.cpp
字号:
// ticktacktoeView.cpp : implementation of the CTicktacktoeView class
//
#include "stdafx.h"
#include "ticktacktoe.h"
#include "ticktacktoeDoc.h"
#include "ticktacktoeView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTicktacktoeView
IMPLEMENT_DYNCREATE(CTicktacktoeView, CView)
BEGIN_MESSAGE_MAP(CTicktacktoeView, CView)
//{{AFX_MSG_MAP(CTicktacktoeView)
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTicktacktoeView construction/destruction
CTicktacktoeView::CTicktacktoeView()
{
// TODO: add construction code here
bmpMax.LoadBitmap(IDB_MAX);
bmpMin.LoadBitmap(IDB_MIN);
htoc=true;
OnStart();
myDepth=3;
computerFirst=false;
}
CTicktacktoeView::~CTicktacktoeView()
{
}
BOOL CTicktacktoeView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CTicktacktoeView drawing
void CTicktacktoeView::OnDraw(CDC* pDC)
{
CTicktacktoeDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
//Draw background:
CBrush tmpBrush;
tmpBrush.CreateSolidBrush(RGB(255,255,255));
CRect tmpRect(0,0,400,400);
pDC->FillRect(tmpRect,&tmpBrush);
tmpBrush.DeleteObject();
//Draw chessboard:
CPen tmpPen;
tmpPen.CreatePen(PS_SOLID,2,RGB(0,0,0));
CPen* pOldPen=pDC->SelectObject(&tmpPen);
int xStartPoint,yStartPoint,space;
xStartPoint=45;
yStartPoint=20;
space=100;
for(int i=0;i<4;i++)
{
pDC->MoveTo(xStartPoint,yStartPoint+i*space);
pDC->LineTo(xStartPoint+3*space,yStartPoint+i*space);
pDC->MoveTo(xStartPoint+i*space,yStartPoint);
pDC->LineTo(xStartPoint+i*space,yStartPoint+3*space);
}
pDC->SelectObject(pOldPen);
//Draw chessman:
CDC tmpDC;
xStartPoint+=10;
yStartPoint+=10;
if(tmpDC.CreateCompatibleDC(pDC)==FALSE) MessageBox("Fail to create DC!");
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(chessman[i][j]==1) //computer
{
tmpDC.SelectObject(bmpMax);
pDC->BitBlt(xStartPoint+i*space,yStartPoint+j*space,
space,space,&tmpDC,0,0,SRCCOPY);
}
else if(chessman[i][j]==-1) //human
{
tmpDC.SelectObject(bmpMin);
pDC->BitBlt(xStartPoint+i*space,yStartPoint+j*space,
space,space,&tmpDC,0,0,SRCCOPY);
}
//TODO: Update the status bar.
}
/////////////////////////////////////////////////////////////////////////////
// CTicktacktoeView diagnostics
#ifdef _DEBUG
void CTicktacktoeView::AssertValid() const
{
CView::AssertValid();
}
void CTicktacktoeView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTicktacktoeDoc* CTicktacktoeView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTicktacktoeDoc)));
return (CTicktacktoeDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTicktacktoeView message handlers
void CTicktacktoeView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CView::OnMouseMove(nFlags, point);
}
void CTicktacktoeView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
int x,y,px,py;
x=point.x;//鼠标位置的x坐标
y=point.y;//鼠标位置的x坐标
int xStartPoint,yStartPoint,space;
xStartPoint=45;//棋盘的左边界
yStartPoint=20;//棋盘的右边界
space=100;//棋盘每格的跨度
px=(x-xStartPoint)/space;//鼠标所在棋盘的列
py=(y-yStartPoint)/space;//鼠标所在棋盘的行
if(px<0||px>2||py<0||py>2||chessman[px][py]!=0||finished)//鼠标点击不在棋盘上
{
CView::OnLButtonUp(nFlags, point);
return;
}
CDC *pDC=GetDC();
CDC tmpDC;
if(tmpDC.CreateCompatibleDC(pDC)==FALSE) MessageBox("fail to create DC!");
xStartPoint+=10;//所画棋子的x坐标
yStartPoint+=10;//所画棋子的y坐标
if(htoc)//人机对战
{
if(manTurn&&(chessman[px][py]==0))//轮到人走棋,并且鼠标点击位置为空
{
while(manual.size()>step) manual.pop_back();//表明有悔棋,棋谱也需要后退
vector<int> vec_manual;//临时存储人走棋的信息
vec_manual.push_back(-1);vec_manual.push_back(px),vec_manual.push_back(py);
manual.push_back(vec_manual);//将走棋的步法存入棋谱manual中
vec_manual.clear();
step++;
tmpDC.SelectObject(bmpMin);
pDC->BitBlt(xStartPoint+space*px,yStartPoint+space*py,space,
space,&tmpDC,0,0,SRCCOPY);//画棋子
chessman[px][py]=-1;//改变棋盘状态chessman
manTurn=0;//该电脑下了
switch(Result())//Result() 用于判断是否应该结束
{
case -1: AfxMessageBox("You Win!");//如果此时棋盘状态为人赢,显示消息框
break;
case 2: AfxMessageBox("平局!");//如果此时棋盘状态为平局,显示消息框
break;
case 0: ComputerDown();//电脑下棋
}
}
}
else
{
//将manual[][3]按照step指示的位置重置大小;
manual.resize(step);
//side指示下棋方
if(side==-1)
{
//如果下棋方是"MIN"方:将对应位置的chessman[3][3]置为"-1";
//在manual[][3]中写入相应数据;在棋盘上添加"MIN棋子"图片;
chessman[px][py]=-1;
manual.resize(manual.size()+1);
step=manual.size();
manual[manual.size()-1].resize(3);
manual[manual.size()-1][0]=-1;
manual[manual.size()-1][1]=px;
manual[manual.size()-1][2]=py;
tmpDC.SelectObject(bmpMin);
pDC->BitBlt(xStartPoint+px*space,yStartPoint+py*space,
space,space,&tmpDC,0,0,SRCCOPY);
//判断结果,显示胜负信息
switch(Result())
{
case -1: MessageBox("Min方胜利!","胜负已定",MB_OK);
break;
case 2: MessageBox("平局!","胜负已定",MB_OK);
break;
}
side=1;
}
else
{
//如果下棋方是"MAX"方:将对应位置的chessman[3][3]置为"1";
//在manual[][3]中写入相应数据;在棋盘上添加"MAX棋子"图片;
chessman[px][py]=1;
manual.resize(manual.size()+1);
step=manual.size();
manual[manual.size()-1].resize(3);
manual[manual.size()-1][0]=1;
manual[manual.size()-1][1]=px;
manual[manual.size()-1][2]=py;
tmpDC.SelectObject(bmpMax);
pDC->BitBlt(xStartPoint+px*space,yStartPoint+py*space,
space,space,&tmpDC,0,0,SRCCOPY);
//判断结果,显示胜负信息
switch(Result())
{
case 1: MessageBox("Max方胜利!","胜负已定",MB_OK);
break;
case 2: MessageBox("平局!","胜负已定",MB_OK);
break;
}
side=-1;
}
}
CView::OnLButtonDown(nFlags, point);
}
/////////////////////////////////////////////////////////////////////////////
// CTicktacktoeView Custom Functions.
void CTicktacktoeView::OnStart()
{
// TODO: Add your message handler code here and/or call default
int i;
if(!chessman.empty())
{
for(i=0;i<3;i++)
chessman[i].clear();
chessman.clear();
}
chessman.resize(3);
for(i=0;i<chessman.size();i++)
chessman[i].resize(3);
manual.resize(0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -