C# 9.0 - TDD - Snake Game

Ahmet Murat Gençay
3 min readDec 12, 2020

Test-Driven Development (TDD) is a software development approach in which test cases are developed to specify and validate what the code will do. In simple terms, test cases for each functionality are created and tested first and if the test fails then the new code is written in order to make the test pass and making the code simple and bug-free.

We will make a simple snake game with a TDD approach. With this approach, you will see the convenience of performing tasks step by step using TDD.

TDD

Iteration 1

Let start with determining the initial restrictions. The snake must have a length, and I think it should be a minimum of 3 and also have a direction of movement, now let’s make the necessary development for these needs.

As seen above, we first wrote the test methods that define our needs, and then we made the necessary changes to make them pass.

Iteration 2

Now it’s time to reveal the basic mechanics of our game. There are certain rules that our snake must follow while moving in a certain direction.

  1. It can go up and down when going right
  2. It can go up and down when going left
  3. It can go left and right when going up
  4. It can go left and right when going down

In the code below, we have created two test methods for incorrect and correct moves and made improvements to fulfill these rules.

We are developing this game as a console application, so I created a record” to make our work on the console easier.

Records entered our lives with C # 9.0. There are many nice features, the most important of which are “init only setters” and “with expression”. You can find the details in the links.

Iteration 3

Let’s continue with the game mechanics. We had implemented some rules in the previous stages, and now we move on to the movement process.

The game runs in a loop. The snake moves with each step in a specific direction. So we can know where the snake is at a given step.

To gain performance, we delete the last element of the snake at each step and add the record with new position information at the beginning for where it needs to move.

Iteration 4

Now let’s move on to the progress features in the game. Progress in the snake game takes place by eating. Foods appear in random places on the screen from the first moment of the game to the end, and a new one is formed as we eat.

During the game we want the foods to be in random places, but during the testing, we have to work with exact values.

We need random console points in our game, so we have a dependency on the Random class. If we want to write testable code, we need to abstract our dependencies well, so we create IRandomPointGenerator to abstract our random console points generation.

Iteration 5

Now it’s time to calculate the score. When the snake eats the food, our score will increase and we will be informed.

Iteration 6

Finally, we construct the structure required for the completion of the game. Since there is no wall logic in our game, the game may end only with the snake eating its own tail.

Main Application

One of the features that come with C # 9.0 is “Top-level statements”. Thanks to this feature, the main method is not required for the entry point. We can start writing our code directly.

Finally

Here is our perfect snake game.

References

  1. What’s new in C# 9.0
  2. with expression (C# reference)
  3. TDD

--

--