예제 #1
0
    def take_turn(self, game):
        turn = WordsmushTurn(game)

        move_complete = False
        move_rx = re.compile('^(help|play|pass|resign|rem|clear|\d,\d)', re.IGNORECASE)
        tile_rx = re.compile('^(?P<x>\d),(?P<y>\d)( (?P<pos>\d))?', re.IGNORECASE)

        while not move_complete:
            move_text = ''
            print(game)
            print(turn)

            while not move_rx.match(move_text):
                move_text = loop_input("Enter move or 'help'")
            
            if move_text == 'play':
                if game.is_playable(turn):
                    game.play(self, turn)
                    move_complete = True
                else:
                    print("'%s' is not a playable word." % turn.word.upper())
            elif move_text == 'help':
                self.print_help()
            elif move_text == 'clear':
                turn.clear_tiles()
            elif move_text == 'pass':
                move_complete = True
            elif move_text == 'resign':
                turn.resign = True
                game.play(self, turn)
                move_complete = True
            elif move_text.startswith('rem'):
                rem_tile = int(move_text[3:])
                turn.remove_tile_at_position(rem_tile-1)
            else:  # add tile
                tile_move = tile_rx.match(move_text)
                if tile_move:
                    tile_move_dict = tile_move.groupdict()
                    tile_x = int(tile_move_dict.get('x'))
                    tile_y = int(tile_move_dict.get('y'))
                    tile_pos = tile_move_dict.get('pos')
                    tile_pos = int(tile_pos)-1 if tile_pos else None

                    try:
                        game_tile = game.get_tile(tile_x-1, tile_y-1)
                        turn.add_tile(game_tile, tile_pos)
                    except IndexError:
                        print("No such tile.")

        return turn
예제 #2
0
파일: ai.py 프로젝트: majackson/wordsmush
    def get_best_word(self, game):
        turn = WordsmushTurn(game)

        try:
            word_str = next(word for word in self.playable_words[game] 
                            if game.is_playable_word(word) and len(word) > 2)
            self.playable_words[game].remove(word_str)

            tiles_by_letter = game.tiles_by_letter()
            for word_char in word_str:
                turn.add_tile(next(tile for tile in tiles_by_letter[word_char]
                                        if tile not in turn.tiles))
        except StopIteration:  # no more words left to play!
            turn.resign = True

        return turn