c语言图形化界面五子棋的具体代码
时间: 2023-08-12 13:32:06 浏览: 177
以下是一个使用Windows API实现的C语言图形化界面五子棋游戏的代码示例:
```c
#include <windows.h>
// 窗口宽度和高度
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 640
// 棋盘格子数量
#define GRID_NUM 15
// 棋盘格子宽度和高度
#define GRID_WIDTH (WINDOW_WIDTH / GRID_NUM)
#define GRID_HEIGHT (WINDOW_HEIGHT / GRID_NUM)
// 棋子大小
#define PIECE_SIZE (GRID_WIDTH / 2 - 2)
// 棋子颜色
#define PIECE_COLOR_BLACK RGB(0, 0, 0)
#define PIECE_COLOR_WHITE RGB(255, 255, 255)
// 窗口句柄
HWND g_hwnd = NULL;
// 棋盘数组
int g_board[GRID_NUM][GRID_NUM] = {0};
// 当前玩家,1表示黑方,2表示白方
int g_current_player = 1;
// 电脑AI难度,0表示简单,1表示困难
int g_ai_level = 0;
// 电脑AI下棋
void AiPlay()
{
int x, y;
if (g_ai_level == 0)
{
// 简单难度,随机下棋
do
{
x = rand() % GRID_NUM;
y = rand() % GRID_NUM;
} while (g_board[y][x] != 0);
}
else
{
// 困难难度,使用最佳落子算法
// TODO: 实现最佳落子算法
}
g_board[y][x] = 2;
InvalidateRect(g_hwnd, NULL, TRUE);
}
// 判断是否有一方获胜
int CheckWin(int player)
{
int i, j, k;
// 判断横向
for (i = 0; i < GRID_NUM; i++)
{
for (j = 0; j < GRID_NUM - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (g_board[i][j + k] != player)
break;
}
if (k == 5)
return 1;
}
}
// 判断纵向
for (i = 0; i < GRID_NUM - 4; i++)
{
for (j = 0; j < GRID_NUM; j++)
{
for (k = 0; k < 5; k++)
{
if (g_board[i + k][j] != player)
break;
}
if (k == 5)
return 1;
}
}
// 判断正斜线
for (i = 0; i < GRID_NUM - 4; i++)
{
for (j = 0; j < GRID_NUM - 4; j++)
{
for (k = 0; k < 5; k++)
{
if (g_board[i + k][j + k] != player)
break;
}
if (k == 5)
return 1;
}
}
// 判断反斜线
for (i = 0; i < GRID_NUM - 4; i++)
{
for (j = 4; j < GRID_NUM; j++)
{
for (k = 0; k < 5; k++)
{
if (g_board[i + k][j - k] != player)
break;
}
if (k == 5)
return 1;
}
}
return 0;
}
// 绘制棋盘
void DrawBoard(HDC hdc)
{
int i, j;
// 绘制格子线
for (i = 0; i < GRID_NUM; i++)
{
MoveToEx(hdc, i * GRID_WIDTH, 0, NULL);
LineTo(hdc, i * GRID_WIDTH, WINDOW_HEIGHT);
}
for (i = 0; i < GRID_NUM; i++)
{
MoveToEx(hdc, 0, i * GRID_HEIGHT, NULL);
LineTo(hdc, WINDOW_WIDTH, i * GRID_HEIGHT);
}
// 绘制棋子
for (i = 0; i < GRID_NUM; i++)
{
for (j = 0; j < GRID_NUM; j++)
{
if (g_board[i][j] == 1)
{
HBRUSH hBrush = CreateSolidBrush(PIECE_COLOR_BLACK);
HBRUSH hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
Ellipse(hdc, j * GRID_WIDTH + GRID_WIDTH / 2 - PIECE_SIZE, i * GRID_HEIGHT + GRID_HEIGHT / 2 - PIECE_SIZE, j * GRID_WIDTH + GRID_WIDTH / 2 + PIECE_SIZE, i * GRID_HEIGHT + GRID_HEIGHT / 2 + PIECE_SIZE);
SelectObject(hdc, hOldBrush);
DeleteObject(hBrush);
}
else if (g_board[i][j] == 2)
{
HBRUSH hBrush = CreateSolidBrush(PIECE_COLOR_WHITE);
HBRUSH hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
Ellipse(hdc, j * GRID_WIDTH + GRID_WIDTH / 2 - PIECE_SIZE, i * GRID_HEIGHT + GRID_HEIGHT / 2 - PIECE_SIZE, j * GRID_WIDTH + GRID_WIDTH / 2 + PIECE_SIZE, i * GRID_HEIGHT + GRID_HEIGHT / 2 + PIECE_SIZE);
SelectObject(hdc, hOldBrush);
DeleteObject(hBrush);
}
}
}
}
// 处理鼠标点击事件
void OnClick(int x, int y)
{
if (g_board[y][x] == 0)
{
g_board[y][x] = g_current_player;
InvalidateRect(g_hwnd, NULL, TRUE);
if (CheckWin(g_current_player))
{
MessageBox(g_hwnd, "You win!", "Message", MB_OK);
memset(g_board, 0, sizeof(g_board));
g_current_player = 1;
InvalidateRect(g_hwnd, NULL, TRUE);
return;
}
if (g_current_player == 1)
{
g_current_player = 2;
AiPlay();
if (CheckWin(g_current_player))
{
MessageBox(g_hwnd, "You lose!", "Message", MB_OK);
memset(g_board, 0, sizeof(g_board));
g_current_player = 1;
InvalidateRect(g_hwnd, NULL, TRUE);
return;
}
g_current_player = 1;
}
}
}
// 处理窗口绘制事件
void OnPaint(HDC hdc)
{
DrawBoard(hdc);
}
// 处理窗口消息
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
// 初始化随机数种子
srand(GetTickCount());
break;
case WM_LBUTTONDOWN:
// 处理鼠标点击事件
OnClick(LOWORD(lParam) / GRID_WIDTH, HIWORD(lParam) / GRID_HEIGHT);
break;
case WM_PAINT:
{
// 获取绘图上下文
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// 处理窗口绘制事件
OnPaint(hdc);
EndPaint(hwnd, &ps);
break;
}
case WM_DESTROY:
// 退出程序
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// 创建窗口类
WNDCLASS wc = {0};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = "MyWindowClass";
RegisterClass(&wc);
// 创建窗口
g_hwnd = CreateWindow("MyWindowClass", "Five Chess", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL);
if (g_hwnd == NULL)
return 0;
// 显示窗口
ShowWindow(g_hwnd, nCmdShow);
UpdateWindow(g_hwnd);
// 处理消息循环
MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
```
这段代码使用了Windows API来创建窗口和处理鼠标点击事件等消息,实现了一个简单的五子棋游戏。具体实现细节可以参考代码注释。
阅读全文