rmotr-curriculum / post-your-question-questions

0 stars 0 forks source link

OOP Hangman #43

Closed vckelly closed 5 years ago

vckelly commented 6 years ago

Hey Guys, I am having issues with the OOP Hangman assignment. Specifically, the GuessAttempt class tests are acting a little strange. The tests seem to be initializing GuessAttempt class instances with optional arguments 'hit' and 'miss'. This is causing my init function to complain.

My understanding of the class is that we should return booleans using the .is_hit and .is_miss class functions, but that we do not need to store (or initialize with) member attributes representing the results of the previously mentioned member functions.

Let me know if I'm missing something...thanks!

Assignment: http://learn.rmotr.com/python/post-a-question/post-your/questions

User code:

# your code here (if any)
from .exceptions import *

class GuessAttempt(object):

    def is_hit(self):
        if self.guess in self.answer:
            return True 
        else:
            return False

    def is_miss(self):
        if self.guess in self.answer:
            return False 
        else:
            return True

    def __init__(self, guess, word):
        self.guess = guess
        self.answer = word

class GuessWord(object):
    #helper function for __init__
    def _mask_word(self):
        self.masked = ('*' * len(self.answer))

    #helper function for perform attempt
    def _uncover_word(self, character):

        if character in self.masked or character.lower() in self.masked:
            raise InvalidGuessedLetterException('That letter has already been guessed!')

        else:
            idxs = [pos for pos, char in enumerate(self.answer) if char.lower() == character.lower()]
            for i in idxs:
                self.masked = self.masked[:i] + character +  self.masked[i+1:]

    #main perform guess function
    def perform_attempt(self, guess):
        if len(guess) > 1 or (not guess.isalpha()):
            raise InvalidGuessedLetterException('Invalid letter guess!')
        else:
            guess = guess.lower()
            self._uncover_word(guess)
            g_attempt = GuessAttempt(guess, self.answer)
            return g_attempt

    def __init__(self, word):
        if len(word) < 1:
            raise InvalidWordException('Invalid Word!')
        else:
            self.answer = word.lower()

        self.masked = ''
        self._mask_word()    
jsymons commented 6 years ago

So GuessAttempt is kind of a "dumb" class. It has no knowledge about the word. The actual attempts are made within perform_attempt and the logic of if the letter guessed is in the word is handled there. GuessAttempt's just receive the guess that was made, and if it was a hit or a miss.

Let me know if you need further clarification on this.