📄 chessdlg.cpp
字号:
// ChessDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Chess.h"
#include "ChessDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CChessDlg dialog
CChessDlg::CChessDlg(CWnd* pParent /*=NULL*/)
: CDialog(CChessDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CChessDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CChessDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChessDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CChessDlg, CDialog)
//{{AFX_MSG_MAP(CChessDlg)
ON_WM_PAINT()
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChessDlg message handlers
BOOL CChessDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// CenterWindow(GetDesktopWindow()); // center to the hpc screen
nWidth = int(GetSystemMetrics(SM_CXSCREEN));
nHeight = int(GetSystemMetrics(SM_CYSCREEN));
this->MoveWindow(0,0,nWidth,nHeight);
if(nHeight<nWidth) {nWidth+=nHeight;nHeight=nWidth-nHeight;nWidth-=nHeight;}
cx = nWidth/(ROW+2);
cy = nWidth/(COL+2);
startx = nWidth/(ROW+2);
starty = nWidth/(COL+2);
StartGame();
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CChessDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
DrawBackground();
DrawNewButton();
DrawExitButton();
DrawQiban();
DrawGrid();
DrawQizi();
// Do not call CDialog::OnPaint() for painting messages
}
void CChessDlg::DrawQiban()
{
CDC *pDC=GetDC();
CBitmap bm_qipan;
bm_qipan.LoadBitmap(IDB_BITMAP1);
CDC mDC;
mDC.CreateCompatibleDC(pDC);
BITMAP bmInfo;
bm_qipan.GetObject(sizeof(bmInfo),&bmInfo);
proportion = double(bmInfo.bmWidth)/nWidth;
Offset = nHeight-int(bmInfo.bmWidth/proportion);
mDC.SelectObject(&bm_qipan);
pDC->StretchBlt(0+Offset,0,int(bmInfo.bmWidth/proportion),int(bmInfo.bmHeight/proportion),&mDC,0,0,bmInfo.bmWidth,bmInfo.bmHeight,SRCCOPY);
// CRect rect;
// GetWindowRect(rect);
// SetWindowPos(this,rect.left,rect.top,nWidth,nWidth+GetSystemMetrics(SM_CYCAPTION),SWP_SHOWWINDOW|SWP_NOZORDER);
}
void CChessDlg::DrawGrid()
{
CDC* pDC = GetDC();
CPen pen(PS_SOLID,1,RGB(0,0,0));
pDC->SelectObject(&pen);
for (int i = 0; i<=ROW; i++)
{
pDC->MoveTo(startx+cx*(i)+Offset,starty);
pDC->LineTo(startx+cx*(i)+Offset,starty+COL*cy);
}
for (int j = 0; j<=COL; j++)
{
pDC->MoveTo(startx+Offset,starty+(j)*cy);
pDC->LineTo(startx+ROW*cx+Offset,starty+(j)*cy);
}
}
void CChessDlg::DrawQizi()
{
CBitmap bmpBlack,bmpWhite;
bmpBlack.LoadBitmap(IDB_BITMAP2);
bmpWhite.LoadBitmap(IDB_BITMAP3);
CDC mDC;
BITMAP bmInfo;
CDC* pDC = GetDC();
mDC.CreateCompatibleDC(pDC);
for (int m=0;m<=ROW;m++)
for (int n=0;n<=COL;n++)
{
if (nodes[m][n] == 1)//Black User
{
bmpBlack.GetObject(sizeof(bmInfo),&bmInfo);
mDC.SelectObject(&bmpBlack);
pDC->StretchBlt(startx+cx*m-int(bmInfo.bmWidth/proportion/2)+Offset,starty+cy*n-int(bmInfo.bmHeight/proportion/2),int(bmInfo.bmWidth/proportion),int(bmInfo.bmHeight/proportion),&mDC,0,0,bmInfo.bmWidth,bmInfo.bmHeight,SRCCOPY);
}
else if (nodes[m][n] == -1)//White Computer
{
bmpWhite.GetObject(sizeof(bmInfo),&bmInfo);
mDC.SelectObject(&bmpWhite);
pDC->StretchBlt(startx+cx*m-int(bmInfo.bmWidth/proportion/2)+Offset,starty+cy*n-int(bmInfo.bmHeight/proportion/2),int(bmInfo.bmWidth/proportion),int(bmInfo.bmHeight/proportion),&mDC,0,0,bmInfo.bmWidth,bmInfo.bmHeight,SRCCOPY);
}
}
}
void CChessDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
//Black User
int maxlen=0,maxnum=0,winer=0;
if(ClickNewButton(point)) return;
if(ClickExitButton(point)) return;
int i=(point.x-Offset)/cx+(point.x-Offset)%cx/(cx/2)-1;
int j=point.y/cy+point.y%cy/(cy/2)-1;
if(gameover==false&&i>=0&&i<=ROW&&j>=0&&j<=COL&&nodes[i][j]==0&&player==1)
{
nodes[i][j]=1;
Count++;
DrawQizi();
maxlen=bd(i,j,&maxnum);
if(maxlen>=4)
{
gameover=true;
DrawBlackWin();
}
else
if(Count==(ROW+1)*(COL+1))
{
gameover=true;
DrawnChess();
}
player=-1;//White Computer
winer=ComputerAI();
Count++;
DrawQizi();
if(winer==1)
{
gameover=true;
DrawWhiteWin();
}
else
if(Count==(ROW+1)*(COL+1))
{
gameover=true;
DrawnChess();
}
player=1;//Black User
}
CDialog::OnLButtonUp(nFlags, point);
}
void CChessDlg::StartGame()
{
Count=0;
gameover=false;
for(int i=0;i<=ROW;i++)
for(int j=0;j<=COL;j++)
nodes[i][j]=0;
player=1;//Black user
DrawBackground();
DrawNewButton();
DrawExitButton();
DrawQiban();
DrawGrid();
DrawQizi();
}
int CChessDlg::ComputerAI()
{
int i,j,maxlen,maxnum,randnum;
if(gameover) return 0;
//Computer -1
for(i=0;i<=ROW;i++)
for(j=0;j<=COL;j++)
if(nodes[i][j]==0)
{
maxnum=bd(i,j,&maxnum);
if(maxnum>=4) goto loop2;
}
//User 1
player=1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -