Bomb Trap (Puzzle) Checker in Python

Discussion in 'Python' started by lionaneesh, Mar 11, 2012.

  1. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    My cousin recently bought a Bomb Trap puzzle, he was crazy for solving that puzzle and tried a couple of times and often came to me for rechecking his solution, and me being lazy, I thought of making a simple python script to check his solutions and save me from the brain drain.

    The Code



    bomb_trap.py
    Code:
    #!/bin/env/python
    # Checker for a 8x8 bomb trap
    
    # Correct Solution
    game = [
                [0, 0, 0, 0, 0, 0, 1, 0],
                [0, 0, 0, 0, 1, 0, 0, 0],
                [0, 0, 1, 0, 0, 0, 0, 0],
                [1, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 1, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 1],
                [0, 1, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 1, 0, 0, 0, 0]
           ]
    
    def check_ones(array) :
        ones = 0
        for h in array:
            if isinstance(h, list) :
                ones = ones + check_ones(h)
            elif h == 1:
                ones = ones + 1
        return ones
    
    def diagonal_check(game):
        errors = ''
        for i in range(0, 7):
            for j in range(0, 7):
                    if game[i][j] != 1:
                        continue
    
                    # diagonal 1
                    diag1 = []
                    h, k = i, j
                    # addition loop
                    while h <= 7 and h >= 0 and k <= 7 and k >= 0:
                        diag1.append(game[h][k])
                        h = h + 1
                        k = k + 1
                    # subtration loop
                    h, k = i-1, j-1
                    while h <= 7 and h >= 0 and k <= 7 and k >= 0:
                        diag1.append(game[h][k])
                        h = h - 1
                        k = k - 1
    
                    # diagonal 2
                    diag2 = []
                    # add k loop
                    h, k = i, j
                    while h <= 7 and h >= 0 and k <= 7 and k >= 0:
                        diag2.append(game[h][k])
                        h = h - 1
                        k = k + 1
    
                    # add h loop
                    h, k = i + 1, j - 1
                    while h <= 7 and h >= 0 and k <= 7 and k >= 0:
                        diag2.append(game[h][k])
                        h = h + 1
                        k = k - 1
    
                    # at this point we have 2 diagonal arrays and we can simply check
                    # for multiple 1's, if there are multiple 1's in any diag list
                    # it means we have 2 points in a diagonal i.e check failed
    
                    if check_ones(diag1) > 1:
                        errors += "Diagonal 1 check @ point [%d, %d] evaluated to FALSE\n" % (i+1, j+1)
                    if check_ones(diag2) > 1:
                        errors += "Diagonal 2 check @ point [%d, %d] evaluated to FALSE\n" % (i+1, j+1)
        if errors != '':
            print errors
            return False
        return True
    
    check1 = True # Let's be +ve ;)
    errors = ''
    # let the game begin
    
    if check_ones(game) != 8:
        print "Please fill up exactly 8 places."
        exit()
    
    for i in range(0, 7):
        horizontal = []
        vertical   = []
        for j in range(0, 7):
            horizontal.append(game[i][j])
            vertical.append(game[j][i])
        if check_ones(horizontal) > 1:
            errors += "Multiple mines in Horizontal, @ Row [%d]\n" % (i + 1)
        if check_ones(vertical) > 1:
            errors += "Multiple mines in Vertical,  @ Column [%d]\n" % (i + 1)
    
    if errors != '':
        print errors
        check1 = False
    
    # Lets do some diagonal checking now
    
    check2 = diagonal_check(game)
    if check1 and check2 :
        print "Correct :)"
    
    After some hours of trying i was able to get the right solution. I think only 4 solutions are possible but feel free to hunt for more.
     
  2. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Edit: The problem has 12 unique solutions and 92 distinct solutions
     
  3. Dhaval_nandu2010

    Dhaval_nandu2010 New Member

    Joined:
    Sep 10, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    India
    Hey dude i think there are 92 solutions to it.......
    And it is a sort of 8queens problem would like to check out this link....
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice