wordleとは?
https://www.nytimes.com/games/wordle/index.html
ワードル?ウードル?
5文字の英単語を当てるゲームです。
プレイヤーは6回の試行の間に5文字の英単語を推測する。推測の度にタイルの色が変化することでユーザーに情報が与えられ、正答の単語において推測した各文字が正しい位置にあるか、あるいは別の位置に存在するかが明らかにされる。(wikiより)
むずかしそうだったが、以外と仕組みだけは簡単でしたね。コードを写経したのですがね........
Richを使用
richというターミナルに色付けしてくれて、鮮やかにしてれるライブラリをinstall
from words import word_listは別ファイルを作成して、取り入れています。
これは、5文字の単語の羅列でランダムな正解の単語をここから選出します。
多分肝はここ
enumerateを使って、index番号と一緒に回す。
完全合致で、correct_place
一部一致(場所は違えど正解に含まれている文字)で、correct_letter
不一致でincorrect_letter
for i , letter in enumerate(guess):
if answer[i] == guess[i]:
guessed += correct_place(letter)
wordle_pattern.append(SQUARES['correct_place'])
elif letter in answer:
guessed += correct_letter(letter)
wordle_pattern.append(SQUARES['correct_letter'])
else:
guessed += correct_letter(letter)
wordle_pattern.append(SQUARES['incorrect_letter'])
return ''.join(guessed), ''.join(wordle_pattern)
full code
from asyncore import loop
import code
from pickle import TRUE
from unittest import FunctionTestCase
from rich.console import Console
from rich.prompt import Prompt
from random import choice
from words import word_list
WELCOM_MESSAGE = f'\n[white on blue] WELCOME TO WORDLE [/]\n'
PLAYER_INSTRUCTIONS = "You may start guessing\n"
ALLOWED_GUESSES = 6
# game loop
GUESS_STATEMENT = "\nEnter your guess"
SQUARES = {
'correct_place': '🟩',
'correct_letter': '🟨',
'incorrect_letter': '⬛'
}
def correct_place(letter):
return f'[black on green]{letter}[/]'
def correct_letter(letter):
return f'[black on yellow]{letter}[/]'
def incorrect_letter(letter):
return f'[black on white]{letter}[/]'
def check_guess(guess,answer):
# 推測
guessed = []
wordle_pattern = []
for i , letter in enumerate(guess):
if answer[i] == guess[i]:
guessed += correct_place(letter)
wordle_pattern.append(SQUARES['correct_place'])
elif letter in answer:
guessed += correct_letter(letter)
wordle_pattern.append(SQUARES['correct_letter'])
else:
guessed += correct_letter(letter)
wordle_pattern.append(SQUARES['incorrect_letter'])
return ''.join(guessed), ''.join(wordle_pattern)
def game(console,chosen_word):
end_of_game = False
already_guessed = []
full_wordle_pattern = []
all_words_guessed = []
while not end_of_game:
guess = Prompt.ask(GUESS_STATEMENT).upper()
while len(guess) != 5 or guess in already_guessed:
if guess in already_guessed:
console.print("[red]You've already guessed this word")
else:
console.print("[red]Please enter a 5-letter word")
guess =Prompt.ask(GUESS_STATEMENT)
already_guessed.append(guess)
guessed, pattern = check_guess(guess,chosen_word)
all_words_guessed.append(guessed)
full_wordle_pattern.append(pattern)
console.print(*all_words_guessed,sep="\n",)
console.print(*full_wordle_pattern, sep="\n")
# 正解
if guess == chosen_word or len(already_guessed) == ALLOWED_GUESSES:
end_of_game = True
if len(already_guessed) == ALLOWED_GUESSES and guess != chosen_word:
# 不正解の場合
console.print(f"\n[red] WORDLE X/{ALLOWED_GUESSES}[/]")
console.print(f"\n[green]Correct Word:{chosen_word}\n")
else:
# 正解の場合
console.print("正解です!!!")
console.print(f"\n[green]WORDLE {len(already_guessed)}/{ALLOWED_GUESSES}")
# block表示
console.print(*full_wordle_pattern, sep="\n")
# main
if __name__ == '__main__':
console = Console()
chosen_word = choice(word_list)
console.print(WELCOM_MESSAGE)
console.print(PLAYER_INSTRUCTIONS)
game(console,chosen_word)
word_list = ['ABOUT', 'ABOVE', 'ABUSE', 'ACTOR', 'ACUTE', 'ADMIT', 'ADOPT', 'ADULT', 'AFTER', 'AGAIN', 'AGENT', 'AGREE', 'AHEAD', 'ALARM', 'ALBUM', 'ALERT', 'ALIKE', 'ALIVE', 'ALLOW', 'ALONE', 'ALONG', 'ALTER', 'AMONG', 'ANGER', 'ANGLE', 'ANGRY', 'APART', 'APPLE', 'APPLY', 'ARENA', 'ARGUE', 'ARISE', 'ARRAY', 'ASIDE', 'ASSET', 'AUDIO', 'AUDIT', 'AVOID', 'AWARD', 'AWARE', 'BADLY', 'BAKER', 'BASES', 'BASIC', 'BASIS', 'BEACH', 'BEGAN', 'BEGIN', 'BEGUN', 'BEING', 'BELOW', 'BENCH', 'BILLY', 'BIRTH', 'BLACK', 'BLAME', 'BLIND', 'BLOCK', 'BLOOD', 'BOARD', 'BOOST', 'BOOTH', 'BOUND', 'BRAIN', 'BRAND', 'BREAD', 'BREAK', 'BREED', 'BRIEF', 'BRING', 'BROAD', 'BROKE', 'BROWN', 'BUILD', 'BUILT', 'BUYER', 'CABLE', 'CALIF', 'CARRY', 'CATCH', 'CAUSE', 'CHAIN', 'CHAIR', 'CHART', 'CHASE', 'CHEAP', 'CHECK', 'CHEST', 'CHIEF', 'CHILD', 'CHINA', 'CHOSE', 'CIVIL', 'CLAIM', 'CLASS', 'CLEAN', 'CLEAR', 'CLICK', 'CLOCK', 'CLOSE', 'COACH', 'COAST', 'COULD', 'COUNT', 'COURT', 'COVER', 'CRAFT', 'CRASH', 'CREAM', 'CRIME', 'CROSS', 'CROWD', 'CROWN', 'CURVE', 'CYCLE', 'DAILY', 'DANCE', 'DATED', 'DEALT', 'DEATH', 'DEBUT', 'DELAY', 'DEPTH', 'DOING', 'DOUBT', 'DOZEN', 'DRAFT', 'DRAMA', 'DRAWN', 'DREAM', 'DRESS', 'DRILL', 'DRINK', 'DRIVE', 'DROVE', 'DYING', 'EAGER', 'EARLY', 'EARTH', 'EIGHT', 'ELITE', 'EMPTY', 'ENEMY', 'ENJOY', 'ENTER', 'ENTRY', 'EQUAL', 'ERROR', 'EVENT', 'EVERY', 'EXACT', 'EXIST', 'EXTRA', 'FAITH', 'FALSE', 'FAULT', 'FIBER', 'FIELD', 'FIFTH', 'FIFTY', 'FIGHT', 'FINAL', 'FIRST', 'FIXED', 'FLASH', 'FLEET', 'FLOOR', 'FLUID', 'FOCUS', 'FORCE', 'FORTH', 'FORTY', 'FORUM', 'FOUND', 'FRAME', 'FRANK', 'FRAUD', 'FRESH', 'FRONT', 'FRUIT', 'FULLY', 'FUNNY', 'GIANT', 'GIVEN', 'GLASS', 'GLOBE', 'GOING', 'GRACE', 'GRADE', 'GRAND', 'GRANT', 'GRASS', 'GREAT', 'GREEN', 'GROSS', 'GROUP', 'GROWN', 'GUARD', 'GUESS', 'GUEST', 'GUIDE', 'HAPPY', 'HARRY', 'HEART', 'HEAVY', 'HENCE', 'HENRY', 'HORSE', 'HOTEL', 'HOUSE', 'HUMAN', 'IDEAL', 'IMAGE', 'INDEX', 'INNER', 'INPUT', 'ISSUE', 'JAPAN', 'JIMMY', 'JOINT', 'JONES', 'JUDGE', 'KNOWN', 'LABEL', 'LARGE', 'LASER', 'LATER', 'LAUGH', 'LAYER', 'LEARN', 'LEASE', 'LEAST', 'LEAVE', 'LEGAL', 'LEVEL', 'LEWIS', 'LIGHT', 'LIMIT', 'LINKS', 'LIVES', 'LOCAL', 'LOGIC', 'LOOSE', 'LOWER', 'LUCKY', 'LUNCH', 'LYING', 'MAGIC', 'MAJOR', 'MAKER', 'MARCH', 'MARIA', 'MATCH', 'MAYBE', 'MAYOR', 'MEANT', 'MEDIA', 'METAL', 'MIGHT', 'MINOR', 'MINUS', 'MIXED', 'MODEL', 'MONEY', 'MONTH', 'MORAL', 'MOTOR', 'MOUNT', 'MOUSE', 'MOUTH', 'MOVIE', 'MUSIC', 'NEEDS', 'NEVER', 'NEWLY', 'NIGHT', 'NOISE', 'NORTH', 'NOTED', 'NOVEL', 'NURSE', 'OCCUR', 'OCEAN', 'OFFER', 'OFTEN', 'ORDER', 'OTHER', 'OUGHT', 'PAINT', 'PANEL', 'PAPER', 'PARTY', 'PEACE', 'PETER', 'PHASE', 'PHONE', 'PHOTO', 'PIECE', 'PILOT', 'PITCH', 'PLACE', 'PLAIN', 'PLANE', 'PLANT', 'PLATE', 'POINT', 'POUND', 'POWER', 'PRESS', 'PRICE', 'PRIDE', 'PRIME', 'PRINT', 'PRIOR', 'PRIZE', 'PROOF', 'PROUD', 'PROVE', 'QUEEN', 'QUICK', 'QUIET', 'QUITE', 'RADIO', 'RAISE', 'RANGE', 'RAPID', 'RATIO', 'REACH', 'READY', 'REFER', 'RIGHT', 'RIVAL', 'RIVER', 'ROBIN', 'ROGER', 'ROMAN', 'ROUGH', 'ROUND', 'ROUTE', 'ROYAL', 'RURAL', 'SCALE', 'SCENE', 'SCOPE', 'SCORE', 'SENSE', 'SERVE', 'SEVEN', 'SHALL', 'SHAPE', 'SHARE', 'SHARP', 'SHEET', 'SHELF', 'SHELL', 'SHIFT', 'SHIRT', 'SHOCK', 'SHOOT', 'SHORT', 'SHOWN', 'SIGHT', 'SINCE', 'SIXTH', 'SIXTY', 'SIZED', 'SKILL', 'SLEEP', 'SLIDE', 'SMALL', 'SMART', 'SMILE', 'SMITH', 'SMOKE', 'SOLID', 'SOLVE', 'SORRY', 'SOUND', 'SOUTH', 'SPACE', 'SPARE', 'SPEAK', 'SPEED', 'SPEND', 'SPENT', 'SPLIT', 'SPOKE', 'SPORT', 'STAFF', 'STAGE', 'STAKE', 'STAND', 'START', 'STATE', 'STEAM', 'STEEL', 'STICK', 'STILL', 'STOCK', 'STONE', 'STOOD', 'STORE', 'STORM', 'STORY', 'STRIP', 'STUCK', 'STUDY', 'STUFF', 'STYLE', 'SUGAR', 'SUITE', 'SUPER', 'SWEET', 'TABLE', 'TAKEN', 'TASTE', 'TAXES', 'TEACH', 'TEETH', 'TERRY', 'TEXAS', 'THANK', 'THEFT', 'THEIR', 'THEME', 'THERE', 'THESE', 'THICK', 'THING', 'THINK', 'THIRD', 'THOSE', 'THREE', 'THREW', 'THROW', 'TIGHT', 'TIMES', 'TIRED', 'TITLE', 'TODAY', 'TOPIC', 'TOTAL', 'TOUCH', 'TOUGH', 'TOWER', 'TRACK', 'TRADE', 'TRAIN', 'TREAT', 'TREND', 'TRIAL', 'TRIED', 'TRIES', 'TRUCK', 'TRULY', 'TRUST', 'TRUTH', 'TWICE', 'UNDER', 'UNDUE', 'UNION', 'UNITY', 'UNTIL', 'UPPER', 'UPSET', 'URBAN', 'USAGE', 'USUAL', 'VALID', 'VALUE', 'VIDEO', 'VIRUS', 'VISIT', 'VITAL', 'VOICE', 'WASTE', 'WATCH', 'WATER', 'WHEEL', 'WHERE', 'WHICH', 'WHILE', 'WHITE', 'WHOLE', 'WHOSE', 'WOMAN', 'WOMEN', 'WORLD', 'WORRY', 'WORSE', 'WORST', 'WORTH', 'WOULD', 'WOUND', 'WRITE', 'WRONG', 'WROTE', 'YIELD', 'YOUNG', 'YOUTH']
参考:
https://www.freecodecamp.org/news/how-to-build-a-wordle-clone-using-python-and-rich/