Advertisement
ElfikCo

Saper

May 16th, 2019
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <conio.h>
  5. #include <windows.h>
  6.  
  7. void draw(char board[], short checked[], int size, int size_x);
  8. int set_cursor(int, int);
  9. int prepare_console();
  10.  
  11. int main() {
  12. int size_x = 10, size_y = 10, size = size_x * size_y;
  13. int miny = 30;
  14. int pos_x, pos_y, i, j;
  15. char board[100];
  16. short checked[100];
  17. int klik = 0;
  18. CONSOLE_CURSOR_INFO cursorInfo = { 100,0 };
  19. SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
  20.  
  21.  
  22. prepare_console();
  23.  
  24. system("cls");
  25.  
  26. for (i = 0; i < size; i++) {
  27. board[i] = '0';
  28. checked[i] = 0;
  29. }
  30.  
  31. srand(time(0));
  32. for (i = 0; i < miny; i++) {
  33. j = rand() % size + 1;
  34. board[j] = '*';
  35. }
  36.  
  37. for (i = 0; i < size; i++) {
  38. if (board[i] == '*') {
  39. board[i - 1] += 1;
  40. board[i + 1] += 1;
  41. board[i - size_x - 1] += 1;
  42. board[i - size_x] += 1;
  43. board[i - size_x + 1] += 1;
  44. board[i + size_x - 1] += 1;
  45. board[i + size_x] += 1;
  46. board[i + size_x + 1] += 1;
  47. }
  48. }
  49.  
  50. pos_x = 1; pos_y = 1;
  51.  
  52. while (1) {
  53. system("cls");
  54. draw(board, checked, size, size_x);
  55. while (1) {
  56. if (_kbhit()) {
  57. klik = _getch();
  58. }
  59. else continue;
  60. if (klik == 'q') break;
  61. else if (klik == 'w' && pos_y != 0) {
  62. set_cursor(pos_x, pos_y);
  63. set_color(0x0F);
  64. if (checked[pos_x + pos_y * size_x] == 0)
  65. printf("#");
  66. else
  67. printf("%c", board[pos_x + pos_y * size_x]);
  68. set_color(250);
  69. set_cursor(pos_x, --pos_y);
  70. if (checked[pos_x + pos_y * size_x] == 0)
  71. printf("#");
  72. else
  73. printf("%c", board[pos_x + pos_y * size_x]);
  74. }
  75. else if (klik == 's' && pos_y != size / size_x - 1) {
  76. set_cursor(pos_x, pos_y);
  77. set_color(0x0F);
  78. if (checked[pos_x + pos_y * size_x] == 0)
  79. printf("#");
  80. else
  81. printf("%c", board[pos_x + pos_y * size_x]);
  82. set_color(250);
  83. set_cursor(pos_x, ++pos_y);
  84. if (checked[pos_x + pos_y * size_x] == 0)
  85. printf("#");
  86. else
  87. printf("%c", board[pos_x + pos_y * size_x]);
  88. }
  89. else if (klik == 'a' && pos_x != 0) {
  90. set_cursor(pos_x, pos_y);
  91. set_color(0x0F);
  92. if (checked[pos_x + pos_y * size_x] == 0)
  93. printf("#");
  94. else
  95. printf("%c", board[pos_x + pos_y * size_x]);
  96. set_color(250);
  97. set_cursor(--pos_x, pos_y);
  98. if (checked[pos_x + pos_y * size_x] == 0)
  99. printf("#");
  100. else
  101. printf("%c", board[pos_x + pos_y * size_x]);
  102. }
  103. else if (klik == 'd' && pos_x != size_x - 1) {
  104. set_cursor(pos_x, pos_y);
  105. set_color(0x0F);
  106. if (checked[pos_x + pos_y * size_x] == 0)
  107. printf("#");
  108. else
  109. printf("%c", board[pos_x + pos_y * size_x]);
  110. set_color(250);
  111. set_cursor(++pos_x, pos_y);
  112. if (checked[pos_x + pos_y * size_x] == 0)
  113. printf("#");
  114. else
  115. printf("%c", board[pos_x + pos_y * size_x]);
  116. }
  117. else if (klik == 'f') {
  118.  
  119. }
  120. }
  121. if (klik == 'q') return 1;
  122. }
  123. }
  124.  
  125. void draw(char board[], short checked[], int size, int size_x) {
  126. int i;
  127. for (i = 0; i < size; i++) {
  128. if (i % (size_x) == 0 && i != 0) printf("\n");
  129. if (checked[i] == 0) {
  130. printf("#");
  131. }
  132. else {
  133. printf("%c", board[i]);
  134. }
  135. }
  136. }
  137.  
  138. int set_cursor(int x, int y) {
  139. COORD coord = { x,y };
  140. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  141. return 1;
  142. }
  143.  
  144. int set_color(int color) {
  145. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
  146. return 1;
  147. }
  148.  
  149. int prepare_console()
  150. {
  151. int WindowX = 30, WindowY = 30;
  152. SetConsoleTitle("Saper");
  153. HANDLE outHnl = GetStdHandle(STD_OUTPUT_HANDLE);
  154.  
  155. CONSOLE_FONT_INFOEX fontInf;
  156. GetCurrentConsoleFontEx(outHnl, TRUE, &fontInf);
  157. fontInf.cbSize = sizeof(fontInf);
  158. fontInf.nFont = 0;
  159. fontInf.FontFamily = FF_DONTCARE;
  160. fontInf.FontWeight = FW_NORMAL;
  161. fontInf.dwFontSize.X = 16;
  162. fontInf.dwFontSize.Y = 16;
  163. wcscpy_s(fontInf.FaceName, 16,"Terminal");
  164. SetCurrentConsoleFontEx(outHnl, TRUE, &fontInf);
  165.  
  166.  
  167. COORD sizeB = { WindowX,WindowY };
  168. SetConsoleScreenBufferSize(outHnl, sizeB);
  169.  
  170. SMALL_RECT sizeW = { 0, 0, WindowX - 1, WindowY - 1 };
  171. SetConsoleWindowInfo(outHnl, 0, &sizeW);
  172.  
  173. return 1;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement