Alan468

Pietnastka WORKING

Nov 29th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Windows.h>
  2. #include <cstdlib>
  3. #include <cstdio>
  4. #include <ctime>
  5. #include <cmath>
  6.  
  7. #define WND_WIDTH 1000
  8. #define WND_HEIGHT 800
  9.  
  10. #define FIELDS 4
  11.  
  12. #define BTN_SIZE 60
  13. #define GAME_START_X 20
  14. #define GAME_START_Y 20
  15.  
  16. struct field {
  17.     HWND Button;
  18.     int Value;
  19.     int PosX, PosY;
  20.     int Width, Height;
  21. };
  22.  
  23. HWND hwnd ,MovesInfo;
  24. HINSTANCE hInstance;
  25. field Field[(FIELDS*FIELDS)];
  26. int NumberOfMoves = 0;
  27.  
  28. void DrawGameField(int i) {
  29.     char buff[255];
  30.     DestroyWindow(Field[i].Button);
  31.     sprintf_s(buff, "%d", Field[i].Value);
  32.     Field[i] = {
  33.         CreateWindowEx(NULL, "Button", ((Field[i].Value != 0) ? TEXT(buff) : TEXT("")), WS_CHILD | WS_VISIBLE,Field[i].PosX , Field[i].PosY, BTN_SIZE, BTN_SIZE, hwnd, (HMENU)(Field[i].Value), hInstance, NULL),
  34.         Field[i].Value,Field[i].PosX , Field[i].PosY,BTN_SIZE,BTN_SIZE
  35.     };
  36. }
  37.  
  38. bool IsInArray(int Value) {
  39.     for (int i = 0; i < ((FIELDS*FIELDS)); i++) {
  40.         if (Field[i].Value == Value) return true;
  41.     }
  42.     return false;
  43. }
  44.  
  45. void InitGame() {
  46.     int PositionX = GAME_START_X, PositionY = GAME_START_Y;
  47.     int NewValue;
  48.  
  49.     for (int i = 0; i < ((FIELDS*FIELDS) - 1);) {
  50.         NewValue = (rand() % (FIELDS*FIELDS));
  51.  
  52.         if (!IsInArray(NewValue)) {
  53.             Field[i].Value = NewValue;
  54.             Field[i].PosX = PositionX;
  55.             Field[i].PosY = PositionY;
  56.  
  57.             DrawGameField(i);
  58.  
  59.             if (PositionX == (GAME_START_X + (BTN_SIZE * (FIELDS - 1)))) {
  60.                 PositionX = GAME_START_Y;
  61.                 PositionY += BTN_SIZE;
  62.             }
  63.             else {
  64.                 PositionX += BTN_SIZE;
  65.             }
  66.  
  67.             i++;
  68.         }
  69.     }
  70.     Field[(FIELDS*FIELDS) - 1].Value = 0;
  71.     Field[(FIELDS*FIELDS) - 1].PosX = PositionX;
  72.     Field[(FIELDS*FIELDS) - 1].PosY = PositionY;
  73.  
  74.     DrawGameField((FIELDS*FIELDS) - 1);
  75. }
  76.  
  77. void MakeMove() {
  78.     char buff[255];
  79.     sprintf_s(buff, "Ruchy: %d", ++NumberOfMoves);
  80.     SetWindowText(MovesInfo, buff);
  81.     for (int i = 0; i < ((FIELDS*FIELDS)); i++) {
  82.         DrawGameField(i);
  83.     }
  84. }
  85.  
  86. void SwapFields(field &F1, field &F2) {
  87.     int FieldBuff = F1.Value;
  88.     F1.Value = F2.Value;
  89.     F2.Value = FieldBuff;
  90.     MakeMove();
  91. }
  92.  
  93. bool CheckWin() {
  94.     if (Field[0].Value == 1) {
  95.         for (int i = 0; i < (FIELDS*FIELDS) - 2; i++) {
  96.             if (Field[i].Value + 1 != Field[i + 1].Value) return false;
  97.         }
  98.         if (MessageBox(NULL, TEXT("WIN"), TEXT("WIN"), MB_YESNO) == IDYES) {
  99.             for (int i = 0; i < ((FIELDS*FIELDS)); i++) {
  100.                 Field[i].Value = (FIELDS*FIELDS) + 10;
  101.             }
  102.             MakeMove();
  103.             InitGame();
  104.         }
  105.         return true;
  106.     }
  107.     return false;
  108. }
  109.  
  110. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  111.     switch (msg) {
  112.     case WM_COMMAND:
  113.         for (int i = 0; i < (FIELDS*FIELDS); i++) {
  114.             if (wParam == Field[i].Value) {
  115.  
  116.                 if (Field[i].Value != 0) {
  117.                     if (i - (FIELDS) >= 0 && Field[i - (FIELDS)].Value == 0) { // Pole nad puste
  118.                         SwapFields(Field[i], Field[i - (FIELDS)]);
  119.                     }
  120.                     else if (i + (FIELDS) < (FIELDS*FIELDS) && Field[i + (FIELDS)].Value == 0) { // Pole pod puste
  121.                         SwapFields(Field[i], Field[i + (FIELDS)]);
  122.                     }
  123.                     else if (i  % FIELDS != 0 && i - 1 >= 0 && Field[i - 1].Value == 0) { // Pole na lewo puste
  124.                         SwapFields(Field[i], Field[i - 1]);
  125.                     }
  126.                     else if ((i + 1) % FIELDS != 0 && i + 1 < (FIELDS*FIELDS) && Field[i + 1].Value == 0) { // Pole na prawo puste
  127.                         SwapFields(Field[i], Field[i + 1]);
  128.                     }
  129.                     break;
  130.                 }
  131.                 CheckWin();
  132.             }
  133.         }
  134.         break;
  135.     case WM_CLOSE: DestroyWindow(hwnd); break;
  136.     case WM_DESTROY: PostQuitMessage(0); break;
  137.     default: return DefWindowProc(hwnd, msg, wParam, lParam);
  138.     }
  139.     return 0;
  140. }
  141.  
  142. int WINAPI WinMain(HINSTANCE HInstance, HINSTANCE hPrevInstance, LPSTR ilCmdLine, int nCmdShow) {
  143.     srand(time(NULL));
  144.  
  145.     WNDCLASSEX window;
  146.     MSG msg;
  147.     TCHAR Class_Name[] = TEXT("OKNO_TEST"), Title[] = TEXT("Tytul");
  148.     window.cbClsExtra = NULL;
  149.     window.cbSize = sizeof(WNDCLASSEX);
  150.     window.cbWndExtra = NULL;
  151.     window.hbrBackground = (HBRUSH)(COLOR_WINDOW );
  152.     window.hCursor = LoadCursor(NULL, IDC_ARROW);
  153.     window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  154.     window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  155.     window.hInstance = hInstance;
  156.     window.lpfnWndProc = WndProc;
  157.     window.lpszClassName = Class_Name;
  158.     window.lpszMenuName = 0;
  159.     window.style = CS_VREDRAW | CS_HREDRAW;
  160.  
  161.     RegisterClassEx(&window);
  162.     hInstance - HInstance;
  163.     hwnd = CreateWindowEx(WS_EX_WINDOWEDGE, Class_Name, Title, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, WND_WIDTH, WND_HEIGHT, NULL, NULL, hInstance, NULL);
  164.  
  165.     InitGame();
  166.  
  167.     MovesInfo = CreateWindowEx(NULL, "STATIC", TEXT("Ruchy: 0"), WS_CHILD | WS_VISIBLE, GAME_START_X+(FIELDS*BTN_SIZE)+10, GAME_START_Y, 100, 50, hwnd, NULL, hInstance, NULL);
  168.  
  169.     ShowWindow(hwnd, nCmdShow);
  170.     UpdateWindow(hwnd);
  171.  
  172.     while (GetMessage(&msg, NULL, 0, 0)) {
  173.         TranslateMessage(&msg);
  174.         DispatchMessage(&msg);
  175.     }
  176.  
  177.     UnregisterClass(Class_Name, hInstance);
  178.     return (int)msg.wParam;
  179. }
Add Comment
Please, Sign In to add comment