用mfc写一个五子棋游戏代码
时间: 2024-01-27 16:05:16 浏览: 80
以下是一个简单的 MFC 五子棋游戏代码示例:
```
// ChessBoard.h
class CChessBoard : public CWnd
{
public:
CChessBoard();
protected:
afx_msg void OnPaint();
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP()
private:
enum { BOARD_SIZE = 15, GRID_SIZE = 30 };
enum Piece { EMPTY_PIECE, BLACK_PIECE, WHITE_PIECE };
Piece m_board[BOARD_SIZE][BOARD_SIZE];
bool m_isBlackTurn;
bool m_isGameOver;
void DrawGrid(CDC& dc, int row, int col);
void DrawPiece(CDC& dc, int row, int col);
bool IsWin(int row, int col);
};
// ChessBoard.cpp
#include "stdafx.h"
#include "ChessBoard.h"
CChessBoard::CChessBoard()
{
for (int i = 0; i < BOARD_SIZE; ++i)
{
for (int j = 0; j < BOARD_SIZE; ++j)
{
m_board[i][j] = EMPTY_PIECE;
}
}
m_isBlackTurn = true;
m_isGameOver = false;
Create(NULL, _T("Five Chess"), WS_OVERLAPPEDWINDOW, CRect(0, 0, GRID_SIZE * BOARD_SIZE, GRID_SIZE * BOARD_SIZE));
}
void CChessBoard::OnPaint()
{
CPaintDC dc(this);
// draw board
dc.FillSolidRect(0, 0, GRID_SIZE * BOARD_SIZE, GRID_SIZE * BOARD_SIZE, RGB(238, 221, 187));
for (int i = 0; i < BOARD_SIZE; ++i)
{
for (int j = 0; j < BOARD_SIZE; ++j)
{
DrawGrid(dc, i, j);
DrawPiece(dc, i, j);
}
}
}
void CChessBoard::OnLButtonDown(UINT nFlags, CPoint point)
{
if (m_isGameOver)
{
return;
}
int row = point.y / GRID_SIZE;
int col = point.x / GRID_SIZE;
if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE)
{
return;
}
if (m_board[row][col] != EMPTY_PIECE)
{
return;
}
m_board[row][col] = m_isBlackTurn ? BLACK_PIECE : WHITE_PIECE;
Invalidate();
if (IsWin(row, col))
{
CString message;
message.Format(_T("%s wins!"), m_isBlackTurn ? _T("Black") : _T("White"));
MessageBox(message);
m_isGameOver = true;
return;
}
m_isBlackTurn = !m_isBlackTurn;
}
void CChessBoard::DrawGrid(CDC& dc, int row, int col)
{
dc.Rectangle(col * GRID_SIZE, row * GRID_SIZE, (col + 1) * GRID_SIZE, (row + 1) * GRID_SIZE);
}
void CChessBoard::DrawPiece(CDC& dc, int row, int col)
{
if (m_board[row][col] == BLACK_PIECE)
{
dc.FillEllipse(col * GRID_SIZE, row * GRID_SIZE, GRID_SIZE, GRID_SIZE, &dc.GetHalftoneBrush());
}
else if (m_board[row][col] == WHITE_PIECE)
{
dc.Ellipse(col * GRID_SIZE, row * GRID_SIZE, (col + 1) * GRID_SIZE, (row + 1) * GRID_SIZE);
}
}
bool CChessBoard::IsWin(int row, int col)
{
Piece piece = m_board[row][col];
// check horizontal
int count = 0;
for (int i = col - 4; i <= col + 4; ++i)
{
if (i >= 0 && i < BOARD_SIZE && m_board[row][i] == piece)
{
++count;
if (count >= 5)
{
return true;
}
}
else
{
count = 0;
}
}
// check vertical
count = 0;
for (int i = row - 4; i <= row + 4; ++i)
{
if (i >= 0 && i < BOARD_SIZE && m_board[i][col] == piece)
{
++count;
if (count >= 5)
{
return true;
}
}
else
{
count = 0;
}
}
// check diagonal 1
count = 0;
for (int i = -4; i <= 4; ++i)
{
if (row + i >= 0 && row + i < BOARD_SIZE && col + i >= 0 && col + i < BOARD_SIZE && m_board[row + i][col + i] == piece)
{
++count;
if (count >= 5)
{
return true;
}
}
else
{
count = 0;
}
}
// check diagonal 2
count = 0;
for (int i = -4; i <= 4; ++i)
{
if (row - i >= 0 && row - i < BOARD_SIZE && col + i >= 0 && col + i < BOARD_SIZE && m_board[row - i][col + i] == piece)
{
++count;
if (count >= 5)
{
return true;
}
}
else
{
count = 0;
}
}
return false;
}
BEGIN_MESSAGE_MAP(CChessBoard, CWnd)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
// MainFrm.cpp
#include "stdafx.h"
#include "MainFrm.h"
#include "ChessBoard.h"
CMainFrame::CMainFrame()
{
m_chessBoard.Create(NULL, NULL, WS_CHILD | WS_VISIBLE, CRect(0, 0, 450, 450), this, 123);
}
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
END_MESSAGE_MAP()
// MainFrm.h
class CMainFrame : public CFrameWnd
{
public:
CMainFrame();
protected:
CChessBoard m_chessBoard;
DECLARE_MESSAGE_MAP()
};
```
这个示例使用 MFC 创建了一个简单的五子棋游戏界面,并实现了落子、判断胜负等基本功能。你可以根据需要进行修改和扩展。
阅读全文