Exemple #1
0
class Agent(object):

    def __init__(self):
        self.options = ''.join(hangman.LETTER_SET)
        self.last_choice = ''
        self.bot = Bot()
        self.flog = open(LOG_FILE, 'a')

    def append(self, text):
        for line in text.splitlines():
            if line.startswith('The word was: '):
                word = line.split(':')[-1].strip().lower()
                logging.warn(word)
                self.flog.write(word + '\n')
                self.flog.flush()
    
    def interact(self, msg):
        # print 'Agent::interact'
        text = msg.get('text', '')
        self.append(text)
        if text.startswith('There is already a game running'):
            logging.warn("/stop")
            return '/stop'
        elif text.startswith('Correct letter') or text.startswith('Incorrect letter') or text.startswith('Word:'):
            # logging.warn("/react")
            (word, life)= parse(text)
            print (word, life)
            for x in word:
                if x.isalpha():
                    self.options = self.options.replace(x, '')
            # Remove last from candidates
            self.options = self.options.replace(self.last_choice, '')
            # Ask bot
            (self.last_choice, i) = self.bot.next(word, life, self.options)
            logging.warn((self.last_choice, i))
            if i < 0.05 or life <= 1:
                return '/stop'
            return self.last_choice
        elif (text.startswith('You win!') or text.startswith('Game stopped')  or text.startswith('You lose')):
            self.last_choice = ''
            self.options = ''.join(hangman.LETTER_SET)
            return '/start'
        elif text.statswith('Your victories: '):
            return '/stop'
        else:
            logging.warn(msg)
            logging.warn("WTF")
            sys.exit(1)
Exemple #2
0
 def __init__(self):
     self.options = ''.join(hangman.LETTER_SET)
     self.last_choice = ''
     self.bot = Bot()
     self.flog = open(LOG_FILE, 'a')