|
| 1 | +import numpy as np |
| 2 | +from Layout import * |
| 3 | +from Square import * |
| 4 | + |
| 5 | +class Board(object): |
| 6 | + |
| 7 | + |
| 8 | + def __init__(self, sudokuarray, length, w, h): |
| 9 | + self.sudokuarray = sudokuarray |
| 10 | + self.board = [[0for y in range(0,length)] for x in range(0,length)] |
| 11 | + self.box = [0 for x in range(0,length)] |
| 12 | + self.row = [0 for x in range(0,length)] |
| 13 | + self.column = [0 for x in range(0,length)] |
| 14 | + self.length = length |
| 15 | + self.countHor = 1 |
| 16 | + self.countVert = 1 |
| 17 | + self.countBox = 1 |
| 18 | + self.boxW = w |
| 19 | + self.boxH = h |
| 20 | + self.solution = [[0for y in range(0,length)] for x in range(0,length)] |
| 21 | + |
| 22 | + |
| 23 | + def addtosolution(self,board): |
| 24 | + for x in range(0,self.length): |
| 25 | + for y in range(0,self.length): |
| 26 | + self.solution[x][y] = board[x][y].number |
| 27 | + |
| 28 | + def solveBoard(self): |
| 29 | + return self.board[0][0].FillBoard() |
| 30 | + |
| 31 | + def createBoard(self): |
| 32 | + for x in range(0, self.length): |
| 33 | + newBox = Box(int(self.length)) |
| 34 | + newRow = Row(int(self.length)) |
| 35 | + newColumn = Column(int(self.length)) |
| 36 | + self.box[x] = newBox |
| 37 | + self.row[x] = newRow |
| 38 | + self.column[x] = newColumn |
| 39 | + |
| 40 | + self.squareNumber = 0 |
| 41 | + self.newSquare = None |
| 42 | + self.lastSquare = None |
| 43 | + for x in range(0,self.length): |
| 44 | + for y in range(0,self.length): |
| 45 | + |
| 46 | + if self.sudokuarray[x][y] == 0: |
| 47 | + self.squareNumber += 1 |
| 48 | + self.newSquare = EmptySquare(self.row[x], self.column[y], self.box[self.countBox-1], self.length, 0,self) |
| 49 | + else: |
| 50 | + self.newSquare = PreExistingSquare(self.row[x], self.column[y], self.box[self.countBox-1], self.sudokuarray[x][y], self.length,self) |
| 51 | + if x == 0 and y == 0: |
| 52 | + self.lastSquare = self.newSquare |
| 53 | + |
| 54 | + self.box[self.countBox-1].addsquare(self.newSquare) |
| 55 | + self.row[x].addsquare(self.newSquare) |
| 56 | + self.column[y].addsquare(self.newSquare) |
| 57 | + #print self.column[y].squares[x].number |
| 58 | + self.board[x][y] = self.newSquare |
| 59 | + self.countHor += 1 |
| 60 | + |
| 61 | + if self.countHor == self.boxW+1: |
| 62 | + |
| 63 | + self.countHor = 1 |
| 64 | + self.countBox += 1 |
| 65 | + |
| 66 | + if self.countVert == self.boxH and y == (self.length -1): |
| 67 | + self.countHor = 1 |
| 68 | + self.countVert = 0 |
| 69 | + elif y == (self.length -1) and self.countVert != self.boxH: |
| 70 | + self.countBox -= self.boxH |
| 71 | + |
| 72 | + self.lastSquare.addNext(self.newSquare) |
| 73 | + self.lastSquare = self.newSquare |
| 74 | + |
| 75 | + self.countVert += 1 |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +# array = np.array([[7,0,0,8,0,0,0,0,0], [9,0,0,0,0,0,0,6,0], [0,6,0,2,0,1,5,0,0], |
| 80 | +# [0,0,0,0,4,0,8,0,0], [0,0,7,0,0,0,3,0,0], [4,0,0,0,2,5,0,0,7], |
| 81 | +# [0,4,6,0,0,0,0,8,3], [0,0,3,5,0,0,0,0,0], [0,0,0,0,9,0,0,2,0]]) |
| 82 | +# x = Board(array, 9,3,3) |
| 83 | +# x.createBoard() |
| 84 | +# x.solveBoard() |
0 commit comments