I wanted to de-rust my python abilities with a little project but I'm running into issues.
For context, I'm trying to make a game of go in tkinter. I'm using a 2D list to represent the board and then displaying it. Black stones are represented as 1
, white stone as 2
and 0
are empty space (example below).
The list :
board = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 2, 2, 0, 2, 0, 0, 0],
[0, 2, 2, 1, 2, 0, 0, 0, 0],
[2, 2, 1, 1, 1, 2, 2, 2, 0],
[1, 1, 1, 0, 1, 1, 1, 2, 2],
[0, 0, 0, 0, 0, 0, 2, 1, 1],
[0, 0, 0, 0, 1, 0, 1, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]]
Would represent this board
The problem is, I don't know how to deal with surrounded/dead groups. For example with this board:
[[0, 2, 2, 0, 0],
[2, 1, 1, 2, 0],
[2, 2, 1, 2, 0],
[0, 0, 2, 0, 0],
[0, 0, 0, 0, 0]]
I want to get:
[[0, 2, 2, 0, 0],
[2, 0, 0, 2, 0],
[2, 2, 0, 2, 0],
[0, 0, 2, 0, 0],
[0, 0, 0, 0, 0]]
And same if 1
replaces 2
and vise versa.
I thought of doing a flood fill from a random empty point to see which groups would remain, but a line of alive stones could stop the flood like so (representing flooded part with 9
):
[[0, 0, 2, 9, 9],
[0, 1, 1, 2, 9],
[2, 2, 1, 2, 9],
[9, 9, 2, 9, 9],
[9, 9, 9, 9, 9]]
And we are left not knowing anything more. An idea I had would be to make the flood affect every value and running it might leave only the closed groups. It would only affect the first "layer" except if the group is open (I don't know if this makes sense). I tried fiddling with a version of this fill I had on my computer but can't get it to work. I don't have any other ideas on how to tackle this problem. Using the solution for this post leaves a bug with this board for example:
[[0, 1, 0],
[1, 2, 1],
[0, 2, 0]]
Gives:
[[0, 1, 0],
[1, 3, 1],
[0, 2, 0]]
But the middle stone is not dead...
Any help would be very welcomed !