Advertisement
Usow_Maxim

[C++] Визуальное представление массива и изменение его

Sep 24th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream> //cout cin
  3. #include <cstdlib> //atoi(*arr)
  4. #include <Windows.h> //Sleep()
  5. #include <conio.h> //_kbhit, _getch()
  6. #include <clocale> //setlocale()
  7. #include <ctime> //srand(time(0))
  8.  
  9. using namespace std;
  10.  
  11. class IChar {
  12.     char symbol = '#';
  13.     int value = rand() % 32000;
  14. public:
  15.     char getSymbol() {
  16.         return symbol;
  17.     }
  18.     void setSymbol(char sym) {
  19.         symbol = sym;
  20.     }
  21.     int getValue() {
  22.         return value;
  23.     }
  24.     void setValue(int val) {
  25.         value = val;
  26.     }
  27. };
  28.  
  29. int Write_int(bool anyNumber);
  30.  
  31. IChar** pixels;
  32. int sizeX, sizeY, x, y;
  33. bool onWrite;
  34. enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
  35. enum eSelect { NUN = 0, SYMBOL, VALUE };
  36. eDirection dir;
  37. eSelect sel;
  38.  
  39. void Setup() {
  40.     cout << "Введите размер Массива 2x2:\nSizeX: ";
  41.     sizeX = Write_int(false);
  42.     cout << "SizeY: ";
  43.     sizeY = Write_int(false);
  44.     srand(time(0));
  45.     pixels = new IChar*[sizeX];
  46.     for (int i = 0; i < sizeX; i++)
  47.         pixels[i] = new IChar[sizeY];
  48.     x, y = 0;
  49.     onWrite = false;
  50.     dir = STOP;
  51.     sel = NUN;
  52. }
  53.  
  54. void Draw() {
  55.     system("cls");
  56.     HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
  57.     for (int i = 0; i < sizeX; i++) {
  58.         for (int j = 0; j < sizeY; j++) {
  59.             if (i == x && j == y)
  60.                 SetConsoleTextAttribute(console, FOREGROUND_GREEN);
  61.             else
  62.                 SetConsoleTextAttribute(console, FOREGROUND_BLUE);
  63.             cout << pixels[i][j].getSymbol();
  64.         }
  65.         cout << endl;
  66.     }
  67.     SetConsoleTextAttribute(console, FOREGROUND_RED);
  68.     cout << "\nValue: " << pixels[x][y].getValue() << endl;
  69.  
  70.     cout << "WASD - Управление\nE - Change symbol\nF - Change value";
  71. }
  72.  
  73. void Input() {
  74.     if (_kbhit) {
  75.         switch (_getch()) {
  76.             case 'w':
  77.                 dir = UP;
  78.                 break;
  79.             case 'a':
  80.                 dir = LEFT;
  81.                 break;
  82.             case 'd':
  83.                 dir = RIGHT;
  84.                 break;
  85.             case 's':
  86.                 dir = DOWN;
  87.                 break;
  88.             case 'e':
  89.                 sel = SYMBOL;
  90.                 break;
  91.             case 'f':
  92.                 sel = VALUE;
  93.             default:
  94.                 break;
  95.         }
  96.     }
  97. }
  98.  
  99. void Logic() {
  100.     switch (dir) {
  101.         case UP:
  102.             x--;
  103.             break;
  104.         case LEFT:
  105.             y--;
  106.             break;
  107.         case RIGHT:
  108.             y++;
  109.             break;
  110.         case DOWN:
  111.             x++;
  112.             break;
  113.         default:
  114.             break;
  115.     }
  116.     switch (sel) {
  117.         case SYMBOL:
  118.             pixels[x][y].setSymbol(_getch());
  119.             break;
  120.         case VALUE:
  121.             pixels[x][y].setValue(Write_int(true));
  122.             break;
  123.         default:
  124.             break;
  125.     }
  126.     dir = STOP;
  127.     sel = NUN;
  128.     if (x < 0) x = sizeX - 1;
  129.     if (x >= sizeX) x = 0;
  130.     if (y < 0) y = sizeY - 1;
  131.     if (y >= sizeY) y = 0;
  132. }
  133.  
  134. int main()
  135. {
  136.     setlocale(LC_ALL, "Rus");
  137.     Setup();
  138.     while (true) {
  139.         Draw();
  140.         Input();
  141.         Logic();
  142.         Sleep(10);
  143.     }
  144.  
  145.     for (int i = 0; i < sizeX; i++)
  146.         delete[] pixels[i];
  147.     return 0;
  148. }
  149.  
  150. int Write_int(bool anyNumber) {
  151.     char array[10];
  152.     while (true) {
  153.         cin >> array;
  154.         if ((atoi(array) && atoi(array) > 0 && !anyNumber) || (atoi(array) && anyNumber))
  155.             break;
  156.         else {
  157.             if (anyNumber)
  158.                 printf("Введите значение n: ");
  159.             else
  160.                 printf("Введите значение n > 0: ");
  161.         }
  162.     }
  163.     return atoi(array);
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement