Go4Expert (http://www.go4expert.com/)
-   Python (http://www.go4expert.com/articles/python-tutorials/)
-   -   Bomb Trap (Puzzle) Checker in Python (http://www.go4expert.com/articles/bomb-trap-puzzle-checker-python-t27993/)

 lionaneesh 11Mar2012 17:44

Bomb Trap (Puzzle) Checker in Python

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: python

`#!/bin/env/python# Checker for a 8x8 bomb trap# Correct Solutiongame = [            [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 onesdef 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 Truecheck1 = True # Let's be +ve ;)errors = ''# let the game beginif 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 nowcheck2 = 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.

 lionaneesh 11Mar2012 20:36

Re: Bomb Trap (Puzzle) Checker in Python

Edit: The problem has 12 unique solutions and 92 distinct solutions

 Dhaval_nandu2010 14Apr2012 08:04

Re: Bomb Trap (Puzzle) Checker in Python

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....

 All times are GMT +5.5. The time now is 11:43.