Skip to content

Instantly share code, notes, and snippets.

@cggonzal
Created January 6, 2019 20:32
Show Gist options
  • Save cggonzal/23e4916a25ce23e10fe0bfad59b312c6 to your computer and use it in GitHub Desktop.
Save cggonzal/23e4916a25ce23e10fe0bfad59b312c6 to your computer and use it in GitHub Desktop.
import random
class NQueens:
def __init__(self,n):
self.board, self.queenPositions = self.getNewBoard(n)
self.n = n
def getNewBoard(self,n):
# queens are represented as ones in 2d list of all zeros
# Since it's a 2d list, each element is a row of zeros except for the queen
board = []
queensPos = []
for x in range(n): # makes n x n board of zeros
board.append([0]*n)
for x in range(n): # sets a random value of each row to be 1, denoting the queen
randomIndex = random.randint(0,n-1)
board[x][randomIndex] = 1
queensPos.append( (x,randomIndex) )
return (board,queensPos)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment