五子棋游戏(图形化界面)——C语言代码实现

#include<stdio.h>
#include<graphics.h>
#include<conio.h>

#define WIDTH 40
#define ROW_COL 15

void BoardInit();
void PieceDrawing(int x, int y, int* cnt, int* map);
int JudgeBoundary(int a);
int WinJudge(int* map);

int main()
{
	MOUSEMSG msg;
	int cnt = 0;		//记录轮数
	int map[ROW_COL][ROW_COL] = { 0 };

	BoardInit();

	while (1)
	{
		//持续监听鼠标点击事件
		msg = GetMouseMsg();
		if (msg.uMsg == WM_LBUTTONDOWN)
		{
			PieceDrawing(msg.x, msg.y, &cnt, &map[0][0]);
		}
	}

	return 0;
}

void BoardInit()
{
	//绘制棋盘
	initgraph(WIDTH * (ROW_COL + 1), WIDTH * (ROW_COL + 1));//初始化窗口大小
	setbkcolor(YELLOW);//背景颜色
	cleardevice();//用背景颜色遮盖原始屏幕
	setlinestyle(PS_SOLID, 2);//设置成实线,宽度为2个像素实线的粗细
	setcolor(RGB(0, 0, 0));//设置成黑色,前景颜色 不能用setlinrcolor因为没有定义line
	settextcolor(RGB(0, 0, 0));
	settextstyle(50, 0, _T("Consolas"));
	for (int i = 1; i <= ROW_COL; i++)
	{
		line(i * WIDTH, WIDTH, i * WIDTH, ROW_COL * WIDTH);//x改变,平行于与y轴的线
		line(WIDTH, i * WIDTH, ROW_COL * WIDTH, i * WIDTH);//y改变,平行于x轴的线
	}
}

void PieceDrawing(int x, int y, int* cnt, int* map)
{
	x = JudgeBoundary(x) - 1;
	y = JudgeBoundary(y) - 1;
	int* p = map + x + y * ROW_COL;
	if (x != -1 && y != -1 && *p == 0)
	{
		(*cnt)++;
		if ((*cnt) % 2 == 0)
		{
			setfillcolor(RGB(255, 255, 255));
			*p = -1;
		}
		else
		{
			setfillcolor(RGB(0, 0, 0));
			*p = 1;
		}
		fillcircle((x + 1) * WIDTH, (y + 1) * WIDTH, WIDTH / 2 - 1);
		int isWin = WinJudge(map);
		printf("iswin:%d\n", isWin);
		if (isWin != 0)
		{
			printf("game over\n");
			if (isWin > 0)
			{
				outtextxy(WIDTH, WIDTH * ROW_COL / 2, _T("黑子获胜"));
			}
			else
			{
				outtextxy(WIDTH, WIDTH * ROW_COL / 2, _T("白子获胜"));
			}
			while (1)
			{
				//持续监听鼠标点击事件
				MOUSEMSG msg = GetMouseMsg();
				if (msg.uMsg == WM_LBUTTONDOWN)
				{
					*cnt = 0;
					for (int i = 0; i < ROW_COL; i++)
					{
						for (int j = 0; j < ROW_COL; j++)
						{
							*(map + i + j * ROW_COL) = 0;
						}
					}
					closegraph();
					BoardInit();
					break;
				}
			}
		}
	}
}

int JudgeBoundary(int a)
{
	//找到鼠标点击处最近的落子点
	if (a % WIDTH < WIDTH * 0.3)
	{
		a = a / WIDTH;
	}
	else if (a % WIDTH > WIDTH * 0.7)
	{
		a = a / WIDTH + 1;
	}
	else
	{
		a = 0;
	}

	if (a > ROW_COL)
	{
		a = 0;
	}
	return a;
}

int WinJudge(int* map)
{
	int isWin = 0;
	int x[4] = { -1,0,1,1 };
	int y[4] = { -1,-1,-1,0 };
	//搜索是否有五子连珠
	for (int i = 0; i < ROW_COL; i++)
	{
		for (int j = 0; j < ROW_COL; j++)
		{
			int* p = map + j*ROW_COL + i;
			for (int k = 0; k < 4; k++)
			{
				if (i + 2 * x[k] >= 0 && i + 2 * x[k] < ROW_COL &&
					i - 2 * x[k] >= 0 && i - 2 * x[k] < ROW_COL &&
					j + 2 * y[k] >= 0 && j + 2 * y[k] < ROW_COL &&
					j - 2 * y[k] >= 0 && i - 2 * y[k] < ROW_COL)
				{
					isWin = *(p + x[k] + y[k] * ROW_COL) + *(p + 2 * x[k] + 2 * y[k] * ROW_COL) + *(p - x[k] - y[k] * ROW_COL) + *(p - 2 * x[k] - 2 * y[k] * ROW_COL) + *p;
					//printf("search:%d\n", isWin);
					if (abs(isWin)==5)
					{
						return isWin;
					}
				}
			}
		}
	}
	return 0;
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Windows API实现C语言图形化界面五子棋游戏代码示例: ```c #include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT("FiveChess"); HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; if (!RegisterClass(&wndclass)) { MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppName, TEXT("Five Chess"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; switch (message) { case WM_PAINT: hdc = BeginPaint(hwnd, &ps); // 在窗口中绘制出棋盘和棋子 EndPaint(hwnd, &ps); return 0; case WM_LBUTTONDOWN: // 处理用户下棋事件 return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); } ``` 这段代码只是一个简单的框架,需要根据具体需求进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值