This week’s code kata is a little bit more advanced. This time one has to use several different techniques in order to solve it. I admit, the exercise itself is not my idea (found it on the net), however I like it very much, since it’s about soccer.
So, here it goes; There are given several teams and their strenght expressed as integer values (for a list of country – strength point values see this link: http://en.wikipedia.org/wiki/FIFA_World_Rankings). These values can be used to predict the outcome of a game between two teams. The formula for that is the following: (In order to keep it simple, we assume that the team with the higher probability to win, wins the game). Of course teams’ strength are not constant. They have to be changed as teams play games. Winners’ strength increases while losers’ strength decreases.
- I: importance of the game. This can be a constant; 60 is its default value.
- GD: goal difference. This is a random number between 1-6.
- R: result. Its value is 1 for a win, 0 for a loss.
- Win(expected): given by the formula above.
What the kata requests you to do is to predict the winner of a game, increase their strenght and decrease losers’ strength. If this is too easy for you, then select several teams, and predict the winner of a whole tournament.
This kata is nice for a number of reasons;
- In order to solve it in TDD, it needs the random number either to be injected (dependency injection rulez!) or faked.
- Finally a kata, which requires you to have several classes.
- It familiarizes its solver with JUnit’s assert for floating point numbers (remember two floating point numbers cannot be considered truly equal; When testing such numbers for equality, you need to pass in a third argument to the assert. That is a delta walue, which tells the maximum difference between the numbers at which they are considered equal.)
Solve it, have fun, and if you really want to play it on the hard level, have a time limit of 30 minutes (OK, up to one hour).