def run(self): self.session.thread_is_busy = True print("Thread starting...") second_candidate = None if (len(self.match.move_list) > 0 and len(self.session.candidate_list) >= 2): last_move = self.match.move_list[-1] first_candidate = self.session.candidate_list[0] if (first_candidate.srcx == last_move.srcx and first_candidate.srcy == last_move.srcy and first_candidate.dstx == last_move.dstx and first_candidate.dsty == last_move.dsty and first_candidate.prom_piece == last_move.prom_piece): second_candidate = self.session.candidate_list[1] candidates = calc_move(self.match, second_candidate) self.session.candidate_list.clear() if (len(candidates) > 0): gmove = candidates[0] self.session.match.do_move(gmove.srcx, gmove.srcy, gmove.dstx, gmove.dsty, gmove.prom_piece) if (len(candidates) >= 3): self.session.candidate_list.append(candidates[1]) self.session.candidate_list.append(candidates[2]) prnt_board(self.session.match, 0) else: print("no move found!") self.session.thread_is_busy = False
def word_piece(session, params): match = session.match if (match.status == match.STATUS['setup']): tokens = params.split(" ") if (len(tokens) != 2): print("??? 2 params required.") return True try: piece = PIECES[tokens[0]] except KeyError: print("??? 1. param error") return True x, y = coord_to_index(tokens[1]) if (match.board.is_inbounds(x, y) == False): print("??? 2. param error") return True else: match.board.writefield(x, y, piece) prnt_board(match, 0) else: print("wrong status") return True
def word_setup(session, params): match = session.match match.status = match.STATUS['setup'] match.score = 0 match.board.clear() match.move_list.clear() prnt_match_attributes(match, ", ") prnt_board(match, 0) print("board setup started - please set pieces") return True
def word_undo(session, params): match = session.match if (len(params) > 0): count = abs(int(params)) else: count = 1 for i in range(count): match.undo_move() prnt_board(match, 0) if (match.evaluate_status() == match.STATUS['open']): match.status = match.STATUS['paused'] return True
def word_close(session, params): match = session.match if (match.status == match.STATUS['setup']): match.update_attributes() if (match.board.verify() == False): print("invalid position") else: match.status = match.STATUS['open'] prnt_match_attributes(session.match, ", ") prnt_board(match, 0) print("board setup finished") else: print("wrong status") return True
def word_load(session, params): try: fobject = open(immanuels_dir + "/" + params.strip() + ".txt", "r") except FileNotFoundError: print("??? file not found: " + params.strip()) return True match = cMatch() tokens = fobject.read().split(";") index = 0 # ----------------------- attributes = list_match_attributes(match) for i in range(len(attributes)): for classattr in attributes: label_len = len(classattr.label) if (classattr.label == tokens[index][:label_len]): if (classattr.label == "begin"): value = datetime.now() elif (classattr.label == "time_start"): value = 0 else: strvalue = tokens[index].replace(classattr.label + ":", "") if (strvalue == "None"): value = None elif (strvalue == "True"): value = True elif (strvalue == "False"): value = False else: try: value = int(strvalue) except ValueError: value = strvalue if (classattr.label[:6] == "board."): setattr(match.board, classattr.label, value) else: setattr(match, classattr.label, value) break index += 1 # ----------------------- # ----------------------- strboard = tokens[index].replace("board:", "") index += 1 for y in range(8): for x in range(8): idx = (y * 24) + (x * 3) strfield = strboard[idx:idx + 3] match.board.writefield(x, y, PIECES[strfield]) # ----------------------- # ----------------------- movecnt = int(tokens[index].replace("movelistcnt:", "")) index += 1 for i in range(movecnt): move = cMove() attributes = list_move_attributes(move) for classattr in attributes: label_len = len(classattr.label) if (classattr.label == tokens[index][:label_len]): if (classattr.label == "match"): value = match else: strvalue = tokens[index].replace(classattr.label + ":", "") if (strvalue == "None"): value = None elif (strvalue == "True"): value = True elif (strvalue == "False"): value = False else: try: value = int(strvalue) except ValueError: value = strvalue setattr(move, classattr.label, value) index += 1 match.move_list.append(move) index += 1 # ----------------------- fobject.close() match.update_attributes() session.match = match prnt_match_attributes(session.match, ", ") prnt_board(session.match, 0) return True
def word_move(session, params): match = session.match match.status = match.evaluate_status() if (match.status != match.STATUS['open']): print("??? " + reverse_lookup(match.STATUS, match.status)) return True elif (match.is_next_color_human() == False): print("??? wrong color") return True prom_piece = "blk" matchobj = re.search( r"^\s*(?P<src>[a-hA-H][1-8])\s*[-xX]*\s*(?P<dst>[a-hA-H][1-8])\s*$", params) if (matchobj): srcx, srcy = coord_to_index(matchobj.group("src")) dstx, dsty = coord_to_index(matchobj.group("dst")) else: matchobj = re.search( r"^\s*(?P<src>[a-hA-H][1-8])\s*[-xX]*\s*(?P<dst>[a-hA-H][1-8])\s*[-,;]*\s*(?P<prom>\w+)\s*$", params) if (matchobj): srcx, srcy = coord_to_index(matchobj.group("src")) dstx, dsty = coord_to_index(matchobj.group("dst")) prom_piece = matchobj.group("prom") valid = False for piece in PIECES: if (piece == prom_piece): valid = True break if (valid == False): return True else: matchobj = re.search(r"^\s*(?P<short>[0oO][-][0oO])\s*$", params) if (matchobj): if (match.next_color() == COLORS['white']): srcx = match.board.wKg_x srcy = match.board.wKg_y else: srcx = match.board.bKg_x srcy = match.board.bKg_y dstx = srcx + 2 dsty = srcy else: matchobj = re.search( r"^\s*(?P<long>[0oO][-][0oO][-][0oO])\s*$", params) if (matchobj): if (match.next_color() == COLORS['white']): srcx = match.board.wKg_x srcy = match.board.wKg_y else: srcx = match.board.bKg_x srcy = match.board.bKg_y dstx = srcx - 2 dsty = srcy else: return True if (match.is_move_valid(srcx, srcy, dstx, dsty, PIECES[prom_piece])[0]): match.do_move(srcx, srcy, dstx, dsty, PIECES[prom_piece]) prnt_board(match, 0) else: print("invalid move!") return True
def word_show(session, params): prnt_match_attributes(session.match, ", ") prnt_board(session.match, 0) return True