AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Breaking conways game of life10/2/2023 ![]() We're testing for 4 neighbors, but what about the others. The same is true for Test #3 (live cell with more than 3 neighbors dies). In addition for Test #2 (live cell with 2 or 3 neighbors lives), we're only testing for 3 neighbors. We really should add another test for 0 neighbors to make sure we're covering the possibilities. For Test #1 (live cell with less than 2 neighbors dies), we're only testing for 1 neighbor. ![]() We've implemented all 4 of the rules, but we're not really testing all of the possible states at this point. I know that technically I'm supposed to write a failing test before writing *any* code, but I like to break rules from time to time. To get started, I created a class library to hold our method and an MSTest project to hold our unit tests. As a note, the rules have the British spelling "neighbours", but I'll be using the American spelling "neighbors" throughout the article. We can create a method that takes in the current cell state (live/dead) and the number of live neighbors and then spits out a new state (live/dead). Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.Any live cell with more than three live neighbours dies, as if by overcrowding.Any live cell with two or three live neighbours lives on to the next generation.Any live cell with fewer than two live neighbours dies, as if caused by under-population.The rules for Conway's Game of Life are pretty simple: In a future article, we'll see that NUnit doesn't have these same limitations and lets us do some cool stuff (but wait for next time for that). I like to start here because it's built in and the test runner integrates well with Visual Studio. First we'll write a failing unit test (Red), then we'll write just enough code to get the test to pass (Green), and then we'll Refactor our code to make things easier to understand. If you're not familiar with TDD (Test-Driven Development), we're going to follow a Red-Green-Refactor pattern. We won't hook up a UI to this (at least not yet), but we'll get the "business logic" working. I'm looking for some TDD practice, so we'll create a method that implements the rules. Check out the Wikipedia article to see some of these patterns. I've always been interested in this - primarily because I find watching the patterns to be pretty mesmerizing. Recently, I picked Conway's Game of Life (it's pretty cool when you Google it: there's an in-browser version of it). We all need coding practice - this is one way that we get better as programmers.
0 Comments
Read More
Leave a Reply. |