Python Word Game Assignment

Page 1

IMPLEMENTING A WORD GAME SIMILAR TO SCRABBLE Word Game Assignment For any Assignment related queries, Call us at : - +1(315) 557-6473 You can mail us at : - support@programminghomeworkhelp.com or Reach us at : - https://www.programminghomeworkhelp.com/

In this problem set, you'll implement a version of the word game!

Let's begin by describing the word game: This game is a lot like Scrabble or Words With Friends. Letters are dealt to players, who then construct one or more words using their letters. Each valid word earns the user points, based on the length of the word and the letters in that word.

Dealing

● A player is dealt a hand of HAND_SIZE letters of the alphabet, chosen at random. This may include multiple instances of a particular letter.

● The player arranges the hand into as many words as they want out of the letters, but using each letter at most once.

● Some letters may remain unused, though the size of the hand when a word is played does affect its score.

Scoring

● The score for the hand is the sum of the score for each word formed.

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

o First component: the sum of the points for letters in the word.

o Second component: either [7 * word_length - 3 * (n-word_length)] or 1, whichever value is greater, where:

▪ word_length is the number of letters used in the word

▪ n is the number of letters available in the current hand

● Letters are scored as in Scrabble; A is worth 1, B is worth 3, C is worth 3, D is worth 2, E is worth 1, and so on. We have defined the dictionary SCRABBLE_LETTER_VALUES that maps each lowercase letter to its Scrabble letter value.

● Examples:

o For example, if n=6 and the hand includes 1 'w', 2 'e's, and 1 'd' (as well as two other letters), playing the word 'weed' would be worth 176 points: (4+1+1+2) * (7*4 - 3*(6-4)) = 176. The first term is the sum of the values of each letter used; the second term is the special computation that rewards a player for playing a longer word, and penalizes them for any left over letters.

Problem 1: Word scores

As a reminder, here are the rules for scoring a word: The score for a word is the product of two components: First component: the sum of the points for letters in the word. Second component: either [7 * word_length - 3 * (n-word_length)] or 1, whichever value is greater, where: word_length is the number of letters used in the word n is the number of letters available in the current hand

You should use the SCRABBLE_LETTER_VALUES dictionary defined at the top of ps3.py. Do not assume that there are always 7 letters in a hand! The parameter n is the total number of letters in the hand when the word was entered. Finally, you may find the str.lower function helpful: s = “My string” print(s.lower())

>>>> “my string”

If you don’t know what this does you could try typing help(str.lower) in your Spyder shell to see the documentation for the functions.

Testing: If this function is implemented correctly, and you run test_ps3.py, the test_get_word_score()tests will pass. You should also test your implementation of get_word_score yourself, using some reasonable English words. Note that the wildcard tests will crash due to a KeyError

Problem 1 - Word Scores

Title: Calculating Word Scores

Content: Function: get_word_score Rules: Sum letter points. Apply the formula for the second component.

# Word Game

import math

import random

import string

VOWELS = 'aeiou'

CONSONANTS = 'bcdfghjklmnpqrstvwxyz'

HAND_SIZE = 7

SCRABBLE_LETTER_VALUES = { '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"

Problem #
}

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())

print(" ", len(wordlist), "words loaded.") return wordlist

def get_frequency_dict(sequence): """

"""

sequence: string or list return: dictionary

# freqs: dictionary (element_type -> int) freq = {} for x in sequence: freq[x] = freq.get(x,0) + 1 return freq # (end of helper code)

# Problem #1: Scoring a 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 for letters in the word. The second component is the larger of: 1, or 7*wordlen - 3*(n-wordlen), where wordlen is the length of the word and n is the hand length when the word was played

Letters are scored as in Scrabble; A is worth 1, B is worth 3, C is worth 3, D is worth 2, E is worth 1, and so on.

word: string n: int >= 0

returns: int >= 0

""" pass # TO DO... Remove this line when you implement this function

# Problem #1: Scoring a 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 for letters in the word. The second component is the larger of: 1, or 7*wordlen - 3*(n-wordlen), where wordlen is the length of the word and n is the hand length when the word was played

"""

