하강 매트릭스 구현

하강 매트릭스 구현

컴퓨터가 등장한 이후 헐리우드에서는 해커나 프로그래머를 컴퓨터에 앉아 컴퓨터에 임의의 키를 입력하는 누군가가 궁극적으로 시뮬레이션과 같은 낙하 매트릭스로 컴파일되는 모습을 크게 보여주었습니다. 여기서는 C++를 사용하여 콘솔에서 유사한 하강 행렬 시뮬레이션을 구현해 보겠습니다.

C++를 사용하는 명령줄의 Falling-Matrix

여기서 아이디어는 두 개의 연속 문자가 무작위로 정의된 특정 양의 간격을 가질 수도 있고 그렇지 않을 수도 있는 정의된 너비에 걸쳐 임의의 문자를 인쇄하는 것입니다. '하강 효과'를 가지려면 연속적인 라인을 인쇄하는 사이에 일정량의 지연을 구현해야 합니다.

CPP
   // C++ program for implementation of falling matrix.   #include       #include      #include      #include      #include      #include      // Width of the matrix line   const     int     width     =     70  ;   // Defines the number of flips in Boolean Array 'switches'   const     int     flipsPerLine     =     5  ;   // Delay between two successive line print   const     int     sleepTime     =     100  ;   using     namespace     std  ;   int     main  ()   {      int     i     =     0       x     =     0  ;      // srand initialized with time function      // to get distinct rand values at runtime      srand  (  time  (  NULL  ));      // Used to decide whether to print      // the character in that particular iteration      bool     switches  [  width  ]     =     {  0  };      // Set of characters to print from      const     string     ch     =     '1234567890qwertyuiopasdfghjkl'      'zxcvbnm./';[]!@#$%^&*()-=_+'  ;      const     int     l     =     ch  .  size  ();      // Green font over black console duh!      system  (  'Color 0A'  );      // Indefinite Loop      while     (  true  )      {      // Loop over the width      // Increment by 2 gives better effect      for     (  i     =     0  ;     i      <     width  ;     i     +=     2  )      {      // Print character if switches[i] is 1      // Else print a blank character      if     (  switches  [  i  ])      cout      < <     ch  [  rand  ()     %     l  ]      < <     ' '  ;      else      cout      < <     ' '  ;      }      // Flip the defined amount of Boolean values      // after each line      for     (  i     =     0  ;     i     !=     flipsPerLine  ;     ++  i  )      {      x     =     rand  ()     %     width  ;      switches  [  x  ]     =     !  switches  [  x  ];      }      // New Line      cout      < <     endl  ;      // Using sleep_for function to delay      // chrono milliseconds function to convert to milliseconds      this_thread  ::  sleep_for  (  chrono  ::  milliseconds  (  sleepTime  ));      }      return     0  ;   }   
