Matrix Effect

closed account (SGb4jE8b)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

#include <iostream>
#include <windows.h>

int Modulus(int iN, int iMod) {
	int iQ = (iN/iMod);
	return iN - (iQ*iMod);
}

char GetChar(int iGenerator, char cBase, int iRange) {
	return (cBase + Modulus(iGenerator, iRange));
}

int main() {
	// Color code
	HANDLE  hConsole;
	hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hConsole, 2);

	char caRow[80];
	int j = 7;
	int k = 2;
	int l = 5;
	int m = 1;
	while (true) {
		// Output a random row of characters
		for ( int i = 0; i < 80; ++i ) 
		{
			if (caRow[i] != ' ') {
				caRow[i] = GetChar(j + i*i, 33, 30);
				if (((i*i + k) % 71) == 0) {
					SetConsoleTextAttribute(hConsole,  7);
				} else {
					SetConsoleTextAttribute(hConsole,  2);
				}
			}
			std::cout << caRow[i];
			SetConsoleTextAttribute(hConsole,  2);
		}
		j = (j + 31);
		k = (k + 17);
		l = (l + 47);
		m = (m + 67);
		caRow[Modulus(j, 80)] = '-';
		caRow[Modulus(k, 80)] = ' ';
		caRow[Modulus(l, 80)] = '-';
		caRow[Modulus(m, 80)] = ' ';
		// Delay
		Sleep(10);
	} //end while
    return 0;
}


Please Guys, i need somebody to explain to me in detail how this program works. I downloaded it from xoax.net, but i can understand it. I believe that there are
many guys here that can explain this for me. I will appreciate it so much.
Is there something in particular that you don't understand?
Is it the color changes? Or is it the random number generation?

closed account (SGb4jE8b)
The random number or character generation.
closed account (SGb4jE8b)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
		for ( int i = 0; i < 80; ++i ) 
		{
			if (caRow[i] != ' ') {
				caRow[i] = GetChar(j + i*i, 33, 30);
				if (((i*i + k) % 71) == 0) {
					SetConsoleTextAttribute(hConsole,  7);
				} else {
					SetConsoleTextAttribute(hConsole,  2);
				}
			}
			std::cout << caRow[i];
			SetConsoleTextAttribute(hConsole,  2);
		}
		j = (j + 31);
		k = (k + 17);
		l = (l + 47);
		m = (m + 67);
		caRow[Modulus(j, 80)] = '-';
		caRow[Modulus(k, 80)] = ' ';
		caRow[Modulus(l, 80)] = '-';
		caRow[Modulus(m, 80)] = ' ';
1
2
if (caRow[i] != ' ') {
				caRow[i] = GetChar(j + i*i, 33, 30);


Here, every character that is not a black space is replaced by a character whose ASCII code is between 33 and 62. The very first time the loop starts, chances are that every character in the buffer will be something other than a blank space. If you lookup an ASCII chart, you'll find that these values include numerals, mathematical symbols, and some punctuation. On screen, the effect is gibberish that the programer figured looked "code like".


The values j and i * i are used to make the result appear random. Imagine a clock face with the numbers 33 through 62 laid out on the circumfirence. Picture the hour hand pointed at 33 and advancing clock wise past j + (i * i) numbers. If the value happened to be negative, imagine the hour hand moving counter clock wise. The value of j remains the same over the entire buffer during one iteration of the outer loop, but the value i*i is obviously different for each position in the buffer.

 
j = (j + 31);


Each time the loop runs, j has been adjusted by 31. When ever this sum exceeds the positive upper range of the int data type, it simply rolls over to the negative range. The trick here is that 31 is a prime number, and more importantly, it is prime relative to the range of the int data type. This results in a pattern of values that won't repeat for quite some time, creating the illusion that they are random.
Notice that the program does the same with k, l, and m using different prime numbers.

1
2
3
4
caRow[Modulus(j, 80)] = '-';
caRow[Modulus(k, 80)] = ' ';
caRow[Modulus(l, 80)] = '-';
caRow[Modulus(m, 80)] = ' ';


These line simply ensure that at least two characters
in the buffer will not be changed during the next iteration of the loop by
placing two blank spaces into the buffer. Placing two "-" characters into the buffer ensures that there will be at least two non-spaces that will be changes. Occasionally, a blank space will be changed to a "-" so the output does not merely accumulate blank spaces.


This program also demonstrates some bad behaviors that should be avoided.
First, since the outer loop has no terminating condition, a break sequence is required to stop the program from executing. This assumes that break sequence handling is enabled, which is not always true.

Second, it assumes that Standard Out and Standard In are both the console. If these handles have been redirected, this program could dump a lot of gibberish into a file before anyone had a change to terminate the process.

Third, it does not restore the text attribute of the console. When I run this program from a command interpreter, I have to manually reset the color attribute after I stop the program.
Topic archived. No new replies allowed.