五字棋步骤
五子棋截频:
void CMyView::OnDraw(CDC* pDC) {
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDC->TextOut(50,50,"棋盘:");
pDC->MoveTo(100,100);
pDC->LineTo(100,250);
pDC->LineTo(250,250);
pDC->LineTo(250,100);
pDC->LineTo(100,100);
pDC->MoveTo(150,100);
pDC->LineTo(150,250);
pDC->MoveTo(200,100);
pDC->LineTo(200,250);
pDC->MoveTo(100,150);
pDC->LineTo(250,150);
pDC->MoveTo(100,200);
pDC->LineTo(250,200);
// TODO: add draw code for native data here
}
// Attributes
public:
CMyDoc* GetDocument();
//两个鼠标
HCURSOR hcursorwhite;
HCURSOR hcursorblack;
//棋盘数组
int x;//下的棋子数
int a[3][3];
int sign;
// colorwhite TRUE时白棋下,否则黑棋下
bool colorwhite;
//棋子位图
CBitmap m_bmblack;
CBitmap m_bmwhite;
afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT
message);
CMyView::CMyView()
{
// TODO: add construction code here
x=0;
sign=0;
hcursorblack=AfxGetApp()->LoadCursor(IDC_CURSOR1);
hcursorwhite=AfxGetApp()->LoadCursor(IDC_CURSOR2);
m_bmwhite.LoadBitmap(IDB_WHITE);
m_bmblack.LoadBitmap(IDB_BLACK);
//清理棋盘
//数组值为0
示没有棋子
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
a[i][j]=0;
//白棋先下
colorwhite=true;
}
代码:
void CMyView::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(point.x>100&&point.x<251&&point.y>100&&point.y<251)
{
int px=(point.x-100)/50;
int py=(point.y-100)/50;
//是否已经有棋子
if(colorwhite&&a[px][py]==0&&sign=0)sign=0保证游戏有人赢了就不能再下了
{
Dc.SelectObject(m_bmwhite);
pDC->BitBlt(px*50+106,py*50+103,260,260,&Dc,0,0,SRCCOPY);
//表示存在白棋
a[px][py]=1;
//检查是否结束
//over(point);
//换黑棋下
colorwhite=false;
}
else if(a[px][py]==0&&sign==0)
{
Dc.SelectObject(m_bmblack);
pDC->BitBlt(px*50+106,py*50+103,260,260,&Dc,0,0,SRCCOPY);
a[px][py]=-1;
//over(point);
colorwhite=true;
}
}
x++;
if(x>=5)
{
for(int b=0;b<=3;b++)
{
if((a[b][0]==a[b][1]&&a[b][1]==a[b][2]&&a[b][2]==1)||(a[0][b]==a[1][b]&&a[1][b]==a[2][
b]&&a[2][b]==1))
{pDC->TextOut(10,300,"第一人获胜停止游戏"); sign=1; break;}
if((a[b][0]==a[b][1]&&a[b][1]==a[b][2]&&a[b][2]==2)||(a[0][b]==a[1][b]&&a[1][b]==a[2][
b]&&a[2][b]==2))
{ pDC->TextOut(10,300,"第二人获胜停止游戏");sign=1;break;}
if((a[0][0]==a[1][1]&&a[1][1]==a[2][2]&&a[1][1]==1)||(a[2][0]==a[1][1]&&a[1][1]==a[0][
2]&&a[1][1]==1))
{ pDC->TextOut(10,300,"第一人获胜停止游戏");sign=1;break;}
if((a[0][0]==a[1][1]&&a[1][1]==a[2][2]&&a[1][1]==2)||(a[2][0]==a[1][1]&&a[1][1]==a[0][
2]&&a[1][1]==2))
{ pDC->TextOut(10,300,"第二人获胜停止游戏");sign=1;break;
}
}
}
if(x==9)
{
pDC->TextOut(10,300,"平局");
}
CView::OnLButtonUp(nFlags, point);
}
HCURSOR hcursorwhite; hcursorwhite=AfxGetApp()->LoadCursor(IDC_CURSOR2);
添加重玩功能:在资源视图中的菜单中添加重玩项,再右击选择添加建立类向导,选择视图类中添加函数ONREPLAY void CMyView::OnReplay()
{
// TODO: Add your command handler code here
CClientDC dc(this);
dc.TextOut(300,300,"成功~");
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
a[i][j]=0;
x=0;
sign=0;
colorwhite=true;
Invalidate(true);
}