Hello, I do C ++ programming and I try to write a console program so that it gives out numbers at random from 0 to 9, while the numbers would move from top to bottom, like in the good old movie "The Matrix". I ran into a problem. When a program selects a number at random, it remains fixed. Here is the code:

#include <Windows.h>; #include <iostream>; using namespace std; int main () { HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute (hStdout, FOREGROUND_GREEN + FOREGROUND_INTENSITY); // Цвет для текста char Matr = rand() % 9 + 48; // Попытка сделать случайный выбор числа CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo (hStdout, &csbi); const int WindWidth = csbi.srWindow.Right - csbi.srWindow.Left + 1; //Константа, определяющая ширину окна const int WindHeight = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; //Высота окна for(int i = 0; i < WindWidth * WindHeight; i++) cout << " "; //изначальная заливка всей консоли пробелами COORD orig = {0, 0}; //Координаты начала консоли SetConsoleCursorPosition (hStdout, orig); COORD num[250]; //Массив, явно превышающий размеры окна консоли. Чтобы цифры шли по всему экрану for(int i = 0; i<WindWidth; i++) { num[i].X = i; // Х существует в каждой клетке num[i].Y = rand() % WindHeight; // Координаты по Y в разбросанном порядке } while (true) { for(int i = 0; i < WindWidth; i++) { SetConsoleCursorPosition (hStdout, num[i]); cout << ' '; num[i].Y++; if(num[i].Y == WindHeight) num[i].Y = 0; SetConsoleCursorPosition (hStdout, num[i]); cout << Matr; } SetConsoleCursorPosition (hStdout, orig); //Перемещение курсора на начало во избежании появления 26 строки Sleep(55); } system ("pause"); return 0; } 

I am interested in how to make it so that each time a new number is selected. I need the number (for example 2), reaching the bottom edge, to appear at the top, while turning into another number. And at the same time that on each coordinate X there were different numbers that are chosen at random again each time, touching the bottom border. I have an assumption that this should be done with the help of arrays, but, to be honest, I think late now, so I ask for help. Thank you in advance :)

UPD If this is too complicated and will require a global restructuring of the code, you can not make every number change as you pass the bottom border. Just so that at least on every x-coordinate there is a number.

  • one
    The rand() % 9 + 48 code rand() % 9 + 48 will give a character from '0' to '8'. It should be divided by 10, not 9. - gammaker
  • something very tricky to display random numbers on the similarity of the matrix - sudo97

1 answer 1

Declare Matr as an array of characters, fill it with random characters, and in the loop output the i-th element of this array.

 char *Matr = new char[WindWidth]; for (int i = 0; i < WindWidth; i++) Matr[i] = rand() % 9 + 48; .... while (true) { for(int i = 0; i < WindWidth; i++) { .... SetConsoleCursorPosition (hStdout, num[i]); cout << Matr[i]; } .... } 

With this option, the number will not change when passing through the border. If you want to change, track this moment and score a new random symbol in Matr [i].

  • it is also necessary to take into account that these are pseudo-random numbers. and you need to use some kind of constantly changing key - for example, the function time () from the ctime (or time.h) library - sudo97