{{{#!python
# mine sweeper

import unittest

class MineSweeper:
    search_direction = {
        'up':(-1,0),
        'down':(1,0),
        'left':(0,-1),
        'right':(0,1),
        'upleft':(-1,-1),
        'upright':(-1,1),
        'downleft':(1,-1),
        'downright':(1,1)
    }

    def __init__(self, data):
        self.info = self.parseData(data)

    def parseData(self, data):
        result = []
        for row, line in enumerate(data.split('\n')):
            result.append([])
            for col, mine in enumerate(line):
                result[row].append(mine=='*')
        return result
    
    def search(self, row, col, direction):
        try:
            if row+direction[0] < 0 or col+direction[1] < 0: raise IndexError
            return self.info[row+direction[0]][col+direction[1]]
        except IndexError:
            return False

    def get(self, row, col):
        if self.info[row][col]: return '*'
        return reduce(lambda x,y:x+y, [self.search(row, col, direction) \
            for name, direction in self.search_direction.items()])

    def __str__(self):
        result = []
        for row in range(len(self.info)):
            for col in range(len(self.info[row])):
                result.append(str(self.get(row, col)))
            result.append('\n')
        return ''.join(result[:-1])


class TestMine(unittest.TestCase):
    def testMineSweeper(self):
        data = '''*.*.
...*
.*..
....'''
        ms = MineSweeper(data)
        self.assertEquals('*', ms.get(0,0))
        self.assertEquals(2, ms.get(0,1))
        self.assertEquals(3, ms.get(1,2))

    def testMineSweeper(self):
        data = '''*....
.....
.*...
....*'''
        ms = MineSweeper(data)
        self.assertEquals('*', ms.get(0,0))
        self.assertEquals(2, ms.get(1,1))
        self.assertEquals(0, ms.get(0,4))

    def testRun(self):
        data = '''*....
.....
.*...
....*'''

        expected = '''*1000
22100
1*111
1111*'''
        self.assertEquals(expected, str(MineSweeper(data)))


if __name__ == '__main__':
    unittest.main()
}}}