MITx 6.00.1x Python Notes: A Word Game

Problem

This game is a lot like Scrabble or Words With Friends. Letters are dealt to players, who then construct one or more words out of their letters. Each valid word receives a score, based on the length of the word and the letters in that word.
The rules of the game are as follows:
Dealing
  • A player is dealt a hand of $n$ letters chosen at random (assume $n=7$ for now).
  • The player arranges the hand into as many words as they want out of the letters, using each letter at most once.
  • Some letters may remain unused (these won't be scored).
Scoring
  • The score for the hand is the sum of the scores for each word formed.
  • The score for a word is the sum of the points for letters in the word, multiplied by the length of the word, plus 50 points if all $n$ letters are used on the first word created.
  • 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.
  • For example, 'weed' would be worth 32 points ($(4+1+1+2)$ for the four letters, then multiply by len('weed') to get $(4+1+1+2)\times4=32$). Be sure to check that the hand actually has 1 'w', 2 'e's, and 1 'd' before scoring the word!
  • As another example, if $n=7$ and you make the word 'waybill' on the first try, it would be worth 155 points (the base score for 'waybill' is $(4+1+4+3+1+1+1)\times7=105$, plus an additional 50 point bonus for using all $n$ letters).
Here is how the game output will look (human player):

Loading word list from file...
   83667 words loaded.
Enter n to deal a new hand, r to replay the last hand, or e to end game: n
Current Hand: p z u t t t o
Enter word, or a "." to indicate that you are finished: tot
"tot" earned 9 points. Total: 9 points
Current Hand: p z u t
Enter word, or a "." to indicate that you are finished: .
Total score: 9 points.

Enter n to deal a new hand, r to replay the last hand, or e to end game: r
Current Hand: p z u t t t o
Enter word, or a "." to indicate that you are finished: top
"top" earned 15 points. Total: 15 points
Current Hand: z u t t
Enter word, or a "." to indicate that you are finished: tu
That is not a valid word. Please choose another word
Current Hand: z u t t
Enter word, or a "." to indicate that you are finished: .
Total score: 15 points.

Enter n to deal a new hand, r to replay the last hand, or e to end game: n
Current Hand: a q w f f i p
Enter word, or a "." to indicate that you are finished: paw
"paw" earned 24 points. Total: 24 points
Current Hand: q f f i
Enter word, or a "." to indicate that you are finished: qi
"qi" earned 22 points. Total: 46 points
Current Hand: f f
Enter word, or a "." to indicate that you are finished: .
Total score: 46 points.

Enter n to deal a new hand, r to replay the last hand, or e to end game: n
Current Hand: a r e t i i n
Enter word, or a "." to indicate that you are finished: inertia
"inertia" earned 99 points. Total: 99 points
Run out of letters. Total score: 99 points.

Enter n to deal a new hand, r to replay the last hand, or e to end game: e

And the following is the result of computer & human players:

Enter n to deal a new hand, r to replay the last hand, or e to end game: n

Enter u to have yourself play, c to have the computer play: u

Current Hand: a s r e t t t
Enter word, or a "." to indicate that you are finished: tatters
"tatters" earned 99 points. Total: 99 points

Run out of letters. Total score: 99 points.

Enter n to deal a new hand, r to replay the last hand, or e to end game: r

Enter u to have yourself play, c to have the computer play: c

Current Hand:  a s r e t t t
"stretta" earned 99 points. Total: 99 points

Total score: 99 points.

Enter n to deal a new hand, r to replay the last hand, or e to end game: x
Invalid command.

Enter n to deal a new hand, r to replay the last hand, or e to end game: n

Enter u to have yourself play, c to have the computer play: me
Invalid command.

Enter u to have yourself play, c to have the computer play: you
Invalid command.

Enter u to have yourself play, c to have the computer play: c

Current Hand:  a c e d x l n
"axled" earned 65 points. Total: 65 points

Current Hand:  c n
Total score: 65 points.

Enter n to deal a new hand, r to replay the last hand, or e to end game: n

Enter u to have yourself play, c to have the computer play: u

Current Hand: a p y h h z o
Enter word, or a "." to indicate that you are finished: zap 
"zap" earned 42 points. Total: 42 points

Current Hand: y h h o
Enter word, or a "." to indicate that you are finished: oy
"oy" earned 10 points. Total: 52 points

Current Hand: h h
Enter word, or a "." to indicate that you are finished: .
Goodbye! Total score: 52 points.

Enter n to deal a new hand, r to replay the last hand, or e to end game: r

Enter u to have yourself play, c to have the computer play: c

Current Hand:  a p y h h z o
"hypha" earned 80 points. Total: 80 points

Current Hand:  z o
Total score: 80 points.

Enter n to deal a new hand, r to replay the last hand, or e to end game: e

The required p4words.txt file: download

Python Code:


2 条评论:

  1. getting an invalid o/p in func as
    Function call: playGame()
    Test 10: Invalid input test. If the input is invalid, a message - 'Invalid command.' - should print out.
    Your output:
    Enter n to deal a new hand, r to replay the last hand, or e to end game: n
    Enter u to have yourself play, c to have the computer play: x
    Invalid command.
    Enter u to have yourself play, c to have the computer play: y
    *** Error: Your code called 'dealHand' more times than necessary. Be sure you are following the program specifications.

    *** ERROR: Failed to reject invalid input!
    Expected 'Invalid command.'
    Got '*** Error: Your code called 'dealHand' more times than necessary. Be sure you are following the program specifications.' ***
    Correct output:
    Enter n to deal a new hand, r to replay the last hand, or e to end game: n

    Enter u to have yourself play, c to have the computer play: x
    Invalid command.

    Enter u to have yourself play, c to have the computer play: y
    Invalid command.

    Enter u to have yourself play, c to have the computer play: z
    Invalid command.

    Enter u to have yourself play, c to have the computer play: k
    Invalid command.

    Enter u to have yourself play, c to have the computer play: s
    Invalid command.

    Enter u to have yourself play, c to have the computer play: w
    Invalid command.

    Enter u to have yourself play, c to have the computer play: c

    Hand passed to compPlayHand: a a a q s t v i i j


    Enter n to deal a new hand, r to replay the last hand, or e to end game: e

    None

    回复删除
    回复
    1. Just move the
      n = HAND_SIZE
      hand = dealHand(n)
      after 'if user_n == 'u':' and also after 'if user_n == 'c':'

      删除