MIT6_0001F16_ProblemSet3

本次实验来自MIT6_0001F16课程,任务是创建一个word Game。玩家需用给定字母拼出单词以得分,详细规则见MIT官方PDF。

MIT6_0001F16_ProblemSet3

此次实验的实验目的主要在于完成一个word Game游戏的编写,该游戏核心内容就是给用户一些字母,让用户利用这些字母组成正确的单词并获得相应的得分。
具体要求详见mit的pdf作业内容

# 6.0001 Problem Set 3
#
# The 6.0001 Word Game
# Created by: Kevin Luu <luuk> and Jenna Wiens <jwiens>
#
# Name          : Ding
# Collaborators :
# Time spent    :

import math            #调用数学方法
import random           #调用随机库

VOWELS = 'aeiou'     #元音字母
CONSONANTS = 'bcdfghjklmnpqrstvwxyz' #辅音字母
HAND_SIZE = 7           #定义HAND_SIZE,可以通过修改该值达到修改整个游戏HAND_SIZE的目的

#每个字母的得分,用字典存起来
SCRABBLE_LETTER_VALUES = {
   
   
   '*':0 ,'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2, 'h': 4, 'i': 1, 'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10
}

# -----------------------------------
# Helper code
# (you don't need to understand this helper code)

WORDLIST_FILENAME = "words.txt"

#读取文件中的字符
#运行后返回一个包含巨量单词的LIST
def load_words():
    """
    Returns a list of valid words. Words are strings of lowercase letters.
    
    Depending on the size of the word list, this function may
    take a while to finish.
    """
    
    print("Loading word list from file...")
    # inFile: file
    inFile = open(WORDLIST_FILENAME, 'r')
    # wordlist: list of strings
    wordlist = []
    for line in inFile:
        wordlist.append(line.strip().lower())
    #将所有单词的小写形式读取到wordlist中,
    print("  ", len(wordlist), "words loaded.")
    return wordlist

#形成一个dic,dic内容为{ 字母:字母出现次数,  }
#          freqs: dictionary (element_type -> int)
def get_frequency_dict(sequence):
    """
    Returns a dictionary where the keys are elements of the sequence
    and the values are integer counts, for the number of times that
    an element is repeated in the sequence.

    sequence: string or list
    return: dictionary
    """
    
    # freqs: dictionary (element_type -> int)
    #创建一个空字典
    freq = {
   
   }  
    #对每一个sequence中的字母进行操作
    for x in sequence:
        #如果x不存在,则添加其到字典中,赋值为1
        #如果x已经存在,则将其对应的key值+1
        freq[x] = freq.get(x,0) + 1
    #返回字典    
    return freq
	

# (end of helper code)
# -----------------------------------

#
# Problem #1: Scoring a word
#
#给定一个word给出它的得分
def get_word_score(word, n):
    """
    Returns the score for a word. Assumes the word is a
    valid word.

    You may assume that the input word is always either a string of letters, 
    or the empty string "". You may not assume that the string will only contain 
    lowercase letters, so you will have to handle uppercase and mixed case strings 
    appropriately. 

	The score for a word is the product of two components:

	The first component is the sum of the points 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值