Turn lines of text into a grid[row][column] (taking care to pad the lines to get a proper rectangle)
Central data structure for the flood-fill is a stack
If the randomly chosen point is blank, then fill it, and push the coordinates of its 4 neighbours onto the stack
Handle the neighbouring points the same way
Src:
#!/usr/bin/python
import random
lines='''
+++++++ ++++++++++ ++++++++++++++++++++ +++++
+ + + + + + + +
+ + + + + + +++++
+ + + ++++ + +
+++++++ + + +
+ ++++ + +
+ + + +
+ + + +
+ + + +
++++++++++ + +
+ +
+ +
++++++++++++ + +
+ + + +
+ + + +
+ + + +
++++++++++++ ++++++++++++++
'''.split("\n")
# maximum number of columns and rows
colmax= max( [ len(line) for line in lines ] )
rowmax=len(lines)
padding=' ' * colmax
grid= [ list(lines[row]+padding)[0:colmax] for row in range(0,rowmax) ]
for l in grid: print( ''.join(l) ) # print the grid
print '-' * colmax # print a separating line
# creat a stack, and put a random coordinate on it
pointstack=[]
pointstack.append( ( random.randint(0,colmax), # col
random.randint(0,rowmax) ) ) # row
# floodfill
while len(pointstack)>0:
(col,row)=pointstack.pop()
if col>=0 and col<colmax and row>=0 and row<rowmax:
if grid[row][col]==' ':
grid[row][col]='O'
if col<(colmax-1): pointstack.append( (col+1,row))
if col>0: pointstack.append( (col-1,row))
if row<(rowmax-1): pointstack.append( (col,row+1) )
if row>0: pointstack.append( (col,row-1) )
for l in grid: print( ''.join(l) ) # print the grid