Minesweeper-3

嗨嗨嗨~~~,没想到吧,我一天两篇~,嗨嗨嗨~~~

好啦,废话不多说,直接启动~


这次来Minesweeper和Game的连接。

上期Game里我有一个system("start Minesweeper.exe"),这就是打开难度选择(名字不同可以改一下)。

但Minesweeper.cpp中真的这么简单吗?

绝对不可能。

上篇结尾我说了我是自己输入的信息,可总不能玩个游戏还要自己敲代码吧~

Coding~启动~

先是全局变量int x, y, winx, winy, level;

食不食觉得很熟悉?

没错,就是那几个变量。

不过这是两个程序,所以得靠文件传输(如果kun闲得慌,可以试试winsock)。

向winProc加入以下程序:

case WM_COMMAND: {
    switch(wParam) {
        case 1: {
			level = 1;
			x = 9;
			y = 9;
			winx = 230;
			winy = 260;
			break;
		}
		case 2: {
			level = 2;
			x = 16;
			y = 16;
			winx = 370;
			winy = 400;
			break;
		}
		case 3: {
			level = 3;
			x = 30;
			y = 16;
			winx = 650;
			winy = 400;
			break;
		}
    }
    system("start Game.exe");
    break;
}

别问我数据从哪来,问就是算的。

可这还在cpp里呢,说好的文件读入呢?

别急,

//记得倒入<iostream>哦
namespace std {
    void write()
    {
        freopen("winnum.data", "w", stdout);
        cout << x << " " << y << " " << winx << " " << winy << " " << level;
        //printf("%d %d %d %d %d", x, y, winx, winy, level);
        //endl也行
    }
}

 然后往WM_COMMAND里加入std::write();

不过,它会一直留在那,所以得加上一句PostQuitMessage(0);

这样,就完工了~

整体程序:

//Minesweeper.cpp/.exe/.dev
#include <windows.h>
#include <iostream>

int x, y, winx, winy, level;

namespace std {
	void write()
	{
		freopen("winnum.data", "w", stdout);
		cout << x << endl;
		cout << y << endl;
		cout << winx << endl;
		cout << winy << endl;
		cout << level << endl;
	}
}


LRESULT CALLBACK WndProc_main(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam){
	HDC hdc;
	PAINTSTRUCT ps;
	switch(Msg)
	{
		case WM_COMMAND: {
			switch (wParam)
			{
				case 1: {
					level = 1;
					x = 9;
					y = 9;
					winx = 230;
					winy = 260;
					break;
				}
				case 2: {
					level = 2;
					x = 16;
					y = 16;
					winx = 370;
					winy = 400;
					break;
				}
				case 3: {
					level = 3;
					x = 30;
					y = 16;
					winx = 650;
					winy = 400;
					break;
				}
			}
			std::write();
			system("start Game.exe");
			PostQuitMessage(0);
      		return 0;
		}
		case WM_PAINT: {
			hdc = BeginPaint(hWnd, &ps);
			TextOut(hdc, 111, 25, "Minesweeper", lstrlen("Minesweeper"));
	        EndPaint(hWnd, &ps);
	        break;
		}
        case WM_DESTROY: {
      		PostQuitMessage(0);
      		return 0;
      	}
        default:return DefWindowProc(hWnd,Msg,wParam,lParam);
   }
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){
	WNDCLASSEX wcex;
	memset(&wcex,0,sizeof(wcex));
	wcex.cbSize=sizeof(wcex);
	wcex.lpfnWndProc=WndProc_main;
	wcex.hInstance=hInstance;
	wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
	wcex.style=CS_DBLCLKS|CS_SAVEBITS|CS_GLOBALCLASS;
	wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszClassName="WindowClass";
	wcex.hIcon=LoadIcon(NULL,IDI_APPLICATION);
	wcex.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
	if (!RegisterClassEx(&wcex)) {
		MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}
	HWND hWnd = CreateWindowExA(WS_EX_ACCEPTFILES,"WindowClass", "Minesweeper",
  		WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		310,
		150,
		NULL, NULL, hInstance, NULL);
	if(!hWnd){
		MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}
	HWND EASY = CreateWindow("Button", "Easy", WS_VISIBLE | WS_CHILD, 25, 55, 80, 32, hWnd, (HMENU)1, NULL, NULL);
	HWND MIDDLE = CreateWindow("Button", "Middle", WS_VISIBLE | WS_CHILD, 115, 55, 80, 32, hWnd, (HMENU)2, NULL, NULL);
	HWND HARD = CreateWindow("Button", "Hard", WS_VISIBLE | WS_CHILD, 205, 55, 80, 32, hWnd, (HMENU)3, NULL, NULL);
	ShowWindow(hWnd,nShowCmd);
	UpdateWindow(hWnd);
	MSG msg={};
	while (GetMessage(&msg, NULL, 0, 0) > 0) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return 0;
}
//Game.cpp/.exe/.dev
#include <windows.h>
#include <iostream>
#include <stdlib.h>
#include <ctime>

int x, y, winx, winy, level;
HWND button[30][30];
bool bflag[30][30];
bool flag[30][30];
bool win = 0;
int number[32][32] = {0};
int score = 0;
int should;

