View Single Post
Old 08-09-2016, 10:54 PM   #3
GoldenEagle
Grizzled Veteran
 
Join Date: Dec 2002
Location: Little Rock, AR
Oklahoma

Everyone who ever played football at the youth or high skill level will know this drill. It was always the first drill the day that pads went on. The basic idea is that players line-up (usually in-between tackling dummies) and proceed to knock the crap out of each other. The better player will usually win, by either getting a successful block or fighting through the block.

This is the first part of the game that we will build. I am a firm believer in test driven development. Basically, it is a programming technique where you write a test first, make it fail, and then implement the logic to get it to pass. It helps ensure that you have test coverage of your code (which has just a ton of benefits) and makes for an easier design.

In order to get the test to even fail, you have to build out some shell classes. I did this by creating a player class with a strength attribute. This class will eventually be made abstract (or more likely, an interface) and I will instead inherit more specialized player classes from it. But for now, it works for the purpose of creating this test.

I also needed a function to attempt the block. The return type is boolean and it is set to return false. I ran the test by creating two player objects and setting the strength attribute to 100 for one and 50 for the other. I ran the test, which failed. That is what we want.

I then created a very simple algorithm for determining if a block was successful. If player A's strength was stronger than player B, then the block was successful and the method returned true. When I ran the test again, it passed.

Obviously, this will need to be revisited in the future. Strength is just one component of a successful block in football. There is also technique, size of the players involved, and even some randomness (which is really hard to test, but possible). I will need to revisit this in the future and think of a better design. For staters, the attempt block function doesn't belong in the player class.

But it is start. Just like that very first day in practice when you ran the Oklahoma drill over and over again. We have a test in place and can refactor our design. If we break something, our test will tell us.

I probably want get this detailed in the future, but I wanted to stress the importance of TDD and explain my process.
__________________
Xbox 360 Gamer Tag: GoldenEagle014
GoldenEagle is offline   Reply With Quote