def cmd_expansion(cb, peer_removals): try: if len(peer_removals) == 4: row_block_peers = peer_removals[0:2] col_block_peers = peer_removals[2:] if not rcn_row_expansion(cb, row_block_peers): return False # -------------------------------------> Fail! if not rcn_col_expansion(cb, col_block_peers): return False # -------------------------------------> Fail! else: for peer_sub in peer_removals: colors.remove_small_square = colors.remove_row_col digits = g.DIGITS i = peer_sub.index('!') + 1 digits = digits.replace(peer_sub[i], '') peers = [] for digit in digits: peer = peer_sub.replace(peer_sub[i - 1:i + 1], digit) peers.append(peer) for cmd in peers: c.set(cmd) cmdx = c.cmd_main retVal = can_do_cmd(cmdx) if retVal is False: sigs.fail_msg = cmdx return False # ------------------------------> Fail! elif retVal is True: cc.base_cmd(cb, cmdx) return True except Exception as e: logger_except.exception(e) sys.exit()
def can_do_cmd(basic_cmd): ''' Checks legality of command. :param basic_cmd = srring of length = 7 :return: True | False | None ''' try: c.set(basic_cmd) grid_index = c.grid_index square = c.square operation = c.operation value = c.value grid = board.grid_list[grid_index] cell = grid[square] if operation == SET: if cell[0] in g.DIGITS and value in cell: return True else: return False elif operation == CLR: if cell[0] in g.DIGITS and value in cell: if len(cell) > 1: return True else: return False else: return None except Exception as e: logger_except.exception(e) sys.exit()
def base_cmd(cb, basic_cmd): ''' The single point of contact for writing a new grid value It there is not an exception simply returns :param cmd: is basic_cmd e.g. xXyYOn O = operation , ADD | CLR | SET :return: ''' try: cmd = basic_cmd c.set(cmd) op = c.operation sqr = c.square val = c.value grid_index = c.grid_index cell = board.grid_list[grid_index][sqr] dual_force_handler(cell=cell, cmd=cmd, op=op, sqr=sqr) if cell == '': raise Exception(f'cmd = {cmd}, cell = {cell}') if val not in cell and not sigs.is_grid: raise Exception(f'cmd = {cmd}, ' f'val = {val} not in cell = {cell}') if op == CLR: cell = clr_cmd(cell=cell, cmd=cmd, gridIndex=grid_index, sqr=sqr, val=val) elif op == SET: cell = set_cmd(cell, val) elif op == ADD: cell = add_cmd(cell, val) if sigs.step != sigs.steps.no_step: color_cmd(cb=cb, gridIndex=grid_index, op=op, sqr=sqr, val=val) board.cmd(grid_index, sqr, cell) dual_force_handler(cell=cell, cmd=cmd, op=op, sqr=sqr) s.grid_cmd.index = grid_index s.grid_cmd.square = sqr s.grid_cmd.cell = cell s.gui_cmd_name = s.gui_cmd_type.cmd cb() if sigs.step == sigs.steps.every_step: s.gui_cmd_name = s.gui_cmd_type.wait cb() return except Exception as e: logger_except.exception(e) sys.exit()
def basic_cmd(cb, from_cmd): ''' Basic Cmd. todo-2021-04.30-0723 - removal to be verified. of bramch to basc_cmd_grid() if sigs.is_grid == True self.is_Load is also True main.gui needs to insure if self.is_grid == True that the basic_cmd_grid(from_cmd) is called directly ''' try: c.set(from_cmd) operation = c.operation c.do_lists() cmd_list = c.cmds_list for cmd in cmd_list: retVal = can_do_cmd(cmd) ''' retVal can be True | False | None. retVal can only be None for operation == g.CLR (removal) if retVal is False for operation = g.SET it can't assert the value in all grids elif retVal is False for operation = g.CLR means trying to remove lass value in a cell ''' if retVal is False: sigs.fail_msg = f'basic_cmd = {cmd} failed' return False # -------------------------------> early fail! sigs.fail_msg = '' for cmd in cmd_list: retVal = can_do_cmd(cmd) ''' retVal can be True | False | None. retVal is True for operation == g.SET retVal is conditionally True for operation = g.CLR (removal( ''' if retVal is True: cc.base_cmd(cb, cmd) if operation == SET: if not basic_cmd_removals(cb, cmd_list): return False # --------------------------------> early fail! return True except Exception as e: logger_except.exception(e) sys.exit()
def basic_cmd_removals(cb, cmd_list): try: colors.remove_small_square = colors.remove_row_col for cmd in cmd_list: c.set(cmd) c.do_peers() peer_list = c.peer_subs if not cmd_expansion(cb, peer_list): return False # ------------------------------> Fail! if not block_only_removals(cb): return False # ------------------------------> Fail! return True except Exception as e: logger_except.exception(e) sys.exit()
def block_only_removals(cb): ''' Block only peer removals in rcn-grid generate removals in rnc & ncr grids. subs: set-up in c.block_subs e.g. 'b2n6-s4578' B = base of block removals e.g 'b2n6-s' blk_subs: e.g. ['b2n6-s4', 'b2n6-s5', 'b2n6-s7', 'b2n6-s8'] in this example for b2n6-s4: sub_list = ['r2c4-n6', 'r2n6-c4', 'n6c4-r2', 'b2n6-s4'] will pop off last entry to: ['r2c4-n6', 'r2n6-c4', 'n6c4-r2'] will iterate through this list of length three doing removals if able :param x: Call back to GUI in main.py :return: True/False False: could not remove last digit in cell: Fail! ''' try: subs = c.block_subs B = subs[:-4] blk_subs = [B + subs[-4], B + subs[-3], B + subs[-2], B + subs[-1]] colors.remove_small_square = colors.remove_block_only for blk_sub in blk_subs: c.set(blk_sub) c.do_lists() sub_list = c.cmds_list sub_list.pop() for sub in sub_list: retVal = can_do_cmd(sub) if retVal is False: return False # ---------------------------> Fail! if retVal is True: cc.base_cmd(cb, sub) return True except Exception as e: logger_except.exception(e) sys.exit()
def rcn_col_expansion(cb, col_block): col_peer = col_block[0] blk_peer = col_block[1] blk_nums = blk_peer[1:3] digits = g.DIGITS i = col_peer.index('!') + 1 digits = digits.replace(col_peer[i], '') col_peers = [ col_peer.replace(col_peer[i - 1:i + 1], digit) for digit in digits ] total_peers = [] for col_peer in col_peers: c.set(col_peer) block_cmd = c.block_cmd total_peers.append(col_peer) if block_cmd[1] in blk_nums: total_peers.append(block_cmd) for removal in total_peers: c.set(removal) cmdx = c.cmd_main if cmdx[0] == 'b': colors.remove_small_square = colors.remove_rcn_block else: colors.remove_small_square = colors.remove_row_col retVal = can_do_cmd(cmdx) if retVal is False: sigs.fail_msg = cmdx return False # -------------------------> Fail! elif retVal is True: cc.base_cmd(cb, cmdx) return True
def basic_cmd_grid(from_cmd): try: # todo - Add Traces.py hooks if sigs.is_grid: # Only True if "self.is_Load_" is also True if c.multiple_cmds_from_last_digits(from_cmd): multiple_cmd_list = c.multiple_cmds_list for rcn_cmd in multiple_cmd_list: rcn_cmd = rcn_cmd.replace(SET, ADD) c.set(rcn_cmd) c.do_lists() add_list = c.cmds_list for cmd in add_list: cc.base_cmd(cmd) else: c.set(from_cmd) c.do_lists() set_list = c.cmds_list for cmd in set_list: cc.base_cmd(cmd) return except Exception as e: logger_except.exception(e) sys.exit()