Skip to content

Commit

Permalink
chore: update rec04 for 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
cmumatt committed Feb 8, 2024
1 parent f55c9e9 commit 7e3d97d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 22 deletions.
73 changes: 53 additions & 20 deletions recitation-4/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Recitation 4: classes, objects, and sets

__Wode "Nimo" Ni__
__Matthew Davis__

_22/02/11_
_2024-02-09_

## Overview

In this recitation, we'll continue with our wordle example. This time, we will:

Expand All @@ -14,52 +16,83 @@ In this recitation, we'll continue with our wordle example. This time, we will:
* Sets
* Unique members
* Unordered
* Set operations: union, difference etc
* Set operations: union, difference, etc.
* Objects and classes
* Examples:
* https://docs.python.org/3/library/csv.html#csv.writer
* https://docs.python-requests.org/en/latest/
* Methods
* State (aka "field")
* Methods (aka "functions")
* State (aka "fields")

```python
class Writer:
row_count = 0
file = ??
# constructor
row_count = 0 # field
file = "" # field
# constructor
def __init__(self, file):
self.file = file
def write_row(self):
# method
def write_row(self, data):
self.row_count += 1
writer1 = csv.Writer("egg.csv")
print("Wrote: ",data," to ",self.file)
writer1 = Writer("egg.csv")
writer1.write_row(["egg", "price", "color"])
writer2 = csv.Writer("chicken.csv")
writer2 = Writer("chicken.csv")
```

## Task 1: help the human players out

In the desktop version of wordle, the game shows you the characters on a colored keyboard, which is helpful for keeping track of the valid characters to use. Let's try to do that.
* Open the `recitation-24` repo in a new Codespace.
* `git status` to see that you have checked out branch `main`
* Create and checkout your own branch: `git checkout -b "<andrewid>"`
* Open `recitation-4/wordle.py` and find method/function `play()`
* `play()` is the interactive mode of the game and has an optional argument that tells it to print out valid characters after each step (`char_hint`).
* Play the game once to see this working: in your terminal:
* `$ cd recitation-4`
* `$ python3 wordle.py`
* Notice the list of valid letters remaining is empty!
* In `recitation-4/wordle.py` add code to `valid_letters` to make it work.

* `play()` is the same old interactive mode we had.
* It now has an argument that tells it to print out valid characters after each step (`char_hint`).
* Finish `valid_letters` to make it actually work.
In the desktop version of wordle, the game shows you the characters on a colored keyboard, which is helpful for keeping track of the valid characters to use. Let's try to do that.

## Task 2: play the wordle game programmatically

In addition to a collection of pure functions we wrote before, we now introduce the `WordleGame` __class__, which represents a single game of Wordle. Let's try using it first:
The `WordleGame` __class__ inside `wordle.py` represents a single game of Wordle. Let's try using it first:

* Instantiate a `WordleGame` __object__ with a predefined answer `apple`.
```
$ python3
>>> import wordle
>>> game = wordle.WordleGame('./words.json','apple')
```
* Guess 3 times, in which the final guess is `apple`.
* Print out the history of hints and if you indeed won the game using methods defined in the class.
```
>>> game.guess('snarl')
['🟥', '🟥', '🟨', '🟥', '🟨']
>>> game.guess('apply')
['🟩', '🟩', '🟩', '🟩', '🟥']
>>> game.guess('apple')
['🟩', '🟩', '🟩', '🟩', '🟩']
```
* Print out the history of hints, and if you indeed won the game using methods defined in the class.
```
>>> if(game.game_won):
... print("YAY! You won!")
... game.show_history()
...
🟥 🟥 🟨 🟥 🟨
🟩 🟩 🟩 🟩 🟥
🟩 🟩 🟩 🟩 🟩
```

## Task 3: play the wordle game programmatically, many times

If you can play the game once in a program, you can play it many times.
If you can play the game once with a program, you can play it many times.

* In `play_wordle_games`, create a loop that goes through `n` times
* In each step:
* In each loop iteration:
* Instantiate a `WordleGame` __object__ without a predefined answer
* Take 5 guesses
* Print out the history
* Record if we won
* Finally, compute the success rate of your first aRTiFicIAl iNTelLigeNCe.
* Finally, compute and print the success rate of your first aRTiFicIAl iNTelLigeNCe.
2 changes: 1 addition & 1 deletion recitation-4/wordle-solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def play(self, char_hint=False):
valid_chars.sort()
print("Valid characters left: ", valid_chars)
if self.game_won:
print("YAY! You won! Tweet your result:")
print("YAY! You won! Toot your result on Mastodon:")
self.show_history()

def show_history(self):
Expand Down
2 changes: 1 addition & 1 deletion recitation-4/wordle.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def play(self, char_hint=False):
valid_chars.sort()
print("Valid characters left: ", valid_chars)
if self.game_won:
print("YAY! You won! Tweet your result:")
print("YAY! You won! Toot your result on Mastodon:")
self.show_history()

def show_history(self):
Expand Down

0 comments on commit 7e3d97d

Please sign in to comment.