diff --git a/ConsoleApplication1/ConsoleApplication1/ConsoleApplication1.cpp b/ConsoleApplication1/ConsoleApplication1/ConsoleApplication1.cpp index 9d6500d..5cc2564 100644 --- a/ConsoleApplication1/ConsoleApplication1/ConsoleApplication1.cpp +++ b/ConsoleApplication1/ConsoleApplication1/ConsoleApplication1.cpp @@ -1,20 +1,549 @@ -// ConsoleApplication1.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы. +// lab16.cpp : . // +#define _CRT_SECURE_NO_WARNINGS -#include +#include +#include +#include "framework.h" +#include "lab16.h" +#include -int main() +#define MAX_LOADSTRING 100 +#define N 10 +#define M 15 +#define WIDTH 30 +#define HEIGHT 20 + +// : +int steps = 0; +int gold = 0; +HINSTANCE hInst; // +WCHAR szTitle[MAX_LOADSTRING]; // +WCHAR szWindowClass[MAX_LOADSTRING]; // + +// , : +ATOM MyRegisterClass(HINSTANCE hInstance); +BOOL InitInstance(HINSTANCE, int); +LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); +INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); + +int APIENTRY wWinMain(_In_ HINSTANCE hInstance, + _In_opt_ HINSTANCE hPrevInstance, + _In_ LPWSTR lpCmdLine, + _In_ int nCmdShow) { - std::cout << "Hello World!\n"; + UNREFERENCED_PARAMETER(hPrevInstance); + UNREFERENCED_PARAMETER(lpCmdLine); + + // TODO: . + + // + LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); + LoadStringW(hInstance, IDC_LAB16, szWindowClass, MAX_LOADSTRING); + MyRegisterClass(hInstance); + + // : + if (!InitInstance (hInstance, nCmdShow)) + { + return FALSE; + } + + HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_LAB16)); + + MSG msg; + + // : + while (GetMessage(&msg, nullptr, 0, 0)) + { + if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + } + + return (int) msg.wParam; } -// Запуск программы: CTRL+F5 или меню "Отладка" > "Запуск без отладки" -// Отладка программы: F5 или меню "Отладка" > "Запустить отладку" -// Советы по началу работы -// 1. В окне обозревателя решений можно добавлять файлы и управлять ими. -// 2. В окне Team Explorer можно подключиться к системе управления версиями. -// 3. В окне "Выходные данные" можно просматривать выходные данные сборки и другие сообщения. -// 4. В окне "Список ошибок" можно просматривать ошибки. -// 5. Последовательно выберите пункты меню "Проект" > "Добавить новый элемент", чтобы создать файлы кода, или "Проект" > "Добавить существующий элемент", чтобы добавить в проект существующие файлы кода. -// 6. Чтобы снова открыть этот проект позже, выберите пункты меню "Файл" > "Открыть" > "Проект" и выберите SLN-файл. + +// +// : MyRegisterClass() +// +// : . +// +ATOM MyRegisterClass(HINSTANCE hInstance) +{ + WNDCLASSEXW wcex; + + wcex.cbSize = sizeof(WNDCLASSEX); + + wcex.style = CS_HREDRAW | CS_VREDRAW; + wcex.lpfnWndProc = WndProc; + wcex.cbClsExtra = 0; + wcex.cbWndExtra = 0; + wcex.hInstance = hInstance; + wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_LAB16)); + wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); + wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); + wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_LAB16); + wcex.lpszClassName = szWindowClass; + wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); + + return RegisterClassExW(&wcex); +} + +// +// : InitInstance(HINSTANCE, int) +// +// : +// +// : +// +// , +// . +// +BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) +{ + hInst = hInstance; // + + HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, + CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr); + + if (!hWnd) + { + return FALSE; + } + + ShowWindow(hWnd, nCmdShow); + UpdateWindow(hWnd); + + return TRUE; +} + +int map[N][M] = { + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0}, + {0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 3, 3, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 3, 3, 0, 0, 0}, + + {0, 0, 0, 0, 0, 0, 3, 3, 3, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 2, 0, 0, 2, 0}, + {0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 2, 0, 0, 2, 0}, + {0, 0, 0, 0, 3, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, +}; +void drawMap(HDC hdc) { + HBRUSH hBrushEmptyCell = CreateSolidBrush(RGB(200, 200, 200)); + HBRUSH hBrushGold = CreateSolidBrush(RGB(255, 255, 0)); + HBRUSH hBrushWall = CreateSolidBrush(RGB(0, 0, 0)); + HBRUSH hBrushMan = CreateSolidBrush(RGB(0, 0, 255)); + HBRUSH brush[4] = { hBrushEmptyCell, hBrushMan, hBrushWall, hBrushGold }; + + int i, j; + + for (i = 0; i < N; i++) { + for (j = 0; j < M; j++) { + + int x1 = j * WIDTH; + int x2 = (j + 1) * WIDTH; + int y1 = i * HEIGHT; + int y2 = (i + 1) * HEIGHT; + + RECT r = { x1, y1, x2, y2 }; + FillRect(hdc, &r, brush[map[i][j]]); + } + } + for (i = 0; i < 4; i++) + DeleteObject(brush[i]); + + char strC[80]; + sprintf(strC, "steps = %d gold = %d", steps, gold); + + TCHAR strWin[80]; + OemToChar(strC, strWin); + + HFONT hFont = CreateFont(HEIGHT, 0, 0, 0, 0, 0, 0, 0, + DEFAULT_CHARSET, 0, 0, 0, 0, L"Courier New"); + SelectObject(hdc, hFont); + SetTextColor(hdc, RGB(0, 0, 128)); + + TextOut(hdc, 10, HEIGHT * N, (LPCWSTR)strWin, _tcslen(strWin)); + + DeleteObject(hFont); +} + + +void Left() { + int i, j; + + for (i = 0; i < N; i++) { + for (j = 0; j < M; j++) { + if (map[i][j] == 1) { + if (map[i][j - 1] == 0) { + map[i][j - 1] = 1; + map[i][j] = 0; + steps++; + } + else if (map[i][j - 1] == 3) { + map[i][j - 1] = 1; + map[i][j] = 0; + steps++; + gold++; + } + } + } + } +} +void Right() { + int i, j; + + for (i = 0; i < N; i++) { + for (j = M - 2; j >= 0; j--) { + if (map[i][j] == 1) { + if (map[i][j + 1] == 0) { + map[i][j + 1] = 1; + map[i][j] = 0; + steps++; + } + else if (map[i][j + 1] == 3) { + map[i][j + 1] = 1; + map[i][j] = 0; + steps++; + gold++; + } + } + } + } +} +void Up() { + int i, j; + + for (i = 0; i < N; i++) { + for (j = 0; j < M; j++) { + if (map[i][j] == 1) { + if (map[i - 1][j] == 0) { + map[i - 1][j] = 1; + map[i][j] = 0; + steps++; + } + else if (map[i - 1][j] == 3) { + map[i - 1][j] = 1; + map[i][j] = 0; + steps++; + gold++; + } + } + } + } +} +void Down() { + int i, j; + + for (i = N - 2; i >= 0; i--) { + for (j = 0; j < M; j++) { + if (map[i][j] == 1) { + if (map[i + 1][j] == 0) { + map[i + 1][j] = 1; + map[i][j] = 0; + steps++; + } + else if (map[i + 1][j] == 3) { + map[i + 1][j] = 1; + map[i][j] = 0; + steps++; + gold++; + } + } + } + } +} +void Save() { + FILE* fout; + fout = fopen("C:\\Temp\\Lection12\\game.txt", "wt"); + if (fout == NULL) { + printf("\n \n"); + return; + } + + fprintf(fout, "%d ", N); + fprintf(fout, "%d\n", M); + int i, j; + + for (i = 0; i < N; i++) { + for (j = 0; j < M; j++) { + fprintf(fout, "%d ", map[i][j]); + } + } + fprintf(fout, "%d ", steps); + fprintf(fout, "%d", gold); + + fclose(fout); + +} +void Load() { + FILE* fin; + fin = fopen("C:\\Temp\\Lection12\\game.txt", "rt"); + if (fin == NULL) { + printf(" "); + return; + } + + int n, m; + fscanf(fin, "%d", &n); + fscanf(fin, "%d", &m); + int i, j; + + for (i = 0; i < n; i++) { + for (j = 0; j < m; j++) { + fscanf(fin, "%d", &map[i][j]); + } + } + fscanf(fin, "%d ", &steps); + fscanf(fin, "%d", &gold); + + fclose(fin); +} +void SaveBinary() { + FILE* fout; + fout = fopen("C:\\Temp\\Lection12\\game_bin.bin", "wb"); + if (fout == NULL) { + printf(" "); + return; + } + int n = N, m = M; + fwrite(&n, sizeof(n), 1, fout); + fwrite(&m, sizeof(m), 1, fout); + fwrite(map, sizeof(map), 1, fout); + fwrite(&steps, sizeof(steps), 1, fout); + fwrite(&gold, sizeof(gold), 1, fout); + + fclose(fout); +} +void LoadBinary() { + FILE* fin; + fin = fopen("C:\\Temp\\Lection12\\game_bin.bin", "rb"); + if (fin == NULL) { + printf(" "); + return; + } + int n, m; + fread(&n, sizeof(n), 1, fin); + fread(&m, sizeof(m), 1, fin); + fread(map, sizeof(map), 1, fin); + fread(&steps, sizeof(steps), 1, fin); + fread(&gold, sizeof(gold), 1, fin); + + fclose(fin); +} +void CreateWall() { + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + if (map[i][j] == 1) { + map[i][j - 1] = 2; + steps++; + } + } + } + +} +void CreateGold() { + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + if (map[i][j] == 1) { + map[i][j + 1] = 3; + steps++; + } + } + } + +} +void Right2() { + for (int i = 0; i < N; i++) { + for (int j = 1; j < M; j++) { + if (map[i][j] == 1) { + + if (map[i - 1][j] == 2) { + map[i - 1][j] = 0; + steps++; + } + } + } + } + +} +void Down2() { + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + if (map[i][j] == 1) { + if (map[i + 1][j] == 0) { + map[i + 1][j] = 3; + steps++; + } + } + } + } + +} +void Destroy() { + int i, j; + for (int j = M - 1; j >= 0; j--) { + for (int i = 0; i < N; i++) { + if (map[i][j] == 1) { + int a; + for (a = j + 1; a < M && map[i][a] == 2; a++) { + map[i][a] = 0; + } + break; + } + } + } + +} +void Destroy2() { + int i, j; + for (i = 0; i < N; i++) { + for (j = 0; j < M - 1; j++) { + if (map[i][j] == 1) { + if (map[i][j + 1] == 2) { + map[i][j + 1] = 0; + steps++; + } + if (map[i][j - 1] == 2) { + map[i][j - 1] = 0; + steps++; + } + if (map[i + 1][j + 1] == 2) { + map[i + 1][j + 1] = 0; + steps++; + } + if (map[i - 1][j - 1] == 2) { + map[i - 1][j - 1] = 0; + steps++; + } + if (map[i - 1][j] == 2) { + map[i - 1][j] = 0; + steps++; + } + if (map[i + 1][j] == 2) { + map[i + 1][j] = 0; + steps++; + } + if (map[i - 1][j + 1] == 2) { + map[i - 1][j + 1] = 0; + steps++; + } + if (map[i + 1][j - 1] == 2) { + map[i + 1][j - 1] = 0; + steps++; + } + j++; + } + } + } +} +LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +{ + switch (message) + { + case WM_COMMAND: + { + case WM_KEYDOWN: + switch (wParam) { + case VK_DOWN: + Down(); + InvalidateRect(hWnd, NULL, TRUE); + break; + case VK_LEFT: + Left(); + InvalidateRect(hWnd, NULL, TRUE); + break; + case VK_UP: + Up(); + InvalidateRect(hWnd, NULL, TRUE); + break; + case VK_RIGHT: + Right(); + InvalidateRect(hWnd, NULL, TRUE); + break; + case 0x53: //S + //Save(); + SaveBinary(); + break; + case 0x4c: //L + //Load(); + //LoadBinary(); + CreateWall(); + break; + case 0x52: //R + CreateGold(); + break; + case 0x55: //U + Right2(); + InvalidateRect(hWnd, NULL, TRUE); + break; + case 0x44: //D + Down2(); + InvalidateRect(hWnd, NULL, TRUE); + break; + case 0x5a: //Z + Destroy(); + InvalidateRect(hWnd, NULL, TRUE); + break; + case 0x41: //A + Destroy2(); + InvalidateRect(hWnd, NULL, TRUE); + break; + } break; + int wmId = LOWORD(wParam); + switch (wmId) + { + case IDM_ABOUT: + DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); + break; + case IDM_EXIT: + DestroyWindow(hWnd); + break; + default: + return DefWindowProc(hWnd, message, wParam, lParam); + } + } + break; + case WM_PAINT: + { + PAINTSTRUCT ps; + HDC hdc = BeginPaint(hWnd, &ps); + drawMap(hdc); + EndPaint(hWnd, &ps); + } + break; + case WM_DESTROY: + PostQuitMessage(0); + break; + default: + return DefWindowProc(hWnd, message, wParam, lParam); + } + return 0; +} + +// " ". +INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) +{ + UNREFERENCED_PARAMETER(lParam); + switch (message) + { + case WM_INITDIALOG: + return (INT_PTR)TRUE; + + case WM_COMMAND: + if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) + { + EndDialog(hDlg, LOWORD(wParam)); + return (INT_PTR)TRUE; + } + break; + } + return (INT_PTR)FALSE; +} +