Skip to content

Commit

Permalink
add sharing, fix losing
Browse files Browse the repository at this point in the history
  • Loading branch information
zachmerrill committed Feb 12, 2022
1 parent cd60a5a commit 3c04465
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
8 changes: 7 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@
game.print_board()
# Check if the game is won
if game.is_won():
print('You won!')
print('You won!\n')
game.print_share()
break
# Check if the game is lost
if game.is_lost():
print('You lost!\n')
game.print_share()
break
24 changes: 23 additions & 1 deletion game.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import os
from text import Text
from wordle import Wordle
import pyperclip

# Wordle requirements
# Wordle rules
MAX_LETTERS = 5
MAX_GUESSES = 6

Expand All @@ -11,6 +12,7 @@ class Game:
guess = ''
guess_count = 0
board = [['⬚']*5 for i in range(6)]
share_board = []
keyboard = 'Q W E R T Y U I O P \n A S D F G H J K L \n Z X C V B N M'
keyboard_correct = [] # Stores the correct keys so we don't overwrite them
wordle = Wordle()
Expand All @@ -32,6 +34,7 @@ def check_guess(self, word):
if word not in self.wordle.accepted:
return
self.guess = word
self.share_board.append([])
for i in range(0, 5):
letter_upper = word[i].upper()
if self.wordle.wotd[i] == word[i]:
Expand All @@ -42,23 +45,42 @@ def check_guess(self, word):
letter_upper, correct_letter)
# Add letter to keyboard list so we don't overwrite later
self.keyboard_correct.append(letter_upper)
# Add space to emoji board
self.share_board[self.guess_count].append('🟩')
elif word[i] in self.wordle.wotd:
# Letter is present but in incorrect location
present_letter = Text.apply(letter_upper, Text.YELLOW)
self.board[self.guess_count][i] = present_letter
if word[i] not in self.keyboard_correct:
self.keyboard = self.keyboard.replace(
letter_upper, present_letter)
self.share_board[self.guess_count].append('🟨')

else:
# Letter is not present
absent_letter = Text.apply(letter_upper, Text.RED)
self.board[self.guess_count][i] = letter_upper
self.keyboard = self.keyboard.replace(
letter_upper, absent_letter)
self.share_board[self.guess_count].append('⬛')

self.guess_count += 1

def __init__(self):
self.print_board()

def is_won(self):
return self.wordle.wotd == self.guess

def is_lost(self):
return self.guess_count == MAX_GUESSES

def print_share(self):
share_str = 'Wordle ' + str(self.wordle.wordle_number) + \
' ' + str(self.guess_count) + '/' + str(MAX_GUESSES) + '\n\n'
for i in range(0, self.guess_count):
for j in range(0, MAX_LETTERS):
share_str += self.share_board[i][j]
share_str += '\n'
print(share_str)
pyperclip.copy(share_str)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
requests==2.27.1
pyperclip==1.8.2
6 changes: 4 additions & 2 deletions wordle.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@


class Wordle:
wordle_number = 0
wotd = []
accepted = []

Expand All @@ -32,5 +33,6 @@ def __init__(self):
self.accepted = ast.literal_eval(
content[accepted_start+len(LIST_DIVIDER):accepted_end]) + answers
# Initialize the word of the day based on todays date
self.wotd = answers[(datetime.datetime.now() -
WORDLE_START_DATE).days]
self.wordle_number = (datetime.datetime.now() -
WORDLE_START_DATE).days
self.wotd = answers[self.wordle_number]

0 comments on commit 3c04465

Please sign in to comment.