top of page

Python - Wordle 遊戲

  • 作家相片: Lucy Tsai
    Lucy Tsai
  • 2022年7月27日
  • 讀畢需時 1 分鐘

利用Python製作自己的Wordle遊戲。

Python程式:

from colorama import Fore

# set name
words = []
word = "catch"
mychoice = "_____"
rightwords = 0
finalword = ""
chance = 1

# Functions
def chooseWord():
    global mychoice, rightwords, finalword, chance
    finalword = ""

    if chance >= 6:
        print("Out of Chances")
        quit()

    print(Fore.LIGHTGREEN_EX + "Chance ", chance)
    chance += 1

    mychoice = input(Fore.LIGHTWHITE_EX + "Five letter: ")
    rightwords = 0

    if len(mychoice) != 5:
        print("not 5 words")
        chooseWord()
    else:
        checkWord()

def checkWord():
    global mychoice, rightwords, finalword

    for i in range(5):
        if mychoice[i] == word[i]:
            rightwords += 1

        if rightwords >= 5:
            print(Fore.LIGHTYELLOW_EX + "All pass")
            quit()

    # check right words
    for i in range(5):
        # individual right
        if mychoice[i] == word[i]:
            #print("the ", i + 1, " is correct")
            # correct space
            finalword += Fore.LIGHTYELLOW_EX + mychoice[i]
        elif mychoice[i] in word:
            # wrong space
            finalword += Fore.WHITE + mychoice[i]
        else:
            # doesn't exist
            finalword += Fore.LIGHTGREEN_EX + mychoice[i]

    print(finalword)
    if rightwords < 5:
        chooseWord()

# Main
print(Fore.LIGHTBLUE_EX + """
Rules:
Guess a five lettered word.

Yellow - correct space
Grey - right word, but wrong space
Green - not in the word

You have 6 chances.
""")
chooseWord()


Console:



 
 
 

最新文章

查看全部
ChatGPT 與 Google 影響之研究

ㄧ、背景介紹 (一)ChatGPT的背景 ChatGPT(Generative Pre-trained Transformer)是一個由OpenAI公司開發的人工智能聊天機器人。OpenAI是一個非營利組織,由OpenAI...

 
 
 

留言


Post: Blog2_Post
bottom of page