VC2022五子棋小游戏代码
时间: 2023-10-09 19:06:46 浏览: 94
以下是一个基于VC++ MFC框架的五子棋小游戏代码示例:
```
// CMy5ChessDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "My5Chess.h"
#include "My5ChessDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedOk();
};
CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
ON_BN_CLICKED(IDOK, &CAboutDlg::OnBnClickedOk)
END_MESSAGE_MAP()
// CMy5ChessDlg 对话框
CMy5ChessDlg::CMy5ChessDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_MY5CHESS_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMy5ChessDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CMy5ChessDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
// CMy5ChessDlg 消息处理程序
BOOL CMy5ChessDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,
// 系统会自动调用此函数来设置主窗口的图标
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
// 初始化棋盘
for (int i = 0; i < BOARD_WIDTH; ++i)
{
for (int j = 0; j < BOARD_WIDTH; ++j)
{
m_board[i][j] = NONE;
}
}
// 初始化当前玩家为黑方
m_currentPlayer = BLACK;
// 初始化游戏状态为未开始
m_gameState = GAME_NOT_STARTED;
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CMy5ChessDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 绘制棋盘
void CMy5ChessDlg::DrawBoard(CDC* pDC)
{
const int BOARD_LEFT = 50; // 棋盘左边界
const int BOARD_TOP = 50; // 棋盘上边界
const int GRID_SIZE = 40; // 棋盘格子大小
for (int i = 0; i < BOARD_WIDTH; ++i)
{
for (int j = 0; j < BOARD_WIDTH; ++j)
{
// 绘制棋格
pDC->Rectangle(BOARD_LEFT + i * GRID_SIZE, BOARD_TOP + j * GRID_SIZE,
BOARD_LEFT + (i + 1) * GRID_SIZE, BOARD_TOP + (j + 1) * GRID_SIZE);
// 绘制棋子
if (m_board[i][j] == BLACK)
{
pDC->Ellipse(BOARD_LEFT + i * GRID_SIZE + 2, BOARD_TOP + j * GRID_SIZE + 2,
BOARD_LEFT + (i + 1) * GRID_SIZE - 2, BOARD_TOP + (j + 1) * GRID_SIZE - 2);
}
else if (m_board[i][j] == WHITE)
{
pDC->SelectStockObject(WHITE_BRUSH);
pDC->Ellipse(BOARD_LEFT + i * GRID_SIZE + 2, BOARD_TOP + j * GRID_SIZE + 2,
BOARD_LEFT + (i + 1) * GRID_SIZE - 2, BOARD_TOP + (j + 1) * GRID_SIZE - 2);
pDC->SelectStockObject(BLACK_BRUSH);
pDC->Ellipse(BOARD_LEFT + i * GRID_SIZE + 10, BOARD_TOP + j * GRID_SIZE + 10,
BOARD_LEFT + (i + 1) * GRID_SIZE - 10, BOARD_TOP + (j + 1) * GRID_SIZE - 10);
}
}
}
}
void CMy5ChessDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CPaintDC dc(this); // 用于绘制的设备上下文
DrawBoard(&dc);
CDialogEx::OnPaint();
}
}
HCURSOR CMy5ChessDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
// 判断是否胜利
bool CMy5ChessDlg::IsWin(int x, int y)
{
int i, j, k;
// 判断横向
for (i = x - 4; i <= x; ++i)
{
if (i < 0 || i + 4 >= BOARD_WIDTH)
{
continue;
}
for (j = i; j <= i + 4; ++j)
{
if (m_board[j][y] != m_currentPlayer)
{
break;
}
}
if (j == i + 5)
{
return true;
}
}
// 判断纵向
for (i = y - 4; i <= y; ++i)
{
if (i < 0 || i + 4 >= BOARD_WIDTH)
{
continue;
}
for (j = i; j <= i + 4; ++j)
{
if (m_board[x][j] != m_currentPlayer)
{
break;
}
}
if (j == i + 5)
{
return true;
}
}
// 判断左上到右下斜线
for (i = x - 4, j = y - 4; i <= x, j <= y; ++i, ++j)
{
if (i < 0 || i + 4 >= BOARD_WIDTH || j < 0 || j + 4 >= BOARD_WIDTH)
{
continue;
}
for (k = 0; k < 5; ++k)
{
if (m_board[i + k][j + k] != m_currentPlayer)
{
break;
}
}
if (k == 5)
{
return true;
}
}
// 判断左下到右上斜线
for (i = x - 4, j = y + 4; i <= x, j >= y; ++i, --j)
{
if (i < 0 || i + 4 >= BOARD_WIDTH || j > BOARD_WIDTH - 1 || j - 4 < 0)
{
continue;
}
for (k = 0; k < 5; ++k)
{
if (m_board[i + k][j - k] != m_currentPlayer)
{
break;
}
}
if (k == 5)
{
return true;
}
}
return false;
}
void CMy5ChessDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CDialogEx::OnLButtonDown(nFlags, point);
// 判断是否在棋盘内
const int BOARD_LEFT = 50; // 棋盘左边界
const int BOARD_TOP = 50; // 棋盘上边界
const int GRID_SIZE = 40; // 棋盘格子大小
if (point.x < BOARD_LEFT || point.y < BOARD_TOP ||
point.x > BOARD_LEFT + GRID_SIZE * BOARD_WIDTH ||
point.y > BOARD_TOP + GRID_SIZE * BOARD_WIDTH)
{
return;
}
// 计算点击的是哪个格子
int x = (point.x - BOARD_LEFT) / GRID_SIZE;
int y = (point.y - BOARD_TOP) / GRID_SIZE;
// 判断是否已经有棋子
if (m_board[x][y] != NONE)
{
return;
}
// 放置棋子
m_board[x][y] = m_currentPlayer;
// 判断胜负
if (IsWin(x, y))
{
CString strWinner;
if (m_currentPlayer == BLACK)
{
strWinner = _T("黑方");
}
else
{
strWinner = _T("白方");
}
MessageBox(strWinner + _T("胜利!"), _T("游戏结束"), MB_OK | MB_ICONINFORMATION);
// 重置棋盘
for (int i = 0; i < BOARD_WIDTH; ++i)
{
for (int j = 0; j < BOARD_WIDTH; ++j)
{
m_board[i][j] = NONE;
}
}
// 重置游戏状态为未开始
m_gameState = GAME_NOT_STARTED;
// 重绘棋盘
Invalidate();
return;
}
// 判断是否平局
bool bDraw = true;
for (int i = 0; i < BOARD_WIDTH; ++i)
{
for (int j = 0; j < BOARD_WIDTH; ++j)
{
if (m_board[i][j] == NONE)
{
bDraw = false;
break;
}
}
if (!bDraw)
{
break;
}
}
if (bDraw)
{
MessageBox(_T("平局!"), _T("游戏结束"), MB_OK | MB_ICONINFORMATION);
// 重置棋盘
for (int i = 0; i < BOARD_WIDTH; ++i)
{
for (int j = 0; j < BOARD_WIDTH; ++j)
{
m_board[i][j] = NONE;
}
}
// 重置游戏状态为未开始
m_gameState = GAME_NOT_STARTED;
// 重绘棋盘
Invalidate();
return;
}
// 切换玩家
m_currentPlayer = m_currentPlayer == BLACK ? WHITE : BLACK;
// 重绘棋盘
Invalidate();
}
```
注意:此代码片段仅供参考,实际应用中可能需要进行修改和完善。例如,此代码中没有处理游戏开始和结束的逻辑,也没有实现悔棋和重新开始功能。
阅读全文