Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

completed PCC02 ananda23-cs #862

Open
wants to merge 1 commit into
base: community
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions 02/ananda23-cs/game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!python3
# Code Challenge 02 - Word Values Part II - a simple game
# http://pybit.es/codechallenge02.html

import itertools
import random

from data import DICTIONARY, LETTER_SCORES, POUCH

NUM_LETTERS = 7


def draw_letters():
"""Pick NUM_LETTERS letters randomly. Hint: use stdlib random"""
return random.choices(POUCH, k=NUM_LETTERS)


def input_word(draw):
"""Ask player for a word and validate against draw.
Use _validation(word, draw) helper."""
word = input("Form a valid word: ")
if _validation(word, draw):
return word

def _validation(word, draw):
"""Validations: 1) only use letters of draw, 2) valid dictionary word"""
for char in word:
if char.upper() not in draw or draw.count(char) != word.count(char):
raise ValueError
if word.lower() not in DICTIONARY:
raise ValueError

# From challenge 01:
def calc_word_value(word):
"""Calc a given word value based on Scrabble LETTER_SCORES mapping"""
return sum(LETTER_SCORES.get(char.upper(), 0) for char in word)


# Below 2 functions pass through the same 'draw' argument (smell?).
# Maybe you want to abstract this into a class?
# get_possible_dict_words and _get_permutations_draw would be instance methods.
# 'draw' would be set in the class constructor (__init__).
def get_possible_dict_words(draw):
"""Get all possible words from draw which are valid dictionary words.
Use the _get_permutations_draw helper and DICTIONARY constant"""
return [word for word in _get_permutations_draw(draw) if word.lower() in DICTIONARY]


def _get_permutations_draw(draw):
"""Helper for get_possible_dict_words to get all permutations of draw letters.
Hint: use itertools.permutations"""
return [''.join(p) for k in range(1, NUM_LETTERS+1) for p in itertools.permutations(draw, k)]


# From challenge 01:
def max_word_value(words):
"""Calc the max value of a collection of words"""
return max(words, key=calc_word_value)


def main():
"""Main game interface calling the previously defined methods"""
draw = draw_letters()
print('Letters drawn: {}'.format(', '.join(draw)))

word = input_word(draw)
word_score = calc_word_value(word)
print('Word chosen: {} (value: {})'.format(word, word_score))

possible_words = get_possible_dict_words(draw)

max_word = max_word_value(possible_words)
max_word_score = calc_word_value(max_word)
print('Optimal word possible: {} (value: {})'.format(
max_word, max_word_score))

game_score = word_score / max_word_score * 100
print('You scored: {:.1f}'.format(game_score))


if __name__ == "__main__":
main()