Letters are scored as in Scrabble; A is worth 1, B is worth 3, C is worth 3, D is worth 2, E is worth 1, and so on.

word: string n: int >= 0 returns: int >= 0 """ pass # TO DO... Remove this line when you implement this function

# Make sure you understand how this function works and what it does!

def display_hand(hand): """

Displays the letters currently in the hand.

For example: display_hand({'a':1, 'x':2, 'l':3, 'e':1})

Should print out something like: a x x l l l e

The order of the letters is unimportant.

#
#

hand: dictionary (string -> int)

hand={}

num_vowels = int(math.ceil(n / 3))

for i in range(num_vowels):

x = random.choice(VOWELS)

hand[x] = hand.get(x, 0) + 1 for i in range(num_vowels, n):

x = random.choice(CONSONANTS)

hand[x] = hand.get(x, 0) + 1

return hand

for letter in hand.keys(): for j in range(hand[letter]): print(letter, end=' ') # print all on the same line print() # print an empty line

"""

# Make sure you understand how this function works and what it does!

# You will need to modify this for Problem #4.

# def deal_hand(n): """

Returns a random hand containing n lowercase letters.

ceil(n/3) letters in the hand should be VOWELS (note, ceil(n/3) means the smallest integer not less than n/3).

Hands are represented as dictionaries. The keys are letters and the values are the number of times the particular letter is repeated in that hand.

n: int >= 0

returns: dictionary (string -> int) """

#

Problem 2: Dealing with hands

Representing hands

A hand is the set of letters held by a player during the game. The player is initially dealt a set of random letters. For example, the player could start out with the following hand: a, q, l, m, u, i, l. In our program, a hand will be represented as a dictionary: the keys are (lowercase) letters and the values are the number of times the particular letter is repeated in that hand. For example, the above hand would be represented as:

hand = {'a':1, 'q':1, 'l':2, 'm':1, 'u':1, 'i':1}

Notice how the repeated letter 'l' is represented. With a dictionary representation, the usual way to access a value is hand['a'], where 'a' is the key we want to find. However, this only works if the key is in the dictionary; otherwise, we get a KeyError. To avoid this, we can instead use the function call hand.get('a',0). This is the "safe" way to access a value if we are not sure the key is in the dictionary.

d.get(key,default) returns the value for key if key is in the dictionary d, else it returns default. If default is not given, it returns None, so that this method never raises a KeyError.

Converting words into dictionary representation

One useful function we've defined for you is get_frequency_dict, defined near the top of ps3.py. When given a string of letters as an input, it returns a dictionary where the keys are letters and the values are the number of times that letter is represented in the input string. For example:

>> get_frequency_dict("hello")

{'h': 1, 'e': 1, 'l': 2, 'o': 1}

As you can see, this is the same kind of dictionary we use to represent hands.

Displaying a hand

Given a hand represented as a dictionary, we want to display it in a user-friendly way.

We have provided the implementation for this in the display_hand function. Take a few minutes right now to read through this function carefully and understand what it does and how it works.

Generating a random hand

The hand a player is dealt is a set of letters chosen at random. We provide you with a function that generates a random hand, deal_hand. The function takes as input a positive integer n, and returns a new dictionary representing a hand of n lowercase letters. Again, take a few minutes to read through this function carefully and understand what it does and how it works.

Removing letters from a hand (you implement this!)

The player starts with a full hand of n letters. As the player spells out words, letters from the set are used up. For example, the player could start with the following hand: a, q, l, m, u, i, l The player could choose to play the word quail. This would leave the following letters in the player's hand: l, m.

You will now write a function that takes a hand and a word as inputs, uses letters from that hand to spell the word, and returns a new hand containing only the remaining letters. Your function should not modify the input hand. For example:

>> hand = {'a':1, 'q':1, 'l':2, 'm':1, 'u':1, 'i':1}

>> display_hand(hand) a q l l

m u i

>> new_hand = update_hand(hand, 'quail')

>> new_hand {'l': 1, 'm': 1}

>> display_hand(new_hand) l m

>> display_hand(hand) a q l l

m u i

(NOTE: Alternatively, in the above example, after the call to update_hand the value of new_hand could be the dictionary {'a':0, 'q':0, 'l':1, 'm':1, 'u':0, 'i':0.} The exact value depends on your implementation; but the output of display_hand() should be the same in either case.)

Representation: Hands are dictionaries with letter counts.

Example: Hand {'a':1, 'q':1, 'l':2, 'm':1, 'u':1, 'i':1}

Function: update_hand to update hand after a word is played.

Problem #2: Update a hand by removing letters # def update_hand(hand, word): """

