Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <stdlib.h>
- #include <chrono>
- #include <ctime>
- #include <thread>
- using namespace std;
- void SetTheCursorOnXY(int x, int y);
- void ShiftToRight(char* arr, int stepsToMoveTheCharToRight, int lenght);
- void ShuffleArray(char* arr, int lenght);
- void ShowConsoleCursor(bool showFlag);
- void ShuffleSymbols(char* arr, int length);
- int GetRandomNumInRange(int min, int max);
- int FindFirstSymbol(char* arr, int length);
- char** CreateMatrix(int, int, int);
- const int MATRIX_ROWS = 80;
- const int MATRIX_COLS = 25;
- int main()
- {
- //HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- //Removing the console cursor
- //ShowConsoleCursor(false);
- //Making the text green
- //SetConsoleTextAttribute(hConsole, 10);
- srand(time(NULL));
- //Creating matrix
- char** matrix = CreateMatrix(MATRIX_ROWS, MATRIX_COLS, MATRIX_COLS * 3);
- //Shuffling the amtrix
- for (int i = 0; i < MATRIX_ROWS; i++)
- {
- ShuffleArray(matrix[i], 75);
- }
- //Infinity printing
- while (true) {
- for (int row = 0; row < MATRIX_ROWS; row++)
- {
- for (int col = 0; col < MATRIX_COLS; col++)
- {
- //Set the cursor on specific positon in the console
- SetTheCursorOnXY(row, col);
- //Printing the symbol on that position
- cout << matrix[row][col];
- }
- //Shufling the symbols
- ShuffleSymbols(matrix[row], 75);
- //Shifting the symbols with 1 position to make the lines moving
- ShiftToRight(matrix[row], 1, 75);
- }
- //freezing the console for a while after printing the full matrix
- //Sleep(15);
- }
- return 0;
- }
- void SetTheCursorOnXY(int x, int y) {
- COORD coord;
- coord.X = x;
- coord.Y = y;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
- }
- char** CreateMatrix(int rows, int colSizeWithSymbols, int colWholeSize) {
- char** matrix = new char* [rows];
- //Creating matrix with only spaces in it
- for (int row = 0; row < rows; row++)
- {
- matrix[row] = new char[colWholeSize + 1];
- for (int col = 0; col < colWholeSize; col++)
- {
- matrix[row][col] = ' ';
- }
- matrix[row][colWholeSize] = '\0';
- }
- //set uping the symbol lines somewhere in the row
- for (int row = 0; row < rows; row++)
- {
- //random num to set up the text somewhere in the row.
- int howManyTimesToMoveTheTextToRight = rand() % 2;
- for (int col = 0; col < colSizeWithSymbols; col++)
- {
- //Getting random symbol in specific range.
- int randSymbol = GetRandomNumInRange(33, 127);
- //Depending on the random num from above we are multiplying the col with it.
- matrix[row][col + (howManyTimesToMoveTheTextToRight * colSizeWithSymbols)] = randSymbol;
- }
- }
- return matrix;
- }
- void ShuffleArray(char* arr, int lenght) {
- //Random shifting to shuffle the matrix.
- int randHowManyTimesToShiftIt = rand() % 30;
- ShiftToRight(arr, randHowManyTimesToShiftIt, lenght);
- }
- void ShuffleSymbols(char* arr, int length) {
- //Get the first symbol of the text
- int startPoint = FindFirstSymbol(arr, length);
- //Every symbol is getting the value of his neighbour
- for (int i = startPoint + MATRIX_COLS - 1; i >= startPoint; i--)
- {
- arr[i % length] = arr[(i - 1) % length];
- }
- //Random symbol for the last element.
- int randSymbol = GetRandomNumInRange(33,127);
- arr[startPoint % length ] = randSymbol;
- }
- int FindFirstSymbol(char* arr, int length) {
- int index = 0;
- for (int i = 0; i < length; i++)
- {
- if (i == 0 && arr[i] != ' ') {
- i++;
- while (arr[i] != ' ')
- i++;
- }
- if (arr[i] != ' ') {
- index = i;
- break;
- }
- }
- return index;
- }
- void ShiftToRight(char* arr, int stepsToMoveTheCharToRight, int length) {
- char* newText = new char[length];
- for (int i = 0; i < length; i++)
- {
- int newPosition = (i + stepsToMoveTheCharToRight) % (length);
- newText[newPosition] = arr[i];
- }
- for (int i = 0; i < length; i++)
- {
- arr[i] = newText[i];
- }
- delete[] newText;
- }
- void ShowConsoleCursor(bool showFlag)
- {
- HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO cursorInfo;
- GetConsoleCursorInfo(out, &cursorInfo);
- cursorInfo.bVisible = showFlag; // set the cursor visibility
- SetConsoleCursorInfo(out, &cursorInfo);
- }
- int GetRandomNumInRange(int min, int max) {
- int randNum = rand() % max;
- if (randNum < min) {
- randNum = min;
- }
- return randNum;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement