Step 6
The code for any application, even as simple as the Tic-Tac-Toe game, can quickly get out of hand unless we take a modular approach and break the problem down into smaller tasks, each of which can be coded with one or more functions. Here's a modular structure chart for the provided (partial) implementation:
You must carefully study the modular structure and the description of each function before completing the code. I recommend working through the exercise in the following order:
- Implement
switchPlayer
- Implement
checkRow
- Implement
checkColumns
- Implement
checkColumn
- Implement
checkMajorDiagonal
- Implement
checkMinorDiagonal
- Complete the implementation of
game
We are taking a top-down approach to design step-wise refinement to the implementation of this game.
In this approach, it may not be possible to run the application to test the correctness of your implementation as you progress through (because at any point, some of the functions are not implemented yet). You can use the debugger to step through the code to inspect its implementation. Ideally, we must perform unit testing. For now, to keep things simple, we let go of writing tests (but we will do it later in this course). Luckily, each function is small in scope and the application (game) is simple as a whole. It will not be difficult to get the code working correctly with a few attempts.