基于Dialog,左键开始下“O”,右键下“X”
void CQuanChaDlg::OnRButtonDown(UINT nFlags, CPoint point)
{
if(RightOrLeft == 1 && !oneWin)
{
CBrush* brush = new CBrush(RGB(255,255,255));
CDC* pDC = this->GetDC();
pDC->SelectObject(brush);
for(int i=0;i<180;i+=60)
{
for(int j=0;j<180;j+=60)
{
CRect rect(i+M,j+M,i+60+M,j+60+M);
CPoint tempPoint;
tempPoint = rect.CenterPoint(); //获得中心
//当前点在矩形中 && 在点容器中不存在
if(rect.PtInRect(point) && this->isLocation(tempPoint)) //这个位置不存在,则在if内改变
{
pDC->Ellipse(tempPoint.x-28,tempPoint.y-28,tempPoint.x+28,tempPoint.y+28);
pDC->DrawText(_T("X"),&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE );
this->Location(tempPoint); //画了该点后,将该点插入到向量中去
chessState[(tempPoint.x-M)/60][(tempPoint.y-M)/60] = 2; //该位置下入黑棋,将相应数组变量置为1
if(isWin(point))
{
MessageBox(_T("这局'X'棋获胜了!"));
oneWin = true;
}
this->RightOrLeft--;
}
}
}
this->ReleaseDC(pDC);
}
else if(oneWin || chessPoint.size() == 9)
{
MessageBox(_T("想再来一局吗?"),_T("提示"),MB_OK);
InitialState();
RightOrLeft = 0;
chessPoint.clear();
oneWin = false;
RedrawWindow();
}
CDialog::OnRButtonDown(nFlags, point);
}
//右键
void CQuanChaDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
if(RightOrLeft == 0 && !oneWin)
{
CBrush* brush = new CBrush(RGB(0,0,0));
CDC* pDC = this->GetDC();
pDC->SelectObject(brush);
for(int i=0;i<180;i+=60)
{
for(int j=0;j<180;j+=60)
{
CRect rect(i+M,j+M,i+60+M,j+60+M);
CPoint tempPoint;
tempPoint = rect.CenterPoint(); //获得中心
//当前点在矩形中 && 在点容器中不存在
if(rect.PtInRect(point) && this->isLocation(tempPoint)) //这个位置不存在,则在if内改变
{
pDC->Ellipse(tempPoint.x-28,tempPoint.y-28,tempPoint.x+28,tempPoint.y+28);
pDC->DrawText(_T("O"),&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
this->Location(tempPoint); //画了该点后,将该点插入到向量中去
chessState[(tempPoint.x-M)/60][(tempPoint.y-M)/60] = 1; //该位置下入黑棋,将相应数组变量置为1
if(isWin(point))
{
MessageBox(_T("这局'O'棋获胜了!"));
oneWin = true;
}
this->RightOrLeft++;
}
}
}
this->ReleaseDC(pDC);
}
else if(oneWin || chessPoint.size() == 9)
{
MessageBox(_T("想再来一局吗?"),_T("提示"),MB_OK);
InitialState();
RightOrLeft = 0;
chessPoint.clear();
oneWin = false;
RedrawWindow();
}
CDialog::OnLButtonDown(nFlags, point);
}
//画棋盘
void CQuanChaDlg::DrawChessBorad( CDC *pDC )
{
for(int i=0;i<=3;i++)
{
pDC->MoveTo(0+M,i*60+M);
pDC->LineTo(3*60+M,i*60+M);
}
for(int i=0;i<=3;i++)
{
pDC->MoveTo(i*60+M,0+M);
pDC->LineTo(i*60+M,3*60+M);
}
}
void CQuanChaDlg::InitialState()
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
chessState[i][j] = 0;
}
void CQuanChaDlg::Location( CPoint point )
{
chessPoint.push_back(point);
}
bool CQuanChaDlg::isLocation( CPoint point )
{
bool flag = true;
std::vector<CPoint>::iterator iter;
for(iter=chessPoint.begin();iter!=chessPoint.end();++iter)
if(point.x == iter->x && point.y == iter->y)
flag = false;
return flag;
}
//判胜
bool CQuanChaDlg::isWin( CPoint cp )
{
int cp_x = (cp.x-M)/60; //将该点的中心坐标转化为数组分量
int cp_y = (cp.y-M)/60;
int i = cp_x; //循环变量初始化数组分量值
int j = cp_y;
int count = 1; //同颜色棋子的计数
int nowChessColor = this->chessState[cp_x][cp_y]; //获取当前颜色代码(1或2)
///////////////////// 1.横向搜索 //////////
// 1.1 横向向前搜索
while(1)
{
if(i>0 && i<3) //数组不越界的情况
{
i--; //i向前搜索
if(nowChessColor == chessState[i][j])
{
count++;
if(count == 3) //计数器累加到3 返回true
return true;
}
else
break; //不同色,退出while
}
else
break; //数组越界,直接退出while
}
i = cp_x;
//1.2 向后搜索
while(1)
{
if(i>=0 && i<3)
{
i++;
if(nowChessColor == chessState[i][j])
{
count++;
if(count==3)
return true;
}
else
break;
}
else
break;
}
i = cp_x;
count = 1; //计数器清理为1
////////////////////2. 纵向搜索 /////////////////////
//2.1 纵向向上搜索
while(1)
{
if(j>0 && j<3)
{
j--;
if(nowChessColor == chessState[i][j])
{
count++;
if(count==3)
return true;
}
else
break;
}
else
break;
}
j = cp_y;
//2.2 纵向向下
while(1)
{
if(j>=0 && j<3)
{
j++;
if(nowChessColor == chessState[i][j])
{
count++;
if(count==3)
return true;
}
else
break;
}
else
break;
}
j = cp_y;
count = 1;
/////////////////////3. 左下——右上方向 //////////////
//3.1 左下
while(1)
{
if(i>0 && i<3 && j>=0 && j<3)
{
i--;
j++;
if(nowChessColor == chessState[i][j])
{
count++;
if(count==3)
return true;
}
else
break;
}
else
break;
}
i = cp_x;
j = cp_y;
//3.2 右上
while(1)
{
if(i>=0 && i<3 && j>0 && j<3)
{
i++;
j--;
if(nowChessColor == chessState[i][j])
{
count++;
if(count==3)
return true;
}
else
break;
}
else
break;
}
i = cp_x;
j = cp_y;
count = 1;
//////////////////////////4. 左上——右下/////////////////
//4.1 左上
while(1)
{
if(i>0 && i<3 && j>0 && j<3)
{
i--;
j--;
if(nowChessColor == chessState[i][j])
{
count++;
if(count==3)
return true;
}
else
break;
}
else
break;
}
i = cp_x;
j = cp_y;
//4.2 右下
while(1)
{
if(i>=0 && i<3 && j>=0 && j<3)
{
i++;
j++;
if(nowChessColor == chessState[i][j])
{
count++;
if(count==3)
return true;
}
else
break;
}
else
break;
}
return false;
}