Does NOT assume that hand contains every letter in word at least as many times as the letter appears in word. Letters in word that don't

def update_hand(hand, word):

Does NOT assume that hand contains every letter in word at least as many times as the letter appears in word. Letters in word that don't appear in hand should be ignored. Letters that appear in word more times than in hand should never result in a negative count; instead, set the count in the returned hand to 0 (or remove the letter from the dictionary, depending on how your code is structured).

Updates the hand: uses up the letters in the given word and returns the new hand, without those letters in it.

Has no side effects: does not modify hand.

word: string

hand: dictionary (string -> int)

returns: dictionary (string -> int) """

#
#
Problem #2: Update a hand by removing letters
"""

appear in hand should be ignored. Letters that appear in word more times than in hand should never result in a negative count; instead, set the count in the returned hand to 0 (or remove the letter from the dictionary, depending on how your code is structured).

Updates the hand: uses up the letters in the given word and returns the new hand, without those letters in it.

Has no side effects: does not modify hand.

word: string hand: dictionary (string -> int) returns: dictionary (string -> int) """

Problem 3. Valid words

At this point, we have not written any code to verify that a word given by a player obeys the rules of the game. A valid word is in the word list (we ignore the case of words here) and it is composed entirely of letters from the current hand.

Content:Function: is_valid_word

Rules:

Word must be in the word list.

Word must be composed of letters from the hand.

Testing: Use test_is_valid_word.

Problem #3: Test word validity

def is_valid_word(word, hand, word_list): """ Returns True if word is in the word_list and is entirely composed of letters in the hand. Otherwise, returns False. Does not mutate hand or word_list.

word: string hand: dictionary (string -> int)

word_list: list of lowercase strings returns: boolean """ pass # TO DO... Remove this line when you implement this function

#

# Problem #4: Playing a hand

def calculate_handlen(hand):

Returns the length (number of letters) in the current hand.

hand: dictionary (string-> int) returns: integer

pass # TO DO... Remove this line when you implement this function

def play_hand(hand, word_list):

Function: play_hand

Example Play:

Input: Current hand and words.

Output: Score updates and remaining hand.

#
"""
"""
""“

Allows the user to play the given hand, as follows:

* The hand is displayed.

* The user may input a word.

* When any word is entered (valid or invalid), it uses up letters from the hand.

* An invalid word is rejected, and a message is displayed asking the user to choose another word.

* After every valid word: the score for that word is displayed, the remaining letters in the hand are displayed, and the user is asked to input another word.

* The sum of the word scores is displayed when the hand finishes.

* The hand finishes when there are no more unused letters.

The user can also finish playing the hand by inputing two exclamation points (the string '!!') instead of a word.

hand: dictionary (string -> int)

word_list: list of lowercase strings

returns: the total score for the hand # Problem #5: Playing a hand # def calculate_handlen(hand): """

Returns the length (number of letters) in the current hand.

hand: dictionary (string-> int)

returns: integer """

pass # TO DO... Remove this line when you implement this function def play_hand(hand, word_list): """

Allows the user to play the given hand, as follows:

* The hand is displayed.

* The user may input a word.

* When any word is entered (valid or invalid), it uses up letters from the hand.

* An invalid word is rejected, and a message is displayed asking the user to choose another word.

* After every valid word: the score for that word is displayed, the remaining letters in the hand are displayed, and the user is asked to input another word.

* The sum of the word scores is displayed when the hand finishes.

