diff --git a/lab16/lab16.sln b/lab16/lab16.sln new file mode 100644 index 0000000..698b756 --- /dev/null +++ b/lab16/lab16.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34616.47 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lab16", "lab16\lab16.vcxproj", "{F4C0ED61-6755-488D-A984-0D703472FF53}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F4C0ED61-6755-488D-A984-0D703472FF53}.Debug|x64.ActiveCfg = Debug|x64 + {F4C0ED61-6755-488D-A984-0D703472FF53}.Debug|x64.Build.0 = Debug|x64 + {F4C0ED61-6755-488D-A984-0D703472FF53}.Debug|x86.ActiveCfg = Debug|Win32 + {F4C0ED61-6755-488D-A984-0D703472FF53}.Debug|x86.Build.0 = Debug|Win32 + {F4C0ED61-6755-488D-A984-0D703472FF53}.Release|x64.ActiveCfg = Release|x64 + {F4C0ED61-6755-488D-A984-0D703472FF53}.Release|x64.Build.0 = Release|x64 + {F4C0ED61-6755-488D-A984-0D703472FF53}.Release|x86.ActiveCfg = Release|Win32 + {F4C0ED61-6755-488D-A984-0D703472FF53}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {921002F2-DB84-45A9-822A-8CFF87DF7DB4} + EndGlobalSection +EndGlobal diff --git a/lab16/lab16/Resource.h b/lab16/lab16/Resource.h new file mode 100644 index 0000000..dfa70d4 --- /dev/null +++ b/lab16/lab16/Resource.h @@ -0,0 +1,30 @@ +//{{NO_DEPENDENCIES}} +// Включаемый файл, созданный в Microsoft Visual C++. +// Используется lab16.rc + +#define IDS_APP_TITLE 103 + +#define IDR_MAINFRAME 128 +#define IDD_LAB16_DIALOG 102 +#define IDD_ABOUTBOX 103 +#define IDM_ABOUT 104 +#define IDM_EXIT 105 +#define IDI_LAB16 107 +#define IDI_SMALL 108 +#define IDC_LAB16 109 +#define IDC_MYICON 2 +#ifndef IDC_STATIC +#define IDC_STATIC -1 +#endif +// Следующие стандартные значения для новых объектов +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS + +#define _APS_NO_MFC 130 +#define _APS_NEXT_RESOURCE_VALUE 129 +#define _APS_NEXT_COMMAND_VALUE 32771 +#define _APS_NEXT_CONTROL_VALUE 1000 +#define _APS_NEXT_SYMED_VALUE 110 +#endif +#endif diff --git a/lab16/lab16/framework.h b/lab16/lab16/framework.h new file mode 100644 index 0000000..a8f6dc8 --- /dev/null +++ b/lab16/lab16/framework.h @@ -0,0 +1,15 @@ +// header.h: включаемый файл для стандартных системных включаемых файлов +// или включаемые файлы для конкретного проекта +// + +#pragma once + +#include "targetver.h" +#define WIN32_LEAN_AND_MEAN // Исключите редко используемые компоненты из заголовков Windows +// Файлы заголовков Windows +#include +// Файлы заголовков среды выполнения C +#include +#include +#include +#include diff --git a/lab16/lab16/lab16.cpp b/lab16/lab16/lab16.cpp new file mode 100644 index 0000000..bcafc2e --- /dev/null +++ b/lab16/lab16/lab16.cpp @@ -0,0 +1,534 @@ +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include +#include "framework.h" +#include "lab16.h" +int steps = 0; +int gold = 0; +#define MAX_LOADSTRING 100 +#define N 10 +#define M 15 +#define WIDTH 80 +#define HEIGHT 50 + + +// Глобальные переменные: +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) +{ + 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; +} + + + +// +// ФУНКЦИЯ: 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 }; + 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); + + 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]); +} + +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("e:\\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("e:\\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 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++; + } + } + } +} +void doMidasHand(int i, int j) { + if (map[i][j] == 2) { + map[i][j] = 3; + if (i > 0) doMidasHand(i - 1, j); + if (i < N - 1) doMidasHand(i + 1, j); + if (i > 0) doMidasHand(i, j - 1); + if (i < M - 1) doMidasHand(i, j + 1); + } +} +void midasHandToRight() { + for (int i = 0; i < N; ++i) { + for (int j = 0; j < M - 1; ++j) { + if (map[i][j] == 3 && map[i][j + 1] == 2) { + doMidasHand(i, j + 1); + } + } + } +} +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(); + break; + case 0x4c: //L + Load(); + break; + case 0x46: //F + 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; + case 0x4d: //M + midasHandToRight(); + 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; +} diff --git a/lab16/lab16/lab16.h b/lab16/lab16/lab16.h new file mode 100644 index 0000000..d00d47e --- /dev/null +++ b/lab16/lab16/lab16.h @@ -0,0 +1,3 @@ +#pragma once + +#include "resource.h" diff --git a/lab16/lab16/lab16.ico b/lab16/lab16/lab16.ico new file mode 100644 index 0000000..b3ec03b Binary files /dev/null and b/lab16/lab16/lab16.ico differ diff --git a/lab16/lab16/lab16.rc b/lab16/lab16/lab16.rc new file mode 100644 index 0000000..fe5ec63 Binary files /dev/null and b/lab16/lab16/lab16.rc differ diff --git a/lab16/lab16/lab16.vcxproj b/lab16/lab16/lab16.vcxproj new file mode 100644 index 0000000..b16c160 --- /dev/null +++ b/lab16/lab16/lab16.vcxproj @@ -0,0 +1,148 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {f4c0ed61-6755-488d-a984-0d703472ff53} + lab16 + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) + true + + + Windows + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + true + + + Windows + true + true + true + + + + + Level3 + true + _DEBUG;_WINDOWS;%(PreprocessorDefinitions) + true + + + Windows + true + + + + + Level3 + true + true + true + NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + true + + + Windows + true + true + true + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lab16/lab16/lab16.vcxproj.filters b/lab16/lab16/lab16.vcxproj.filters new file mode 100644 index 0000000..2f292a6 --- /dev/null +++ b/lab16/lab16/lab16.vcxproj.filters @@ -0,0 +1,49 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + + + Исходные файлы + + + + + Файлы ресурсов + + + + + Файлы ресурсов + + + Файлы ресурсов + + + \ No newline at end of file diff --git a/lab16/lab16/small.ico b/lab16/lab16/small.ico new file mode 100644 index 0000000..b3ec03b Binary files /dev/null and b/lab16/lab16/small.ico differ diff --git a/lab16/lab16/targetver.h b/lab16/lab16/targetver.h new file mode 100644 index 0000000..d90003d --- /dev/null +++ b/lab16/lab16/targetver.h @@ -0,0 +1,6 @@ +#pragma once + +// // При включении SDKDDKVer.h будет задана самая новая из доступных платформ Windows. +// Если вы планируете сборку приложения для предыдущей версии платформы Windows, включите WinSDKVer.h и +// задайте желаемую платформу в макросе _WIN32_WINNT, прежде чем включать SDKDDKVer.h. +#include