잊지 않겠습니다.

0002. Minesweeper

시끌벅적 2009. 1. 7. 11:59

import os
import unittest


class Minesweeper:
    def __init__(self):
        self.rows = []
        pass
   
    def clearRow(self):
        self.rows = []
   
    def insertRow(self, row):
        self.rows.append(row)
   
    def printArray2D(self, array_2d):
        for row in array_2d:
            for item in row:
                print item,
            print
   
    def printMine(self):
        self.printArray2D(self.rows)
   
    def initLocation(self):
        mine_locations = []
        for row in self.rows:
            row_locations = []
            for item in row:
                row_locations.append(0)
            mine_locations.append(row_locations)
        self.mine_locations = mine_locations
   
    def isInRange(self, row_location, col_location):
        if row_location >= 0 and col_location >= 0 and row_location < len(self.rows) and col_location < len(self.rows[0]):
            return True
        else:
            return False
       
    def getMineLocation(self):
        self.initLocation()       
        row_location = 0
        for row in self.rows:
            col_location = 0
            for item in row:
                if item == '*':
                    self.mine_locations[row_location][col_location] = '*'
                    for row_index in [-1, 0, 1]:
                        for col_index in [-1, 0, 1]:
                            if self.isInRange(row_location + row_index, col_location + col_index) and self.mine_locations[row_location + row_index][col_location + col_index] != '*':
                                self.mine_locations[row_location + row_index][col_location + col_index] += 1
                col_location += 1
            row_location += 1
   
    def printMineLocation(self):
        self.printArray2D(self.mine_locations)
   
class MinesweeperTest(unittest.TestCase):
    def setUp(self):
        pass
    def testCheckValue(self):
        m = Minesweeper()
        m.insertRow(['.','.','*','.'])
        m.insertRow(['.','*','.','.'])
        m.printMine()
       
        self.assertEqual(m.isInRange(0,0), True)
        self.assertEqual(m.isInRange(5,0), False)
        self.assertEqual(m.isInRange(1,0), True)
        self.assertEqual(m.isInRange(0,5), False)
       
        m.getMineLocation()
        m.printMineLocation()
   
    def testCheckMine2(self):
        m = Minesweeper()
        m.insertRow(['*', '*', '.', '.', '.'])
        m.insertRow(['.', '.','.','.','.'])
        #m.insertRow(['.', '.','.','.','.'])
        m.insertRow(['.', '*','.','.','.'])

        m.getMineLocation()
        m.printMineLocation()
        self.assertEqual(True, True)
    #def tearDown(self):
    #    pass

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

  

require 'test/unit'

class Minesweeper
    attr_reader :rows, :calc_locations
    attr_writer :rows, :calc_locations
   
    def initialize
        self.rows = []
        self.calc_locations = []
    end
   
    def print_rows
        @rows.length.times do |row_index|
            @rows[row_index].length.times {|col_index| print @rows[row_index][col_index].to_s() + ' ' }  
            puts ' '
        end
    end

    def calc_rows
        self.calc_locations = []
        rows.length.times do |row_index|
            row = []
            rows[row_index].length.times do |col_index|
               if rows[row_index][col_index] != '*' : row.push(0)
               else  row.push('*')
               end
            end
            @calc_locations.push(row)
        end

        @rows.length.times do |row_index|
            @rows[row_index].length.times do |col_index|
                -1.upto(1) { |i| -1.upto(1) { |j| @calc_locations[i+row_index][j+col_index] += 1 if self.is_in_range(i+row_index,j+col_index) } } if @rows[row_index][col_index] == '*'
            end
        end
    end

    def is_in_range(row_index, col_index)
        return false if row_index < 0
        return false if col_index < 0
        return false if row_index >= @calc_locations.length
        return false if col_index >= @calc_locations[row_index].length
        return false if @rows[row_index][col_index] == '*'
        return true       
    end

    def print_calc_rows
        @calc_locations.length.times do |row_index|
            @calc_locations[row_index].length.times {|col_index| print @calc_locations[row_index][col_index].to_s() + ' ' }  
            puts ' '
        end
    end   
end

class MinesweeperTest < Test::Unit::TestCase
    def test_case1
        mine = Minesweeper.new()
        mine.rows.push(['.', '*', '.', '.', '*'])
        mine.rows.push(['.', '.', '*', '.', '.'])
        mine.rows.push(['.', '.', '*', '.', '.'])
        mine.rows.push(['.', '.', '*', '.', '.'])
        mine.print_rows()
        mine.calc_rows()
        mine.print_calc_rows()
        assert_equal(1, 1)

        assert_equal(true, mine.is_in_range(0,0))
        assert_equal(false, mine.is_in_range(6, 0))
        assert_equal(false, mine.is_in_range(0, 1))
        assert_equal(1, mine.calc_locations[0][0])
        assert_equal(2, mine.calc_locations[0][2])
    end
end

Posted by Y2K
,