Course: CSCI 1250
Description: GitHub
Assignment 6 Writeup
I added type annotations and doc-strings to the functions. This helped a little bit, since some of the code was ambiguous, but now it’s a little more clear what the functions do.
I think the majority of the code is pretty self-explanatory, so I didn’t see the necessity of adding comments to every line since I dislike comments that just restate what the code does, I usually just write comments on why I did something, rather than what I did (if that makes sense).
Whenever I added type hints, I needed to change the functions a little bit, with get_empty_tile_position
in particular. I had to ensure it would return a base value of (-1, -1)
if the tile wasn’t found (I could have also thrown an error, but I don’t really like exceptions)
Originally, the generate_puzzle
had the possibility of generating an unsolvable puzzle, so I implemented count_inversions
and is_solvable
to check if the puzzle is solvable. (Reference: cs.princeton.edu)
I also moved the keydown logic into a separate function, since the main
function was pretty lengthy, and I also changed the “You Win” text to white, so it’s more readable on the gray background.
The save and load functions were pretty easy, I just turned the list[list[int]]
into a string by joining it with "\n"
for different rows, and " "
for different columns. In the load_game_state
function, I split each row by None
(which Python turns into any whitespace) and then mutate the puzzle
variable to load it.
I also added the breadth-first search algorithm (with bfs_solver
). Since I didn’t want to solve the puzzle manually (which would have been shorter than writing the BFS algorithm), I implemented the algorithm to solve the puzzle. I originally wanted to implement the A* algorithm, but it caused the program to hang, so I went with BFS. I originally learned about BFS from advent of code last year, so I was a little familiar with it.
I think if I were to refactor this code further, I would probably make the majority of the logic into a class rather than a bunch of functions, just to encapsulate everything a little better. (Even though I’m not particularly fond of OOP)