#--------------------------------------------------------------------------------
# 	Problem: 
#   
#   	Implement the "paint fill" function that one might see on many image editing programs.
#   	That is, given a screen (represented by a two-dimensional array of colors), a point, and a new color,
#   	fill in the surrounding area until the color changes from the original color. 
#
#	Assumption:
#	-The region is 4 connected which means diagonally adjacent cells are not adjacent.
#
#	Solution:
#	-Fill current cell then check if adjacent cell is the same as source color
#   	-Recursively apply to all cells
#	-Base Case: current cell is nto the source color
#
#	Key Conept:
#	-Recursion
#
#--------------------------------------------------------------------------------

import numpy
import matplotlib

ARRAY_HEIGHT = 20
ARRAY_WIDTH = 20

arr = [	[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 0, 5, 5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

def fill(arr, x, y, color):
	filler(arr, x, y, arr[y][x], color)

def filler(arr, x, y, srcColor, newColor):
	if(x < 0 or y < 0 or x >= ARRAY_HEIGHT or y >= ARRAY_HEIGHT):
		return
	else:
		if(arr[y][x] == srcColor):
			arr[y][x] = newColor
			filler(arr, x    , y - 1, srcColor, newColor)
			filler(arr, x    , y + 1, srcColor, newColor)
			filler(arr, x - 1, y    , srcColor, newColor)		
			filler(arr, x + 1, y    , srcColor, newColor)
			return
		else:
			return

def main():
	fill(arr, 13, 12, 9)
	for i in range(0, ARRAY_WIDTH):
		print(str(arr[i]) + "\n")
	fill(arr, 9, 13, 7)
	for i in range(0, ARRAY_WIDTH):
		print(str(arr[i]) + "\n")

if __name__ == "__main__":
    main()
