Requirement 8 – win condition (IV)

The last requirement is the last win condition. It is pretty similar to the last two; in this case, in a diagonal direction.

If a player inserts a disc and connects more than three discs of his color in a straight diagonal line, then that player wins.

This is a possible implementation for this last requirement. The code is very similar to the other win conditions because the same statement must be fulfilled:

...
private void checkWinCondition(int col, int row) {
  ...
  // Diagonal checks
  int startOffset = Math.min(col, row);
  int column = col - startOffset, auxRow = row - startOffset; 
  stringJoiner = new StringJoiner("");
  do {
    stringJoiner.add(board[column++][auxRow++].toString());
  } while (column < COLUMNS && auxRow < ROWS);

if (winPattern.matcher(stringJoiner.toString()).matches()) { winner = currentPlayer; System.out.println(currentPlayer + " wins"); return; } startOffset = Math.min(col, ROWS - 1 - row); column = col - startOffset; auxRow = row + startOffset; stringJoiner = new StringJoiner(""); do { stringJoiner.add(board[column++][auxRow--].toString()); } while (column < COLUMNS && auxRow >= 0); if (winPattern.matcher(stringJoiner.toString()).matches()) { winner = currentPlayer; System.out.println(currentPlayer + " wins"); } } ...

What we have got is a class with one constructor, three public methods, and three private methods. The logic of the application is distributed among all methods. The biggest flaw here is that this class is very difficult to maintain. The crucial methods, such as checkWinCondition, are non-trivial, with potential for bug entries in future modifications.

If you want to take a look at the full code, you can find it in the https://bitbucket.org/vfarcic/tdd-java-ch05-design.git repository.

We made this small example to demonstrate the common problems with this approach. Topics such as the SOLID principle requires a bigger project to become more illustrative.

In large projects with hundreds of classes, the problems become hours wasted in a sort of surgical development. Developers spend a lot of their time investigating tricky code and understanding how it works, instead of creating new features.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.135.175