C++ 五子棋

19 篇文章 0 订阅
7 篇文章 0 订阅

C++ 五子棋

废话不多说直接上代码

#include<atlimage.h>
#include<Windows.h>
#include <mmsystem.h>
#pragma comment(lib, "WINMM.LIB")
#define Pen_w 3
#define G_C 16
#define q_C 24
CImage bg;
bool ys;
bool end;
int Win_Width, Win_Height;
HPEN black_pen;
HBRUSH whites,blacks,yellows;
HFONT hFont;
int QI_W, QI_X_start, QI_x;
unsigned char all_qizi[G_C][G_C];
void make_qipan(HDC hdc)
{
	end = false;
	for (int i = 0; i < G_C; i++)
		for (int j = 0; j < G_C; j++)
			all_qizi[i][j] = 0;
	ys = false;
	int tmp2 = QI_x / 2;
	int tmp = QI_W - tmp2;
	RECT re;
	re.left = 0;
	re.top = 0;
	re.right = Win_Width;
	re.bottom = Win_Height;
	SetStretchBltMode(hdc, HALFTONE);
	bg.StretchBlt(hdc, re);
	re.left = 0;
	re.top = 0;
	re.right = QI_X_start-QI_x;
	re.bottom = QI_X_start;
	SetBkColor(hdc, RGB(255, 212, 97)); 
	SelectObject(hdc, hFont);
	wchar_t *t = L"白方出棋环节";
	DrawText(hdc,t, wcslen(t),&re, DT_CENTER);
	/*HGDIOBJ old = SelectObject(hdc, whites);
	Ellipse(hdc, 10, 10, 80, 80);
	SelectObject(hdc, old);*/
	HGDIOBJ old= SelectObject(hdc, black_pen);
	for (int i = QI_x / 2; i < QI_W; i+= QI_x)
	{
		MoveToEx(hdc, QI_X_start + i, tmp2,NULL);
		LineTo(hdc, QI_X_start + i, tmp);
		MoveToEx(hdc, QI_X_start + tmp2, i, NULL);
		LineTo(hdc, QI_X_start + tmp, i);
	}
	SelectObject(hdc, old);
}
class Vector2_int
{
public:
	int x, y;
	Vector2_int(int x, int y) :x(x), y(y) {}
	bool operator == (Vector2_int &t)
	{
		if (this->x == t.x && this->y == t.y)
			return true;
		return false;
	}
	static Vector2_int zero;
	bool isNULL()
	{
		if (this->x == 0 && this->y == 0)
			return true;
		return false;
	}
};
Vector2_int Vector2_int::zero= Vector2_int(0, 0);
Vector2_int getqizi(int x, int y)
{
	if (x<QI_X_start || x>Win_Width - QI_X_start)
		return Vector2_int::zero;
	int ss = QI_x / 2;
	for (int i = ss; i < QI_W; i += QI_x)//高度 y
		for (int j = ss; j < QI_W; j += QI_x)
			if (abs(i - y )< ss &&abs(j + QI_X_start - x) < ss)
				return Vector2_int(j + QI_X_start, i);
	return Vector2_int::zero;
}
LRESULT CALLBACK MyWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE h1, HINSTANCE h2, LPSTR cmd, int show)
{
	bg.Load(L"bg.jpg");
	yellows = CreateSolidBrush(RGB(255, 212, 97));
	black_pen = CreatePen(PS_SOLID, Pen_w, RGB(0, 0, 0));
	whites = CreateSolidBrush(RGB(255, 255, 255));
	blacks = CreateSolidBrush(RGB(0, 0, 0));
	Win_Width = GetSystemMetrics(SM_CXSCREEN);
	Win_Height = GetSystemMetrics(SM_CYSCREEN);
	hFont = CreateFont(30, 15, 0, 0, FW_BOLD, 0, 0, 0, GB2312_CHARSET, 0, 0, 0, 0, TEXT("隶书"));
	mciSendString(TEXT("open 2.mp3 alias bgm"), NULL, 0, NULL);
	mciSendString(TEXT("open 1.mp3 alias bgm1"), NULL, 0, NULL);
	mciSendString(L"play bgm repeat", 0, 0, 0);

	QI_W = Win_Width > Win_Height ? Win_Height : Win_Width;
	QI_X_start = Win_Width > Win_Height ? (Win_Width - Win_Height) / 2 : (Win_Height - Win_Width) / 2;
	QI_x = QI_W / G_C;//得到每个格子的长度
	WNDCLASS wnd = {
		CS_HREDRAW,
		MyWindowProc,          // 使用自定义的窗口过程函数
		0,0,h1,
		LoadIcon(NULL,IDI_APPLICATION),
		LoadCursor(NULL,IDC_ARROW),
		(HBRUSH)(GetStockObject(WHITE_BRUSH)),
		NULL, L"MyWindow"
	};
	// 注册窗口类
	RegisterClass(&wnd);
	// 创建窗口
	HWND hWnd = CreateWindow(L"MyWindow", L"newWindow", WS_POPUP,
		0, 0, Win_Width, Win_Height, NULL, NULL, h1, NULL);
	// 显示窗口
	ShowWindow(hWnd, 1);
	// 更新窗口
	UpdateWindow(hWnd);
	//消息循环
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0)) {    // 循环获取消息
		TranslateMessage(&msg);               // 翻译消息
		DispatchMessage(&msg);                // 派发消息
	}
	DeleteObject(blacks);
	DeleteObject(whites);
	DeleteObject(black_pen);
	DeleteObject(yellows);
	DeleteObject(hFont);
	return 0;

}
int gety(int x, int k, int b)
{
	return x*k + b;
}
void win(int f, HWND hwnd)
{
	if (f == 1)
		MessageBox(hwnd, L"白方获胜", L"", 0);
	else if (f == 2)
		MessageBox(hwnd, L"黑方获胜", L"", 0);
	make_qipan(GetDC(hwnd));
}
bool iswin(int x,int y,int f)
{
	int sum = 0;
	int t = 0;
	sum = 0;
	for (int i = 0; i < G_C; i++)
	{
		t = gety(i, 1, y - x);
		if (t < 0 || t >= G_C)continue;
		if (all_qizi[i][t] == f)
			sum++;
		else
			if (sum != 5)
				sum = 0;
	}
	if (sum == 5)
		return true;
	sum = 0;
	for (int i = 0; i < G_C; i++)
	{
		t = gety(i, -1, x + y);
		if (t < 0 || t >= G_C)continue;
		if (all_qizi[i][t] == f)
			sum++;
		else
			if (sum != 5)
				sum = 0;
	}
	if (sum == 5)
		return true;
	sum = 0;
	for (int i = 0; i < G_C; i++)
		if (all_qizi[x][i] == f)
			sum++;
		else
			if (sum != 5)
				sum = 0;
	if (sum == 5)
		return true;
	sum = 0;
	for (int i = 0; i < G_C; i++)
		if (all_qizi[i][y] == f)
			sum++;
		else
			if (sum != 5)
				sum = 0;
	if (sum == 5)
		return true;
	return false;
}
LRESULT CALLBACK MyWindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
	switch (Msg) {
		case WM_PAINT:
		{
			HDC hdc = GetDC(hWnd);
			make_qipan(hdc);
			break;
		}
		case WM_LBUTTONUP:
		{
			if (end)break;
			int x= LOWORD(lParam);
			int y= HIWORD(lParam);
			if (x > Win_Width - 60 && y <  60)
			{
				PostQuitMessage(0);
				break;
			}
			Vector2_int t = getqizi(x, y);
			if (t.isNULL()) break;
			int xx= (t.x-( QI_X_start + QI_x / 2))/ QI_x;
			int yy =t.y/ QI_x;
			if (all_qizi[xx][yy] != 0)
				break;
			ys = ys ? false : true;
			all_qizi[xx][yy] = ys ? 1 : 2;
			HDC hdc = GetDC(hWnd);
			HGDIOBJ old=ys? SelectObject(hdc, whites) : SelectObject(hdc, blacks);
			Ellipse(hdc, t.x - q_C, t.y - q_C, t.x + q_C, t.y + q_C);
			//if (ys)  SelectObject(hdc, blacks); else SelectObject(hdc, whites);
			//Ellipse(hdc, 10, 10, 80, 80);
			if (ys)
			{
				RECT re;
				re.left = 0;
				re.top = 0;
				re.right = QI_X_start - QI_x;
				re.bottom = QI_X_start;
				FillRect(hdc, &re, yellows);
				SetBkColor(hdc, RGB(255, 212, 97));
				SelectObject(hdc, hFont);
				wchar_t *t = L"黑方出棋环节";
				DrawText(hdc, t, wcslen(t), &re, DT_CENTER);
			}
			else
			{
				RECT re;
				re.left = 0;
				re.top = 0;
				re.right = QI_X_start - QI_x;
				re.bottom = QI_X_start;
				FillRect(hdc, &re, yellows);
				SetBkColor(hdc, RGB(255, 212, 97));
				SelectObject(hdc, hFont);
				wchar_t *t = L"白方出棋环节";
				DrawText(hdc, t, wcslen(t), &re, DT_CENTER);
			}
			SelectObject(hdc, old);
			end = iswin(xx, yy, ys ? 1 : 2);
			mciSendString(L"stop bgm1", 0, 0, 0);
			mciSendString(L"seek bgm1 to start", 0, 0, 0);
			mciSendString(L"play bgm1", 0, 0, 0);
			if (end)
			{
				win(ys ? 1 : 2, hWnd);
			}
			break;
		}
		case WM_DESTROY:
			PostQuitMessage(0);
			return 0;
	}
	return DefWindowProc(hWnd, Msg, wParam, lParam);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值