如何在gomoku中存储作品

当前正在学习MFC并决定制作游戏Gomoku。这是迄今为止的代码。如何在gomoku中存储作品

**mainframe.h** 

class CMainFrame : public CFrameWnd

{

public:

CMainFrame();

protected:

DECLARE_DYNAMIC(CMainFrame)

public:

public:

public:

public:

virtual ~CMainFrame();

#ifdef _DEBUG

virtual void AssertValid() const;

virtual void Dump(CDumpContext& dc) const;

#endif

protected:

DECLARE_MESSAGE_MAP()

void DrawBoard(CDC* pDC);

int m_nNextChar;

int board[15][15];

static const int EMPTY = 0, WHITE = 1, BLACK = 2;

public:

afx_msg void OnLButtonDown(UINT nFlags, CPoint point);

afx_msg void OnRButtonDown(UINT nFlags, CPoint point);

afx_msg void OnPaint();

};

**mainframe.cpp**

#include "stdafx.h"

#include "01.win32tomfc.h"

#include "MainFrm.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#endif

// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)

ON_WM_LBUTTONDOWN()

ON_WM_RBUTTONDOWN()

ON_WM_PAINT()

END_MESSAGE_MAP()

int diameter = 23;

int size = 40;

int xCod;

int yCod;

int xCodx;

int yCody;

// CMainFrame ¹¹Ôì/Îö¹¹

CMainFrame::CMainFrame()

{

m_nNextChar = BLACK;

Create(NULL, _T("Generic Sample Application"));

CRect rect(0, 0, 700, 700);

CalcWindowRect(&rect);

SetWindowPos(NULL, 0, 0, rect.Width(), rect.Height(),

SWP_NOZORDER | SWP_NOMOVE | SWP_NOREDRAW);

}

CMainFrame::~CMainFrame()

{

}

// CMainFrame Õï¶Ï

#ifdef _DEBUG

void CMainFrame::AssertValid() const

{

CFrameWnd::AssertValid();

}

void CMainFrame::Dump(CDumpContext& dc) const

{

CFrameWnd::Dump(dc);

}

#endif //_DEBUG

// CMainFrame ÏûÏ¢´¦Àí³ÌÐò

void CMainFrame::DrawBoard(CDC * pDC)

{

CPen pen(PS_SOLID, 1, RGB(0, 0, 0));

CPen* pOldPen = pDC->SelectObject(&pen);

for (int i = 1; i <= 16; i++) {

pDC->MoveTo(40 * i, 40);

pDC->LineTo(40 * i, 640);

pDC->MoveTo(40, 40 * i);

pDC->LineTo(640, 40 * i);

}

pDC->SelectObject(pOldPen);

}

void CMainFrame::OnLButtonDown(UINT nFlags, CPoint point)

{

CClientDC dc(this);

CPen pen(PS_SOLID, 1, RGB(0, 0, 0));

CPen* pOldPen = dc.SelectObject(&pen);

dc.SelectStockObject(BLACK_BRUSH);

xCod = (point.x + (size/2))/size;

xCod = (xCod * size) - diameter/2;

yCod = (point.y + (size/2))/size;

yCod = (yCod * size) - diameter/2;

xCodx = xCod + diameter;

yCody = yCod + diameter;

if (m_nNextChar != BLACK)

return;

else {

if (xCod > 20 && yCod <= 640 && xCodx < 655 && yCody > 40) {

dc.Ellipse(xCod, yCod, xCodx, yCody);

}

}

m_nNextChar = WHITE;

CFrameWnd::OnLButtonDown(nFlags, point);

}

void CMainFrame::OnRButtonDown(UINT nFlags, CPoint point)

{

CClientDC dc(this);

CPen pen(PS_SOLID, 1, RGB(0, 0, 0));

CPen* pOldPen = dc.SelectObject(&pen);

dc.SelectStockObject(WHITE_BRUSH);

xCod = (point.x + (size/2))/size;

xCod = (xCod * size) - diameter/2;

yCod = (point.y + (size/2))/size;

yCod = (yCod * size) - diameter/2;

xCodx = xCod + diameter;

yCody = yCod + diameter;

if (m_nNextChar != WHITE)

return;

else {

if (xCod > 20 && yCod <= 640 && xCodx < 655 && yCody > 40) {

dc.Ellipse(xCod, yCod, xCodx, yCody);

}

}

m_nNextChar = BLACK;

CFrameWnd::OnRButtonDown(nFlags, point);

}

void CMainFrame::OnPaint()

{

CPaintDC dc(this);

DrawBoard(&dc);

}

我的代码绘制一个15×15网格的函数DrawBoard()和分别在OnLButtonDown中和OnRButtonDown绘制黑色和白色片。事情是当我运行程序并点击绘制黑色部分然后是白色部分时,白色部分可以绘制在黑色部分上,反之亦然。所以我想创建一个二维阵列板[15] [15]来存放一个作品时,它的绘制,以便一个不同的作品不能被绘制在当前的作品将是最好的(我在这里正确的轨道上)。我试过了,但我似乎无法弄清楚如何去做。我不擅长编程,并意识到这可能很容易,但一些帮助真的会被赞赏。请解释我将如何以正确的方式去做。

