0

I have just finished with my food fill algorithm in python. It runs on an N*N matrix filled with integers. I would like to somehow animate it's workings. Is it somehow possible on the console? I think of something like updateing the nodes with a wait() inbetween the updates.

gen
  • 9,528
  • 14
  • 35
  • 64
  • 1
    You could use the [`colorconsole`](https://pypi.python.org/pypi/colorconsole) which will allow you to do cursor positioning as well as colorize the output if desired. – martineau Jul 28 '13 at 17:08

1 Answers1

2

You could use something like this:

#! /usr/bin/python3
import time

m = [ [c for c in line] for line in '''............................
..XXXXXXXXXX...........XXX..
..X........X...........X.X..
..XXXXXX...X....XXXXXXXX.X..
.......X...X....X........X..
....XXXX...XXXXXX........X..
....X....................X..
....X.................XXXX..
....XXXXXXXXXXXXXXXXXXX.....'''.split ('\n') ]

def flood (matrix, start):
    maxX = len (matrix [0] )
    maxY = len (matrix)
    tbf = [start]
    while tbf:
        x, y = tbf [0]
        tbf = tbf [1:]
        if x < 0 or x >= maxX or y < 0 or y >= maxY: continue
        if matrix [y] [x] == 'X': continue
        matrix [y] [x] = 'X'
        tbf += [ (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) ]
        print ('\x1b[0J\x1b[1;1H') #Clear screen and position cursor top left
        for line in matrix: print (''.join (line) )
        time.sleep (.2)

#flood (m, (0, 0) )
flood (m, (4, 2) )

This should work on any console that supports ANSI escape sequences (CSI). You can use the same CSI codes for outputting colours (Wiki).

Hyperboreus
  • 31,997
  • 9
  • 47
  • 87
  • its not working as I expected, it prints the whole map after every step, not update the one printed originally, however, its good anyway – gen Jul 28 '13 at 18:09
  • Does your console support ANSI? Which console do you use? – Hyperboreus Jul 28 '13 at 18:11
  • Try this: `print ('\x1b[31;1mThis is red')` on your console. If it supports ANSI, it should print bold red. – Hyperboreus Jul 28 '13 at 18:12
  • Because if your console supports ANSI, this code does indeed overwrite the original map in each step and does not print it below the one already printed. – Hyperboreus Jul 28 '13 at 18:14
  • From the wiki link above: The Win32 console does not natively support ANSI escape sequences at all. Software such as Ansicon[6] can however act as a wrapper around the standard Win32 console and add support for ANSI escape sequences. Other software can manipulate the console with the ioctl-like Console API interlaced with the text output. Some software internally interprets ANSI escape sequences in text being printing and translates them to these calls[citation needed]. – Hyperboreus Jul 28 '13 at 18:15
  • Then use the `colorconsole` module as martineua pointed out. This should use WinAPI-specific codes to do the same thing. The `print ('\x1b[0J\x1b[1;1H')` in my code clears the screen and position the cursor top left. Once you get this on your windows console, you should be set. – Hyperboreus Jul 28 '13 at 18:18