Esempio n. 1
0
    def gameloop():
        #步驟一:印出提示訊息
        print("請猜出 0 到 9 之間的正整數...")
        print("總共有三次機會猜出正確數字...")
        print("準備好了嗎...")

        #步驟二:建立計算核心物件
        g = Guess()

        #步驟三:遊戲的主要迴圈
        count = 0  #記錄次數
        flag = 0  #記錄答對與否
        while count < 3:
            count += 1
            n = input("請輸入:")

            result = g.answer(int(n))
            if result == 0:
                #步驟四之一:答對
                print("答對!")
                print("你猜了" + str(count) + "次...")
                flag = 1
            elif result == 1:
                print("大一點!")
            elif result == 2:
                print("小一點!")
            else:
                print("不正確的輸入...")

            if flag == 1:
                break

        #步驟四之二:沒有答對
        if flag == 0:
            print("猜了三次也沒猜對,答案是" + str(g.number) + "。")
Esempio n. 2
0
def gameMain():
    word = Word('words.txt')
    guess = Guess(word.randFromDB())

    finished = False
    hangman = Hangman()
    maxTries = hangman.getLife()

    while guess.numTries < maxTries:
        display = hangman.get(maxTries - guess.numTries)
        print(display)
        guess.display()
        guessedChar = input('Select a letter: ')
        if guessedChar.isalpha():
            if len(guessedChar) != 1:
                print('One character at a time!')
                continue
            if guessedChar in guess.guessedChars:
                print('You already guessed \"' + guessedChar + '\"')
                continue
            finished = guess.guess(guessedChar)
            if finished == True:
                break
        else:
            print('You must input Alphabet!!!')
            continue

    if finished == True:
        print('Success!! The answer is %s!!' %guess.secretWord)
    else:
        print(hangman.get(0))
        print('It is your guessed spell :', guess.guessedChars)
        print('Current : ', guess.currentStatus)
        print('Your Last Chance!! Insert Full Word!!')
        lastanswer = input('>>>>>>>> ')
        if lastanswer == guess.secretWord:
            print('Success!!')
        else:
            guess.answer = lastanswer
            print('word [' + guess.secretWord + ']')
            print('guess [' + guess.answer + ']')
            print('Fail')
class Controller:
    def __init__(self):
        self.g = Guess()
        self.count = 0
        self.flag = 0

        self.app = View(master=Tk())
        self.app.guess["command"] = self.guess
        self.app.again["command"] = self.again
        self.app.mainloop()

    def guess(self):
        self.count += 1
        result = self.g.answer(int(self.app.input.get()))
        if result == 0:
            self.app.display["text"] = "答對!你猜了" + str(self.count) + "次..."
            self.app.guess["state"] = "disabled"
            self.flag = 1
        elif result == 1:
            self.app.display["text"] = "大一點!"
        elif result == 2:
            self.app.display["text"] = "小一點!"
        else:
            self.app.display["text"] = "不正確的輸入..."

        if self.flag == 0 and self.count == 3:
            self.app.guess["state"] = "disabled"
            self.app.display["text"] = "猜了三次也沒猜對,答案是" + str(
                self.g.number) + "。"

        self.app.input.delete(0, 200)

    def again(self):
        self.g = Guess()
        self.count = 0
        self.flag = 0
        self.app.display["text"] = ""
        self.app.guess["state"] = "normal"