0

I'm quite new to Pyhton, I use it since September in Spyder (Python 3.4). and I would like to create a program that solves a sudoku grid and displays it (in IPython console) during its completion. I hopefully did successfully the sudoku solver and I get a printable grid in output (I only post the printable grid code):

def sudoku_pp(g):
    c = g.replace("0","_")
    s = ""
    l = 0
    for i in range(13):
        if i % 4 == 0:
            for _ in range(25):
                s += "-"        
        else:
            k = 9*l 
            s += "|"           
            for j in range(9):
                s += " "+c[k+j]
                if j%3 == 2:
                    s += " |"
            l += 1
        s += "\n"
    return(s)

where g is a string containing every number line to line, e.g. SUD1 = '530070000600195000098000060800060003400803001700020006060000280000419005000080079' and

>>>print(sudoku_pp(SUD1))
-------------------------
| 5 3 _ | _ 7 _ | _ _ _ |
| 6 _ _ | 1 9 5 | _ _ _ |
| _ 9 8 | _ _ _ | _ 6 _ |
-------------------------
| 8 _ _ | _ 6 _ | _ _ 3 |
| 4 _ _ | 8 _ 3 | _ _ 1 |
| 7 _ _ | _ 2 _ | _ _ 6 |
-------------------------
| _ 6 _ | _ _ _ | 2 8 _ |
| _ _ _ | 4 1 9 | _ _ 5 |
| _ _ _ | _ 8 _ | _ 7 9 |
-------------------------

My sudoku solver prints a full grid each step, how to erase it entirely and rewrite another one on it?

Thanks a lot and sorry for bad language.

Maxence1402
  • 107
  • 10

2 Answers2

2

In bash you would simply have to use tput, but I know no equivalent to this tool in Python. However, you can still print the escape codes that would do the job.

The tput command for moving the cursor to the previous line and erasing it is, according to this post:

tput cuu1
tput el

Which translates in term of escape codes into \033[K\033[A.

So basically, you’d just have to do this 13 times based on your example to erase the full sudoku:

print(13 * "\033[K\033[A")
print(sudoku_pp(SUD1))

I have only tested this solution in a iPython terminal in Linux, but not in Spyder itself, let me know if it works.

EDIT: Also note this answer about using curses.

Community
  • 1
  • 1
Arcturus B
  • 5,181
  • 4
  • 31
  • 51
  • sorry, that didn't work, and in fact it added one more blank line between grids :( Don't go too much trouble. – Maxence1402 Jan 08 '16 at 21:03
  • @Maxence1402, what platform are you working with (Windows, *nix, etc.)? I might be able to test the solution further if you give me more details on this. Also, you should consider using [curses](https://docs.python.org/3.5/library/curses.html). – Arcturus B Jan 10 '16 at 11:23
  • I use Windows 10, 64 bits, and Spyder 2.7, Python 3.4. I tried once using curses but I only were able to rewrite the last line.. – Maxence1402 Jan 12 '16 at 22:16
1

Add the following to your print statement:

print(sudoku_pp(SUD1), flush=True, end='\r')
Jeffrey Swan
  • 537
  • 2
  • 8
  • 26
  • 1
    Thanks for your response but it only deletes last line, I'm currently writing 13 lines + 1 line for CR so i'm still writing 13 lines with '\r' and putting '\r'*13 does not work... – Maxence1402 Jan 06 '16 at 21:39
  • Yes, that's my mistake. Sorry about that. Why not just insert a sys.stdout.flush() call? – Jeffrey Swan Jan 06 '16 at 21:43