-
Notifications
You must be signed in to change notification settings - Fork 1
/
woordle_game.py
163 lines (144 loc) · 5.39 KB
/
woordle_game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import discord
import random
from sqlite3 import Timestamp
from discord.utils import get # For emojis
from constants import SKIN_MAP, LETTER_MAP
WORD_LENGTH = 5
MAX_GUESSES = 6
ALPHABET = [letter for letter in "abcdefghijklmnopqrstuvwxyz"]
class WoordleGame:
"""Class representing a woordle game"""
def __init__(self, word, author: discord.member, id: int,
message: discord.message, time: Timestamp) -> None:
"""
Initializes a WoordleGame
Parameters
----------
word : str
Word for the current WoordleGame
author : discord.member
Author who created the WoordleGame
id : int
ID of current WoordleGames
message : discord.message
Discord message with the first guess
time : Timestamps
Start time of sending the discord message
"""
self.word = word
self.woordle_list = [letter for letter in self.word] if self.word is not None else None
self.author = author
self.id = id
self.message = message
self.board = [['⬛'] * WORD_LENGTH for _ in range(MAX_GUESSES)]
self.row = 1
self.playing = True
self.timestart = time
self.wrong_guesses = 0
self.wordstring = ""
self.failed = True
self.time = 0
self.letters = LETTER_MAP.copy()
def add_row(self) -> None:
"""
Add a row to the WoordleGame
"""
self.row += 1
def right_guess(self, guess: str) -> bool:
"""
Check if the current guess is the correct one
Return
------
check : bool
Return True if correct, else False
"""
return self.word.lower() == guess.lower()
def stop(self) -> None:
"""
End the playing state of the WoordleGame
"""
self.playing = False
def display(self, client: discord.client) -> str:
"""
Construct a string representing the current board of the WoordleGame
Parameters
----------
client : discord.client
Discord Woordle bot
Return
------
board : str
String representation of the current board of the WoordleGame
"""
# Set current board
board = ""
for i in range(self.row):
for j in range(WORD_LENGTH):
board += self.board[i][j]
board += "\n"
board += "\n"
# Set list of status of chosen letters
for index, letter in enumerate(ALPHABET):
if index == len(ALPHABET)/2:
board += "\n"
if get(client.emojis, name=self.letters[letter]) is None:
board += self.letters[letter]
else:
board += str(get(client.emojis, name=self.letters[letter]))
return board
def display_end(self, client: discord.client, skin: str = "Default") -> str:
"""
Convert the board string to a board string with emojis
Return
------
end_board : str
String representation of the current board of the WoordleGame
"""
end_board = ""
for i in range(self.row):
for j in range(WORD_LENGTH):
if self.board[i][j][2:7] == "green":
color = "green"
elif self.board[i][j][2:8] == "yellow":
color = "yellow"
elif self.board[i][j][2:6] == "gray":
color = "gray"
# Handle special cases
if skin == "Random":
emoji_name = f"{color}_{str(random.choice(ALPHABET)).upper()}"
end_board += str(get(client.emojis, name=emoji_name))
else:
end_board += SKIN_MAP[skin][color]
end_board += "\n"
end_board += "\n"
return end_board
def update_board(self, guess: str, client: discord.client) -> None:
"""
Update the board according to the current guess
Parameters
----------
guess : str
Current guess
client : discord.client
Discord Woordle bot
"""
temp_list = self.woordle_list.copy()
# Handle all correct and incorrect spots
for index, letter in enumerate(guess):
if letter.upper() == self.word[index].upper():
temp_list.remove(letter.upper())
emoji_name = "green_" + str(letter).upper()
else:
emoji_name = "gray_" + str(letter).upper()
self.board[self.row - 1][index] = str(get(client.emojis, name=emoji_name))
if self.letters[str(letter).lower()] != "green_" + str(letter).upper():
self.letters[str(letter).lower()] = emoji_name
# Handle all correct letters on wrong spots
for index, letter in enumerate(guess):
yellow_name = "yellow_" + str(letter).upper()
green_emoji = "green_" + str(letter).upper()
if self.board[self.row - 1][index] != str(get(client.emojis, name=green_emoji)) and letter.upper() in temp_list:
temp_list.remove(letter.upper())
self.board[self.row - 1][index] = str(get(client.emojis, name=yellow_name))
if self.letters[str(letter).lower()] != green_emoji:
self.letters[str(letter).lower()] = yellow_name