Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random Matrix char output
02-26-2010, 09:51 PM (This post was last modified: 02-26-2010 09:54 PM by drdebcol.)
Post: #1
Random Matrix char output
This is interesting program, it actually makes some kind of movement in C++.
It is not perfect, but it can be considered as an introduction into core game programming in C++ (i mean manipulation with characters). It is not the same output as in Matrix movie, that kind i made here in Delphi :
http://www.pro9ramming.com/new-matrix-sc...t-955.html
It is output of random characters on random places.
You can see it in the picture :
[Image: 42610_matrixrandomchars.jpg]
I used 2-dimensional array or matrix "of unsigned short int".
Time is set to 1/4 of a second or 250 mili-seconds. For that delay time i used sleep() function which i made, you have it here :
http://www.pro9ramming.com/sleep-functio...t-965.html
It generates random chars, or better said it generates numbers (0..255) and outputs them as chars. For generating them i used random() function, you have it here :
http://www.pro9ramming.com/random-functi...-1140.html
And for looping of frames or screens (every 1/4 seconds) i used label and goto, you have it here :
http://www.pro9ramming.com/c-label-and-goto-t-961.html
So it basically fills random chars on random places in 2-dimensional array or matrix every 1/4 seconds and outputs that on the screen.
And another explanation would be width and height of screen. Well i just counted chars that can be outputted in one line and row.
So there are 24 lines in which you can put 79 characters. That is standard size of Dev-Cpp window.
Because i used system("cls") the command for cleaning screen, it can maybe look flashy because that is slow command.
And for text color i used system() function and standard color method used in batch files (so this only works on Windows).
color method is used like this :
COLOR [attr]
Sets the default console foreground and background colors.
Color attributes are specified by TWO hex digits -- the first
corresponds to the background; the second the foreground (text). Each digit
can be any of the following values:
Code:
0 = Black       8 = Gray
    1 = Blue        9 = Light Blue
    2 = Green       A = Light Green
    3 = Aqua        B = Light Aqua
    4 = Red         C = Light Red
    5 = Purple      D = Light Purple
    6 = Yellow      E = Light Yellow
    7 = White       F = Bright White
If no argument is given, this command restores the color to what it was
when CMD.EXE started. This value either comes from the current console
window, the /T command line switch or from the DefaultColor registry
value.
That would be all about explanations and now the code :
Code:
# include <iostream>
# include <cstdio>

using namespace std;

unsigned short int a[24][79];

int random(int lowest, int highest)
{
srand(time(0)+rand());
int random_integer;
int range=(highest-lowest)+1;
for(int i=0; i<20; i++)
{
  random_integer=lowest+int(range*rand()/(RAND_MAX+1.0));
}
return random_integer;
}

void sleep(clock_t wait)
{
clock_t goal;
goal = wait + clock();
while( goal > clock());
}

void output()
{
unsigned short int i,j;
for (i=0;i<24;i++)
{
   for (j=0;j<79;j++)
    {
     if (a[i][j]>0)
       cout<<char(a[i][j]);
     else
       cout<<" ";
    }
   cout<<endl;
  }      
}

void fill_with_zeros()
{
unsigned short int i,j;
for (i=0;i<24;i++)
{
   for (j=0;j<79;j++)
    {
     a[i][j]=0;
    }
  }      
}
int main()
{
system("color A");
unsigned short int i;
unsigned int br,max;
fill_with_zeros();
br=0;
max=random(100,500);
top:
  for (i=0;i<random(1,3);i++)
  a[random(1,24)][random(1,79)]=random(1,255);
   system("cls");
   output();
   sleep(250);
   br++;
   if (br>max)
     {
       fill_with_zeros();
       br=0;
       max=random(100,500);
     }
goto top;  
cin.get();
}

I hope that you like it !

"I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein
Visit this user's website Find all posts by this user
Quote this message in a reply
02-27-2010, 04:16 PM
Post: #2
RE: Random Matrix char output
Nice share dude.
I definitely like it!
Visit this user's website Find all posts by this user
Quote this message in a reply
02-27-2010, 05:14 PM
Post: #3
RE: Random Matrix char output
If you like the way above, Maybe you'll like this too :
http://www.pro9ramming.com/something-rea...t-966.html
It is not movement, just an ordinary picture, but it took me some time to make !

"I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: