Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <conio.h>
- #include <windows.h>
- void draw(char board[], short checked[], int size, int size_x);
- int set_cursor(int, int);
- int prepare_console();
- int main() {
- int size_x = 10, size_y = 10, size = size_x * size_y;
- int miny = 30;
- int pos_x, pos_y, i, j;
- char board[100];
- short checked[100];
- int klik = 0;
- CONSOLE_CURSOR_INFO cursorInfo = { 100,0 };
- SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
- prepare_console();
- system("cls");
- for (i = 0; i < size; i++) {
- board[i] = '0';
- checked[i] = 0;
- }
- srand(time(0));
- for (i = 0; i < miny; i++) {
- j = rand() % size + 1;
- board[j] = '*';
- }
- for (i = 0; i < size; i++) {
- if (board[i] == '*') {
- board[i - 1] += 1;
- board[i + 1] += 1;
- board[i - size_x - 1] += 1;
- board[i - size_x] += 1;
- board[i - size_x + 1] += 1;
- board[i + size_x - 1] += 1;
- board[i + size_x] += 1;
- board[i + size_x + 1] += 1;
- }
- }
- pos_x = 1; pos_y = 1;
- while (1) {
- system("cls");
- draw(board, checked, size, size_x);
- while (1) {
- if (_kbhit()) {
- klik = _getch();
- }
- else continue;
- if (klik == 'q') break;
- else if (klik == 'w' && pos_y != 0) {
- set_cursor(pos_x, pos_y);
- set_color(0x0F);
- if (checked[pos_x + pos_y * size_x] == 0)
- printf("#");
- else
- printf("%c", board[pos_x + pos_y * size_x]);
- set_color(250);
- set_cursor(pos_x, --pos_y);
- if (checked[pos_x + pos_y * size_x] == 0)
- printf("#");
- else
- printf("%c", board[pos_x + pos_y * size_x]);
- }
- else if (klik == 's' && pos_y != size / size_x - 1) {
- set_cursor(pos_x, pos_y);
- set_color(0x0F);
- if (checked[pos_x + pos_y * size_x] == 0)
- printf("#");
- else
- printf("%c", board[pos_x + pos_y * size_x]);
- set_color(250);
- set_cursor(pos_x, ++pos_y);
- if (checked[pos_x + pos_y * size_x] == 0)
- printf("#");
- else
- printf("%c", board[pos_x + pos_y * size_x]);
- }
- else if (klik == 'a' && pos_x != 0) {
- set_cursor(pos_x, pos_y);
- set_color(0x0F);
- if (checked[pos_x + pos_y * size_x] == 0)
- printf("#");
- else
- printf("%c", board[pos_x + pos_y * size_x]);
- set_color(250);
- set_cursor(--pos_x, pos_y);
- if (checked[pos_x + pos_y * size_x] == 0)
- printf("#");
- else
- printf("%c", board[pos_x + pos_y * size_x]);
- }
- else if (klik == 'd' && pos_x != size_x - 1) {
- set_cursor(pos_x, pos_y);
- set_color(0x0F);
- if (checked[pos_x + pos_y * size_x] == 0)
- printf("#");
- else
- printf("%c", board[pos_x + pos_y * size_x]);
- set_color(250);
- set_cursor(++pos_x, pos_y);
- if (checked[pos_x + pos_y * size_x] == 0)
- printf("#");
- else
- printf("%c", board[pos_x + pos_y * size_x]);
- }
- else if (klik == 'f') {
- }
- }
- if (klik == 'q') return 1;
- }
- }
- void draw(char board[], short checked[], int size, int size_x) {
- int i;
- for (i = 0; i < size; i++) {
- if (i % (size_x) == 0 && i != 0) printf("\n");
- if (checked[i] == 0) {
- printf("#");
- }
- else {
- printf("%c", board[i]);
- }
- }
- }
- int set_cursor(int x, int y) {
- COORD coord = { x,y };
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
- return 1;
- }
- int set_color(int color) {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
- return 1;
- }
- int prepare_console()
- {
- int WindowX = 30, WindowY = 30;
- SetConsoleTitle("Saper");
- HANDLE outHnl = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_FONT_INFOEX fontInf;
- GetCurrentConsoleFontEx(outHnl, TRUE, &fontInf);
- fontInf.cbSize = sizeof(fontInf);
- fontInf.nFont = 0;
- fontInf.FontFamily = FF_DONTCARE;
- fontInf.FontWeight = FW_NORMAL;
- fontInf.dwFontSize.X = 16;
- fontInf.dwFontSize.Y = 16;
- wcscpy_s(fontInf.FaceName, 16,"Terminal");
- SetCurrentConsoleFontEx(outHnl, TRUE, &fontInf);
- COORD sizeB = { WindowX,WindowY };
- SetConsoleScreenBufferSize(outHnl, sizeB);
- SMALL_RECT sizeW = { 0, 0, WindowX - 1, WindowY - 1 };
- SetConsoleWindowInfo(outHnl, 0, &sizeW);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement