示例#1
0
    def test_find_adjacents(self):
        # find_adjacents returns adjacents' indexes
        # find_adjacents returns generator
        # find_adjacents returns empty on non-existent index
        field = Field([['.', '.', '*'],
                       ['*', '.', '.'],
                       ['.', '*', '.']])

        # test invalid indexes
        adjacents = list(field.find_adjacents(0, -1))
        self.assertEquals(adjacents, [])
        adjacents = list(field.find_adjacents(-1, 0))
        self.assertEquals(adjacents, [])
        adjacents = list(field.find_adjacents(3, 3))
        self.assertEquals(adjacents, [])

        adjacents = list(field.find_adjacents(0, 0))
        self.assertEquals(adjacents, [(0, 1), (1, 0), (1, 1)])

        adjacents = list(field.find_adjacents(0, 2))
        self.assertEquals(adjacents, [(0, 1), (1, 1), (1, 2)])

        adjacents = list(field.find_adjacents(2, 0))
        self.assertEquals(adjacents, [(1, 0), (1, 1), (2, 1)])

        adjacents = list(field.find_adjacents(2, 2))
        self.assertEquals(adjacents, [(1, 1), (1, 2), (2, 1)])

        adjacents = list(field.find_adjacents(1, 1))
        self.assertEquals(adjacents, [(0, 0), (0, 1), (0, 2),
                                      (1, 0),         (1, 2),
                                      (2, 0), (2, 1), (2, 2)])
示例#2
0
    def test_is_bomb(self):
        # is_bomb returns bool
        #         raises IndexError
        field = Field([['.', '.', '*'],
                       ['*', '.', '.'],
                       ['.', '*', '.']])

        self.assertRaises(IndexError, field.is_bomb, 0, -1)
        self.assertRaises(IndexError, field.is_bomb, -1, 0)
        self.assertRaises(IndexError, field.is_bomb, 3, 3)

        self.assertTrue(field.is_bomb(2, 1))
        self.assertFalse(field.is_bomb(1, 1))
示例#3
0
def main():
    nlines = 0
    mcols = 0
    field_input = None
    field_count = 0
    expect_size_line = True
    for line in fileinput.input():
        # strip \n
        line = line.strip()

        if expect_size_line:
            # expect two integers
            nlines, mcols = map(int, line.split())
            if nlines > 0:
                # Read fields in next loop
                expect_size_line = False
                # initialize field
                field_input = list()
                linecount = nlines
                field_count += 1
        else:
            # append line as list/row
            field_input.append(list(line))
            # pop line
            linecount -= 1

            if linecount == 0:
                # process next field
                expect_size_line = True

                # Read complete display result
                field = Field(field_input)

                if not field.is_empty():
                    result = field.resolve()

                    if field_count > 1:
                        sys.stdout.write("\n")

                    sys.stdout.write("Field #%d\n" % field_count)
                    for row in result:
                        for cell in row:
                            sys.stdout.write(cell)
                        sys.stdout.write("\n")

    return 0
示例#4
0
    def test_count_adjacents_bombs(self):
        # count_adjacents_bombs returns integer
        # count_adjacents_bombs raises IndexError on invalid index
        field = Field([['.', '.', '*'],
                       ['*', '.', '.'],
                       ['.', '*', '.']])

        self.assertRaises(IndexError, field.count_adjacents_bombs, -1, 0)
        self.assertRaises(IndexError, field.count_adjacents_bombs, 0, -1)
        self.assertRaises(IndexError, field.count_adjacents_bombs, 3, 3)

        self.assertEquals(field.count_adjacents_bombs(0, 0), 1)
        self.assertEquals(field.count_adjacents_bombs(0, 2), 0)
        self.assertEquals(field.count_adjacents_bombs(1, 1), 3)
        self.assertEquals(field.count_adjacents_bombs(1, 2), 2)
        self.assertEquals(field.count_adjacents_bombs(2, 0), 2)
        self.assertEquals(field.count_adjacents_bombs(2, 2), 1)
示例#5
0
    def test_is_empty(self):
        field = Field([])
        self.assertTrue(field.is_empty())

        field = Field([[]])
        self.assertTrue(field.is_empty())

        field = Field([['.']])
        self.assertFalse(field.is_empty())

        field = Field([['.', '.'], ['.', '.']])
        self.assertFalse(field.is_empty())
示例#6
0
    def test_find_bombs(self):
        # find_bombs shuold returns bomb indexes
        # find_bombs returns generator
        field = Field([])
        indexes = list(field.find_bombs())
        self.assertEquals(indexes, [])

        field = Field([[]])
        indexes = list(field.find_bombs())
        self.assertEquals(indexes, [])

        field = Field([['.']])
        indexes = list(field.find_bombs())
        self.assertEquals(indexes, [])

        field = Field([['*']])
        indexes = list(field.find_bombs())
        self.assertEquals(indexes, [(0, 0)])

        field = Field([['.', '*']])
        indexes = list(field.find_bombs())
        self.assertEquals(indexes, [(0, 1)])

        field = Field([['*', '*']])
        indexes = list(field.find_bombs())
        self.assertEquals(indexes, [(0, 0), (0, 1)])

        field = Field([['.', '.', '*'],
                       ['*', '.', '.'],
                       ['.', '*', '.']])
        indexes = list(field.find_bombs())
        self.assertEquals(indexes, [(0, 2), (1, 0), (2, 1)])
示例#7
0
    def test_resolve_field(self):
        field = Field([])
        self.assertEquals(field.resolve(), [])

        field = Field([[]])
        self.assertEquals(field.resolve(), [[]])

        field = Field([['.']])
        self.assertEquals(field.resolve(), [['0']])

        field = Field([['*']])
        self.assertEquals(field.resolve(), [['*']])

        field = Field([['.', '*']])
        self.assertEquals(field.resolve(), [['1', '*']])

        field = Field([['*', '*']])
        self.assertEquals(field.resolve(), [['*', '*']])

        field = Field([['.', '.', '*'],
                       ['*', '.', '.'],
                       ['.', '*', '.']])
        self.assertEquals(field.resolve(), [['1', '2', '*'],
                                            ['*', '3', '2'],
                                            ['2', '*', '1']])