Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <stdlib.h>
  4. #include <chrono>
  5. #include <ctime>
  6. #include <thread>
  7.  
  8. using namespace std;
  9.  
  10. void SetTheCursorOnXY(int x, int y);
  11. void ShiftToRight(char* arr, int stepsToMoveTheCharToRight, int lenght);
  12. void ShuffleArray(char* arr, int lenght);
  13. void ShowConsoleCursor(bool showFlag);
  14. void ShuffleSymbols(char* arr, int length);
  15.  
  16.  
  17. int GetRandomNumInRange(int min, int max);
  18.  
  19. int FindFirstSymbol(char* arr, int length);
  20.  
  21. char** CreateMatrix(int, int, int);
  22.  
  23.  
  24. const int MATRIX_ROWS = 80;
  25. const int MATRIX_COLS = 25;
  26.  
  27. int main()
  28. {
  29. //HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  30.  
  31. //Removing the console cursor
  32. //ShowConsoleCursor(false);
  33.  
  34. //Making the text green
  35. //SetConsoleTextAttribute(hConsole, 10);
  36.  
  37. srand(time(NULL));
  38.  
  39. //Creating matrix
  40. char** matrix = CreateMatrix(MATRIX_ROWS, MATRIX_COLS, MATRIX_COLS * 3);
  41.  
  42. //Shuffling the amtrix
  43. for (int i = 0; i < MATRIX_ROWS; i++)
  44. {
  45. ShuffleArray(matrix[i], 75);
  46. }
  47.  
  48. //Infinity printing
  49. while (true) {
  50.  
  51. for (int row = 0; row < MATRIX_ROWS; row++)
  52. {
  53. for (int col = 0; col < MATRIX_COLS; col++)
  54. {
  55. //Set the cursor on specific positon in the console
  56. SetTheCursorOnXY(row, col);
  57. //Printing the symbol on that position
  58. cout << matrix[row][col];
  59. }
  60. //Shufling the symbols
  61. ShuffleSymbols(matrix[row], 75);
  62.  
  63. //Shifting the symbols with 1 position to make the lines moving
  64. ShiftToRight(matrix[row], 1, 75);
  65. }
  66. //freezing the console for a while after printing the full matrix
  67. //Sleep(15);
  68. }
  69.  
  70. return 0;
  71. }
  72.  
  73. void SetTheCursorOnXY(int x, int y) {
  74. COORD coord;
  75. coord.X = x;
  76. coord.Y = y;
  77. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  78. }
  79.  
  80. char** CreateMatrix(int rows, int colSizeWithSymbols, int colWholeSize) {
  81.  
  82. char** matrix = new char* [rows];
  83. //Creating matrix with only spaces in it
  84. for (int row = 0; row < rows; row++)
  85. {
  86. matrix[row] = new char[colWholeSize + 1];
  87. for (int col = 0; col < colWholeSize; col++)
  88. {
  89. matrix[row][col] = ' ';
  90. }
  91. matrix[row][colWholeSize] = '\0';
  92. }
  93.  
  94. //set uping the symbol lines somewhere in the row
  95. for (int row = 0; row < rows; row++)
  96. {
  97. //random num to set up the text somewhere in the row.
  98. int howManyTimesToMoveTheTextToRight = rand() % 2;
  99.  
  100. for (int col = 0; col < colSizeWithSymbols; col++)
  101. {
  102. //Getting random symbol in specific range.
  103. int randSymbol = GetRandomNumInRange(33, 127);
  104. //Depending on the random num from above we are multiplying the col with it.
  105. matrix[row][col + (howManyTimesToMoveTheTextToRight * colSizeWithSymbols)] = randSymbol;
  106. }
  107.  
  108. }
  109.  
  110. return matrix;
  111. }
  112.  
  113. void ShuffleArray(char* arr, int lenght) {
  114. //Random shifting to shuffle the matrix.
  115. int randHowManyTimesToShiftIt = rand() % 30;
  116.  
  117. ShiftToRight(arr, randHowManyTimesToShiftIt, lenght);
  118. }
  119.  
  120. void ShuffleSymbols(char* arr, int length) {
  121. //Get the first symbol of the text
  122. int startPoint = FindFirstSymbol(arr, length);
  123.  
  124. //Every symbol is getting the value of his neighbour
  125. for (int i = startPoint + MATRIX_COLS - 1; i >= startPoint; i--)
  126. {
  127. arr[i % length] = arr[(i - 1) % length];
  128. }
  129.  
  130. //Random symbol for the last element.
  131. int randSymbol = GetRandomNumInRange(33,127);
  132.  
  133. arr[startPoint % length ] = randSymbol;
  134. }
  135.  
  136. int FindFirstSymbol(char* arr, int length) {
  137. int index = 0;
  138.  
  139. for (int i = 0; i < length; i++)
  140. {
  141. if (i == 0 && arr[i] != ' ') {
  142. i++;
  143. while (arr[i] != ' ')
  144. i++;
  145. }
  146.  
  147. if (arr[i] != ' ') {
  148. index = i;
  149. break;
  150. }
  151. }
  152.  
  153. return index;
  154. }
  155.  
  156. void ShiftToRight(char* arr, int stepsToMoveTheCharToRight, int length) {
  157.  
  158. char* newText = new char[length];
  159.  
  160. for (int i = 0; i < length; i++)
  161. {
  162. int newPosition = (i + stepsToMoveTheCharToRight) % (length);
  163. newText[newPosition] = arr[i];
  164. }
  165.  
  166. for (int i = 0; i < length; i++)
  167. {
  168. arr[i] = newText[i];
  169. }
  170.  
  171. delete[] newText;
  172. }
  173.  
  174. void ShowConsoleCursor(bool showFlag)
  175. {
  176. HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
  177.  
  178. CONSOLE_CURSOR_INFO cursorInfo;
  179.  
  180. GetConsoleCursorInfo(out, &cursorInfo);
  181. cursorInfo.bVisible = showFlag; // set the cursor visibility
  182. SetConsoleCursorInfo(out, &cursorInfo);
  183. }
  184.  
  185. int GetRandomNumInRange(int min, int max) {
  186. int randNum = rand() % max;
  187.  
  188. if (randNum < min) {
  189. randNum = min;
  190. }
  191.  
  192. return randNum;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement