📄 fivechesssdlg.cpp
字号:
// FiveChesssDlg.cpp : implementation file
//
#include "stdafx.h"
#include "FiveChesss.h"
#include "FiveChesssDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFiveChesssDlg dialog
CFiveChesssDlg::CFiveChesssDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFiveChesssDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFiveChesssDlg)
// 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);
doubledo = FALSE;
model = 1;
lx = -1;
ly = -1;
}
void CFiveChesssDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFiveChesssDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFiveChesssDlg, CDialog)
//{{AFX_MSG_MAP(CFiveChesssDlg)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFiveChesssDlg message handlers
BOOL CFiveChesssDlg::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
// TODO: Add extra initialization here
//OnRestrat() ;
bmp_black.LoadBitmap(IDB_H);
bmp_white.LoadBitmap(IDB_B);
bmp_Background.LoadBitmap(IDB_QP);
return TRUE; // return TRUE unless you set the focus to a control
}
void CFiveChesssDlg::Show(CPaintDC * pDC){
CDC dcMemory1,dcMemory2,dcMemoryQB;
dcMemory1.CreateCompatibleDC(pDC);
dcMemory1.SelectObject(&bmp_black);
dcMemory2.CreateCompatibleDC(pDC);
dcMemory2.SelectObject(&bmp_white);
dcMemoryQB.CreateCompatibleDC(pDC);
dcMemoryQB.SelectObject(&bmp_Background);
pDC->BitBlt(5,5,500,500,&dcMemoryQB,0,0 ,SRCCOPY);
//绘制棋子
for(int i = 0;i < 15;i++){
for(int j = 0;j < 15;j++){
switch(fivegame.GetMap(i,j)){
case 'H':
pDC->BitBlt(i*26+19,j*26+19,24,24,&dcMemory1,0,0 ,SRCCOPY);
break;
case 'B':
pDC->BitBlt(i*26+19,j*26+19,24,24,&dcMemory2,0,0 ,SRCCOPY);
break;
}
}
}
//绘制落子标记
if(lx > -1 && ly > -1){
for(i = 0;i < 6;i++){
pDC->SetPixel(lx*26+23+i+5,ly*26+25+5,RGB(255,0,0));
pDC->SetPixel(lx*26+23+i+5,ly*26+26+5,RGB(255,0,0));
}
for(i = 0;i < 6;i++){
pDC->SetPixel(lx*26+25+5,ly*26+23+i+5,RGB(255,0,0));
pDC->SetPixel(lx*26+26+5,ly*26+23+i+5,RGB(255,0,0));
}
}
/*
CDC mdc,mdcb,mdch;
CBitmap bmp,bmpb,bmph;
CBitmap * ob,*obb,*obh;
mdc.CreateCompatibleDC(dc);
mdcb.CreateCompatibleDC(dc);
mdch.CreateCompatibleDC(dc);
bmp.LoadBitmap(IDB_QP);
bmph.LoadBitmap(IDB_H);
bmpb.LoadBitmap(IDB_B);
ob = mdc.SelectObject(&bmp);
obb = mdcb.SelectObject(&bmpb);
obh = mdch.SelectObject(&bmph);
//显示棋盘
//dc->BitBlt(5,5,500,500,&mdc,0,0,SRCCOPY);
///////////////////////////////////
//绘制棋子
for(int i = 0;i < 15;i++){
for(int j = 0;j < 15;j++){
switch(fivegame.GetMap(i,j)){
case 'H':
mdc.BitBlt(i*26+14,j*26+14,24,24,&mdch,0,0,SRCCOPY);
break;
case 'B':
mdc.BitBlt(i*26+14,j*26+14,24,24,&mdcb,0,0,SRCCOPY);
break;
}
}
}
////////////////////////////////////
//绘制落子标记
if(lx > -1 && ly > -1){
for(i = 0;i < 6;i++){
mdc.SetPixel(lx*26+23+i,ly*26+25,RGB(255,0,0));
mdc.SetPixel(lx*26+23+i,ly*26+26,RGB(255,0,0));
}
for(i = 0;i < 6;i++){
mdc.SetPixel(lx*26+25,ly*26+23+i,RGB(255,0,0));
mdc.SetPixel(lx*26+26,ly*26+23+i,RGB(255,0,0));
}
}
///////////////////////////////////
//显示棋盘
// dc->BitBlt(5,5,500,500,&mdc,0,0,SRCCOPY);
mdc.SelectObject(ob);
mdcb.SelectObject(obb);
mdch.SelectObject(obh);
*/
}
void CFiveChesssDlg::Play(CPoint p){
int x,y;
rect.left = p.x-19;
rect.top = p.y-19;
rect.right = p.x+26;
rect.bottom = p.y+26;
x = (p.x - 19)/26;
y = (p.y - 19)/26;
int tx=lx, ty=ly;
tx = x * 26 + 19;
ty = y * 26 + 19;
rect.left = tx-6;
rect.top = ty-6;
rect.right = tx+26;
rect.bottom = ty+26;
Play(x,y);
}
void CFiveChesssDlg::Play(int x,int y){
int sx=x;
CString str;
// str.Format(_T("x: %d, y: %d"), x, y);
// AfxMessageBox(str);
if(x>=0 && x<15 && y>=0 && y<15){
lx = x;
x = fivegame.Play(x,y);
// str.Format(_T("x: %d"), x);
// AfxMessageBox(str);
if(x >= 5){
//行棋失败
//AfxMessageBox(_T("行棋失败"));
x = -1;
}
else {
//行棋成功
ly = y;
InvalidateRect(&rect,TRUE);
switch(x){
case -1:
AfxMessageBox(_T("很遗憾,你输了!"));
break;
case 1:
AfxMessageBox(_T("恭喜,你赢了!"));
break;
case 4:
AfxMessageBox(_T("悔棋,此功能暂未完善!"));
break;
}
if(model){
AutoPlay();
}
}
}
}
void CFiveChesssDlg::AutoPlay(){
int x,y;
fivegame.AutoPlay(x,y);
lx = x;
x = fivegame.Play(x,y);
ly = y;
int tx=lx, ty=ly;
tx = lx * 26 + 19;
ty = ly * 26 + 19;
rect.left = tx-6;
rect.top = ty-6;
rect.right = tx+26;
rect.bottom = ty+26;
InvalidateRect(&rect,TRUE);
switch(x){
case -1:
AfxMessageBox(_T("恭喜,你赢了!"));
break;
case 1:
AfxMessageBox(_T("很遗憾,你输了!"));
break;
case 4:
AfxMessageBox(_T("悔棋,此功能暂未完善!"));
break;
}
}
void CFiveChesssDlg::OnRestrat()
{
fivegame.Clear();
lx = -1;
ly = -1;
Invalidate(FALSE);
// CRect rect(x, y, 20, 20);
// InvalidateRect(&rect,TRUE);
if(1 == model){
AutoPlay();
}
}
void CFiveChesssDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
Show(&dc);
// Do not call CDialog::OnPaint() for painting messages
}
void CFiveChesssDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(!doubledo){
Play(point);
}
CDialog::OnLButtonDown(nFlags, point);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -