Skip to content

Commit c73923b

Browse files
committed
name change
1 parent 8ea7693 commit c73923b

File tree

2 files changed

+176
-0
lines changed

2 files changed

+176
-0
lines changed

Board.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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()

Square.py

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
from Layout import *
2+
class Square(object):
3+
4+
def __init__(self, row, column, box, length, board):
5+
self.number = 0
6+
self.box = box
7+
self.column = column
8+
self.row = row
9+
self.nextsquare = 0
10+
self.length = length
11+
self.board = board
12+
self.test = 0
13+
14+
15+
16+
def printboard(self):
17+
for x in range(0,self.length):
18+
for y in range(0,self.length):
19+
#print self.board[x][y].number
20+
test = 0
21+
#person = input('stoop')
22+
23+
def addnumber(self, number):
24+
self.number = number
25+
26+
def addNext(self, nextsquare):
27+
self.nextsquare = nextsquare
28+
29+
def getnumber(self):
30+
return self.number
31+
32+
def search(self, number):
33+
#print "search number: ", number
34+
#print "kolonne 0", self.column.squares[0].number
35+
#print " rad 0", self.row.squares[0].number
36+
#print " box 0 ", self.box.squares[0].number
37+
if not self.column.exists(number) and not self.row.exists(number) and not self.box.exists(number):
38+
#print "True"
39+
return True
40+
#print "False"
41+
return False
42+
def FillBoard(self):
43+
next = False
44+
45+
if isinstance(self,PreExistingSquare):
46+
#print "pree"
47+
if self.nextsquare != 0:
48+
#print "number : ", self.number
49+
#person = input('preexisting')
50+
self.nextsquare.FillBoard()
51+
else:
52+
#print "else som den ikke skal inn i"
53+
self.nextsquare = self.nextsquare
54+
return True
55+
else:
56+
for i in range(1,self.box.getlength()+1):
57+
#print self.test, "testing"
58+
#print "number",self.row.squares[0].number
59+
#print "i", i
60+
#person = input('empty')
61+
#if this test is true, it did not find anything.
62+
#print "nextsquare", self.nextsquare
63+
if self.search(i):
64+
#print "not found"
65+
self.number = i
66+
#print self.number
67+
68+
if self.nextsquare == 0:
69+
#print "nextsquare =0"
70+
self.board.addtosolution(self.board.board)
71+
return True
72+
else:
73+
next = self.nextsquare.FillBoard()
74+
#print "next" , next
75+
self.number = 0
76+
#print "return false"
77+
return False
78+
79+
80+
81+
82+
83+
class PreExistingSquare(Square):
84+
85+
def __init__(self, row, column, box, number,length, board):
86+
super(PreExistingSquare, self).__init__(row,column,box,length,board)
87+
self.addnumber(number)
88+
class EmptySquare(Square):
89+
90+
def __init__(self, row, column, box, length, number, board):
91+
super(EmptySquare, self).__init__(row,column,box,length, board)
92+
self.number = number

0 commit comments

Comments
 (0)