Java
   import     java.util.Random  ;   public     class   FallingMatrix     {      // Width of the matrix line      static     final     int     width     =     70  ;      // Defines the number of flips in Boolean Array 'switches'      static     final     int     flipsPerLine     =     5  ;      // Delay between two successive line prints      static     final     int     sleepTime     =     100  ;      public     static     void     main  (  String  []     args  )     {      // Used to decide whether to print the character in that particular iteration      boolean  []     switches     =     new     boolean  [  width  ]  ;      // Set of characters to print from      String     ch     =     '1234567890qwertyuiopasdfghjkl'      +     'zxcvbnm./';[]!@#$%^&*()-=_+'  ;      int     l     =     ch  .  length  ();      // Green font over black console      System  .  out  .  print  (  'u001B[32m'  );      // Indefinite Loop      while     (  true  )     {      // Loop over the width      // Increment by 2 gives a better effect      for     (  int     i     =     0  ;     i      <     width  ;     i     +=     2  )     {      // Print character if switches[i] is true      // Else print a blank character      if     (  switches  [  i  ]  )      System  .  out  .  print  (  ch  .  charAt  (  new     Random  ().  nextInt  (  l  ))     +     ' '  );      else      System  .  out  .  print  (  ' '  );      }      // Flip the defined amount of Boolean values after each line      for     (  int     i     =     0  ;     i      <     flipsPerLine  ;     ++  i  )     {      int     x     =     new     Random  ().  nextInt  (  width  );      switches  [  x  ]     =     !  switches  [  x  ]  ;      }      // New Line      System  .  out  .  println  ();      // Delay      try     {      Thread  .  sleep  (  sleepTime  );      }     catch     (  InterruptedException     e  )     {      e  .  printStackTrace  ();      }      }      }   }   
Python3
   # Python program for implementation of falling matrix.   import   random   import   time   # Width of the matrix line   width   =   70   # Defines the number of flips in Boolean Array 'switches'   flipsPerLine   =   5   # Delay between two successive line print   sleepTime   =   0.1   # Set of characters to print from   ch   =   '1234567890qwertyuiopasdfghjklzxcvbnm./';[]!@#$%^&*()-=_+'   # Used to decide whether to print   # the character in that particular iteration   switches   =   [  0  ]  *  width   # Indefinite Loop   while   True  :   # Loop over the width   # Increment by 2 gives better effect   for   i   in   range  (  0     width     2  ):   # Print character if switches[i] is 1   # Else print a blank character   if   switches  [  i  ]:   print  (  ch  [  random  .  randint  (  0     len  (  ch  )  -  1  )]   end  =  ' '  )   else  :   print  (  ' '     end  =  ' '  )   # Flip the defined amount of Boolean values   # after each line   for   _   in   range  (  flipsPerLine  ):   x   =   random  .  randint  (  0     width  -  1  )   switches  [  x  ]   =   not   switches  [  x  ]   # New Line   print  ()   # Using sleep function to delay   time  .  sleep  (  sleepTime  )   
JavaScript
   // Importing required modules   const     sleep     =     require  (  'util'  ).  promisify  (  setTimeout  );   // Width of the matrix line   let     width     =     70  ;   // Defines the number of flips in Boolean Array 'switches'   let     flipsPerLine     =     5  ;   // Delay between two successive line print   let     sleepTime     =     100  ;     // in milliseconds   // Set of characters to print from   let     ch     =     '1234567890qwertyuiopasdfghjklzxcvbnm./';[]!@#$%^&*()-=_+'  ;   // Used to decide whether to print   // the character in that particular iteration   let     switches     =     Array  (  width  ).  fill  (  0  );   // Indefinite Loop   async     function     fallingMatrix  ()     {      while     (  true  )     {      // Loop over the width      // Increment by 2 gives better effect      for     (  let     i     =     0  ;     i      <     width  ;     i     +=     2  )     {      // Print character if switches[i] is 1      // Else print a blank character      process  .  stdout  .  write  (  switches  [  i  ]     ?     ch  [  Math  .  floor  (  Math  .  random  ()     *     ch  .  length  )]     +     ' '     :     ' '  );      }      // Flip the defined amount of Boolean values      // after each line      for     (  let     _     =     0  ;     _      <     flipsPerLine  ;     _  ++  )     {      let     x     =     Math  .  floor  (  Math  .  random  ()     *     width  );      switches  [  x  ]     =     !  switches  [  x  ];      }      // New Line      console  .  log  ();      // Using sleep function to delay      await     sleep  (  sleepTime  );      }   }   fallingMatrix  ();   

이것은 콘솔에 놀라운 Falling-Matrix 시뮬레이션을 인쇄합니다. 메모 :

  • 시스템이 비활성화되어 있으므로 이 프로그램은 IDE에서 실행 버튼을 사용하여 실행되지 않습니다.
  • 이 프로그램을 컴파일하는 동안 컴파일러 오류가 발생하는 경우. GCC에서 아래 명령을 사용하여 컴파일하십시오.
     $ g++ -std=c++11 abc.cpp -o falling.o   
    $ falling.o