Пример #1
0
def gameMain():
    word = Word('words.txt')
    guess = Guess(word.randFromDB())
    hangman = Hangman()

    while hangman.remainingLives > 0:

        display = hangman.currentShape()
        print(display)
        display = guess.displayCurrent()
        print('Current: ' + display)
        display = guess.displayGuessed()
        print('Already Used: ' + display)

        success = guess.guess(getChar())
        if success == 1:
            continue
        elif success == 2:
            continue
        elif success == False:
            hangman.decreaseLife()
        
        if guess.finished():
            break

    if guess.finished() == True:
        print('**** ' + guess.displayCurrent() + ' ****')
        print('Success')
    else:
        print(hangman.currentShape())
        print('word [' + guess.secretWord + ']')
        print('guess [' + guess.displayCurrent() + ']')
        print('Fail')
Пример #2
0
class TestGuess(unittest.TestCase):
    def setUp(self):
        self.g1 = Guess(self.secretWord)

    def tearDown(self):
        pass

    def wordcorrect(self):  #단어의 전체를 다 맞춘 경우 처리
        if self.currentStatus == self.secretWord:
            self.assertEqual(self.g1.finished(), True)
        else:
            self.assertEqual(self.g1.finished(), False)

    def testDisplayCurrent(self):  #부분적으로 맞추어진 단어의 상태가 올바르게 유지되는가?

        i = 0
        while i < 7:
            self.assertEqual(self.g1.displayCurrent(), self.g1.currentStatus)
        # self.assertEqual(self.g1.displayCurrent(), '_ e _ _ _ _ _ ')
        # self.g1.guess('a')
        # self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ _ ')
        # self.g1.guess('t')
        # self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ t ')
        # self.g1.guess('u')
        # self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')

    def testDisplayGuessed(self):  #이용된 글자들의 집합을 나타내는 데이터 유지 경우 처리
        i = 0
        string = ' e n '
        while i < 7:
            self.assertEqual(self.g1.displayGuessed(), string)
            self.g1.guess('guessedChar')
            string += ' ' + 'guessedChar' + ' '
