0

I have implemented Game of Life.

What I cannot work out is how to print every iteration of the cell array in the shell. I want to make the output appear animated.

Here is my array print code

def print_board board
  wipe_screen
  board.each_with_index do |row,y|
    row.each_with_index do |cell,x|
      print cell == 1 ? '*' : ' '
    end
    print "\n"
  end
end

and here is the wipe_screen method:

def wipe_screen
  Board_Height.each do |_|
    # print "\r"
    # $stdout.write "\e[A\e[2K"
  end
end

You can see in the commented out bits what I have already tried.

How can I achieve animated output inside the shell?

EDIT:

Allow me to clarify with an example.

It simply prints out each iteration of the Game of Life cycle):

*  *     * * ** ** 
*** * **  * *  * **
** **** * *  **** * 
** *   **  * **  ** 
*   *  **  *  ***** 
*****  *  **** *   
*****  * * ** *   * 
* ** *** * **   ****
* **  * ** ***** * 
** *  *   * *****   
*            **  * 
**  ****     *** * 
   *** *     *** * 
   *  **     ***** 
      **     ***** 
      **       *   
       **      ****
        **    * * *
     ****   * *****
     *      ****   
*            **  * 
*   ****     *** * 
   *** *     *** * 
      **     ***** 
      **       *** 
      **       *** 
       ***     * **
       * *     ****
     *****  ** ****
     *      ****   
*            **  * 
    ****     *** * 
    ** *     *** * 
      **       *** 
      **       *** 
      **       *** 
       ***     * **
       * *       **
     *****  ***  **
     *      ****   
             **  * 
    ****     *** * 
    ** *       * * 
      **       *** 
      **       *** 
      **       *** 
       ***     ** *
       * *        *
     *****  ****  *
     *      ****   

Whereas what I want is:

*  *     * * ** ** 
*** * **  * *  * **
** **** * *  **** * 
** *   **  * **  ** 
*   *  **  *  ***** 
*****  *  **** *   
*****  * * ** *   * 
* ** *** * **   ****
* **  * ** ***** * 
** *  *   * ***** 

Then, as it iterates over the game of life cycle, replaced by:

  *      ****   
          **  * 
  ****     *** * 
  ** *       * * 
   **       *** 
   **       *** 
   **       *** 
    ***     ** *
    * *        *
  *****  ****  *
  *      ****  

EDIT:

game of life gist

bogl
  • 1,864
  • 1
  • 15
  • 32
Thermatix
  • 2,757
  • 21
  • 51

1 Answers1

3

For an animated look, do not wipe the screen, but return the cursor to the home position. At least in bash and tcsh, you may achieve that with

print "\033[0;0H]"

Then continue and overwrite the updated cell contents. Use STDOUT.flush at the end of the page to empty the output buffer.

bogl
  • 1,864
  • 1
  • 15
  • 32
  • That's fairly close to what I want, how does it work? specifically,how to make it below the input line? – Thermatix Feb 12 '16 at 12:55
  • This is not a Ruby issue, but shell specific. Have a look at Cursor movement in the shell documentation, e.g.: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html . – bogl Feb 12 '16 at 12:59
  • Just fairly close? You can move the cursor to any location. Just replace 0;0 with your coordinates. – bogl Feb 12 '16 at 12:59