* The hand finishes when there are no more unused letters.

The user can also finish playing the hand by inputing two exclamation points (the string '!!') instead of a word.

hand: dictionary (string -> int)

word_list: list of lowercase strings

returns: the total score for the hand

# BEGIN PSEUDOCODE <-- Remove this comment when you implement this function

# Keep track of the total score

# As long as there are still letters left in the hand:

# Display the hand

# Ask user for input

# If the input is two exclamation points:

# End the game (break out of the loop)

# Otherwise (the input is not two exclamation points):

# If the word is valid:

# Tell the user how many points the word earned,

# and the updated total score

# Otherwise (the word is not valid):

# Reject invalid word (print a message)

# update the user's hand by removing the letters of their inputted word

# Game is over (user entered '!!' or ran out of letters),

# so tell user the total score

# Return the total score as result of function

Problem 6. Playing a game

A game consists of playing multiple hands. We need to implement two final functions to complete our wordgame. Implement the substitute_hand and play_game functions according to their specifications.For the game, you should use the HAND_SIZE constant to determine the number of letters ina hand.

Do not assume that there will always be 7 letters in a hand! Our goal is to keep the code modular - if you want to try playing your word game with 10 letters or 4 letters you will be able to do it by simply changing the value of HAND_SIZE ! When implementing substitution, you might want to check the methods associated with dictionaries, such as .keys , or review the del keyword . You may also want to look at the code for deal_hand to see how random.choice can be used to select an element at random from a set of elements (such as a string).

Note that we are not providing you with pseudocode for this problem. However, as you are deciding how to implement these functions, you may want to write your own as a guideline.

Problem #6: Playing a game

# procedure you will use to substitute a letter in a hand # def substitute_hand(hand, letter): """ Allow the user to replace all copies of one letter in the hand (chosen by user) with a new letter chosen from the VOWELS and CONSONANTS at random. The new letter should be different from user's choice, and should not be any of the letters already in the hand. If user provide a letter not in the hand, the hand should be the same. Has no side effects: does not mutate hand. For example: substitute_hand({'h':1, 'e':1, 'l':2, 'o':1}, 'l') might return:

# #

{'h':1, 'e':1, 'o':1, 'x':2} -> if the new letter is 'x'

The new letter should not be 'h', 'e', 'l', or 'o' since those letters were already in the hand.

hand: dictionary (string -> int) letter: string returns: dictionary (string -> int)

pass # TO DO... Remove this line when you implement this function

def play_game(word_list):

Allow the user to play a series of hands

* Asks the user to input a total number of hands

* Accumulates the score for each hand into a total score for the entire series

"""
"""

* For each hand, before playing, ask the user if they want to substitute one letter for another. If the user inputs 'yes', prompt them for their desired letter.

This can only be done once during the game. Once the substitue option is used, the user should not be asked if they want to substitute letters in the future.

* For each hand, ask the user if they would like to replay the hand. If the user inputs 'yes', they will replay the hand and keep the better of the two scores for that hand. This can only be done once during the game. Once the replay option is used, the user should not be asked if they want to replay future hands. Replaying the hand does not count as one of the total number of hands the user initially wanted to play.

* Note: if you replay a hand, you do not get the option to substitute a letter - you must play whatever hand you just had.

* Returns the total score for the series of hands

word_list: list of lowercase strings

print("play_game not implemented.") # TO DO... Remove this line when you implement this function

#

# Build data structures used for entire session and play game

# Do not remove the "if __name__ == '__main__':" line - this code is executed # when the program is run directly, instead of through an import statement #

if __name__

== '__main__':

word_list = load_words()

play_game(word_list)

"""

Key Takeaways:

•Successfully implemented a word game using Python.

•Key functionalities include scoring, hand management, and word validation.

•Tested various scenarios including wildcard functionality.

•All tests passed successfully, demonstrating a robust and functional implementation.

Conclusion
and Summary
Contact Information: Email: support@programminghomeworkhelp.com Phone: +1(315)557-6473

Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.
Python Word Game Assignment by leo smith - Issuu