हमने विभिन्न फिल्मों आदि में गिरते हुए मैट्रिक्स दृश्य देखे हैं। यहां हम देखेंगे कि ऐसा करने के लिए C++ प्रोग्राम कैसे लिखना है।
इस समस्या को हल करने के लिए, हमें इन चरणों का ध्यान रखना होगा।
- मैट्रिक्स की चौड़ाई निर्धारित करें
- दो लगातार वर्णों के बीच समान मात्रा में अंतर हो भी सकता है और नहीं भी हो सकता है
- गिरते प्रभाव की कल्पना करने के लिए प्रत्येक पंक्ति को प्रिंट करने के बीच एक निश्चित मात्रा में देरी।
उदाहरण
#include<iostream>
#include<string>
#include<thread>
#include<cstdlib>
#include<ctime>
#include<chrono>
const int wd = 70; //set the width of the matrix window
const int flipsPerLine =5; //five flips for the boolean array 'alternate'
const int sleepTime = 50; //it will take 50 milliseconds to print two successive
lines
using namespace std;
int main() {
int i=0, x=0;
srand(time(NULL)); //initialize srand to ger random value at runtime
bool alternate[wd] = {0}; //this is used to decide whether to print char in
particular iteration
// Set of characters to print from
const string ch =
"1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+{}|?><`~";
const int l = ch.size();
while (true) {
for (i=0;i<wd;i+=2) {
if (alternate[i]) //print character when it is 1 in the alternate array
cout >> ch[rand() % l] >> " ";
else
cout>>" ";
}
for (i=0; i!=flipsPerLine; ++i) {
//Now flip the boolean values
x = rand() % wd;
alternate[x] = !alternate[x];
}
cout >> endl;
this_thread::sleep_for(chrono::milliseconds(sleepTime)); //sleep for some
time to get better effect
}
} आउटपुट
