def makeMove(self, movable_statement): """ Takes a MOVABLE statement and makes the corresponding move. This will result in a change of the game state, and therefore requires updating the KB in the Game Master. The statement should come directly from the result of the MOVABLE query issued to the KB, in the following format: (movable tile3 pos1 pos3 pos2 pos3) Args: movable_statement: A Statement object that contains one of the currently viable moves Returns: None """ ### Student code goes here if GameMaster.isMovableLegal(self, movable_statement): vars = movable_statement.terms currTile = str(vars[0]) oldX = str(vars[1]) oldY = str(vars[2]) newX = str(vars[3]) newY = str(vars[4]) q = parse_input('fact: (posn ?t ' + newX + ' ' + newY + ')') match = self.kb.kb_ask(q) oldTile = match[0].bindings_dict['?t'] self.kb.kb_retract( parse_input('fact: (posn ' + currTile + ' ' + oldX + ' ' + oldY + ')')) self.kb.kb_retract( parse_input('fact: (posn ' + oldTile + ' ' + newX + ' ' + newY + ')')) self.kb.kb_assert( parse_input('fact: (posn ' + currTile + ' ' + newX + ' ' + newY + ')')) self.kb.kb_assert( parse_input('fact: (posn ' + oldTile + ' ' + oldX + ' ' + oldY + ')')) pass
def makeMove(self, movable_statement): """ Takes a MOVABLE statement and makes the corresponding move. This will result in a change of the game state, and therefore requires updating the KB in the Game Master. The statement should come directly from the result of the MOVABLE query issued to the KB, in the following format: (movable tile3 pos1 pos3 pos2 pos3) Args: movable_statement: A Statement object that contains one of the currently viable moves Returns: None """ ### Student code goes here if GameMaster.isMovableLegal(self, movable_statement): # general infomation movable_tile = str(movable_statement.terms[0].term.element) from_x = str(movable_statement.terms[1].term.element) from_y = str(movable_statement.terms[2].term.element) to_x = str(movable_statement.terms[3].term.element) to_y = str(movable_statement.terms[4].term.element) # retract fact fact = "fact: (addr " + movable_tile + " " + from_x + " " + from_y + ")" self.kb.kb_retract(parse_input(fact)) fact = "fact: (addr empty " + to_x + " " + to_y + ")" self.kb.kb_retract(parse_input(fact)) # assert fact fact = "fact: (addr " + movable_tile + " " + to_x + " " + to_y + ")" self.kb.kb_assert(parse_input(fact)) fact = "fact: (addr empty " + from_x + " " + from_y + ")" self.kb.kb_assert(parse_input(fact))
def makeMove(self, movable_statement): """ Takes a MOVABLE statement and makes the corresponding move. This will result in a change of the game state, and therefore requires updating the KB in the Game Master. The statement should come directly from the result of the MOVABLE query issued to the KB, in the following format: (movable disk1 peg1 peg3) Args: movable_statement: A Statement object that contains one of the currently viable moves Returns: None """ ### Student code goes here # judge if legal if GameMaster.isMovableLegal(self, movable_statement): ### ge-information movable_disk = movable_statement.terms[0] from_where = movable_statement.terms[1] to_where = movable_statement.terms[2] Game_status = self.getGameState() #### get the one under movable curr_peg = int(from_where.term.element[3:]) curr_peg_state = Game_status[curr_peg - 1] # initial peg num is 1 if len(curr_peg_state) < 2: next_top = "base" + str(curr_peg) else: next_top = "disk" + str(curr_peg_state[1]) ### get the top on target_peg target_peg = int(to_where.term.element[3:]) target_peg_state = Game_status[target_peg - 1] # initial peg num is 1 if len(target_peg_state) < 1: target_top = "base" + str(target_peg) else: target_top = "disk" + str(target_peg_state[0]) ## fact above # construct the question disk_str = str(movable_disk) questionAbove = "fact: (above disk ?x)".replace("disk", disk_str) # ask askPegNumber = parse_input(questionAbove) statement_matched = self.kb.kb_ask(askPegNumber) # retract Above fact for ind, item in statement_matched.list_of_bindings: fact = item[0] if fact in self.kb.facts: self.kb.kb_retract(fact) # retract 2 original tops del_fact1 = parse_input("fact: (top " + str(movable_disk) + " " + str(from_where) + ")") del_fact2 = parse_input("fact: (top " + str(target_top) + " " + str(to_where) + ")") self.kb.kb_retract(del_fact1) self.kb.kb_retract(del_fact2) # add new Above general_fact = "fact: (above disk_x disk_y)" general_fact = general_fact.replace("disk_x", disk_str) general_fact = general_fact.replace("disk_y", target_top) new_fact = parse_input(general_fact) self.kb.kb_assert(new_fact) ## fact top general_fact = "fact: (top disk peg)" general_fact = general_fact.replace("disk", next_top) general_fact = general_fact.replace("peg", str(from_where)) new_fact = parse_input(general_fact) self.kb.kb_assert(new_fact) general_fact = "fact: (top disk peg)" general_fact = general_fact.replace("disk", str(movable_disk)) general_fact = general_fact.replace("peg", str(to_where)) new_fact = parse_input(general_fact) self.kb.kb_assert(new_fact) pass