Пример #3
0
class TestGuess(unittest.TestCase):
    def setUp(self):
        self.g1 = Guess('default')
        self.g2 = Guess('test')

    def tearDown(self):
        pass

    #생성자가 잘 작동되나 확인

    def testConstruct(self):
        self.assertEqual(self.g2.secretWord, 'test')
        self.assertEqual(self.g2.currentStatus, '_e__')
        self.assertEqual(self.g2.guessedChars, {'', 'e', 'n'})

    #guess함수 확인, False 와 True리턴값 확인
    def testGuess(self):
        self.assertEqual(self.g1.guess('q'), False)
        self.assertEqual(self.g1.guessedChars, {'', 'q', 'e', 'n'})
        self.assertEqual(self.g1.guess('D'), True)
        self.assertEqual(self.g1.currentStatus, 'de_____')
        self.assertEqual(self.g1.guess('a'), True)
        self.assertEqual(self.g1.currentStatus, 'de_a___')

    #정답을 맞췄을 때와 아닐 때 리턴값확인
    def testFinished(self):
        word_list = ['d', 'a', 'f', 'a', 'u', 'l', 't']
        self.assertEqual(self.g1.finished(), False)
        for w in word_list:
            self.g1.guess(w)
        self.assertEqual(self.g1.finished(), True)

    def testDisplayCurrent(self):
        self.assertEqual(self.g1.displayCurrent(), '_ e _ _ _ _ _ ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ _ ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')
        #없는 문자 추가되었을 때
        self.g1.guess('q')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')

    def testDisplayGuessed(self):
        self.assertEqual(self.g1.displayGuessed(), ' e n ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayGuessed(), ' a e n ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t u ')
        #중복된 문자추가
        self.g1.guess('a')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t u ')
        #len(charater) > 1인 문자
        self.g1.guess('aa')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t u ')
Пример #4
0
class TestGuess(unittest.TestCase):
    def setUp(self):
        self.g1 = Guess('default')

    def tearDown(self):
        pass

    def testDisplayCurrent(self):
        self.assertEqual(self.g1.displayCurrent(), '_ e _ _ _ _ _ ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ _ ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')

    def testDisplayGuessed(self):
        self.assertEqual(self.g1.displayGuessed(), ' e n ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayGuessed(), ' a e n ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t u ')

    def testGuess(self):
        self.assertEqual(self.g1.secretWord, 'default')
        self.assertEqual(self.g1.guess('a'), True)
        self.assertEqual(self.g1.currentStatus, '_e_a___')
        self.g1.guess('a')
        self.assertEqual(self.g1.guess('u'), True)
        self.g1.guess('u')
        self.assertEqual(self.g1.currentStatus, '_e_au__')
        self.assertEqual(self.g1.guess('w'), False)
        self.assertEqual(self.g1.secretWord, 'default')
        self.g1.guess('w')
        self.assertEqual(self.g1.secretWord, 'default')
        self.assertEqual(self.g1.currentStatus, '_e_au__')
        self.g1.guess('d')
        self.g1.guess('f')
        self.g1.guess('l')
        self.assertEqual(self.g1.finished(), False)
        self.g1.guess('t')
        self.assertEqual(self.g1.finished(), True)

    def testHangman(self):
        hang = hangman.Hangman()
        self.assertEqual(hang.remainingLives, 6)
        hang.decreaseLife()
        self.assertEqual(hang.remainingLives, 5)

    def testWord(self):
        wor = word.Word('words.txt')
        self.assertEqual(wor.test(), 'default')
        self.assertEqual(type(wor.randFromDB()), str)
Пример #5
0
class TestGuess(unittest.TestCase):
    def setUp(self):
        self.g1 = Guess('default')
        self.g2 = Guess("aaaaaaaa")

    def tearDown(self):
        pass

    def testDisplayCurrent(self):
        self.g1.guess('e')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ _ _ _ _ ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ _ ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')

    def testDisplayGuessed(self):
        self.g1.guess("e")
        self.g1.guess("n")
        self.assertEqual(self.g1.displayGuessed(), 'e n ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayGuessed(), 'a e n ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayGuessed(), 'a e n t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayGuessed(), 'a e n t u ')

    def testDisplayCurrent2(self):
        self.g2.guess("bbb")
        self.assertEqual(self.g2.displayCurrent(), "_ _ _ _ _ _ _ _ ")
        self.g2.guess(":")
        self.assertEqual(self.g2.displayCurrent(), "_ _ _ _ _ _ _ _ ")
        self.g2.guess("a")
        self.assertEqual(self.g2.displayCurrent(), "a a a a a a a a ")

    def testDisplayGuessed2(self):
        self.g2.guess("b")
        self.assertEqual(self.g2.displayGuessed(), "b ")
        self.g2.guess(":")
        self.assertEqual(self.g2.displayGuessed(), "b ")
        self.g2.guess("a")
        self.assertEqual(self.g2.displayGuessed(), "a b ")

    def testFinished(self):
        self.g1.guess("e")
        self.assertEqual(self.g1.finished(), False)
        self.assertEqual(self.g2.finished(), False)
        self.g2.guess("a")
        self.assertEqual(self.g2.finished(), True)
Пример #6
0
class TestGuess(unittest.TestCase):
    def setUp(self):
        self.g1 = Guess('default')

    def tearDown(self):
        pass

    def testDisplayCurrent(self):
        self.assertEqual(self.g1.displayCurrent(), '_ e _ _ _ _ _ ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ _ ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')

    def testDisplayGuessed(self):
        self.assertEqual(self.g1.displayGuessed(), ' e n ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayGuessed(), ' a e n ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t u ')

    def testguess(self):
        #리턴 값이 올바른가
        self.assertTrue(self.g1.guess('d'))
        self.assertFalse(self.g1.guess('r'))
        #부분적으로 맞추어진 단어의 상태가 올바르게 유지되는가
        #이용된 글자들의 집합을 나타내는 데이터는 올바르게 유지되는가
        self.assertEqual(self.g1.currentStatus, 'de_____')
        self.g1.guess('a')
        self.assertEqual(self.g1.currentStatus, 'de_a___')
        self.g1.guess('t')
        self.assertEqual(self.g1.currentStatus, 'de_a__t')
        self.g1.guess('u')
        self.assertEqual(self.g1.currentStatus, 'de_au_t')

    def testfinished(self):
        #단어 전체를 다 맞춘 경우에 대한 처리가 올바른가?
        self.assertFalse(self.g1.finished())
        self.g1.guess('d')
        self.g1.guess('r')
        self.g1.guess('a')
        self.g1.guess('t')
        self.g1.guess('u')
        self.g1.guess('f')
        self.g1.guess('l')
        self.assertTrue(self.g1.finished())
Пример #7
0
class TestGuess(unittest.TestCase):

    def setUp(self):
        self.g1 = Guess('default')

    def tearDown(self):
        pass

    def testDisplayCurrent(self):
        self.assertEqual(self.g1.displayCurrent(), '_ e _ _ _ _ _ ')
        self.g1.guess('a')
        self.assertEqual(self.g1.finished(), False)
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ _ ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')
        self.g1.guess('d')
        self.assertEqual(self.g1.displayCurrent(), 'd e _ a u _ t ')
        self.g1.guess('f')
        self.assertEqual(self.g1.displayCurrent(), 'd e f a u _ t ')
        self.g1.guess('k')
        self.assertEqual(self.g1.displayCurrent(), 'd e f a u _ t ')
        self.g1.guess('l')
        self.assertEqual(self.g1.displayCurrent(), 'd e f a u l t ')
        self.assertEqual(self.g1.finished(), True)

    def testDisplayGuessed(self):
        self.assertEqual(self.g1.displayGuessed(), ' e n ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayGuessed(), ' a e n ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t u ')
        self.g1.guess('d')
        self.assertEqual(self.g1.displayGuessed(), ' a d e n t u ')
        self.g1.guess('f')
        self.assertEqual(self.g1.displayGuessed(), ' a d e f n t u ')
        self.g1.guess('k')
        self.assertEqual(self.g1.displayGuessed(), ' a d e f k n t u ')
        self.g1.guess('l')
        self.assertEqual(self.g1.displayGuessed(), ' a d e f k l n t u ')

        self.g1.guess('a')
        self.assertEqual(self.g1.guess('a'), True)
        self.g1.guess('b')
        self.assertEqual(self.g1.guess('b'), False)
Пример #8
0
class TestGuess(unittest.TestCase):
    def setUp(self):
        self.g1 = Guess('apple')

    def tearDown(self):
        pass

    def testDisplayCurrent(self):
        self.g1.guess('a')
        self.assertEqual(self.g1.displayCurrent(), 'a _ _ _ _ ')
        self.g1.guess('p')
        self.assertEqual(self.g1.displayCurrent(), 'a p p _ _ ')
        self.g1.guess('l')
        self.assertEqual(self.g1.displayCurrent(), 'a p p l _ ')

    def testDisplayGuessed(self):
        self.g1.guess('t')
        self.assertEqual(self.g1.displayGuessed(), ' t ')
        self.g1.guess('q')
        self.assertEqual(self.g1.displayGuessed(), ' q t ')
        self.g1.guess('s')
        self.assertEqual(self.g1.displayGuessed(), ' q s t ')
        self.g1.guess('n')
        self.assertEqual(self.g1.displayGuessed(), ' n q s t ')

    def testFinish(self):
        self.g1.secretWord = 'apple'
        self.g1.currentStatus = 'apple'
        self.assertTrue(self.g1.finished())
Пример #9
0
def gameMain():
    word = Word('words.txt')
    guess = Guess(word.randFromDB())
    hangman = Hangman()

    while hangman.remainingLives > 0:

        display = hangman.currentShape()
        print(display)
        display = guess.displayCurrent()
        print('Current: ' + display)
        display = guess.displayGuessed()
        print('Already Used: ' + display)

        guessedChar = input('Select a letter: ')
        if len(guessedChar) != 1:
            print('One character at a time!')
            continue
        if guessedChar in guess.guessedChars:
            print('You already guessed \"' + guessedChar + '\"')
            continue

        success = guess.guess(guessedChar)
        if success == False:
            hangman.decreaseLife()

        if guess.finished() == True:
            print('**** ' + guess.displayCurrent() + ' ****')
            print('Success')
            break
    else:
        print(hangman.currentShape())
        print('word [' + guess.secretWord + ']')
        print('guess [' + guess.displayCurrent() + ']')
        print('Fail')
Пример #10
0
class TestGuess(unittest.TestCase):
    def setUp(self):
        self.g1 = Guess('default')

    def testDisplayCurrent(self):
        self.assertEqual(self.g1.secretWord, 'default')
        self.assertEqual(self.g1.currentStatus, '_e____')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ _ _ _ _ ')
        self.g1.guess('a')
        self.assertEqual(self.g1.currentStatus, '_e_a___')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ _ ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')
        self.assertEqual(self.g1.guess('d'),
                         True)  #단어에 있는 글자가 들어갔을 때 Guess 리턴 값
        self.g1.guess('f')
        self.assertEqual(self.g1.guess('j'), False)  #단어에 없는 글자가 들어갔을 때 리턴 값
        self.g1.guess('l')
        self.assertEqual(self.g1.displayCurrent(), 'd e f a u l t ')

    def testFinished(self):
        self.g1.guess('d')
        self.g1.guess('e')
        self.g1.guess('f')
        self.assertNotEqual(
            self.g1.finished(),
            True)  #finished함수가 secretWord를 완성시키지 않았을 때 종료하지 않는지
        self.g1.guess('a')
        self.g1.guess('u')
        self.g1.guess('l')
        self.g1.guess('t')
        self.assertEqual(self.g1.finished(), True)  #종료가 잘 되는지

    def testDisplayGuessed(self):
        self.assertEqual(self.g1.displayGuessed(), ' e n ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayGuessed(), ' a e n ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t u ')
        self.g1.guess('u')  #전에 입력되었던 값이 입력되었을 때
        self.assertEqual(self.g1.displayGuessed(), ' a e n t u ')
        self.g1.guess('U')  #대문자가 입력이 되었을때
        self.assertEqual(self.g1.displayGuessed(), ' a e n t u ')
Пример #11
0
class TestGuess(unittest.TestCase):
    def setUp(self):
        self.p1 = Guess('abcdedcba')

    def tearDown(self):
        pass


# test cases

    def testDisplayCurrent(self):
        self.p1.guess('e')
        self.assertEqual(self.p1.displayCurrent(), '_ _ _ _ e _ _ _ _ ')
        self.p1.guess('a')
        self.assertEqual(self.p1.displayCurrent(), 'a _ _ _ e _ _ _ a ')
        self.p1.guess('t')
        self.assertEqual(self.p1.displayCurrent(), 'a _ _ _ e _ _ _ a ')
        self.p1.guess('a')  # 이미 시도한 문자 입력시
        self.assertEqual(self.p1.displayCurrent(), 'a _ _ _ e _ _ _ a ')

    def testDisplayGuessed(self):
        self.p1.guess('e')
        self.p1.guess('n')
        self.assertEqual(self.p1.displayGuessed(), 'e n ')
        self.p1.guess('a')
        self.assertEqual(self.p1.displayGuessed(), 'a e n ')
        self.p1.guess('t')
        self.assertEqual(self.p1.displayGuessed(), 'a e n t ')
        self.p1.guess('u')
        self.assertEqual(self.p1.displayGuessed(), 'a e n t u ')
        self.p1.guess('c')  #틀린 소문자
        self.assertEqual(self.p1.displayGuessed(), 'a c e n t u ')

    def testGuess(self):
        self.assertEqual(self.p1.guess('e'), True)
        self.assertEqual(self.p1.currentStatus, '____e____')
        self.assertEqual(self.p1.guessedChars, ['e'])
        self.assertEqual(self.p1.guess('a'), True)
        self.assertEqual(self.p1.currentStatus, 'a___e___a')
        self.assertEqual(self.p1.guessedChars, ['e', 'a'])
        self.assertEqual(self.p1.guess('d'), True)
        self.assertEqual(self.p1.currentStatus, 'a__ded__a')
        self.assertEqual(self.p1.guessedChars, ['e', 'a', 'd'])
        self.assertEqual(self.p1.guess('u'), False)
        self.assertEqual(self.p1.currentStatus, 'a__ded__a')
        self.assertEqual(self.p1.guessedChars, ['e', 'a', 'd', 'u'])

    def testFinished(self):  # 단어의 전체를 다 맞춘경우 True를 반환하는지 테스트
        self.p1.guess('a')
        self.p1.guess('b')
        self.p1.guess('c')
        self.p1.guess('d')
        self.p1.guess('e')
        self.p1.guess('d')
        self.p1.guess('c')
        self.p1.guess('b')
        self.p1.guess('a')
        self.assertTrue(self.p1.finished())
Пример #12
0
def gameMain():
    word = Word('words.txt')
    guess = Guess(word.randFromDB())

    finished = False
    hangman = Hangman()

    while hangman.remainingLives > 0:

        display = hangman.currentShape()
        print(display)
        guess.displayCurrent()
        print("used letter: ")
        print()
        for i in string.ascii_lowercase:
            if i in guess.guessedChars:
                print(colored(i, 'red'), end=' ')
            else:
                print(colored(i, 'green'), end=' ')
        print()
        print()

        guessedChar = input('Select a letter: ')
        if len(guessedChar) != 1:
            print('One character at a time!')
            continue
        if guessedChar in guess.guessedChars:
            print('You already guessed \"' + guessedChar + '\"')
            continue

        success = guess.guess(guessedChar)
        print(success)
        if not success:
            hangman.decreaseLife()
        if guess.finished():
            break

    if guess.finished():
        print(guess.displayCurrent())
        print('Success')
    else:
        print(hangman.currentShape())
        print(guess.secretWord)
        guess.displayCurrent()
        print('Fail')
Пример #13
0
class TestGuess(unittest.TestCase):
    def setUp(self):
        self.g1 = Guess('default')

    def tearDown(self):
        pass

    def testDisplayCurrent(self):
        self.g1.guess('e')
        self.assertEqual(self.g1.displayCurrent(),
                         list('▢ e ▢ ▢ ▢ ▢ ▢'.split(' ')))
        self.assertFalse(self.g1.finished())
        self.g1.guess('a')
        self.assertEqual(self.g1.displayCurrent(),
                         list('▢ e ▢ a ▢ ▢ ▢'.split(' ')))
        self.assertFalse(self.g1.finished())
        self.g1.guess('t')
        self.assertEqual(self.g1.displayCurrent(),
                         list('▢ e ▢ a ▢ ▢ t'.split(' ')))
        self.assertFalse(self.g1.finished())
        self.g1.guess('u')
        self.assertEqual(self.g1.displayCurrent(),
                         list('▢ e ▢ a u ▢ t'.split(' ')))
        self.assertFalse(self.g1.finished())
        self.g1.guess('l')
        self.assertEqual(self.g1.displayCurrent(),
                         list('▢ e ▢ a u l t'.split(' ')))
        self.assertFalse(self.g1.finished())
        self.g1.guess('d')
        self.assertEqual(self.g1.displayCurrent(),
                         list('d e ▢ a u l t'.split(' ')))
        self.assertFalse(self.g1.finished())
        self.g1.guess('f')
        self.assertEqual(self.g1.displayCurrent(),
                         list('d e f a u l t'.split(' ')))
        self.assertTrue(self.g1.finished())

    def testDisplayGuessed(self):
        self.g1.guess('e')
        self.assertEqual(self.g1.displayGuessed(), [('e', 1)])
        self.g1.guess('d')
        self.assertEqual(self.g1.displayGuessed(), [('e', 1), ('d', 0)])
        self.g1.guess('a')
        self.assertEqual(self.g1.displayGuessed(), [('e', 1), ('d', 0),
                                                    ('a', 3)])
        self.g1.guess('t')
        self.assertEqual(self.g1.displayGuessed(), [('e', 1), ('d', 0),
                                                    ('a', 3), ('t', 6)])
        self.g1.guess('l')
        self.assertEqual(self.g1.displayGuessed(),
                         [('e', 1), ('d', 0), ('a', 3), ('t', 6), ('l', 5)])
        self.g1.guess('f')
        self.assertEqual(self.g1.displayGuessed(),
                         [('e', 1), ('d', 0), ('a', 3), ('t', 6), ('l', 5),
                          ('f', 2)])
        self.g1.guess('u')
        self.assertEqual(self.g1.displayGuessed(),
                         [('e', 1), ('d', 0), ('a', 3), ('t', 6), ('l', 5),
                          ('f', 2), ('u', 4)])
Пример #14
0
class TestGuess(unittest.TestCase):

    def setUp(self):
        self.g1 = Guess('default')

    def tearDown(self):
        pass

    def testDisplayCurrent(self):
        self.assertEqual(self.g1.displayCurrent(), '_ e _ _ _ _ _ ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ _ ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')
        self.g1.guess('d')
        self.assertEqual(self.g1.displayCurrent(), 'd e _ a u _ t ')
        self.g1.guess('f')
        self.assertEqual(self.g1.displayCurrent(), 'd e f a u _ t ')
        self.g1.guess('l')
        self.assertEqual(self.g1.displayCurrent(), 'd e f a u l t ')

    def testDisplayGuessed(self):
        self.assertEqual(self.g1.displayGuessed(), ' e n ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayGuessed(), ' a e n ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t u ')

    def testGuess(self):
        self.assertTrue(self.g1.guess('d'))
        self.assertFalse(self.g1.guess('z'))
        # 리턴값이 올바른가?

        self.assertEqual('de%s' % ("_" * (len('default') - 2)), self.g1.currentStatus)

        # 부분적으로 맞추어진 단어의 상태가 올바르게 유지되는가?
        self.assertEqual({'e', '', 'n', 'd', 'z'}, self.g1.guessedChars)
        # 이용된 글자들의 집합을 나타내는 데이터는 올바르게 유지되는가?

        for c in "efault":
            self.g1.guess(c)
        is_finished = self.g1.finished()
        self.assertTrue(is_finished)
        # 단어의 전체를 다 맞춘 경우에 대한 처리가 올바른가?

        if __name__ == '__main__':
            unittest.main()
Пример #15
0
    def gameMain():
        word = Word('words.txt')
        guess = Guess(word.randFromDB())

        print('%d words in DB' % word.count)

        finished = False
        hangman = Hangman()

        while hangman.remainingLives > 0:

            display = hangman.currentShape()
            print(display)
            display = guess.displayCurrent()
            print('Current: ' + display)
            display = guess.displayGuessed()
            print('Already Used: ' + display)

            guessedChar = input("Select a Letter: ")

            if len(guessedChar) == 1:
                if 65 <= ord(guessedChar) <= 90 or 97 <= ord(
                        guessedChar) <= 122:
                    if 65 <= ord(guessedChar) <= 90:
                        guessedChar = chr(ord(guessedChar) + 32)
                    if guessedChar not in guess.used_set:
                        success = guess.guess(guessedChar)
                        if not success:
                            hangman.decreaseLife()

            finished = guess.finished()
            if finished:
                break

        if finished:
            print('Success')
            print('word [' + guess.word + ']')
        else:
            print('word [' + guess.word + ']')
            print("Guess:", end=" ")
            for i in range(len(guess.current)):
                print(guess.current[i], end=" ")
            print()
            print('Fail')
class TestGuess(unittest.TestCase):

    def setUp(self):
        self.g1 = Guess('default')
        self.g2 = Guess('heeeey')

    def tearDown(self):
        pass

    def testDisplayCurrent(self):
        self.assertEqual(self.g1.displayCurrent(), '_ e _ _ _ _ _ ')
        self.assertEqual(self.g2.displayCurrent(), '_ e e e e _ ')
        self.g1.guess('a')
        self.g2.guess('a')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ _ ')
        self.assertEqual(self.g2.displayCurrent(), '_ e e e e _ ')
        self.g1.guess('t')
        self.g2.guess('t')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ t ')
        self.assertEqual(self.g2.displayCurrent(), '_ e e e e _ ')
        self.g1.guess('u')
        self.g2.guess('u')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')
        self.assertEqual(self.g2.displayCurrent(), '_ e e e e _ ')
        self.g2.guess('h')
        self.assertEqual(self.g2.displayCurrent(), 'h e e e e _ ')
        self.g2.guess('y')
        self.assertEqual(self.g2.displayCurrent(), 'h e e e e y ')
        self.assertEqual(self.g2.finished(), True)

    def testDisplayGuessed(self):
        self.assertEqual(self.g1.displayGuessed(), ' e n ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayGuessed(), ' a e n ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t u ')
Пример #17
0
class TestGuess(unittest.TestCase):
    def setUp(self):
        self.g1 = Guess('default')

    def tearDown(self):
        pass

    def testDisplayCurrent(self):
        self.assertEqual(self.g1.displayCurrent(), '_ e _ _ _ _ _ ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ _ ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')

        self.g1.guess('a')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')
        self.g1.guess('c')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')
        self.g1.guess('D')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')
        self.g1.guess('가')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')
        self.g1.guess('d')
        self.assertEqual(self.g1.displayCurrent(), 'd e _ a u _ t ')
        self.g1.guess(' ')
        self.assertEqual(self.g1.displayCurrent(), 'd e _ a u _ t ')
        self.g1.guess('')
        self.assertEqual(self.g1.displayCurrent(), 'd e _ a u _ t ')
        self.g1.guess('*')
        self.assertEqual(self.g1.displayCurrent(), 'd e _ a u _ t ')

    def testDisplayGuessed(self):
        self.assertEqual(self.g1.displayGuessed(), ' e n ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayGuessed(), ' a e n ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t u ')

        self.g1.guess('a')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t u ')
        self.g1.guess('c')
        self.assertEqual(self.g1.displayGuessed(), ' a c e n t u ')
        self.g1.guess('D')
        self.assertEqual(self.g1.displayGuessed(), ' D a c e n t u ')
        self.g1.guess('가')
        self.assertEqual(self.g1.displayGuessed(), ' D a c e n t u 가 ')
        self.g1.guess('d')
        self.assertEqual(self.g1.displayGuessed(), ' D a c d e n t u 가 ')
        self.g1.guess(' ')
        self.assertEqual(self.g1.displayGuessed(), '   D a c d e n t u 가 ')
        self.g1.guess('*')
        self.assertEqual(self.g1.displayGuessed(), '   * D a c d e n t u 가 ')

    def testGuess(self):
        self.assertEqual(self.g1.displayCurrent(), '_ e _ _ _ _ _ ')
        self.assertEqual(self.g1.displayGuessed(), ' e n ')
        self.assertTrue(self.g1.guess('a'))
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ _ ')
        self.assertEqual(self.g1.displayGuessed(), ' a e n ')
        self.assertTrue(self.g1.guess('t'))
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ t ')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t ')
        self.assertTrue(self.g1.guess('u'))
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t u ')
        self.assertFalse(self.g1.guess('c'))
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')
        self.assertEqual(self.g1.displayGuessed(), ' a c e n t u ')
        self.assertFalse(self.g1.guess('D'))
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')
        self.assertEqual(self.g1.displayGuessed(), ' D a c e n t u ')
        self.assertFalse(self.g1.guess('가'))
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')
        self.assertEqual(self.g1.displayGuessed(), ' D a c e n t u 가 ')
        self.assertTrue(self.g1.guess('d'))
        self.assertEqual(self.g1.displayCurrent(), 'd e _ a u _ t ')
        self.assertEqual(self.g1.displayGuessed(), ' D a c d e n t u 가 ')
        self.assertFalse(self.g1.guess(' '))
        self.assertEqual(self.g1.displayCurrent(), 'd e _ a u _ t ')
        self.assertEqual(self.g1.displayGuessed(), '   D a c d e n t u 가 ')
        self.assertFalse(self.g1.guess('*'))
        self.assertEqual(self.g1.displayCurrent(), 'd e _ a u _ t ')
        self.assertEqual(self.g1.displayGuessed(), '   * D a c d e n t u 가 ')

        self.assertFalse(self.g1.finished())
        self.g1.guess('f')
        self.assertFalse(self.g1.finished())
        self.g1.guess('l')
        self.assertTrue(self.g1.finished())
Пример #18
0
class TestGuess(unittest.TestCase):
    def setUp(self):
        self.g1 = Guess('default')
        self.g2 = Guess('aback')

    def tearDown(self):
        pass

    def testDisplayCurrent(self):
        self.assertEqual(self.g1.displayCurrent(), '_ e _ _ _ _ _ ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ _ ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')
        self.g1.guess('u')  # 같은 문자가 한번 더 들어왔을 때 아무런 변화 x
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')

        self.assertEqual(self.g2.displayCurrent(), '_ _ _ _ _ ')
        self.g2.guess('a')  # 두번 나오는 알파벳도 한번에 보이게 출력
        self.assertEqual(self.g2.displayCurrent(), 'a _ a _ _ ')

    def testDisplayGuessed(self):
        self.assertEqual(self.g1.displayGuessed(), ' e n ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayGuessed(), ' a e n ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t u ')
        self.g1.guess('u')  # 같은 문자가 한번 더 들어왔을 때 아무런 변화 x
        self.assertEqual(self.g1.displayGuessed(), ' a e n t u ')

    def testGuess(self):
        self.assertEqual(self.g1.guessedChars,
                         {'', 'n', 'e'})  # 이용된 글자들의 집합을 나타내는 데이터 처리
        self.assertEqual(self.g1.currentStatus, '_e_____')  # 부분적으로 맞추어진 단어의 상태

        self.g1.guess('a')  # 맞췄을 때
        self.assertEqual(self.g1.guessedChars,
                         {'', 'e', 'a', 'n'})  # 이용된 글자들의 집합을 나타내는 데이터 처리
        self.assertEqual(self.g1.currentStatus, '_e_a___')  # 부분적으로 맞추어진 단어의 상태
        self.assertTrue(self.g1.guess('a'))  # 리턴값이 올바른지 (포함되니까 True 리턴)
        self.assertFalse(self.g1.finished())  # 정답을 아직 맞추지 못했을 때 데이터 처리

        self.g1.guess('k')  # 맞추지 못했을 때
        self.assertEqual(self.g1.guessedChars, {'', 'e', 'a', 'n', 'k'})
        self.assertEqual(self.g1.currentStatus, '_e_a___')
        self.assertFalse(self.g1.guess('k'))  # 리턴값이 올바른지 (포함안되니까 False 리턴)
        self.assertFalse(self.g1.finished())

        self.g1.guess('d')
        self.assertEqual(self.g1.guessedChars, {'', 'd', 'e', 'a', 'n', 'k'})
        self.assertEqual(self.g1.currentStatus, 'de_a___')
        self.assertTrue(self.g1.guess('d'))
        self.assertFalse(self.g1.finished())

        self.g1.guess('f')
        self.assertEqual(self.g1.guessedChars,
                         {'', 'd', 'e', 'f', 'a', 'n', 'k'})
        self.assertEqual(self.g1.currentStatus, 'defa___')
        self.assertTrue(self.g1.guess('f'))
        self.assertFalse(self.g1.finished())

        self.g1.guess('u')
        self.assertEqual(self.g1.guessedChars,
                         {'', 'd', 'e', 'f', 'a', 'u', 'n', 'k'})
        self.assertEqual(self.g1.currentStatus, 'defau__')
        self.assertTrue(self.g1.guess('u'))
        self.assertFalse(self.g1.finished())

        self.g1.guess('l')
        self.assertEqual(self.g1.guessedChars,
                         {'', 'd', 'e', 'f', 'a', 'u', 'l', 'n', 'k'})
        self.assertEqual(self.g1.currentStatus, 'defaul_')
        self.assertTrue(self.g1.guess('l'))
        self.assertFalse(self.g1.finished())

        self.g1.guess('t')
        self.assertEqual(self.g1.guessedChars,
                         {'', 'd', 'e', 'f', 'a', 'u', 'l', 't', 'n', 'k'})
        self.assertEqual(self.g1.currentStatus, 'default')
        self.assertTrue(self.g1.guess('t'))
        self.assertTrue(self.g1.finished())
Пример #19
0
class HangmanGame(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        # Initialize word database
        self.word = Word('words.txt')

        # Hangman display window
        self.hangmanWindow = QTextEdit()
        self.hangmanWindow.setReadOnly(True)
        self.hangmanWindow.setAlignment(Qt.AlignLeft)
        font = self.hangmanWindow.font()
        font.setFamily('Courier New')
        self.hangmanWindow.setFont(font)

        # Layout
        hangmanLayout = QGridLayout()
        hangmanLayout.addWidget(self.hangmanWindow, 0, 0)

        # Status Layout creation
        statusLayout = QGridLayout()

        # Display widget for current status
        self.currentWord = QLineEdit()
        self.currentWord.setReadOnly(True)
        self.currentWord.setAlignment(Qt.AlignCenter)
        self.currentWord.setMinimumWidth(400)
        font = self.currentWord.font()
        font.setPointSize(font.pointSize() + 8)
        self.currentWord.setFont(font)
        statusLayout.addWidget(self.currentWord, 0, 0, 1, 2)

        # Display widget for already used characters
        self.guessedChars = QLineEdit()
        self.guessedChars.setReadOnly(True)
        self.guessedChars.setAlignment(Qt.AlignLeft)
        self.guessedChars.setMaxLength(52)
        statusLayout.addWidget(self.guessedChars, 1, 0, 1, 2)

        # Display widget for message output
        self.message = QLineEdit()
        self.message.setReadOnly(True)
        self.message.setAlignment(Qt.AlignLeft)
        self.message.setMaxLength(52)
        statusLayout.addWidget(self.message, 2, 0, 1, 2)

        # Input widget for user selected characters
        self.charInput = QLineEdit()
        self.charInput.setMaxLength(1)
        statusLayout.addWidget(self.charInput, 3, 0)

        # Button for submitting a character
        self.guessButton = QToolButton()
        self.guessButton.setText('Guess!')
        self.guessButton.clicked.connect(self.guessClicked)
        statusLayout.addWidget(self.guessButton, 3, 1)

        # Button for a new game
        self.newGameButton = QToolButton()
        self.newGameButton.setText('New Game')
        self.newGameButton.clicked.connect(self.startGame)
        statusLayout.addWidget(self.newGameButton, 4, 0)

        # Layout placement
        mainLayout = QGridLayout()
        mainLayout.setSizeConstraint(QLayout.SetFixedSize)
        mainLayout.addLayout(hangmanLayout, 0, 0)
        mainLayout.addLayout(statusLayout, 0, 1)

        self.setLayout(mainLayout)

        self.setWindowTitle('Hangman Game')

        # Start a new game on application launch!
        self.startGame()

    def startGame(self):
        self.hangman = Hangman()
        self.guess = Guess(self.word.randFromDB(0))
        self.gameOver = False

        self.hangmanWindow.setPlaceholderText(self.hangman.currentShape())
        self.currentWord.setText(self.guess.displayCurrent())
        self.guessedChars.setText(self.guess.displayGuessed())
        self.message.clear()

    def guessClicked(self):
        guessedChar = self.charInput.text()
        self.charInput.clear()
        self.message.clear()

        if self.gameOver == True:
            # 메시지 출력하고 - message.setText() - 리턴
            self.message.setText("Game Over...")
            return

        # 입력의 길이가 1 인지를 판단하고, 아닌 경우 메시지 출력, 리턴
        if len(guessedChar) != 1:
            self.message.setText("You must input one charactor!")
            return

        # 입력된 문자가 알파벳이 아니면 처리하지 않음
        if not ord('a') <= ord(guessedChar) <= ord('z'):
            self.message.setText("You can input a-z")
            return

        # 이미 사용한 글자인지를 판단하고, 아닌 경우 메시지 출력, 리턴
        if guessedChar in self.guessedChars.text():
            self.message.setText("You already input '%s'!" % guessedChar)
            return

        success = self.guess.guess(guessedChar)
        if success == False:
            # 남아 있는 목숨을 1 만큼 감소
            self.hangman.decreaseLife()
            # 메시지 출력
            if self.hangman.remainingLives > 1:
                self.message.setText("Wrong... %d lives left" %
                                     self.hangman.remainingLives)
            else:
                self.message.setText("Wrong... Only one life left")

        # hangmanWindow 에 현재 hangman 상태 그림을 출력
        self.hangmanWindow.setText(self.hangman.currentShape())

        # currentWord 에 현재까지 부분적으로 맞추어진 단어 상태를 출력
        self.currentWord.setText(self.guess.displayCurrent())

        # guessedChars 에 지금까지 이용한 글자들의 집합을 출력
        self.guessedChars.setText(self.guess.displayGuessed())

        if self.guess.finished():
            # 메시지 ("Success!") 출력하고, self.gameOver 는 True 로
            self.message.setText("Success!")
            self.gameOver = True
            pass

        elif self.hangman.getRemainingLives() == 0:
            # 메시지 ("Fail!" + 비밀 단어) 출력하고, self.gameOver 는 True 로
            self.currentWord.setText(self.guess.secretWord)
            self.message.setText("Fail!")
            self.gameOver = True
            pass
Пример #20
0
class HangmanGame(QWidget):

    def __init__(self, parent=None):
        super().__init__(parent)

        # Initialize word database        
        self.word = Word('words.txt')

        # Hangman display window
        self.hangmanWindow = QTextEdit()
        self.hangmanWindow.setReadOnly(True)
        self.hangmanWindow.setAlignment(Qt.AlignLeft)
        font = self.hangmanWindow.font()
        font.setFamily('Courier New')
        self.hangmanWindow.setFont(font)

        # Layout
        hangmanLayout = QGridLayout()
        hangmanLayout.addWidget(self.hangmanWindow, 0, 0)

        # Status Layout creation
        statusLayout = QGridLayout()

        # Display widget for current status
        self.currentWord = QLineEdit()
	self.currentWord.setFixedWidth(350)
        self.currentWord.setReadOnly(True)
        self.currentWord.setAlignment(Qt.AlignCenter)
        font = self.currentWord.font()
        font.setPointSize(font.pointSize() + 8)
        self.currentWord.setFont(font)
        statusLayout.addWidget(self.currentWord, 0, 0, 1, 2)

        # Display widget for already used characters
        self.guessedChars = QLineEdit()
        self.guessedChars.setReadOnly(True)
        self.guessedChars.setAlignment(Qt.AlignLeft)
        self.guessedChars.setMaxLength(52)
        statusLayout.addWidget(self.guessedChars, 1, 0, 1, 2)

        # Display widget for message output
        self.message = QLineEdit()
        self.message.setReadOnly(True)
        self.message.setAlignment(Qt.AlignLeft)
        self.message.setMaxLength(52)
        statusLayout.addWidget(self.message, 2, 0, 1, 2)

        # Input widget for user selected characters
        self.charInput = QLineEdit()
        self.charInput.setMaxLength(1)
        statusLayout.addWidget(self.charInput, 3, 0)

        # Button for submitting a character
        self.guessButton = QToolButton()
        self.guessButton.setText('Guess!')
        self.guessButton.clicked.connect(self.guessClicked)
        statusLayout.addWidget(self.guessButton, 3, 1)

        # Button for a new game
        self.newGameButton = QToolButton()
        self.newGameButton.setText('New Game')
        self.newGameButton.clicked.connect(self.startGame)
        statusLayout.addWidget(self.newGameButton, 4, 0)

        # Layout placement
        mainLayout = QGridLayout()
        mainLayout.setSizeConstraint(QLayout.SetFixedSize)
        mainLayout.addLayout(hangmanLayout, 0, 0)
        mainLayout.addLayout(statusLayout, 0, 1)

        self.setLayout(mainLayout)

        self.setWindowTitle('Hangman Game')

        # Start a new game on application launch!
        self.startGame()


    def startGame(self):
        self.hangman = Hangman()
        self.guess = Guess(self.word.randFromDB(20))
        self.gameOver = False

        if len(self.guess.secretWord) >=10:
            font = self.currentWord.font()
            font.setPointSize(font.pointSize())
            self.currentWord.setFont(font)


        self.hangmanWindow.setPlaceholderText(self.hangman.currentShape())
        self.currentWord.setText(self.guess.displayCurrent())
        self.guessedChars.setText(self.guess.displayGuessed())
        self.message.clear()

    def guessClicked(self):
        guessedChar = self.charInput.text()
        self.charInput.clear()
        self.message.clear()

        if self.gameOver == True:
            self.message.setText("게임이 끝났습니다.")

        if len(guessedChar) > 1:
            self.message.setText("한 글자만 입력하세요.")

        if guessedChar in self.guess.guessedChars:
            self.message.setText("이미 입력한 값 입니다.")
            self.hangman.decreaseLife()

        success = self.guess.guess(guessedChar)
        if success == False:
            if guessedChar == " ":
                self.message.setText("빈칸을 입력했습니다.")
            else:
                self.hangman.decreaseLife()
                self.hangmanWindow.setPlaceholderText(self.hangman.currentShape())
                self.currentWord.setText(self.guess.displayCurrent())
                self.guessedChars.setText(self.guess.displayGuessed())
                self.message.setText("틀렸습니다 다시 시도하세요.")

        if success == True:
            self.hangmanWindow.setPlaceholderText(self.hangman.currentShape())
            self.currentWord.setText(self.guess.displayCurrent())
            self.guessedChars.setText(self.guess.displayGuessed())

        if self.guess.finished():
            self.message.setText("Success!")
            self.gameOver = True


        elif self.hangman.getRemainingLives() == 0:
            self.message.setText("Fail : "+ self.guess.secretWord)
            self.gameOver = True
Пример #21
0
class HangmanGame(QWidget):

    def __init__(self, parent=None):
        super().__init__(parent)

        # Initialize word database        
        self.word = Word('words.txt')

        # Hangman display window
        self.hangmanWindow = QTextEdit()
        self.hangmanWindow.setReadOnly(True)
        self.hangmanWindow.setAlignment(Qt.AlignLeft)
        font = self.hangmanWindow.font()
        font.setFamily('Courier New')
        self.hangmanWindow.setFont(font)

        # Layout
        hangmanLayout = QGridLayout()
        hangmanLayout.addWidget(self.hangmanWindow, 0, 0)

        # Status Layout creation
        statusLayout = QGridLayout()

        # Display widget for current status
        self.currentWord = QLineEdit()
        self.currentWord.setReadOnly(True)
        self.currentWord.setAlignment(Qt.AlignCenter)
        font = self.currentWord.font()
        font.setPointSize(font.pointSize() + 8)
        self.currentWord.setFont(font)
        statusLayout.addWidget(self.currentWord, 0, 0, 1, 2)

        # Display widget for already used characters
        self.guessedChars = QLineEdit()
        self.guessedChars.setReadOnly(True)
        self.guessedChars.setAlignment(Qt.AlignLeft)
        self.guessedChars.setMaxLength(52)
        statusLayout.addWidget(self.guessedChars, 1, 0, 1, 2)

        # Display widget for message output
        self.message = QLineEdit()
        self.message.setReadOnly(True)
        self.message.setAlignment(Qt.AlignLeft)
        self.message.setMaxLength(52)
        statusLayout.addWidget(self.message, 2, 0, 1, 2)

        # Input widget for user selected characters
        self.charInput = QLineEdit()
        #self.charInput.setMaxLength(1)
        statusLayout.addWidget(self.charInput, 3, 0)

        # Button for submitting a character
        self.guessButton = QToolButton()
        self.guessButton.setText('Guess!')
        self.guessButton.clicked.connect(self.guessClicked)
        statusLayout.addWidget(self.guessButton, 3, 1)

        # Button for a new game
        self.newGameButton = QToolButton()
        self.newGameButton.setText('New Game')
        self.newGameButton.clicked.connect(self.startGame)
        statusLayout.addWidget(self.newGameButton, 4, 0)

        # Layout placement
        mainLayout = QGridLayout()
        mainLayout.setSizeConstraint(QLayout.SetFixedSize)
        mainLayout.addLayout(hangmanLayout, 0, 0)
        mainLayout.addLayout(statusLayout, 0, 1)

        self.setLayout(mainLayout)

        self.setWindowTitle('Hangman Game')

        # Start a new game on application launch!
        self.startGame()


    def startGame(self):
        self.hangman = Hangman()
        self.guess = Guess(self.word.randFromDB())
        self.gameOver = False

        self.hangmanWindow.setPlaceholderText(self.hangman.currentShape())
        self.currentWord.setText(self.guess.displayCurrent())
        self.guessedChars.setText(self.guess.displayGuessed())
        self.message.clear()

    def guessClicked(self):
        guessedChar = self.charInput.text()
        self.charInput.clear()
        self.message.clear()

        if self.gameOver == True:
            # 메시지 출력하고 - message.setText() - 리턴
            self.message.setText('Game Over')
            return

        # 입력의 길이가 1 인지를 판단하고, 아닌 경우 메시지 출력, 리턴
        if len(guessedChar) != 1:
            self.message.setText('Please enter only one character!')
            return

        # 입력한 문자가 대문자라면 소문자로 변경
        # (주석 처리된 코드 : 아스키 코드표는 빈칸을 취급하지 않기 때문에 이 조건문이 맨 앞에 있으면 오류가 뜸)
        #if 65 <= ord(guessedChar) <= 90:
        #    guessedChar = chr(ord(guessedChar) + 32)
        if guessedChar.upper():
            guessedChar = guessedChar.lower()

        # 이미 사용한 글자인지를 판단하고, 아닌 경우 메시지 출력, 리턴
        if guessedChar in self.guess.guessedChars:
        #if guessedChar in self.guess.displayGuessed():
            self.message.setText('You already guessed \"' + guessedChar + '\"')
            return

        # 알파벳 이외의 다른 모든 경우의 수를 차단
        # (아스키 코드표는 빈칸을 취급하지 않기 때문에 이 조건문이 맨 앞에 있으면 오류가 뜸)
        if ord(guessedChar) <= 64 or 91 <= ord(guessedChar) <= 96 or 123 <= ord(guessedChar):
            self.message.setText('Please enter the alphabet!')
            return

        success = self.guess.guess(guessedChar)
        if success == False:
            # 남아 있는 목숨을 1 만큼 감소
            self.hangman.decreaseLife()
            # 메시지 출력
            self.message.setText('Your guess is wrong...')

        # hangmanWindow 에 현재 hangman 상태 그림을 출력
        self.hangmanWindow.setPlaceholderText(self.hangman.currentShape())
        # currentWord 에 현재까지 부분적으로 맞추어진 단어 상태를 출력
        self.currentWord.setText(self.guess.displayCurrent())
        # guessedChars 에 지금까지 이용한 글자들의 집합을 출력
        self.guessedChars.setText(self.guess.displayGuessed())

        if self.guess.finished():
            # 메시지 ("Success!") 출력하고, self.gameOver 는 True 로
            self.message.setText('Success!')
            self.gameOver = True

        elif self.hangman.getRemainingLives() == 0:
            # 메시지 ("Fail!" + 비밀 단어) 출력하고, self.gameOver 는 True 로
            self.message.setText('Fail! ' + self.guess.secretWord)
            self.gameOver = True
class HangmanGame(QWidget):

    def __init__(self, parent=None):
        super().__init__(parent)

        # Initialize word database        
        self.word = Word('words.txt')

        # Hangman display window
        self.hangmanWindow = QTextEdit()
        self.hangmanWindow.setReadOnly(True)
        self.hangmanWindow.setAlignment(Qt.AlignLeft)
        font = self.hangmanWindow.font()
        font.setFamily('Courier New')
        self.hangmanWindow.setFont(font)

        # Layout
        hangmanLayout = QGridLayout()
        hangmanLayout.addWidget(self.hangmanWindow, 0, 0)

        # Status Layout creation
        statusLayout = QGridLayout()

        # Display widget for current status
        self.currentWord = QLineEdit()
        self.currentWord.setReadOnly(True)
        self.currentWord.setAlignment(Qt.AlignCenter)
# 가로길이를 300까지 크기를 키워주면 13글자 이상도 들어 갈 수 있다.
        self.currentWord.setFixedWidth(300)
        font = self.currentWord.font()
        font.setPointSize(font.pointSize() + 8)
        self.currentWord.setFont(font)
        statusLayout.addWidget(self.currentWord, 0, 0, 1, 2)

        # Display widget for already used characters
        self.guessedChars = QLineEdit()
        self.guessedChars.setReadOnly(True)
        self.guessedChars.setAlignment(Qt.AlignLeft)
        self.guessedChars.setMaxLength(52)
        statusLayout.addWidget(self.guessedChars, 1, 0, 1, 2)

        # Display widget for message output
        self.message = QLineEdit()
        self.message.setReadOnly(True)
        self.message.setAlignment(Qt.AlignLeft)
        self.message.setMaxLength(52)
        statusLayout.addWidget(self.message, 2, 0, 1, 2)

        # Input widget for user selected characters
        self.charInput = QLineEdit()
        self.charInput.setMaxLength(1)
        statusLayout.addWidget(self.charInput, 3, 0)

        # Button for submitting a character
        self.guessButton = QToolButton()
        self.guessButton.setText('Guess!')
        self.guessButton.clicked.connect(self.guessClicked)
        statusLayout.addWidget(self.guessButton, 3, 1)

        # Button for a new game
        self.newGameButton = QToolButton()
        self.newGameButton.setText('New Game')
        self.newGameButton.clicked.connect(self.startGame)
        statusLayout.addWidget(self.newGameButton, 4, 0)

        # Layout placement
        mainLayout = QGridLayout()
        mainLayout.setSizeConstraint(QLayout.SetFixedSize)
        mainLayout.addLayout(hangmanLayout, 0, 0)
        mainLayout.addLayout(statusLayout, 0, 1)

        self.setLayout(mainLayout)

        self.setWindowTitle('Hangman Game')

        # Start a new game on application launch!
        self.startGame()


    def startGame(self):
        self.hangman = Hangman()
        #randFromDB() 괄호 사이에 숫자 입력하여 랜덤 길이 넣어준다. !!
        word = self.word.randFromDB(15)
        
        self.guess = Guess(self.word.randFromDB())
        self.gameOver = False
        self.hangmanWindow.setPlaceholderText(self.hangman.currentShape())
        self.currentWord.setText(self.guess.displayCurrent())
        self.guessedChars.setText(self.guess.displayGuessed())
        self.message.clear()


    def guessClicked(self):
        guessedChar = self.charInput.text()
        self.charInput.clear()
        self.message.clear()

        if self.gameOver == True:
        # 메시지 출력하고 - message.setText() - 리턴
            self.message.setText("Game Over")
        # return 추가
            return "Finished"

        

        if len(guessedChar) != 1:
        # 입력의 길이가 1 인지를 판단하고, 아닌 경우 메시지 출력, 리턴
            self.charInput.setText("1글자만 입력하세요.")
            return "Finished"

        # 먼저 guessedChar를 소문자로 변형
        guessedChar = guessedChar.lower()
        
        if guessedChar in guess.guessedChars:
            self.message.setText("이미 추측한 글자입니다.")
            return "Finished"
        # 이미 사용한 글자인지를 판단하고, 아닌 경우 메시지 출력, 리턴

        success = self.guess.guess(guessedChar)
        
        if success == False:
            # 남아 있는 목숨을 1 만큼 감소
            hangman.decreaseLife()
            # 메시지 출력
            self.message.setText("not " + '"' + guessedChar + '"' + "in the word")


        # hangmanWindow 에 현재 hangman 상태 그림을 출력
        self.hangmanWindow.setPlaceholderText(self.hangman.currentShape())
        # currentWord 에 현재까지 부분적으로 맞추어진 단어 상태를 출력
        self.currentWord.setText(self.guess.displayCurrent())
        # guessedChars 에 지금까지 이용한 글자들의 집합을 출력        
        self.guessedChars.setText(self.guess.displayGuessed())

        if self.guess.finished():
            # 메시지 ("Success!") 출력하고, self.gameOver 는 True 로            
            self.message.setText("Success!")
            self.gmaeOver == True

        elif self.hangman.getRemainingLives() == 0:
            # 메시지 ("Fail!" + 비밀 단어) 출력하고, self.gameOver 는 True 로
            self.message.setText("Fail!" + guess.secretWord)
            self.gameOver == True
Пример #23
0
class TestGuess(unittest.TestCase):

    def setUp(self):
        self.g1 = Guess('default')

    def tearDown(self):
        pass

    def testReturn(self):
        self.assertTrue(self.g1.guess('a'))
        self.assertFalse(self.g1.guess('!')) # 실패 케이스
        self.assertTrue(self.g1.guess('t'))
        self.assertTrue(self.g1.guess('u'))
        self.assertFalse(self.g1.guess('k')) # 실패 케이스
        self.assertTrue(self.g1.guess('d'))
        self.assertFalse(self.g1.guess('q')) # 실패 케이스
        self.assertTrue(self.g1.guess('f'))
        self.assertFalse(self.g1.guess('1')) # 실패 케이스
        self.assertTrue(self.g1.guess('l'))

    def testDisplayCurrent(self):
        self.assertEqual(self.g1.displayCurrent(), '_ e _ _ _ _ _ ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ _ ')
        self.g1.guess('!') # 실패 케이스
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ _ ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')
        self.g1.guess('k') # 실패 케이스
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')
        self.g1.guess('d')
        self.assertEqual(self.g1.displayCurrent(), 'd e _ a u _ t ')
        self.g1.guess('q') # 실패 케이스
        self.assertEqual(self.g1.displayCurrent(), 'd e _ a u _ t ')
        self.g1.guess('f')
        self.assertEqual(self.g1.displayCurrent(), 'd e f a u _ t ')
        self.g1.guess('1') #실패 케이스
        self.assertEqual(self.g1.displayCurrent(), 'd e f a u _ t ')
        self.g1.guess('l')
        self.assertEqual(self.g1.displayCurrent(), 'd e f a u l t ')

    def testDisplayGuessed(self):
        self.assertEqual(self.g1.displayGuessed(), ' e n ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayGuessed(), ' a e n ')
        self.g1.guess('!') # 실패 케이스
        self.assertEqual(self.g1.displayGuessed(), ' ! a e n ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayGuessed(), ' ! a e n t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayGuessed(), ' ! a e n t u ')
        self.g1.guess('k') # 실패 케이스
        self.assertEqual(self.g1.displayGuessed(), ' ! a e k n t u ')
        self.g1.guess('d')
        self.assertEqual(self.g1.displayGuessed(), ' ! a d e k n t u ')
        self.g1.guess('q') # 실패 케이스
        self.assertEqual(self.g1.displayGuessed(), ' ! a d e k n q t u ')
        self.g1.guess('f')
        self.assertEqual(self.g1.displayGuessed(), ' ! a d e f k n q t u ')
        self.g1.guess('1') #실패 케이스
        self.assertEqual(self.g1.displayGuessed(), ' ! 1 a d e f k n q t u ')
        self.g1.guess('l')
        self.assertEqual(self.g1.displayGuessed(), ' ! 1 a d e f k l n q t u ')

    def testCorrect(self):
        self.g1.guess('a')
        self.assertFalse(self.g1.finished())
        self.g1.guess('!') # 실패 케이스
        self.assertFalse(self.g1.finished())
        self.g1.guess('t')
        self.assertFalse(self.g1.finished())
        self.g1.guess('u')
        self.assertFalse(self.g1.finished())
        self.g1.guess('k') # 실패 케이스
        self.assertFalse(self.g1.finished())
        self.g1.guess('d')
        self.assertFalse(self.g1.finished())
        self.g1.guess('q') # 실패 케이스
        self.assertFalse(self.g1.finished())
        self.g1.guess('f')
        self.assertFalse(self.g1.finished())
        self.g1.guess('1') #실패 케이스
        self.assertFalse(self.g1.finished())
        self.g1.guess('l')
        self.assertTrue(self.g1.finished())
Пример #24
0
class HangmanGame(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        # Initialize word database
        self.word = Word('words.txt')

        # Hangman display window
        self.hangmanWindow = QTextEdit()
        self.hangmanWindow.setReadOnly(True)
        self.hangmanWindow.setAlignment(Qt.AlignLeft)
        font = self.hangmanWindow.font()
        font.setFamily('Courier New')
        self.hangmanWindow.setFont(font)

        # Layout
        hangmanLayout = QGridLayout()
        hangmanLayout.addWidget(self.hangmanWindow, 0, 0)

        # Status Layout creation
        statusLayout = QGridLayout()

        # Display widget for current status
        self.currentWord = QLineEdit()
        self.currentWord.setReadOnly(True)
        self.currentWord.setAlignment(Qt.AlignCenter)
        font = self.currentWord.font()
        self.FONTSIZE = font.pointSize() + 8
        font.setPointSize(font.pointSize() + 8)
        self.currentWord.setFont(font)
        statusLayout.addWidget(self.currentWord, 0, 0, 1, 2)

        # Display widget for already used characters
        self.guessedChars = QLineEdit()
        self.guessedChars.setReadOnly(True)
        self.guessedChars.setAlignment(Qt.AlignLeft)
        self.guessedChars.setMaxLength(52)
        statusLayout.addWidget(self.guessedChars, 1, 0, 1, 2)

        # Display widget for message output
        self.message = QLineEdit()
        self.message.setReadOnly(True)
        self.message.setAlignment(Qt.AlignLeft)
        self.message.setMaxLength(52)
        statusLayout.addWidget(self.message, 2, 0, 1, 2)

        # Input widget for user selected characters
        self.charInput = QLineEdit()
        self.charInput.setMaxLength(1)
        statusLayout.addWidget(self.charInput, 3, 0)

        # Button for submitting a character
        self.guessButton = QToolButton()
        self.guessButton.setText('Guess!')
        self.guessButton.clicked.connect(self.guessClicked)
        statusLayout.addWidget(self.guessButton, 3, 1)

        # Button for a new game
        self.newGameButton = QToolButton()
        self.newGameButton.setText('New Game')
        self.newGameButton.clicked.connect(self.startGame)
        statusLayout.addWidget(self.newGameButton, 4, 0)

        # Layout placement
        mainLayout = QGridLayout()
        mainLayout.setSizeConstraint(QLayout.SetFixedSize)
        mainLayout.addLayout(hangmanLayout, 0, 0)
        mainLayout.addLayout(statusLayout, 0, 1)

        self.setLayout(mainLayout)

        self.setWindowTitle('Hangman Game')

        # Start a new game on application launch!
        self.startGame()

    def startGame(self):
        self.hangman = Hangman()
        self.guess = Guess(self.word.randFromDB(30))
        self.gameOver = False

        self.charInput.setDisabled(False)
        self.guessButton.setDisabled(False)
        self.hangmanWindow.setPlaceholderText(self.hangman.currentShape())
        self.currentWord.setText(self.guess.displayCurrent())
        self.guessedChars.setText(self.guess.displayGuessed())
        self.message.clear()
        self.message.setText("새 게임!")
        font = self.currentWord.font()
        if len(self.currentWord.text()) > 20:
            font.setPointSize(self.FONTSIZE -
                              (int((len(self.currentWord.text()) - 20) / 4) +
                               3))
            self.currentWord.setFont(font)
        else:
            font.setPointSize(self.FONTSIZE)
            self.currentWord.setFont(font)
        self.currentWord.setText(self.guess.displayCurrent())

    def guessClicked(self):
        guessedChar = self.charInput.text()
        self.charInput.clear()
        self.message.clear()

        if self.gameOver:
            # 메시지 출력하고 - message.setText() - 리턴
            self.message.setText("이미 끝난 게임입니다.")
            return

        # 입력의 길이가 1 인지를 판단하고, 아닌 경우 메시지 출력, 리턴
        elif len(guessedChar) != 1:
            self.message.setText("알파벳 한글자만 입력하세요.")
            return
        # 이미 사용한 글자인지를 판단하고, 아닌 경우 메시지 출력, 리턴
        elif guessedChar in self.guess.guessedChars:
            self.message.setText("이미 입력된 단어 " + guessedChar)
            return
        success = self.guess.guess(guessedChar)
        if not success:
            # 남아 있는 목숨을 1 만큼 감소
            # 메시지 출력
            self.hangman.decreaseLife()
            self.message.setText(guessedChar + " 는 단어에 없습니다.")

        # hangmanWindow 에 현재 hangman 상태 그림을 출력
        # currentWord 에 현재까지 부분적으로 맞추어진 단어 상태를 출력
        # guessedChars 에 지금까지 이용한 글자들의 집합을 출력

        self.hangmanWindow.setPlaceholderText(self.hangman.currentShape())
        self.currentWord.setText(self.guess.displayCurrent())
        self.guessedChars.setText(self.guess.displayGuessed())

        if self.guess.finished():
            # 메시지 ("Success!") 출력하고, self.gameOver 는 True 로
            self.message.setText("축하합니다!")
            self.charInput.setDisabled(True)
            self.guessButton.setDisabled(True)
            self.gameOver = True

        elif self.hangman.getRemainingLives() == 0:
            # 메시지 ("Fail!" + 비밀 단어) 출력하고, self.gameOver 는 True 로
            self.message.setText("졌습니다. 정답은 " + self.guess.secretWord)
            self.charInput.setDisabled(True)
            self.guessButton.setDisabled(True)
            self.gameOver = True
class TestGuess(unittest.TestCase):

    def setUp(self):
        self.g1 = Guess('default')
        self.g2 = Guess('apple')
        self.h1 = Hangman()
        self.w1 = Word('words.txt')

    def tearDown(self):
        pass

    #guess
    def testGuess(self):
        self.assertTrue(self.g1.guess('d'))
        self.assertFalse(self.g1.guess('s'))

    def testFinished(self):
        self.assertFalse(self.g2.finished())
        self.g2.guess('a')
        self.assertFalse(self.g2.finished())
        self.g2.guess('p')
        self.assertFalse(self.g2.finished())
        self.g2.guess('l')
        self.assertTrue(self.g2.finished())

    def testFinished2(self):
        self.assertFalse(self.g2.finished())
        for i in 'apl':
            self.g2.guess(i)
        self.assertTrue(self.g2.finished())

    def testDisplayCurrent(self):
        self.assertEqual(self.g1.displayCurrent(), '_ e _ _ _ _ _ ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ _ ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')

    def testDisplayGuessed(self):
        self.assertEqual(self.g1.displayGuessed(), ' e n ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayGuessed(), ' a e n ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t u ')

    #hangman
    def testDecreaseLife(self):
        self.assertEqual(self.h1.remainingLives, 6)
        self.h1.decreaseLife()
        self.assertEqual(self.h1.remainingLives, 5)

    def testCurrentShape(self):
        self.assertEqual(self.h1.currentShape(), self.h1.text[6])

    #word
    def testTest(self):
        self.assertEqual(self.w1.test(), 'default')

    def testRandFromDB(self):
        self.assertTrue(self.w1.randFromDB() in self.w1.words)
Пример #26
0
class TestGuess(unittest.TestCase):
    def setUp(self):
        self.g1 = Guess('default')

    def tearDown(self):
        pass

    def testDisplayCurrent(self):
        self.assertEqual(self.g1.displayCurrent(), '_ e _ _ _ _ _ ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ _ ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')

        self.g1.guess('q')  # false
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')

        self.g1.guess('a')  # Duplicate letters
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')

        self.g1.guess('1')  # int
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')

        self.g1.guess('D')  # upper
        self.assertEqual(self.g1.displayCurrent(), 'd e _ a u _ t ')

        self.g1.guess('@')  # Special Characters
        self.assertEqual(self.g1.displayCurrent(), 'd e _ a u _ t ')

        self.g1.guess('ㄴ')  #hangle
        self.assertEqual(self.g1.displayCurrent(), 'd e _ a u _ t ')

        self.g1.guess('as')  # len > 1
        self.assertEqual(self.g1.displayCurrent(), 'd e _ a u _ t ')

    def testDisplayGuessed(self):
        self.assertEqual(self.g1.displayGuessed(), ' e n ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayGuessed(), ' a e n ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t u ')
        self.g1.guess('c')
        self.assertEqual(self.g1.displayGuessed(), ' a c e n t u ')
        # Duplicate letters
        self.g1.guess('a')
        self.assertEqual(self.g1.displayGuessed(), ' a c e n t u ')
        # Special Characters
        self.g1.guess('@')
        self.assertEqual(self.g1.displayGuessed(), ' a c e n t u ')
        # int
        self.g1.guess('1')
        self.assertEqual(self.g1.displayGuessed(), ' a c e n t u ')

        # hangle
        self.g1.guess('ㄴ')
        self.assertEqual(self.g1.displayGuessed(), ' a c e n t u ')

    def testGuess(self):
        # secretword in  character

        self.assertFalse(self.g1.guess('c'))
        self.assertTrue(self.g1.guess('a'))

        # currentStatus
        self.assertEqual(self.g1.currentStatus, '_e_a___')
        self.g1.guess('t')
        self.assertEqual(self.g1.currentStatus, '_e_a__t')
        self.g1.guess('u')
        self.assertEqual(self.g1.currentStatus, '_e_au_t')
        self.g1.guess('as')
        self.assertEqual(self.g1.currentStatus, '_e_au_t')
        self.g1.guess('a')
        self.assertEqual(self.g1.currentStatus, '_e_au_t')
        self.g1.guess('1')
        self.assertEqual(self.g1.currentStatus, '_e_au_t')
        self.g1.guess('@')
        self.assertEqual(self.g1.currentStatus, '_e_au_t')
        self.g1.guess('ㄴ')
        self.assertEqual(self.g1.currentStatus, '_e_au_t')

    def testFinished(self):
        # return

        self.assertFalse(self.g1.finished())
        self.g1.guess('a')
        self.assertFalse(self.g1.finished())
        self.g1.guess('d')
        self.assertFalse(self.g1.finished())
        self.g1.guess('f')
        self.assertFalse(self.g1.finished())
        self.g1.guess('u')
        self.assertFalse(self.g1.finished())
        self.g1.guess('l')
        self.assertFalse(self.g1.finished())
        self.g1.guess('t')
        # True
        self.assertTrue(self.g1.finished())
Пример #27
0
class HangmanGame(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        # Initialize word database
        self.word = Word('words.txt')

        # Hangman display window
        self.hangmanWindow = QTextEdit()
        self.hangmanWindow.setReadOnly(True)
        self.hangmanWindow.setAlignment(Qt.AlignLeft)
        font = self.hangmanWindow.font()
        font.setFamily('Arial')
        self.hangmanWindow.setFont(font)

        # Layout
        hangmanLayout = QGridLayout()
        hangmanLayout.addWidget(self.hangmanWindow, 0, 0)

        # Status Layout creation
        statusLayout = QGridLayout()

        # Display widget for current status
        self.currentWord = QLineEdit()
        self.currentWord.setReadOnly(True)
        self.currentWord.setAlignment(Qt.AlignCenter)
        font = self.currentWord.font()
        font.setPointSize(font.pointSize() + 8)
        self.currentWord.setFont(font)
        statusLayout.addWidget(self.currentWord, 0, 0, 1, 2)

        # Display widget for already used characters
        self.guessedChars = QLineEdit()
        self.guessedChars.setReadOnly(True)
        self.guessedChars.setAlignment(Qt.AlignLeft)
        self.guessedChars.setMaxLength(52)
        statusLayout.addWidget(self.guessedChars, 1, 0, 1, 2)

        # Display widget for message output
        self.message = QLineEdit()
        self.message.setReadOnly(True)
        self.message.setAlignment(Qt.AlignLeft)
        self.message.setMaxLength(52)
        statusLayout.addWidget(self.message, 2, 0, 1, 2)

        # Input widget for user selected characters
        self.charInput = QLineEdit()
        self.charInput.setMaxLength(1)
        statusLayout.addWidget(self.charInput, 3, 0)

        # Button for submitting a character
        self.guessButton = QToolButton()
        self.guessButton.setText('Guess!')
        self.guessButton.clicked.connect(self.guessClicked)
        statusLayout.addWidget(self.guessButton, 3, 1)

        # Button for a new game
        self.newGameButton = QToolButton()
        self.newGameButton.setText('New Game')
        self.newGameButton.clicked.connect(self.startGame)
        statusLayout.addWidget(self.newGameButton, 4, 0)

        # Layout placement
        mainLayout = QGridLayout()
        mainLayout.setSizeConstraint(QLayout.SetFixedSize)
        mainLayout.addLayout(hangmanLayout, 0, 0)
        mainLayout.addLayout(statusLayout, 0, 1)

        self.setLayout(mainLayout)

        self.setWindowTitle('이범석의 행맨')

        # Start a new game on application launch!
        self.startGame()

    def startGame(self):
        self.hangman = Hangman()
        self.guess = Guess(self.word.randFromDB())
        self.gameOver = False

        self.hangmanWindow.setPlaceholderText(self.hangman.currentShape())
        self.currentWord.setText(self.guess.displayCurrent())
        self.guessedChars.setText(self.guess.displayGuessed())
        self.message.clear()

    def guessClicked(self):
        guessedChar = self.charInput.text()
        self.charInput.clear()
        self.message.clear()

        if self.gameOver == True:
            self.message.setText("실패했습니다.")
            return True

        if len(guessedChar) != 1:
            self.message.setText("1글자만 입력할 수 있습니다.")
            return True

        if guessedChar in self.guess.guessedChars:
            self.message.setText('이미 \"' + guessedChar + '\"' + "글자를 입력하였습니다.")
            return True

        if not guessedChar.isalpha():
            self.message.setText("올바른 알파벳을 입력해 주세요.")
            return True
        guessedChar = guessedChar.lower()

        success = self.guess.guess(guessedChar)
        if not success:
            self.hangman.decreaseLife()

        self.message.setText("Life:" + str(self.hangman.getRemainingLives()))
        self.hangmanWindow.setPlaceholderText(self.hangman.currentShape())
        self.currentWord.setText(self.guess.displayCurrent())
        self.guessedChars.setText(self.guess.displayGuessed())

        if self.guess.finished():
            self.message.setText("성공!")
            self.gameOver = True

        elif self.hangman.getRemainingLives() == 0:
            self.message.setText("실패!" + " 정답은 : " + self.guess.secretWord)
            self.gameOver = True
Пример #28
0
class TestGuess(unittest.TestCase):
    def setUp(self):
        self.g1 = Guess('default')
        self.g2 = Guess('access')  # word that has same letters
        self.h1 = Hangman()
        self.w1 = Word('words.txt')

    def tearDown(self):
        pass

    def testDisplayCurrent(self):
        self.assertEqual(self.g1.displayCurrent(), '_ e _ _ _ _ _ ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ _ ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')
        self.g1.guess('k')  # letter not in 'default'
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')
        self.g1.guess('a')  # already guessed letter (not processed in main)
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a u _ t ')
        self.g1.guess('d')
        self.assertEqual(self.g1.displayCurrent(), 'd e _ a u _ t ')
        self.g1.guess('f')
        self.assertEqual(self.g1.displayCurrent(), 'd e f a u _ t ')
        self.g1.guess('l')
        self.assertEqual(self.g1.displayCurrent(), 'd e f a u l t ')

        self.assertEqual(self.g2.displayCurrent(), '_ _ _ e _ _ ')
        self.g2.guess('a')
        self.assertEqual(self.g2.displayCurrent(), 'a _ _ e _ _ ')
        self.g2.guess('s')
        self.assertEqual(self.g2.displayCurrent(), 'a _ _ e s s ')

    def testDisplayGuessed(self):
        self.assertEqual(self.g1.displayGuessed(), ' e n ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayGuessed(), ' a e n ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayGuessed(), ' a e n t u ')
        self.g1.guess('k')  # letter not in 'default'
        self.assertEqual(self.g1.displayGuessed(), ' a e k n t u ')
        self.g1.guess('a')  # already guessed letter (not processed in main)
        self.assertEqual(self.g1.displayGuessed(), ' a e k n t u ')
        self.g1.guess('d')
        self.assertEqual(self.g1.displayGuessed(), ' a d e k n t u ')
        self.g1.guess('f')
        self.assertEqual(self.g1.displayGuessed(), ' a d e f k n t u ')
        self.g1.guess('l')
        self.assertEqual(self.g1.displayGuessed(), ' a d e f k l n t u ')

        self.assertEqual(self.g2.displayGuessed(), ' e n ')
        self.g2.guess('a')
        self.assertEqual(self.g2.displayGuessed(), ' a e n ')
        self.g2.guess('s')
        self.assertEqual(self.g2.displayGuessed(), ' a e n s ')

    def testGuess(self):
        self.assertEqual(self.g1.guessedChars, {'e', 'n', ''})
        self.assertEqual(self.g1.currentStatus, '_e_____')
        self.assertTrue(self.g1.guess('a'))
        self.assertEqual(self.g1.guessedChars, {'a', 'e', 'n', ''})
        self.assertEqual(self.g1.currentStatus, '_e_a___')
        self.assertTrue(self.g1.guess('f'))
        self.assertEqual(self.g1.guessedChars, {'a', 'e', 'f', 'n', ''})
        self.assertEqual(self.g1.currentStatus, '_efa___')
        self.assertFalse(self.g1.guess('k'))  # letter not in 'default'
        self.assertEqual(self.g1.guessedChars, {'a', 'e', 'f', 'k', 'n', ''})
        self.assertEqual(self.g1.currentStatus, '_efa___')

        self.assertEqual(self.g2.guessedChars, {'e', 'n', ''})
        self.assertEqual(self.g2.currentStatus, '___e__')
        self.assertTrue(self.g2.guess('a'))
        self.assertEqual(self.g2.guessedChars, {'a', 'e', 'n', ''})
        self.assertEqual(self.g2.currentStatus, 'a__e__')
        self.assertTrue(self.g2.guess('s'))
        self.assertEqual(self.g2.guessedChars, {'a', 'e', 'n', 's', ''})
        self.assertEqual(self.g2.currentStatus, 'a__ess')

    def testFinished(self):
        testList = ['d', 'e', 'f', 'a', 'u', 'l', 't']
        for letter in testList:
            self.g1.guess(letter)
        self.assertTrue(self.g1.finished())

    def testHangman(self):
        self.assertEqual(self.h1.remainingLives, 6)
        self.assertEqual(
            self.h1.currentShape(), '''\
   ____
  |    |
  |
  |
  |
  |
 _|_
|   |______
|          |
|__________|\
''')
        self.h1.decreaseLife()
        self.assertEqual(self.h1.remainingLives, 5)
        self.assertEqual(
            self.h1.currentShape(), '''\
   ____
  |    |
  |    o
  |
  |
  |
 _|_
|   |______
|          |
|__________|\
''')

    def testWord(self):
        self.assertEqual(self.w1.count, 19184)
        self.assertIn(self.w1.randFromDB(), self.w1.words)
Пример #29
0
class HangmanGame(QWidget):

    def __init__(self, parent=None):
        super().__init__(parent)

        # Initialize word database        
        self.word = Word('words.txt')

        # Hangman display window
        self.hangmanWindow = QTextEdit()
        self.hangmanWindow.setReadOnly(True)
        self.hangmanWindow.setAlignment(Qt.AlignLeft)
        font = self.hangmanWindow.font()
        font.setFamily('Courier New')
        self.hangmanWindow.setFont(font)

        # Layout
        hangmanLayout = QGridLayout()
        hangmanLayout.addWidget(self.hangmanWindow, 0, 0)

        # Status Layout creation
        statusLayout = QGridLayout()

        # Display widget for current status
        self.currentWord = QLineEdit()
        self.currentWord.setReadOnly(True)
        self.currentWord.setAlignment(Qt.AlignCenter)
        font = self.currentWord.font()
        font.setPointSize(font.pointSize() + 8)
        self.currentWord.setFont(font)
        statusLayout.addWidget(self.currentWord, 0, 0, 1, 2)

        # Display widget for already used characters
        self.guessedChars = QLineEdit()
        self.guessedChars.setReadOnly(True)
        self.guessedChars.setAlignment(Qt.AlignLeft)
        self.guessedChars.setMaxLength(52)
        statusLayout.addWidget(self.guessedChars, 1, 0, 1, 2)

        # Display widget for message output
        self.message = QLineEdit()
        self.message.setReadOnly(True)
        self.message.setAlignment(Qt.AlignLeft)
        self.message.setMaxLength(52)
        statusLayout.addWidget(self.message, 2, 0, 1, 2)

        # Input widget for user selected characters
        self.charInput = QLineEdit()
        self.charInput.setMaxLength(1)
        statusLayout.addWidget(self.charInput, 3, 0)

        # Button for submitting a character
        self.guessButton = QToolButton()
        self.guessButton.setText('Guess!')
        self.guessButton.clicked.connect(self.guessClicked)
        statusLayout.addWidget(self.guessButton, 3, 1)

        # Button for a new game
        self.newGameButton = QToolButton()
        self.newGameButton.setText('New Game')
        self.newGameButton.clicked.connect(self.startGame)
        statusLayout.addWidget(self.newGameButton, 4, 0)

        # Layout placement
        mainLayout = QGridLayout()
        mainLayout.setSizeConstraint(QLayout.SetFixedSize)
        mainLayout.addLayout(hangmanLayout, 0, 0)
        mainLayout.addLayout(statusLayout, 0, 1)

        self.setLayout(mainLayout)

        self.setWindowTitle('Hangman Game')

        # Start a new game on application launch!
        self.startGame()


    def startGame(self):
        self.hangman = Hangman()
        self.guess = Guess(self.word.randFromDB())

        font = self.currentWord.font()

        if len(self.guess.secretWord) > 13:
            #font.setPointSize(font.pointSize() - 8)
            font.setPointSize(8)
        else:
            #font.setPointSize(font.pointSize() + 8)
            font.setPointSize(12)

        self.currentWord.setFont(font)


        self.gameOver = False

        self.hangmanWindow.setPlaceholderText(self.hangman.currentShape())
        self.currentWord.setText(self.guess.displayCurrent())
        self.guessedChars.setText(self.guess.displayGuessed())
        self.message.clear()

    def displayCurrentStatus(self):
        str = ''
        for i in self.guess.currentStatus:
            str += i
            str += ' '
        return str
    def displayGuessedChars(self):
        str = ''
        for i in self.guess.guessedChars:
            str += i
            str += ' '
        return str
    
    def guessClicked(self):
        guessedChar = self.charInput.text()
        self.charInput.clear()
        self.message.clear()

        if self.gameOver == True:
            # 메시지 출력하고 - message.setText() - 리턴
            self.message.setText("Game_Over")
            return "gameover"

        # 입력의 길이가 1 인지를 판단하고, 아닌 경우 메시지 출력, 리턴
        if len(guessedChar) != 1:
            self.message.setText("Not a len 1 char")
            return "lengthError"
        # 이미 사용한 글자인지를 판단하고, 아닌 경우 메시지 출력, 리턴
        if guessedChar in self.guess.guessedChars:
            self.message.setText("Used char")
            return "Used char"

        success = self.guess.guess(guessedChar)
        if success == False:
            # 남아 있는 목숨을 1 만큼 감소
            self.hangman.decreaseLife() 
            # 메시지 출력
            self.message.setText(guessedChar +" not in word")

        # hangmanWindow 에 현재 hangman 상태 그림을 출력
        self.hangmanWindow.setPlaceholderText(self.hangman.currentShape()) 
        # currentWord 에 현재까지 부분적으로 맞추어진 단어 상태를 출력
        self.currentWord.setText(self.displayCurrentStatus())
        # guessedChars 에 지금까지 이용한 글자들의 집합을 출력
        self.guessedChars.setText(self.displayGuessedChars())

        if self.guess.finished():
            # 메시지 ("Success!") 출력하고, self.gameOver 는 True 로
            self.message.setText("Success!")
            self.gameOver = True
        elif self.hangman.getRemainingLives() == 0:
            # 메시지 ("Fail!" + 비밀 단어) 출력하고, self.gameOver 는 True 로
            self.message.setText("Fail" + self.word)
            self.gameOver = True
Пример #30
0
class TestGuess(unittest.TestCase):
    def setUp(self):
        self.g1 = Guess('default')

    def tearDown(self):
        pass

    def testInit(self):
        self.assertEqual(self.g1.secretWord, 'default')
        self.assertEqual(self.g1.currentStatus, '_e_____')
        self.assertEqual(self.g1.guessedChars, {'', 'n', 'e'})
        self.assertTrue(self.g1.guess(''))

    def testDisplayCurrent(self):
        # 기본 적으로 e가 들어가 았다.
        self.assertEqual(self.g1.displayCurrent(), '_ e _ _ _ _ _ ')
        # 존재하지 않는 알파벳 넣었을 경우
        self.g1.guess('i')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ _ _ _ _ ')
        self.g1.guess(' ')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ _ _ _ _ ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ _ ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ t ')
        # 이미 적은 값을 적은 경우
        self.g1.guess('t')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ t ')
        # 2개 이상의 문자를 한번에 적은 경우
        self.g1.guess('ul')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ t ')
        # 알파벳이 아닌 숫자를 넣은 경우
        self.g1.guess('10')
        self.assertEqual(self.g1.displayCurrent(), '_ e _ a _ _ t ')
        # 나머지 문자 완성
        self.g1.guess('d')
        self.assertEqual(self.g1.displayCurrent(), 'd e _ a _ _ t ')
        self.g1.guess('f')
        self.assertEqual(self.g1.displayCurrent(), 'd e f a _ _ t ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayCurrent(), 'd e f a u _ t ')
        self.g1.guess('l')
        self.assertEqual(self.g1.displayCurrent(), 'd e f a u l t ')

    def testDisplayGuessed(self):
        self.assertEqual(self.g1.displayGuessed(), ' e n ')
        self.g1.guess('i')
        self.assertEqual(self.g1.displayGuessed(), ' e i n ')
        self.g1.guess(' ')
        self.assertEqual(self.g1.displayGuessed(), '   e i n ')
        self.g1.guess('a')
        self.assertEqual(self.g1.displayGuessed(), '   a e i n ')
        self.g1.guess('t')
        self.assertEqual(self.g1.displayGuessed(), '   a e i n t ')
        self.g1.guess('ul')
        self.assertEqual(self.g1.displayGuessed(), '   a e i n t ul ')
        self.g1.guess('10')
        self.assertEqual(self.g1.displayGuessed(), '   10 a e i n t ul ')
        self.g1.guess('d')
        self.assertEqual(self.g1.displayGuessed(), '   10 a d e i n t ul ')
        self.g1.guess('f')
        self.assertEqual(self.g1.displayGuessed(), '   10 a d e f i n t ul ')
        self.g1.guess('u')
        self.assertEqual(self.g1.displayGuessed(), '   10 a d e f i n t u ul ')
        self.g1.guess('l')
        self.assertEqual(self.g1.displayGuessed(),
                         '   10 a d e f i l n t u ul ')

    def testGuess(self):
        self.assertEqual(self.g1.guessedChars, {'', 'n', 'e'})
        self.assertEqual(self.g1.currentStatus, '_e_____')
        self.assertFalse(self.g1.guess('i'))
        self.assertEqual(self.g1.guessedChars, {'i', '', 'n', 'e'})
        self.assertEqual(self.g1.currentStatus, '_e_____')
        self.assertFalse(self.g1.guess(' '))
        self.assertEqual(self.g1.guessedChars, {' ', 'i', '', 'n', 'e'})
        self.assertEqual(self.g1.currentStatus, '_e_____')
        self.assertTrue(self.g1.guess('a'))
        self.assertEqual(self.g1.guessedChars, {'a', ' ', 'i', '', 'n', 'e'})
        self.assertEqual(self.g1.currentStatus, '_e_a___')
        self.assertTrue(self.g1.guess('t'))
        self.assertEqual(self.g1.guessedChars,
                         {'t', 'a', ' ', 'i', '', 'n', 'e'})
        self.assertEqual(self.g1.currentStatus, '_e_a__t')
        self.assertTrue(self.g1.guess('ul'))
        self.assertEqual(self.g1.guessedChars,
                         {'ul', 't', 'a', ' ', 'i', '', 'n', 'e'})
        self.assertEqual(self.g1.currentStatus, '_e_a__t')
        self.assertFalse(self.g1.guess('10'))
        self.assertEqual(self.g1.guessedChars,
                         {'10', 'ul', 't', 'a', ' ', 'i', '', 'n', 'e'})
        self.assertEqual(self.g1.currentStatus, '_e_a__t')
        self.assertTrue(self.g1.guess('d'))
        self.assertEqual(self.g1.guessedChars,
                         {'d', '10', 'ul', 't', 'a', ' ', 'i', '', 'n', 'e'})
        self.assertEqual(self.g1.currentStatus, 'de_a__t')
        self.assertTrue(self.g1.guess('f'))
        self.assertEqual(
            self.g1.guessedChars,
            {'f', 'd', '10', 'ul', 't', 'a', ' ', 'i', '', 'n', 'e'})
        self.assertEqual(self.g1.currentStatus, 'defa__t')
        self.assertTrue(self.g1.guess('u'))
        self.assertEqual(
            self.g1.guessedChars,
            {'u', 'f', 'd', '10', 'ul', 't', 'a', ' ', 'i', '', 'n', 'e'})
        self.assertEqual(self.g1.currentStatus, 'defau_t')
        self.assertTrue(self.g1.guess('l'))
        self.assertEqual(
            self.g1.guessedChars,
            {'l', 'u', 'f', 'd', '10', 'ul', 't', 'a', ' ', 'i', '', 'n', 'e'})
        self.assertEqual(self.g1.currentStatus, 'default')

    def testFinished(self):
        self.g1.currentStatus = 'de'
        self.assertFalse(self.g1.finished())
        self.g1.currentStatus = 'default'
        self.assertTrue(self.g1.finished())