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.java
gives java: 109 (100.00%).
2 TicTacToe extends JavaFX Application
- The
start
method overrides the JavaFX provided default. It is indirectly invoked by thelaunch
inmain
. The argumentprimaryStage
is supplied through this mechanism. hasWon
,isFull
are obviousdrawX
draws two JavaFX lines so that they look like an X.drawO
draws 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
main
method invokeslaunch
. This is typical of a JavaFX Application. The launch invokesstart
. Then onwards, it is all driven by events. - The for-loop in
start
constructed Cell objects, all of which have the samehandleMouseClick
method 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
public
methods 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
whoseTurn
as a renameditIsXsTurn
Boolean. 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?