这就是我试过的。

void CMainFrame::OnLButtonDown(UINT nFlags, CPoint point) 

{

CClientDC dc(this);

CPen pen(PS_SOLID, 1, RGB(0, 0, 0));

CPen* pOldPen = dc.SelectObject(&pen);

dc.SelectStockObject(BLACK_BRUSH);

xCod = (point.x + (size/2))/size;

xCod = (xCod * size) - diameter/2;

yCod = (point.y + (size/2))/size;

yCod = (yCod * size) - diameter/2;

xCodx = xCod + diameter;

yCody = yCod + diameter;

if ((m_nNextChar != BLACK) && (board[xCod][yCod] = WHITE))

return;

else {

if (xCod > 20 && yCod <= 640 && xCodx < 655 && yCody > 40) {

dc.Ellipse(xCod, yCod, xCodx, yCody);

board[xCod][yCod] = BLACK;

}

}

void CMainFrame::OnRButtonDown(UINT nFlags, CPoint point)

{

CClientDC dc(this);

CPen pen(PS_SOLID, 1, RGB(0, 0, 0));

CPen* pOldPen = dc.SelectObject(&pen);

dc.SelectStockObject(WHITE_BRUSH);

xCod = (point.x + (size/2))/size;

xCod = (xCod * size) - diameter/2;

yCod = (point.y + (size/2))/size;

yCod = (yCod * size) - diameter/2;

xCodx = xCod + diameter;

yCody = yCod + diameter;

if (m_nNextChar != WHITE && (board[xCod][yCod] = BLACK))

return;

else {

if (xCod > 20 && yCod <= 640 && xCodx < 655 && yCody > 40) {

dc.Ellipse(xCod, yCod, xCodx, yCody);

board[xCod][yCod] = WHITE;

}

}

m_nNextChar = BLACK;

CFrameWnd::OnRButtonDown(nFlags, point);

}

回答:

您应该完成OnPaint中的所有图纸。不要画其他功能,例如OnLButtonDown。相反,从OnLButtonDown获取必要的信息并致电Invalidate,这将重新绘制窗口。

这里是一个例子。为了简单起见,我创建了一个结构info和一个二维数组datadata存储每个单元格的所有信息,即矩形和颜色。你必须初始化data一次,并根据信息data

#include <vector> 

class CMainFrame : public CFrameWnd

{

...

struct info

{

CRect rect;

int color;

};

std::vector<std::vector<info>> data;

};

CMainFrame::CMainFrame()

{

...

data.resize(15);

for(int i = 0; i < data.size(); i++)

data[i].resize(15);

int xoffset = 20;

int yoffset = 20;

for(int row = 0; row < 15; row++)

{

for(int col = 0; col < 15; col++)

{

data[row][col].rect.SetRect(0, 0, size + 1, size + 1);

data[row][col].rect.MoveToXY(xoffset + row * size, yoffset + col * size);

}

}

}

void CMainFrame::OnLButtonDown(UINT nFlags, CPoint point)

{

CFrameWnd::OnLButtonDown(nFlags, point);

for(int row = 0; row < 15; row++)

{

for(int col = 0; col < 15; col++)

{

if(data[row][col].color)

break;

if(data[row][col].rect.PtInRect(point))

{

data[row][col].color = WHITE;

break;

}

}

}

Invalidate(FALSE);

}

void CMainFrame::OnRButtonDown(UINT nFlags, CPoint point)

{

CFrameWnd::OnRButtonDown(nFlags, point);

for(int row = 0; row < 15; row++)

{

for(int col = 0; col < 15; col++)

{

if(data[row][col].color)

break;

if(data[row][col].rect.PtInRect(point))

{

data[row][col].color = BLACK;

break;

}

}

}

Invalidate(FALSE);

}

void CMainFrame::OnPaint()

{

CPaintDC dc(this);

CPen pen(PS_SOLID, 1, RGB(0, 0, 0));

dc.SelectObject(&pen);

CBrush white, black;

white.CreateSolidBrush(RGB(255, 255, 255));

black.CreateSolidBrush(RGB(0, 0, 0));

for(int row = 0; row < 15; row++)

{

for(int col = 0; col < 15; col++)

{

dc.Rectangle(data[row][col].rect);

if(data[row][col].color)

{

CBrush *oldbrush;

if(data[row][col].color == WHITE)

oldbrush = dc.SelectObject(&white);

else

oldbrush = dc.SelectObject(&black);

dc.Ellipse(data[row][col].rect);

dc.SelectObject(oldbrush);

}

}

}

}

以上是 如何在gomoku中存储作品 的全部内容, 来源链接: utcz.com/qa/257392.html

回到顶部