namespace std{
	void read()
	{
		freopen("winnum.data", "r", stdin);
		cin >> x >> y >> winx >> winy >> level;
	}
	void write()
	{
		freopen("number.data", "w", stdout);
		for (int i = 1; i <= y; i++)
		{
			for (int j = 1; j <= x; j++)
			{
				if (number[i][j] == -1) cout << "● ";
				else cout << number[i][j] << " ";
			}
			cout << endl;
		}
	}
	void init()
	{
		int l;
		if (level == 1)
		{
			l = 10;
			should = 71;
		}
		else if (level == 2)
		{
			l = 40;
			should = 216;
		}
		else
		{
			l = 99;
			should = 381;
		}
		for (int i = 1; i <= l; i++)
		{
			srand(time(0));
			int lx, ly;
			lx = rand() % x + 1;
			ly = rand() % y + 1;
			while (number[ly][lx] == -1)
			{
				lx = rand() % x + 1;
				ly = rand() % y + 1;
			}
			number[ly][lx] = -1;
		}
		for (int i = 1; i <= y; i++)
		{
			for (int j = 1; j <= x; j++)
			{
				if (number[i][j] == -1) continue;
				int num = 0;
				for (int k = -1; k <= 1; k++)
				{
					for (int l = -1; l <= 1; l++)
					{
						if (number[i + k][j + l] == -1) num++;
					}
				}
				number[i][j] = num;
			}
		}
		write(); //For check
	}
}

void delbutton(int hmenu)
{
	hmenu--;
	DestroyWindow(button[hmenu / x][hmenu % x]);
	bflag[hmenu / x][hmenu % x] = 1;
	flag[hmenu / x][hmenu % x] = 1;
}

void paintall(HDC hdc)
{
	for (int i = 0; i < y; i++)
	{
		for (int j = 0; j < x; j++)
		{
			if (bflag[i][j] == 0) DestroyWindow(button[i][j]);
			if (number[i + 1][j + 1] == -1)
			{
				TextOut(hdc, 25 + j * 20 + 6, 55 + i * 20 + 3, "B", lstrlen("B"));
			}
			else if (number[i + 1][j + 1] == 0)
			{
				TextOut(hdc, 25 + j * 20 + 6, 55 + i * 20 + 3, " ", lstrlen(" "));
			}
			else
			{
				char out[2] = {};
				out[0] = char(number[i + 1][j + 1] + '0');
				TextOut(hdc, 25 + j * 20 + 6, 55 + i * 20 + 3, out, lstrlen(out));
			}
		}
	}
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam){
	HDC hdc;
	PAINTSTRUCT ps;
	switch(Msg)
	{
		case WM_COMMAND: {
			if (wParam == 0) {
				system("start Minesweeper.exe");
				PostQuitMessage(0);
				return 0;
			}
			else {
				delbutton(int(wParam));
				if (number[(wParam - 1) / x + 1][(wParam - 1) % x + 1] == -1)
				{
					wParam = WM_PAINT;
				}
				else
				{
					score++;
					break;
				}
			}
		}
		case WM_PAINT: {
			if (win) break;
			hdc = BeginPaint(hWnd, &ps);
			TextOut(hdc, (winx - 88) / 2, 25, "Minesweeper", lstrlen("Minesweeper"));
			if (score == should)
			{
				win = 1;
				paintall(hdc);
				MessageBox(0, "You Win", "Game Over", MB_OK);
				EndPaint(hWnd, &ps);
				return 0;
			}
			for (int i = 0; i < y; i++)
			{
				for (int j = 0; j < x; j++)
				{
					if (flag[i][j])
					{
						if (number[i + 1][j + 1] == -1)
						{
							paintall(hdc);
							MessageBox(0, "You Lose", "Game Over", MB_OK);
							win = 1;
							EndPaint(hWnd, &ps);
							return 0;
						}
						else if (number[i + 1][j + 1] == 0)
						{
							TextOut(hdc, 25 + j * 20 + 6, 55 + i * 20 + 3, " ", lstrlen(" "));
						}
						else
						{
							char out[2] = {};
							out[0] = char(number[i + 1][j + 1] + '0');
							TextOut(hdc, 25 + j * 20 + 6, 55 + i * 20 + 3, out, lstrlen(out));
						}
						flag[i][j] = 0;
					}
				}
			}
	        EndPaint(hWnd, &ps);
			break;
		}
        case WM_DESTROY: {
      		PostQuitMessage(0);
      		return 0;
      	}
        default:return DefWindowProc(hWnd,Msg,wParam,lParam);
   }
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){
	std::read();
	std::init();
	WNDCLASSEX wcex;
	memset(&wcex,0,sizeof(wcex));
	wcex.cbSize=sizeof(wcex);
	wcex.lpfnWndProc=WndProc;
	wcex.hInstance=hInstance;
	wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
	wcex.style=CS_DBLCLKS|CS_SAVEBITS|CS_GLOBALCLASS;
	wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszClassName="WindowClass";
	wcex.hIcon=LoadIcon(NULL,IDI_APPLICATION);
	wcex.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
	if (!RegisterClassEx(&wcex)) {
		MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}
	HWND hWnd = CreateWindowExA(WS_EX_ACCEPTFILES,"WindowClass", "Minesweeper",
  		WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		winx + 10,
		winy + 20,
		NULL, NULL, hInstance, NULL);
	if(!hWnd){
		MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}
	long long num = 1;
	CreateWindow("Button", "Return", WS_VISIBLE | WS_CHILD, 10, 10, 70, 15, hWnd, (HMENU)0, hInstance, NULL);
	for (int i = 0; i < y; i++)
	{
		for (int j = 0; j < x; j++)
		{
			button[i][j] = CreateWindow("Button", " ", WS_VISIBLE | WS_CHILD, 25 + j * 20, 55 + i * 20, 20, 20, hWnd, (HMENU)num, NULL, NULL);
			num++;
		}
	}
	ShowWindow(hWnd,nShowCmd);
	UpdateWindow(hWnd);
	MSG msg={};
	while (GetMessage(&msg, NULL, 0, 0) > 0) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return 0;
}

求助:

我一开始想要用一个cpp中创建两个窗口来省略system()运行(太太太卡了)可发现有问题。

求DALAO帮忙。


好啦,Minesweeper就到此结束啦~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值