Skip to content

Commit

Permalink
2.0!
Browse files Browse the repository at this point in the history
  • Loading branch information
TcMcKrLiTb committed Nov 16, 2022
1 parent d56716e commit 1438fee
Show file tree
Hide file tree
Showing 9 changed files with 295 additions and 37 deletions.
126 changes: 106 additions & 20 deletions MineSweeper/Gaming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ HBITMAP g_hbmBlo[14] = { NULL };
HBITMAP g_hbmBut[4] = { NULL };
HANDLE hHandle = NULL;
HANDLE TimerID_1s = NULL;
int allNums[7];
_Blocks game_map;
int allNums[7];
int __ROW__ = 10, __COL__ = 10, __MINE__ = 10;
void MapPainting(HWND hwnd)
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
HDC hdcMem = CreateCompatibleDC(hdc);
GetNums();
ButPainting(&ps, &hdc, &hdcMem, game_map.game_state);
NumPainting(&ps, &hdc, &hdcMem, ((game_map.size_col) * 25) - 39, allNums[1], allNums[2], allNums[3], allNums[4], allNums[5], allNums[6]);
//Sleep(1000);
Expand Down Expand Up @@ -138,6 +140,34 @@ void ReSizeGameWnd(HWND hwnd)
TRUE);
}

void InttoStr(int x, wchar_t* str)
{
int i = 0;
while (x)
{
str[i] = (x % 10) + '0';
x /= 10;
i++;
}
for (int j = 0; j < i / 2; j++)
{
wchar_t t = str[j];
str[j] = str[i - j - 1];
str[i - j - 1] = t;
}
}

void StrtoInt(int* x, wchar_t* str)
{
int len = 0;
*x = 0;
while (str[len++] != 0);
for (int i = 0; i < len - 1; i++) {
*x *= 10;
*x += str[i] - '0';
}
}

void InitNUMPADs()
{
g_hbmNum[0] = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(164));
Expand All @@ -148,7 +178,7 @@ void InitNUMPADs()
void InitBLOCKs()
{
game_map.game_state = 0;
game_map.InitBox(10, 10);
game_map.InitBox(__ROW__, __COL__);
for (int i = 1; i <= 13; i++)
g_hbmBlo[i] = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE((int)(173 + i) - 1));
}
Expand All @@ -161,7 +191,6 @@ void GameOver(bool winorlose) {
if (Result) {
;
}

}
else
{
Expand All @@ -183,6 +212,44 @@ void GameOver(bool winorlose) {
}
}

void GameRestart()
{
if (TimerID_1s != NULL)
{
if (game_map.game_state == 1)
{
BOOL Result = DeleteTimerQueueTimer(hHandle, TimerID_1s, NULL);
if (Result) {
;
}
}
}
game_map.time_now = 0;
GetNums();
game_map.game_state = 0;
game_map.InitBox(__ROW__, __COL__);
return;
}

void JudgeNum(int* x, int choose)
{
switch (choose)
{
case 1:
*x = *x < 1 ? 1 : *x;
*x = *x > 34 ? 34 : *x;
break;
case 2:
*x = *x < 4 ? 4 : *x;
*x = *x > 60 ? 60 : *x;
break;
case 3:
*x = *x < 1 ? 1 : *x;
*x = *x > ((__ROW__ * __COL__) - 1) ? ((__ROW__ * __COL__) - 1) : *x;
break;
}
}

void GetNums() {
int num1 = game_map.Bombnum();
int num2 = game_map.time_now;
Expand All @@ -193,6 +260,30 @@ void GetNums() {
}
}

void SetGame(int row, int col, int mine)
{
__ROW__ = row;
__COL__ = col;
__MINE__ = mine;
return;
}

int GetGame(int choose)
{
switch (choose)
{
case 1:
return __ROW__;
break;
case 2:
return __COL__;
break;
case 3:
return __MINE__;
break;
}
}

VOID CALLBACK TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired) {
game_map.time_now++;
GetNums();
Expand Down Expand Up @@ -233,7 +324,7 @@ void Lclick(int x, int y, HWND hwnd)
if (game_map.game_state == 0)
{
GameStart(hwnd);
game_map.RandomSetMines(x, y, 10);
game_map.RandomSetMines(x, y, __MINE__);
}
if (game_map._vis[x][y] == false)
{
Expand All @@ -257,29 +348,24 @@ void Lclick(int x, int y, HWND hwnd)
game_map._vis[x][y] = true;
}
}
if (game_map.JudgeisWin() == true) {
if (game_map.JudgeisWin() == true)
{
for (int i = 1; i <= game_map.size_row; i++)
{
for (int j = 1; j <= game_map.size_col; j++)
{
if (game_map._Block[i][j] == -1)
game_map._flag[i][j] = true;
}
}
GameOver(true);
}
}
else
{
if (y > (10 + (25 * (game_map.size_col + 1))) / 2 - 23 && y < (10 + (25 * (game_map.size_col + 1))) / 2 + 22)
{
if (TimerID_1s != NULL)
{
if (game_map.game_state == 1)
{
BOOL Result = DeleteTimerQueueTimer(hHandle, TimerID_1s, NULL);
if (Result) {
;
}
}
}
game_map.time_now = 0;
GetNums();
game_map.game_state = 0;
game_map.InitBox(10, 10);

GameRestart();
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion MineSweeper/Gaming.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ void InitBLOCKs();
void Lclick(int x, int y, HWND hwnd);
void Rclick(int x, int y, HWND hwnd);
void GetNums();
void SetGame(int row, int col, int mine);
void GameOver(bool winorlose);
void GameRestart();
void JudgeNum(int* x, int choose);
void GameStart(HWND hwnd);
void ReSizeGameWnd(HWND hwnd);
void ReSizeFameWnd(HWND hwnd);
void InttoStr(int x, wchar_t* str);
void StrtoInt(int* x, wchar_t* str);
int GetGame(int choose);
Binary file modified MineSweeper/MineSweeper.aps
Binary file not shown.
115 changes: 109 additions & 6 deletions MineSweeper/MineSweeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK Setting(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
Expand Down Expand Up @@ -65,7 +66,7 @@ ATOM MyRegisterClass(HINSTANCE hInstance)
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MINESWEEPER));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+6);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_MINESWEEPER);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
Expand All @@ -76,9 +77,10 @@ ATOM MyRegisterClass(HINSTANCE hInstance)
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance;
HMENU hmenu = LoadMenu(hInstance, MAKEINTRESOURCE(IDC_MINESWEEPER));

HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME,
240, 100, 240, 500, nullptr, nullptr, hInstance, nullptr);
240, 100, 240, 500, nullptr, hmenu, hInstance, nullptr);

if (!hWnd)
{
Expand All @@ -93,6 +95,7 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HMENU hmenu = GetMenu(hWnd);
switch (message)
{
case WM_CREATE:
Expand All @@ -110,6 +113,39 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case IDM_NEWGAME:
GameRestart();
InvalidateRect(hWnd, NULL, TRUE);
break;
case IDM_BEGINNER:
SetGame(10, 10, 10);
GameRestart();
//if (CheckMenuItem(hmenu, IDM_BEGINNER, MF_CHECKED | MF_BYPOSITION))
//MessageBox(hWnd, _T("123"), _T("123"), MB_OK);
//SetMenuItemInfoA(GetMenu(hWnd), IDM_BEGINNER, TRUE, MF_CHECKED);
InvalidateRect(hWnd, NULL, TRUE);
DrawMenuBar(hWnd);
break;
case IDM_MEDIATE:
SetGame(16, 16, 40);
GameRestart();
InvalidateRect(hWnd, NULL, TRUE);
EnableMenuItem(hmenu, IDM_MEDIATE, MF_DISABLED | MF_BYPOSITION);
DrawMenuBar(hWnd);
break;
case IDM_EXPERT:
SetGame(16, 30, 99);
GameRestart();
InvalidateRect(hWnd, NULL, TRUE);
DrawMenuBar(hWnd);
break;
case IDM_CUSTOM:
DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hWnd, Setting);
GameRestart();
InvalidateRect(hWnd, NULL, TRUE);
EnableMenuItem(hmenu, IDM_MEDIATE, MF_DISABLED | MF_BYPOSITION);
DrawMenuBar(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
Expand All @@ -134,7 +170,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
break;
case WM_RBUTTONDOWN:
{
int x, y;
int x = 0, y = 0;
if (wParam && MK_LBUTTON)
{
y = LOWORD(lParam);
Expand All @@ -148,10 +184,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
break;
case WM_PAINT:
{

ReSizeGameWnd(hWnd);
MapPainting(hWnd);

}
break;
case WM_DESTROY:
Expand All @@ -178,9 +212,78 @@ INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
return (INT_PTR)TRUE;
}
if (LOWORD(wParam) == IDC_BUTTON1) {
system("start www.github.com");
system("start https://github.com/TcMcKrLiTb/MineSweeper");
}
break;
}
return (INT_PTR)FALSE;
}

INT_PTR CALLBACK Setting(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{

UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
wchar_t* stri;
stri = (wchar_t*)malloc(sizeof(wchar_t) * 10);
InttoStr(GetGame(1), stri);

if (stri != NULL)
memset(stri, 0, sizeof(stri));
InttoStr(GetGame(1), stri);
if (stri != NULL)
SetDlgItemTextW(hDlg, IDC_EDIT1, stri);

if (stri != NULL)
memset(stri, 0, sizeof(stri));
InttoStr(GetGame(2), stri);
if (stri != NULL)
SetDlgItemTextW(hDlg, IDC_EDIT2, stri);

if (stri != NULL)
memset(stri, 0, sizeof(stri));
InttoStr(GetGame(3), stri);
if (stri != NULL)
SetDlgItemTextW(hDlg, IDC_EDIT3, stri);

free(stri);
return (INT_PTR)TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
if (LOWORD(wParam) == IDOK) {

wchar_t* stri;
int x = 0;
stri = (wchar_t*)malloc(sizeof(wchar_t) * 10);
if (stri != NULL)
GetDlgItemTextW(hDlg, IDC_EDIT1, stri, 10);
StrtoInt(&x, stri);
JudgeNum(&x, 1);
SetGame(x, GetGame(2), GetGame(3));

if (stri != NULL)
GetDlgItemTextW(hDlg, IDC_EDIT2, stri, 10);
StrtoInt(&x, stri);
JudgeNum(&x, 2);
SetGame(GetGame(1), x, GetGame(3));

if (stri != NULL)
GetDlgItemTextW(hDlg, IDC_EDIT3, stri, 10);
StrtoInt(&x, stri);
JudgeNum(&x, 3);
SetGame(GetGame(1), GetGame(2), x);

EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
Binary file modified MineSweeper/MineSweeper.rc
Binary file not shown.
Loading

0 comments on commit 1438fee

Please sign in to comment.