implemented same number in a line or column. still missing no identical lines or columns

This commit is contained in:
Gabriel Augendre 2015-01-01 20:30:04 +01:00
parent 2b75ac84c0
commit 3b14cf087a

View file

@ -58,8 +58,7 @@ class Grid:
representation += "\n" representation += "\n"
return representation return representation
def solve(self): def solve_three(self):
for k in range(0, 10, 1):
for square in self.square_list: for square in self.square_list:
if square.is_empty(): if square.is_empty():
v_prev = square.prev_vert() v_prev = square.prev_vert()
@ -91,6 +90,43 @@ class Grid:
if h_n_next and h_n_next.state == h_next.state: if h_n_next and h_n_next.state == h_next.state:
square.state = h_next.opposite_state() square.state = h_next.opposite_state()
def solve(self):
for i in range(0, 2, 1):
self.solve_three()
self.solve_same_number()
def solve_same_number(self):
for square in self.square_list:
if square.is_empty():
same_line = square.same_line()
count_red = 0
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 count_red == self.size / 2:
square.state = 'B'
elif count_blue == self.size / 2:
square.state = 'R'
if not square.switched:
same_column = square.same_column()
count_red = 0
count_blue = 0
for line_square in same_column:
if line_square.state == 'B':
count_blue += 1
elif line_square.state == 'R':
count_red += 1
if count_red == self.size / 2:
square.state = 'B'
elif count_blue == self.size / 2:
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