Friday, April 15, 2016

8-Queens Matrix is Stored using JSON/XML having first Queen placed, use back-tracking to place remaining Queens to generate final 8-queen's Matrix. Use suitable Software modeling, Design and testing methods. Justify the selection over other methods.

File: b1.py

import json
import unittest

class MyTestCases(unittest.TestCase):
        def test_positive(self):
                self.assertEqual(run("inp2.json"), True)
def test_negative(self):
self.assertEqual(run("inp3.json"), False)

def isattack(board,r,c):
for i in range(r):
if(board[i][c]==1):
return True
i=r-1
j=c-1
while i>=0 and j>=0:
if(board[i][j]==1):
return True
i-=1
j-=1

i=r-1
j=c+1
while i>=0 and j<8:
if(board[i][j]==1):
return True
i-=1
j-=1
return False


def solve(board,row):
i=0
while i<8:
if (not isattack(board,row,i)):
board[row][i]=1
if row==7:
return True
else:
if(solve(board,row+1)):
return True
else:
board[row][i]=0
i=i+1
if i==8:
return False

def run(filename):
board=[[0 for x in range(8)] for x in range(8)]
if __name__=='__main__':
data=[]
with open(filename, 'r') as f:
data=json.load(f)
if data["start"]>7 or data["start"]<0:
print "Invalid input"
return False #exit()
board[0][data["start"]]=1
if solve(board,1):
print "8 Queens solved"
print "Board Configuraion"
for i in range(8):
for j in range(8):
print str(board[i][j])+"  ",
print "\n"
return True
else:
print "8 Queens not solved"

run('inp1.json')
print "---TESTING---"
unittest.main()


File: inp1.json

{"start":2}

File: inp2.json

{"start":5}

File: inp3.json

{"start":9}


Output

Amols-Air:b1 Darwin$ python b1.py
8 Queens solved
Board Configuraion
0   0   1   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   0   1   0   0   0   0  

0   1   0   0   0   0   0   0  

0   0   0   0   0   0   0   1  

0   0   0   0   0   1   0   0  

---TESTING---
Invalid input
.8 Queens solved
Board Configuraion
0   0   0   0   0   1   0   0  

0   1   0   0   0   0   0   0  

0   0   0   0   1   0   0   0  

0   0   0   0   0   0   0   1  

0   0   0   1   0   0   0   0  

0   0   0   0   0   0   1   0  

0   0   1   0   0   0   0   0  

1   0   0   0   0   0   0   0  

.
----------------------------------------------------------------------
Ran 2 tests in 0.002s

OK

No comments:

Post a Comment

Perform a suitable assignment using Xen Hypervisor or equivalent open source to configure it. Give necessary GUI.

 To install kvm on Fedora:  yum install kvm  yum install virt-manager libvirt libvirt-python python-virtinst  su -c "yum install @v...