From 3b14cf087a084eff97eb8cd41f8cdf044b60acf3 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Thu, 1 Jan 2015 20:30:04 +0100 Subject: [PATCH] implemented same number in a line or column. still missing no identical lines or columns --- sources/grid.py | 114 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 79 insertions(+), 35 deletions(-) diff --git a/sources/grid.py b/sources/grid.py index 75e4e46..729dd8d 100644 --- a/sources/grid.py +++ b/sources/grid.py @@ -58,38 +58,74 @@ class Grid: representation += "\n" 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): - for k in range(0, 10, 1): - 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() + for i in range(0, 2, 1): + self.solve_three() + self.solve_same_number() - 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() + 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 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 count_red == self.size / 2: + square.state = 'B' + elif count_blue == self.size / 2: + square.state = 'R' - 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: + 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 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() + if count_red == self.size / 2: + square.state = 'B' + elif count_blue == self.size / 2: + square.state = 'R' class Square: @@ -188,39 +224,47 @@ class Square: return all_prev_horiz_list def all_next_horiz(self): - h_next = self.prev_horiz() + h_next = self.next_horiz() all_next_horiz_list = [] while 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 def all_prev_vert(self): - v_prev = self.prev_horiz() + v_prev = self.prev_vert() all_prev_vert_list = [] while 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 def all_next_vert(self): - v_next = self.prev_horiz() + v_next = self.next_vert() all_next_vert_list = [] while 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 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.extend(self.all_prev_horiz()) - line_list.append(self) line_list.extend(self.all_next_horiz()) return line_list 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.extend(self.all_prev_vert()) - line_list.append(self) line_list.extend(self.all_next_vert()) return line_list