U E D R , A S I H C RSS

Mine Sweeper/Leonardong

~cpp 
# 3h 2m 51s
class MineGround:
    def __init__(self, aRowSize, aColSize, aInfo ):
        self.ground = list()
        self.rowSize = aRowSize
        self.colSize = aColSize
        for row in range( aRowSize ):
            self.ground.append( [None] * aColSize )
        for row in range( aRowSize ):
            for col in range( aColSize ):
                self.ground[row][col] = Room( aInfo[row][col] )
        
    def getZone( self, aRow, aCol ):
        if aRow < 0 or aRow >= self.rowSize or\
           aCol < 0 or aCol >= self.colSize:
            return Room.safyZone()
        return self.ground[aRow][aCol]
    
class Room:
    def __init__(self, aKind):
        self.kind = aKind
    def equals( self, aRoom ):
        return self.kind is aRoom.kind
    def mine(): # static method
        return Room("*")
    mine = staticmethod( mine )
    def safyZone(): # static method
        return Room(".")
    safyZone = staticmethod( safyZone )

class MineSweeper:
    def __init__(self, aGround):
        self.ground = aGround
        self.direction =\
            ( (-1,-1),  (-1,0), (-1,1),\
              (0,-1),           (0, 1),\
              (1,-1),   (1,0),  (1,1))

    def computeMineCount( self, aRow, aCol ):
        count = 0
        for d in self.direction:
            if Room.mine().equals(self.ground.getZone( aRow + d[0], aCol + d[1])):
                count = count + 1
        return count
    
    def sweep( self ):
        result = [""] * self.ground.rowSize
        for row in range( self.ground.rowSize ):
            for col in range ( self.ground.colSize ):
                if Room.safyZone().equals( self.ground.getZone(row,col) ):
                    result[row] = result[row] + str( self.computeMineCount(row, col) )
                else:
                    result[row] = result[row] + "*"
        return result
    def run():
        rowSize = input()
        colSize = input()
        info = [""] * rowSize
        for i in range(rowSize):
            info[i] = input()
        
        afterSweep = MineSweeper( MineGround(rowSize, colSize, info) ).sweep()
        for line in afterSweep:
            print line
    run = staticmethod(run)
            
#########################################################################           
import unittest

class MineSweeperTestCase(unittest.TestCase):
    def test3x5example(self):
        info = ["**...",\
                ".....",\
                ".*..."]
        sweeper = MineSweeper(MineGround(3,5, info))
        
        expected = ["**100",\
                    "33200",\
                    "1*100"]
        self.assertEquals( expected, sweeper.sweep() )
        
    def testComputeMineCount(self):
        sweeper = MineSweeper(MineGround(1,1, ["."]))
        self.assertEquals( 0, sweeper.computeMineCount(0,0) )
        sweeper = MineSweeper(MineGround(1,2, ["*."]))
        self.assertEquals( 1, sweeper.computeMineCount(0,1) )
        self.assertEquals( 0, sweeper.computeMineCount(0,0) )
        
class MineGroundTestCase(unittest.TestCase):        
    def testGetZone(self):
        ground = MineGround(1,1, ["*"])
        self.failUnless( Room.mine().equals( ground.getZone(0,0) ) )
        ground = MineGround(1,1, ["."])
        self.failUnless( Room.safyZone().equals( ground.getZone(0,0) ) )
        self.failUnless( Room.safyZone().equals( ground.getZone(-1,0) ))

#########################################################################
if __name__ == '__main__': 
##    unittest.main()
    MineSweeper.run()

Thread

작은 단계를 밟아가면서 TDD를 적용하다 보니까 시간이 많이 걸렸다. 게다가 모르는 파이선 문법 찾는데도 시간이 걸렸다. 파이선의 새로운 기능을 알게 되어 신기하다. 다음 문제를 풀어볼까나. --Leonardong


Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:46
Processing time 0.0153 sec