Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matrix in CMD [Source][C++]
#1
Thought I'd share this source with you guys, difficulty is... mediocre I guess, but it's very epic nonetheless .

Code:
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
   //green color for text
   system("color 0a");

   //random seed
   srand(time(0));

   char random_char = ((rand() % 127));
   bool going = true, checking = true;

   while (going = true)
   {
      //draw row of characters (38 characters)
      for (int x = 0; x < 38; x++)
      {
         //generate first char
         random_char = ((rand() % 127) + 50);
         checking = true;
         while (checking)
         {
            if  ( //check for:
               // Bad/Small characters.
                  (random_char == 32) || (random_char == 39) || (random_char == 46) ||
                  (random_char == 44) || (random_char == 34) || (random_char == 45) ||
                  (random_char == 58) || (random_char == 59) || (random_char == 94) ||
                  (random_char == 95) || (random_char == 96) || (random_char == 126)
               // If bad character is generated -> generate new random character and check if it is bad
                )
            random_char = ((rand() % 127) + 50);

            //Good character found -> Continue to print it
            else break;
         }
         //Print character + white space
         cout << random_char << " ";
      }
      //Full row of characters successfully printed -> continue for next row
      cout << endl;

      //Delay
      Sleep(20);
   }
   cin.get();
   return 0;
}
Victoire
Reply
#2
lol it's cool I guess, but there are way too many posts (not here necessarily but on HF OMG) about matrix programs.
Reply
#3
(09-15-2011, 04:59 AM)RiXXoN Wrote: lol it's cool I guess, but there are way too many posts (not here necessarily but on HF OMG) about matrix programs.

There arn't over here, so I shared it here. There arn't "that" many posts on matrix programs =P, i've never seen any in my HF experience. =P
Reply
#4
I made that when I was in 10th, So, yeah I know it. Tongue
Reply
#5
All you need to do is pipeline the output of %random% variables in batch and there you have it.
Reply
#6
Revisiting this with some new information to share now that I looked through this with more knowledge on C++. This post hopefully should help others too.

1) endl is bad as it uses more memory. I'd suggest using '\n' instead.

2) These lines are also pointless since they are never read:
  • Code:
    cin.get();
    return 0;

3) You define going as a boolean value which is true, but it's never changed, so instead of doing that and creating a variable to store it in memory for no reason... That's completely pointless, just do this with the while loop
  • Code:
    while (true)

4) You already define checking as a bool which is true at the top in an outer scope
  • Code:
    bool checking = true;

    Why is set to true again after it's already been set to true right before the loop? And again you don't even need it using up a memory address. Just do this with your while loop:
    Code:
    while (true)

    Now you've eliminated all bool variables...

    You could have also just did this with all your while loops:
    Code:
    for (;;)

5) Why are you declaring this in the beginning of the loop, when it never comes back to this point and you redefine the variable anyways once the loop starts?
  • Code:
    char random_char = ((rand() % 127));

    Just do this:
    Code:
    char random_char;

6) srand() isn't really needed here anyway really...

One last thing, probably shouldn't use the namespace std. Try just declaring it explicitly when you need to use it. Example:
Code:
std::cout << "hello";

Here's a full revised code:
Code:
#include "stdafx.h"

#include <iostream>
#include <Windows.h>

int main()
{
   system("color 0a");
   char random_char;

   for (;;)
   {
      for (int x = 0; x < 38; x++)
      {
         random_char = ((rand() % 127) + 50);
         for (;;)
         {
            if  (
                  (random_char == 32) || (random_char == 39) || (random_char == 46) ||
                  (random_char == 44) || (random_char == 34) || (random_char == 45) ||
                  (random_char == 58) || (random_char == 59) || (random_char == 94) ||
                  (random_char == 95) || (random_char == 96) || (random_char == 126)
                )
            random_char = ((rand() % 127) + 50);

            else break;
         }
         std::cout << random_char << " ";
      }
      std::cout << '\n';
      Sleep(20);
   }

}
Reply
#7
Thank you for this, I would rather use the memory efficient code though ^.^ Either way thank you for this release.
Reply
#8
(01-10-2012, 04:13 PM)AlbinoShadow Wrote: Thank you for this, I would rather use the memory efficient code though ^.^ Either way thank you for this release.

What's bad about this one? It's not a very advanced application, you're only occupying a few bytes in memory with this anyway.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)