Code Documentation of Tic-Tac-Toe with JavaFX
This is part of ../../Examples/TicTacToe-JavaFX-UnRedo. It describes the src code. It is assumed that the reader knows how JavaFX works.
1 Src Code
- JavaFX source code of TicTacToe
- ./TicTacToe.java Java src code
- ./TicTacToe.java.html Java src code colorized
- You can build and run it as in
% javac TicTacToe.java; java TicTacToe
sloccount TicTacToe.javagives java: 109 (100.00%).
2 TicTacToe extends JavaFX Application
- The
startmethod overrides the JavaFX provided default. It is indirectly invoked by thelaunchinmain. The argumentprimaryStageis supplied through this mechanism. hasWon,isFullare obviousdrawXdraws two JavaFX lines so that they look like an X.drawOdraws a circle using the JavaFX Ellipse method.
3 Inner Class Named Cell extends JavaFX Pane
- Note that cell is one cell of the 3x3 grid.
- In
setOnMouseClicked(e -> handleMouseClick());, we are using Java8 Lambda Expression. This says when a mouse-click happens within the area of a cell, invoke the method namedhandleMouseClick. We are ignoring the details of the evente.
4 Event Listeners
- The
mainmethod invokeslaunch. This is typical of a JavaFX Application. The launch invokesstart. Then onwards, it is all driven by events. - The for-loop in
startconstructed Cell objects, all of which have the samehandleMouseClickmethod as their event handler. This handler is executing in the context of the specific Cell object where the event occurred.
5 Discussion
- The code linked above has at least one bug. Find and fix its bugs.
- Code smells: Should some of those
publicmethods be private? What other bad smells do you detect? - The business logic and GUI are not well separated. Revise the code.
- How does this program terminate?
- Redo the implementation of
whoseTurnas a renameditIsXsTurnBoolean. Does this make the code better or worse? - This problem was used as a Lab of CEG 4180 OOProg + Design. Do the Undo/Redo.
- Should documentation be embedded in the source code file itself? Perhaps following javadoc suggestions?