1 Replies - 542 Views - Last Post: 26 January 2012 - 10:08 PM Rate Topic: -----

Topic Sponsor:

#1 jone kim  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 07-January 10

PYTHON:problem solving 4 Queen Puzzle

Posted 26 January 2012 - 08:23 PM

i can not figure out the problem with my code. can any one guide me. I can't even get a correct solution.

##creating a 4*4 chess
chess = [0] *4
for i in range (4):
    chess[i] = [0] *4
   


## insert 1 in n rows of 1 column iteratively
def selectRowinFirstCol(chess):
    for n in range (len(chess)):
        chess[n][0] = 1
        print(chess)
        break
        #checkForRow(chess)
        
           
## check whether any of the elements of a ROW contains 1; if !contains 1 return ROW
def checkForRow(chess):
    #Row = 0
    for Row in range(len(chess)):
        for Column in range(len(chess[Row])):
            if chess[Row][Column] != 1:
                print("this is row")
                return Row
               
## check whether the COLUMNS of ROW from checkForRow contains 1, if not returns ROW & COLUMN
def checkForColumn(chess,Row):
    for Column in range(len(chess)):
        if chess[Row][Column] != 1:
            print('return row & column')
            return Row,Column
        
       
## check if the diagonals contain 1 or not
        
def checkForUpperDiagonal(chess,Row,Column):
    for RowDiagonal in range(0,Row):
                for ColumnDiagonal in range(0,Column):
                    while chess[RowDiagonal-1][ColumnDiagonal-1] != 1 or chess[RowDiagonal+1][ColumnDiagonal+1] != 1 :
                        print('upperdiagonal')
                        return True
                    else:
                        checkForColumn(chess,Row)
    
def checkForLowerDiagonal(chess,Row,Column):
    for R in range (Row,3):
        for C in range (Column,3):
            while chess[R+1][C-1] != 1 or chess[R-1][C+1] != 1 :
                print('lowerdiagonal')
                return True
            else:
                checkForColumn(chess,Row)

def chessMove(chess, Row2, Column2):
    chess[Row][Column] = 1
    return chessMove 
            

while True:
    selectRowinFirstCol(chess)
    Row1 = checkForRow(chess)
    Row2,Column2 = checkForColumn(chess,Row1)
    checkForUpperDiagonal(chess,Row2,Column2)
    checkForLowerDiagonal(chess,Row2,Column2)
    chessMove = chessMove(chess, Row, Column)
    print(chessMove)
    break
            




Is This A Good Question/Topic? 0
  • +

Replies To: PYTHON:problem solving 4 Queen Puzzle

#2 sysop_fb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-January 12

Re: PYTHON:problem solving 4 Queen Puzzle

Posted 26 January 2012 - 10:08 PM

Run the code in idle and look at the error.

chessMove = chessMove(chess, Row, Column)
NameError: name 'Row' is not defined

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1