Commits

Christoph Schindler committed 75b01f1

14. Dojo: Mine Sweeper

  • Participants
  • Parent commits bd936f6

Comments (0)

Files changed (4)

MineSweeper/input.txt

+4 4
+*...
+....
+.*..
+....
+3 5
+**...
+.....
+.*...
+0 0

MineSweeper/setup.cfg

+[nosy]
+base_path = ./
+glob_pattern = *.py
+extra_paths = setup.cfg
+options = --stop --with-yanc

MineSweeper/sweeper.py

+#!/usr/bin/env python
+# encoding: utf-8
+"""
+sweeper.py
+
+get input file with minefield, output solved minesweeper field
+
+Created by Coding Dojo on 2013-07-24.
+Copyright (c) 2013 PyUGAT. All rights reserved.
+"""
+
+MINE = -1
+
+class AreaFactory(object):
+    def __init__(self, filename):
+        self.fobj = open(filename, 'r')
+        
+    def read_next_dimension(self):
+        self.width = 4
+        self.height = 4
+        
+    def readline(self):
+        return self.fobj.readline().strip()
+        
+    def read_area(self):
+        return ("abcd")
+        
+    def next_area(self):
+        n,m = self.readline().split(" ")
+        return (Area(int(n), int(m)))
+        
+class Area:
+    def __init__(self,w,h):
+        self.width = w
+        self.height = h
+    
+    def parse_line(self, line):
+        self.line = line
+        self.cells = []
+        for c in line:
+            cell = (Cell(0,0,c))
+            self.cells.append(cell)
+    
+    
+class Cell(object):
+    def __init__(self, x, y, c):
+        self.x = x
+        self.y = y
+        self._value = 0 if c == "." else MINE
+        self.adjacents = []
+        
+    def set_adjacents(self, others):
+        self.adjacents = tuple(others)
+    
+    @property
+    def value(self):
+        currvalue = 0
+        for adj in self.adjacents:
+            currvalue += 1 if adj._value == MINE else 0
+        return currvalue

MineSweeper/test_sweeper.py

+#!/usr/bin/env python
+# encoding: utf-8
+"""
+test_sweeper.py
+
+Created by Coding Dojo on 2013-05-24.
+Copyright (c) 2013 PyUGAT. All rights reserved.
+"""
+
+from nose.tools import *
+
+import sweeper
+   
+def test_01_load_file():
+    assert isinstance(sweeper.AreaFactory("input.txt"),
+             sweeper.AreaFactory)
+   
+def test_02_parse_file():
+    af = sweeper.AreaFactory("input.txt")
+    af.read_next_dimension()
+    assert af.width == 4
+    assert af.height == 4
+    
+def test_03_read_line1():
+    af = sweeper.AreaFactory("input.txt")
+    af1 = af.readline()
+    assert af1 == "4 4"
+    
+def test_04_read_area():
+    af = sweeper.AreaFactory("input.txt")
+    area = af.read_area()
+    assert len(area) == 4
+    
+def test_05_next_area():
+    af = sweeper.AreaFactory("input.txt")
+    area = af.next_area()
+    assert isinstance(area, sweeper.Area)
+    assert area.width == 4
+    assert area.height == 4
+
+def test_06_cell():
+    cell = sweeper.Cell(0, 0, ".")
+    assert cell.x == 0
+    assert cell.y == 0
+    assert cell.value == 0
+    assert cell.adjacents == []
+
+def test_07_cell_bomb():
+    cell = sweeper.Cell(0, 0, '*')
+    assert cell._value == sweeper.MINE
+    
+def test_08_cell_adjacents():
+    cell1 = sweeper.Cell(0, 0, '*')
+    cell2 = sweeper.Cell(0, 1, '.')
+    cell1.set_adjacents([cell2])
+    assert cell2 in cell1.adjacents
+    
+def test_09_cell_value():
+    cell1 = sweeper.Cell(0, 0, '*')
+    cell2 = sweeper.Cell(0, 1, '.')
+    cell3 = sweeper.Cell(1, 1, '*')
+    cell2.set_adjacents( (cell1, cell3))
+    assert cell2.value == 2
+    
+    cell4 = sweeper.Cell(1,333, ".")
+    cell2.set_adjacents( (cell1, cell3, cell4))
+    assert cell2.value == 2
+    
+def test_10_parse_line():
+    a = sweeper.Area(1, 1)
+    a.parse_line(".")
+    assert a is not None
+    
+def test_11_parse_simple_area():
+        a = sweeper.Area(1, 3)
+        a.parse_line("...")
+        c1,c2,c3 = a.cells
+        assert c1._value == 0
+        assert c2._value == 0
+        assert c3._value == 0       
+