def print_out(self): int_to_char = { self.__BLANK: '. ', self._BLACK: 'X ', self._WHITE: 'O ', } print(self._FC_YELLOW + self.name) cell = ChessboardCell() # print column name on table head header = ' ' for col_id in range(self._COLS, -1, -1): cell.from_col_row_id(col_id=col_id, row_id=1) header += cell.col_letter + ' ' print(self._FC_YELLOW + header) # print layout row by row for row_id in range(0, self._ROWS): rowNum = self._ROWS - row_id col_string = '' for col_id in range(0, self._COLS): col_string += int_to_char[self._layout_array[18 - col_id][18 - row_id]] row_string = "%02d" % rowNum + ' ' print(self._FC_YELLOW + row_string + self._FC_RESET + col_string + self._FC_YELLOW + row_string) print(self._FC_YELLOW + header)
def print_out_died_area(self): int_to_char = { 0: 'x ', 1: 'O ', 15: '. ', 16: '- ', 17: '* ', 8: 'B ', 3: 'W ' } print(self._FC_YELLOW + 'Died area') cell = ChessboardCell() # print column name on table head header = ' ' for col_id in range(19, -1, -1): cell.from_col_row_id(col_id=col_id, row_id=1) header += cell.col_letter + ' ' print(self._FC_YELLOW + header) # print layout row by row for row_id in range(0, 19): rowNum = 19 - row_id col_string = '' for col_id in range(0, 19): value = self.__died_area_array[18 - col_id][18 - row_id] if value == 0: col_string += self._BG_RED + int_to_char[ value] + self._FC_RESET else: col_string += int_to_char[value] row_string = "%02d" % rowNum + ' ' print(self._FC_YELLOW + row_string + self._FC_RESET + col_string + self._FC_YELLOW + row_string) print(self._FC_YELLOW + header)
def move_four_corners(self, pause_second=5): target_cells = ['A1', 'A19', 'T1', 'T19'] for this_cell in target_cells: cell = ChessboardCell() cell.from_name(this_cell) target_FK = self.__chessboard_helper.get_interpolated_FK_from_cell( cell) self.__move_to_FK_Position(target_FK) rospy.sleep(pause_second)
def move_row_by_row(self, start_cell_name='T1', step=1, is_upper_letter=True): cell = ChessboardCell() cell.from_name(start_cell_name) start_cell_id = cell.id for index in range(start_cell_id, 361, step): if index % 19 == 0: # change postion to new row cell.from_id(180) temp_FK = self.__chessboard_helper.get_interpolated_FK_from_cell( cell) self.__move_to_FK_Position(temp_FK) cell.from_id(index) print(cell.to_diction()) target_FK = self.__chessboard_helper.get_interpolated_FK_from_cell( cell) if is_upper_letter: self.__robot.current_pose.name = cell.name.upper() target_FK.z = 0.001 else: self.__robot.current_pose.name = cell.name.lower() target_FK.z = 0.098 self.__move_to_FK_Position(target_FK) rospy.sleep(10) self.__robot.update_current_pose_to_diction() self.__robot.write_pose_diction_to_json_file()
def get_first_died_cell(self): ''' return: None means not found! ''' cell = ChessboardCell() for row_id in range(0, self._ROWS): for col_id in range(0, self._COLS): if self.__died_area_array[col_id][row_id] == 0: cell.from_col_row_id(col_id, row_id) return cell return None
def play(self, cell_name, color_code): # print('[Info] ChessBoard.play(cell_name=%s,color=%s)' %(cell_name,color)) cell = ChessboardCell() cell.from_name(cell_name) # value = self.__BLANK # if color =='Black': # value = self._BLACK # elif color == 'White': # value = self._WHITE # else: # logging.info('ChessBoard.play(cell_name=%s,color=%s)' %(cell_name,color)) self._layout_array[cell.col_id][cell.row_id] = color_code
def get_first_cell(self, target_color): ''' return: (x,y) is the target position (-1,-1) means not found! ''' cell = ChessboardCell() for row_id in range(0, self._ROWS): for col_id in range(0, self._COLS): if self._layout_array[col_id][row_id] == target_color: cell.from_col_row_id(col_id, row_id) return cell return None
def test_pickup_from_warehouse(self, auto_pause): self.__robot.eye_open('origin') # self.__robot.eye_open('candy') for i in range(0, 360): self.__robot.action_pickup_chess_from_warehouse() this_cell = ChessboardCell() this_cell.from_id(i) self.__robot.action_place_chess_to_a_cell(this_cell.name) if auto_pause: key = click.getchar() if key == '\x1b': #Escape zero = self.__robot.get_target_pose_by_name('ZERO') self.__robot.goto_here(zero) print('You can shout down the power now.') key = click.getchar()
def test_col_by_col(self, start_col): self.__robot.eye_open('origin') self.__robot.action_pickup_chess_from_warehouse() self.__robot.action_place_chess_to_a_cell('A' + str(start_col)) self.__robot.action_pickup_chess_from_warehouse() self.__robot.action_place_chess_to_a_cell('B' + str(start_col)) for iCol in range(0, 19): for iRow in range(start_col, 19): this_cell = ChessboardCell() this_cell.from_id(iRow * 19 + iCol) next_cell = ChessboardCell() next_cell.from_id(iRow * 19 + iCol) print('******************************* %s' % this_cell.name) print('******************************* %s' % next_cell.name) self.__robot.action_pickup_chess_from_a_cell(this_cell.name) self.__robot.action_place_chess_to_a_cell(next_cell.name) if self.__last_pressed_key == '\x1b': #Esc self.__robot.eef_sleep() zero = self.__robot.get_target_pose_by_name('ZERO') self.__robot.goto_here(zero) return
def test_row_by_row(self, start_cell_name): self.__robot.eye_open('origin') first_cell = ChessboardCell() first_cell.from_name(start_cell_name) second_cell = ChessboardCell() # pickup from warehouse, the first one self.__robot.action_pickup_chess_from_warehouse() self.__robot.action_place_chess_to_a_cell(first_cell.name) # pickup from warehouse, the second one self.__robot.action_pickup_chess_from_warehouse() second_cell.from_id(first_cell.id + 1) self.__robot.action_place_chess_to_a_cell(second_cell.name) for i in range(first_cell.row_id * 19, 361): this_cell = ChessboardCell() this_cell.from_id(i) next_cell = ChessboardCell() next_cell.from_id(i + 2) print('******************************* %s' % this_cell.name) print('******************************* %s' % next_cell.name) self.__robot.action_pickup_chess_from_a_cell(this_cell.name) self.__robot.action_place_chess_to_a_cell(next_cell.name) if self.__last_pressed_key == '\x1b': #Esc self.__robot.eef_sleep() zero = self.__robot.get_target_pose_by_name('ZERO') self.__robot.goto_here(zero) return
def create_four_corners(self): corners = [(0, 0), (0, 18), (18, 0), (18, 18)] for col, row in corners: cell = ChessboardCell() cell.from_col_row_id(col, row) self.__Deal_with_this_cell(cell)
def compare_with(self, target_layout, do_print_out=False): ''' return: total_diff_cells: the number of different cells. last_cell_name: first cell_name that different, might be None my_color: color of last_cell target_color: color of last_cell ''' diffs = [] cell = ChessboardCell() my_cell_color = target_cell_color = self.__BLANK int_to_char = { self.__BLANK: '. ', self._BLACK: 'X ', self._WHITE: 'O ' } if do_print_out: title = self._FC_RESET + ' ' title += self._BG_RED + self._FC_YELLOW + self.name title += self._FC_RESET + ' ' title += self._BG_RED + self._FC_YELLOW + target_layout.name + self._FC_RESET print(title) # print column name on table head header = ' ' for col_id in range(self._COLS, -1, -1): cell.from_col_row_id(col_id=col_id, row_id=1) header += cell.col_letter + ' ' header += ' ' + header print(self._FC_YELLOW + header) # print layout row by row for row_id in range(self._ROWS - 1, -1, -1): rowNum = row_id + 1 my_col_string = target_col_string = '' for col_id in range(self._COLS - 1, -1, -1): print_color = self._FC_RESET target_cell_color = target_layout.get_cell_color_col_row( col_id=col_id, row_id=row_id) if (self._layout_array[col_id][row_id] != target_cell_color): #color is different print_color = self._BG_RED + self._FC_YELLOW cell.from_col_row_id(col_id=col_id, row_id=row_id) my_cell_color = self._layout_array[col_id][row_id] diffs.append((cell.name, my_cell_color, target_cell_color)) if do_print_out: my_col_string += print_color + int_to_char[ self._layout_array[col_id][row_id]] + self._FC_RESET k = target_layout.get_cell_color_col_row(col_id, row_id) target_col_string += print_color + int_to_char[ k] + self._FC_RESET if do_print_out: row_string = self._FC_YELLOW + "%02d" % rowNum col_string = row_string + ' ' + my_col_string + ' ' + row_string + ' ' + target_col_string + ' ' + row_string print(col_string) if do_print_out: print(self._FC_YELLOW + header) print(self._BG_RED + self._FC_YELLOW + '%s' % diffs + self._FC_RESET) return diffs
def set_cell_value_from_name(self, cell_name, new_value): cell = ChessboardCell() cell.from_name(cell_name) self.set_cell_value(cell.col_id, cell.row_id, new_value)
def died_cell_removed_first_one(self): cell = ChessboardCell() cell = self.get_first_died_cell() self.__died_area_array[cell.col_id][cell.row_id] = 15
def play_col_row(self, col_id, row_id, color_code): cell = ChessboardCell() cell.from_col_row_id(col_id=col_id, row_id=row_id) self.play(cell.name, color_code)
# TODO: replace with logging print('--------------------------------------------space X, Y') print(self.__cell_space_x) print(self.__cell_space_y) def get_interpolated_FK_from_cell(self, cell): # target_FK = Pose_FK() x = cell.col_id * self.__cell_space_x + self.__chessboard_left y = cell.row_id * self.__cell_space_y + self.__chessboard_bottom z = 0.02 w = 0 return x, y, z, w if __name__ == "__main__": test1 = ChessboardLayout('Test1') test1.play('T19', app_config.game_rule.cell_color.black) test1.print_out() cell = test1.get_first_cell(app_config.game_rule.cell_color.black) cell = ChessboardCell() # cell.from_col_row_id(x,y) print('x=%d, y=%d, name=%s' % (cell.col_id, cell.row_id, cell.name)) test2 = ChessboardLayout('Test2') test2.play('T19', app_config.game_rule.cell_color.black) test2.play('K10', app_config.game_rule.cell_color.white) x = test2.compare_with(test1, do_print_out=True) print(x)