Example #1
0
def word_piece(session, params):
    match = session.match

    if (session.status == 2):
        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

        idx = coord_to_index(tokens[1])
        if (idx < 0 or idx > 63):
            print("??? 2. param error")
            return True
        else:
            match.board.setfield(idx, piece)
            prnt_board(match)
    else:
        print("wrong status")
    return True
Example #2
0
def word_setup(session, params):
    match = session.match
    session.status = 2
    match.score = 0
    match.board.clear()
    match.minutes.clear()
    prnt_match_attributes(match, ", ")
    prnt_board(match)
    print("board setup started - please set pieces")
    return True
Example #3
0
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)
    if (match.evaluate_status() == match.STATUS['active']):
        session.status = 1
    return True
Example #4
0
def word_close(session, params):
    match = session.match
    if (session.status == 2):
        match.update_attributes()
        if (match.board.verify() == False):
            print("invalid position")
        else:
            session.status = 0
            prnt_match_attributes(session.match, ", ")
            prnt_board(match)
            print("board setup finished")
    else:
        print("wrong status")
    return True
Example #5
0
 def run(self):
     self.session.thread_is_busy = True
     print("Thread starting...")
     second_candidate = None
     if (len(self.match.minutes) > 0 and len(self.session.candidates) >= 2):
         last_move = self.match.minutes[-1]
         first_candidate = self.session.candidates[0]
         if (first_candidate.src == last_move.src
                 and first_candidate.dst == last_move.dst
                 and first_candidate.prompiece == last_move.prompiece):
             second_candidate = self.session.candidates[1]
     candidates = calc_move(self.match, second_candidate)
     self.session.candidates.clear()
     if (len(candidates) > 0):
         gmove = candidates[0]
         self.session.match.do_move(gmove.src, gmove.dst, gmove.prompiece)
         if (len(candidates) >= 3):
             self.session.candidates.append(candidates[1])
             self.session.candidates.append(candidates[2])
         prnt_board(self.session.match)
     else:
         print("no move found!")
     self.session.thread_isbusy = False
Example #6
0
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)
    return True
Example #7
0
def word_move(session, params):
    match = session.match
    match.status = match.evaluate_status()
    if (match.status != match.STATUS['active']):
        print("??? " + reverse_lookup(match.STATUS, match.status))
        return True
    if (((match.next_color() == COLORS['white']
          and session.wplayer_ishuman == False)
         or (match.next_color() == COLORS['black']
             and session.bplayer_ishuman == False))):
        print("??? wrong color")
        return True

    prompiece = "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):
        src = coord_to_index(matchobj.group("src"))
        dst = 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):
            src = coord_to_index(matchobj.group("src"))
            dst = coord_to_index(matchobj.group("dst"))
            prompiece = matchobj.group("prom")

            valid = False
            for piece in PIECES:
                if (piece == prompiece):
                    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']):
                    src = match.board.wKg
                else:
                    src = match.board.bKg
                dst = src + 2
            else:
                matchobj = re.search(
                    r"^\s*(?P<long>[0oO][-][0oO][-][0oO])\s*$", params)
                if (matchobj):
                    if (match.next_color() == COLORS['white']):
                        src = match.board.wKg
                    else:
                        src = match.board.bKg
                    dst = src - 2
                else:
                    return True

    if (match.is_move_valid(src, dst, PIECES[prompiece])[0]):
        match.do_move(src, dst, PIECES[prompiece])
        prnt_board(match)
    else:
        print("invalid move!")
    return True
Example #8
0
def word_show(session, params):
    prnt_match_attributes(session.match, ", ")
    prnt_board(session.match)
    return True