Category: Coding
-
Learn to code through self-directed projects
How to learn to code? Coding encompasses a lot and what to learn depends on what you want to do. Like writing, you can write a joke, a poem, a textbook, or even a novel. Each requires different skills. If you want to write a novel, the best way to learn is probably by actually…
-
BattleShip Part 11: Include columns and rows in display
Planning on wrapping this up by implementing one more feature around displaying the row and column labels. The project isn’t “perfect” but I feel like it’s in a good place (imo). Previously, I was only showing the coordinates and the statuses but not the labels. i.e. (A-J or 1-10). This made figuring out the coordinates…
-
BattleShip Part 10: Tests for Game Cli
Next, I’m going to add some unit tests for the GameCli class. The primary thing this class currently does is print output. To make testing a little easier, I’ll define a class that is in charge of printing instead of printing in the GameCli. It will be pretty simple and also control clearing the terminal…
-
BattleShip Part 9: Unit tests for Game class
Now that our Game class is mostly functional, we can add some unit tests. The first test I’ll write is just checking the game over functionality for a newly created game. Next, I’ll verify attacking all the ship coordinates results in a game over. You’ll notice, I redefined ship_coords and created a new game object.…
-
BattleShip Part 8: Addressing boundary issue
Running the project as-is, I noticed a problem with the output. There are 10 rows but only 9 columns. Also, my validation check is too strict because it only allows a single letter for the attack command. 1-9. So I’ll relax this to allow any numeric ending. This causes a new issue since now I…
-
BattleShip Part 7: Ship Placement Tests
While running the code, I did find a bug where ships could overlap. As I started trying to write a test for the ShipPlacer class, I found it was a little hard to do. There is a bunch of randomizing involved and because the primary thing that happens is updating the ship coordinate state, it…
-
BattleShip Part 6: Ship Placement
The next major part of the project involves randomly placing ships on a board. I decided to construct a new component that handles the placement of ships and builds a set of ship coordinates which is then used by the Game class.’ This component will be responsible for making sure ships are accurately placed on…
-
BattleShip Part 5: Game Mechanics
For the next part, I’m going to build out the game mechanics. I initially thought I might want some kind of Game and Board class. Where the GameCli would interact with the Game and the Game would control the Board. Now that I’m thinking a bit more about how I would want both of these…
-
BattleShip Project Review
I came across this project on a code review subreddit and thought it could be a good case study on how I would try to improve this code. Here is a series of posts on things I would do.
-
Why you should avoid creating objects
This isn’t always a problem and obviously, you can’t completely avoid creating objects somewhere. The issue is that the code that directly creates the object becomes coupled to that object. For example, let’s look at this simple function,. The only way to verify that process works is by using the actual TodoManager class. What if…