def test_kakuro_not_solution(self): game = GameField(3, 3) cell_column = Cell(CellType.RULES, column_rule=3, length_column=2) cell_row = Cell(CellType.RULES, row_rule=17, length_row=2) game.init_cell(0, 1, cell_column) game.init_cell(1, 0, cell_row) solver = Solver(game) with self.assertRaises(exc.KakuroNotSolution): self.assertRaises(solver.solve())
def __init__(self, width, height): self.width = width self.height = height self.field = [] self.row_pairs = [] self.column_pairs = [] self.all_pairs = [] for x in range(0, height): self.field.append([]) for y in range(0, width): self.field[x].append(Cell(CellType.NO_ACTIVE))
def test_get_cell(self): date = '0|1|0|0|16|2' parser = Parser() actual = parser.get_cell(date, 'fake_file') cell = Cell(CellType.RULES, row_rule=0, length_row=0, column_rule=16, length_column=2) expected = [16, 2] for key in actual: self.assertEqual(tuple([0, 1]), key) self.assertTrue(actual[key].get_length_column() in expected) self.assertTrue(actual[key].get_rules()[1] in expected)
def setUp(self): self.game = GameField(4, 4) self.game.init_cell( 0, 1, Cell(CellType.RULES, column_rule=16, length_column=2)) self.game.init_cell( 0, 2, Cell(CellType.RULES, column_rule=15, length_column=3)) self.game.init_cell(1, 0, Cell(CellType.RULES, row_rule=17, length_row=2)) self.game.init_cell(2, 0, Cell(CellType.RULES, row_rule=15, length_row=3)) self.game.init_cell(3, 1, Cell(CellType.RULES, row_rule=3, length_row=2)) self.game.init_cell( 1, 3, Cell(CellType.RULES, column_rule=4, length_column=2))
def put_cell(self, sum_row, length_row, sum_column, length_column, x, y, root): try: row_rule = int(sum_row.get()) column_rule = int(sum_column.get()) row = int(length_row.get()) column = int(length_column.get()) except Exception as e: self.throw_exception('Argument error', 'One or more argument are NaN', root) return if not self.check_arguments(row_rule, column_rule, row, column, x, y, root): return self.close_window(root) cell = Cell(CellType.RULES, row_rule=row_rule, column_rule=column_rule, length_row=row, length_column=column) self.game_field.init_cell(x, y, cell) self.redraw()
def add_cell(self, line): frame = line.split('|') if len(frame) != 6: print('Incorrect line') return try: pos_x = self.get_frame(frame[0]) pos_y = self.get_frame(frame[1]) row_sum = self.get_frame(frame[2]) row_length = self.get_frame(frame[3]) column_sum = self.get_frame(frame[4]) column_length = self.get_frame(frame[5]) except Exception as e: print('incorrect line') return cell = Cell(CellType.RULES, row_rule=row_sum, column_rule=column_sum, length_row=row_length, length_column=column_length) self.game.init_cell(pos_x, pos_y, cell) self.all_line.append(line)
def test_get_rule(self): rule_row = 10 rule_column = 10 c = Cell(CellType.RULES, column_rule=rule_column, row_rule=rule_row) self.assertEqual([rule_row, rule_column], c.get_rules())
def test_get_type(self): type = CellType.NO_ACTIVE c = Cell(type) self.assertEqual(type, c.get_type())
def test_get_value(self): value = 10 c = Cell(CellType.PLAY, value) self.assertEqual(value, c.get_value())
def test_init_put_rule_cell_column(self): game = GameField(10, 10) cell = Cell(CellType.RULES, column_rule=10, length_column=2) game.init_cell(5, 5, cell) self.assertEqual(CellType.PLAY, game.field[6][5].get_type()) self.assertEqual(CellType.PLAY, game.field[7][5].get_type())
def test_init_put_rule_cell_row(self): game = GameField(10, 10) cell = Cell(CellType.RULES, row_rule=10, length_row=2) game.init_cell(5, 5, cell) self.assertEqual(CellType.PLAY, game.field[5][6].get_type()) self.assertEqual(CellType.PLAY, game.field[5][7].get_type())
def test_init_put_play_cell(self): game = GameField(10, 10) cell = Cell(CellType.PLAY, value=10) game.init_cell(5, 5, cell) self.assertEqual(cell, game.field[5][5])
def test_is_rule(self): c = Cell(CellType.RULES) self.assertTrue(c.is_rules())
def init_column(self, pos_x, pos_y, length_column, pair): for x in range(0, length_column): cell = Cell(CellType.PLAY) self.field[pos_x + x][pos_y] = cell pair.column_slaves.append(tuple([pos_x + x, pos_y]))
def init_row(self, pos_x, pos_y, length_row, pair): for y in range(0, length_row): cell = Cell(CellType.PLAY, 0) self.field[pos_x][pos_y + y] = cell pair.row_slaves.append(tuple([pos_x, pos_y + y]))
def print_field(field): for x in range(0, len(field)): line = "" for y in range(0, len(field)): if field[x][y].get_type() == CellType.NO_ACTIVE: line += ' # \t' if field[x][y].get_type() == CellType.PLAY: line += ' ' + str(field[x][y].get_value()) + ' \t' if field[x][y].get_type() == CellType.RULES: line += ' ' + str(field[x][y].get_rules()[1]) + '\\' + str( field[x][y].get_rules()[0]) + '\t' print(line) if __name__ == '__main__': game = GameField(4, 4) #print_field(game.field) #print('===================================') game.init_cell(0, 1, Cell(CellType.RULES, column_rule=16, length_column=2)) game.init_cell(0, 2, Cell(CellType.RULES, column_rule=15, length_column=3)) game.init_cell(1, 0, Cell(CellType.RULES, row_rule=17, length_row=2)) game.init_cell(2, 0, Cell(CellType.RULES, row_rule=15, length_row=3)) game.init_cell(3, 1, Cell(CellType.RULES, row_rule=3, length_row=2)) game.init_cell(1, 3, Cell(CellType.RULES, column_rule=4, length_column=2)) print_field(game.field) #solver = Solver(game) #new_field = solver.solve() #print_field(new_field) print(tuple([1, 2, 3]))