implemented same number in a line or column. still missing no identical lines or columns
This commit is contained in:
parent
2b75ac84c0
commit
3b14cf087a
1 changed files with 79 additions and 35 deletions
114
sources/grid.py
114
sources/grid.py
|
@ -58,38 +58,74 @@ class Grid:
|
||||||
representation += "\n"
|
representation += "\n"
|
||||||
return representation
|
return representation
|
||||||
|
|
||||||
|
def solve_three(self):
|
||||||
|
for square in self.square_list:
|
||||||
|
if square.is_empty():
|
||||||
|
v_prev = square.prev_vert()
|
||||||
|
v_next = square.next_vert()
|
||||||
|
h_prev = square.prev_horiz()
|
||||||
|
h_next = square.next_horiz()
|
||||||
|
|
||||||
|
if not square.switched and v_prev and v_prev.state != ' ':
|
||||||
|
v_p_prev = v_prev.prev_vert()
|
||||||
|
if v_p_prev and v_p_prev.state == v_prev.state:
|
||||||
|
square.state = v_prev.opposite_state()
|
||||||
|
elif v_next and v_next.state == v_prev.state:
|
||||||
|
square.state = v_prev.opposite_state()
|
||||||
|
|
||||||
|
if not square.switched and v_next and v_next.state != ' ':
|
||||||
|
v_n_next = v_next.next_vert()
|
||||||
|
if v_n_next and v_n_next.state == v_next.state:
|
||||||
|
square.state = v_next.opposite_state()
|
||||||
|
|
||||||
|
if not square.switched and h_prev and h_prev.state != ' ':
|
||||||
|
h_p_prev = h_prev.prev_horiz()
|
||||||
|
if h_p_prev and h_p_prev.state == h_prev.state:
|
||||||
|
square.state = h_prev.opposite_state()
|
||||||
|
elif h_next and h_next.state == h_prev.state:
|
||||||
|
square.state = h_prev.opposite_state()
|
||||||
|
|
||||||
|
if not square.switched and h_next and h_next.state != ' ':
|
||||||
|
h_n_next = h_next.next_horiz()
|
||||||
|
if h_n_next and h_n_next.state == h_next.state:
|
||||||
|
square.state = h_next.opposite_state()
|
||||||
|
|
||||||
def solve(self):
|
def solve(self):
|
||||||
for k in range(0, 10, 1):
|
for i in range(0, 2, 1):
|
||||||
for square in self.square_list:
|
self.solve_three()
|
||||||
if square.is_empty():
|
self.solve_same_number()
|
||||||
v_prev = square.prev_vert()
|
|
||||||
v_next = square.next_vert()
|
|
||||||
h_prev = square.prev_horiz()
|
|
||||||
h_next = square.next_horiz()
|
|
||||||
|
|
||||||
if not square.switched and v_prev and v_prev.state != ' ':
|
def solve_same_number(self):
|
||||||
v_p_prev = v_prev.prev_vert()
|
for square in self.square_list:
|
||||||
if v_p_prev and v_p_prev.state == v_prev.state:
|
if square.is_empty():
|
||||||
square.state = v_prev.opposite_state()
|
same_line = square.same_line()
|
||||||
elif v_next and v_next.state == v_prev.state:
|
count_red = 0
|
||||||
square.state = v_prev.opposite_state()
|
count_blue = 0
|
||||||
|
for line_square in same_line:
|
||||||
|
if line_square.state == 'B':
|
||||||
|
count_blue += 1
|
||||||
|
elif line_square.state == 'R':
|
||||||
|
count_red += 1
|
||||||
|
|
||||||
if not square.switched and v_next and v_next.state != ' ':
|
if count_red == self.size / 2:
|
||||||
v_n_next = v_next.next_vert()
|
square.state = 'B'
|
||||||
if v_n_next and v_n_next.state == v_next.state:
|
elif count_blue == self.size / 2:
|
||||||
square.state = v_next.opposite_state()
|
square.state = 'R'
|
||||||
|
|
||||||
if not square.switched and h_prev and h_prev.state != ' ':
|
if not square.switched:
|
||||||
h_p_prev = h_prev.prev_horiz()
|
same_column = square.same_column()
|
||||||
if h_p_prev and h_p_prev.state == h_prev.state:
|
count_red = 0
|
||||||
square.state = h_prev.opposite_state()
|
count_blue = 0
|
||||||
elif h_next and h_next.state == h_prev.state:
|
for line_square in same_column:
|
||||||
square.state = h_prev.opposite_state()
|
if line_square.state == 'B':
|
||||||
|
count_blue += 1
|
||||||
|
elif line_square.state == 'R':
|
||||||
|
count_red += 1
|
||||||
|
|
||||||
if not square.switched and h_next and h_next.state != ' ':
|
if count_red == self.size / 2:
|
||||||
h_n_next = h_next.next_horiz()
|
square.state = 'B'
|
||||||
if h_n_next and h_n_next.state == h_next.state:
|
elif count_blue == self.size / 2:
|
||||||
square.state = h_next.opposite_state()
|
square.state = 'R'
|
||||||
|
|
||||||
|
|
||||||
class Square:
|
class Square:
|
||||||
|
@ -188,39 +224,47 @@ class Square:
|
||||||
return all_prev_horiz_list
|
return all_prev_horiz_list
|
||||||
|
|
||||||
def all_next_horiz(self):
|
def all_next_horiz(self):
|
||||||
h_next = self.prev_horiz()
|
h_next = self.next_horiz()
|
||||||
all_next_horiz_list = []
|
all_next_horiz_list = []
|
||||||
while h_next:
|
while h_next:
|
||||||
all_next_horiz_list.append(h_next)
|
all_next_horiz_list.append(h_next)
|
||||||
h_next = h_next.prev_horiz()
|
h_next = h_next.next_horiz()
|
||||||
return all_next_horiz_list
|
return all_next_horiz_list
|
||||||
|
|
||||||
def all_prev_vert(self):
|
def all_prev_vert(self):
|
||||||
v_prev = self.prev_horiz()
|
v_prev = self.prev_vert()
|
||||||
all_prev_vert_list = []
|
all_prev_vert_list = []
|
||||||
while v_prev:
|
while v_prev:
|
||||||
all_prev_vert_list.append(v_prev)
|
all_prev_vert_list.append(v_prev)
|
||||||
v_prev = v_prev.prev_horiz()
|
v_prev = v_prev.prev_vert()
|
||||||
return all_prev_vert_list
|
return all_prev_vert_list
|
||||||
|
|
||||||
def all_next_vert(self):
|
def all_next_vert(self):
|
||||||
v_next = self.prev_horiz()
|
v_next = self.next_vert()
|
||||||
all_next_vert_list = []
|
all_next_vert_list = []
|
||||||
while v_next:
|
while v_next:
|
||||||
all_next_vert_list.append(v_next)
|
all_next_vert_list.append(v_next)
|
||||||
v_next = v_next.prev_horiz()
|
v_next = v_next.next_vert()
|
||||||
return all_next_vert_list
|
return all_next_vert_list
|
||||||
|
|
||||||
def same_line(self):
|
def same_line(self):
|
||||||
|
"""
|
||||||
|
List of squares in the same line.
|
||||||
|
Does not include the considered square.
|
||||||
|
:return: The list of the squares in the same line.
|
||||||
|
"""
|
||||||
line_list = []
|
line_list = []
|
||||||
line_list.extend(self.all_prev_horiz())
|
line_list.extend(self.all_prev_horiz())
|
||||||
line_list.append(self)
|
|
||||||
line_list.extend(self.all_next_horiz())
|
line_list.extend(self.all_next_horiz())
|
||||||
return line_list
|
return line_list
|
||||||
|
|
||||||
def same_column(self):
|
def same_column(self):
|
||||||
|
"""
|
||||||
|
List of squares in the same column.
|
||||||
|
Does not include the considered square.
|
||||||
|
:return: The list of the squares in the same column.
|
||||||
|
"""
|
||||||
line_list = []
|
line_list = []
|
||||||
line_list.extend(self.all_prev_vert())
|
line_list.extend(self.all_prev_vert())
|
||||||
line_list.append(self)
|
|
||||||
line_list.extend(self.all_next_vert())
|
line_list.extend(self.all_next_vert())
|
||||||
return line_list
|
return line_list
|
||||||
|
|
Loading…
Reference in a new issue