UP | HOME

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

  1. JavaFX source code of TicTacToe
    1. ./TicTacToe.java Java src code
    2. ./TicTacToe.java.html Java src code colorized
    3. You can build and run it as in
      % javac TicTacToe.java; java TicTacToe
      
  2. sloccount TicTacToe.java gives java: 109 (100.00%).

2 TicTacToe extends JavaFX Application

  1. The start method overrides the JavaFX provided default. It is indirectly invoked by the launch in main. The argument primaryStage is supplied through this mechanism.
  2. hasWon, isFull are obvious
  3. drawX draws two JavaFX lines so that they look like an X.
  4. drawO draws a circle using the JavaFX Ellipse method.

3 Inner Class Named Cell extends JavaFX Pane

  1. Note that cell is one cell of the 3x3 grid.
  2. 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 named handleMouseClick. We are ignoring the details of the event e.

4 Event Listeners

  1. The main method invokes launch. This is typical of a JavaFX Application. The launch invokes start. Then onwards, it is all driven by events.
  2. The for-loop in start constructed Cell objects, all of which have the same handleMouseClick method as their event handler. This handler is executing in the context of the specific Cell object where the event occurred.

5 Discussion

  1. The code linked above has at least one bug. Find and fix its bugs.
  2. Code smells: Should some of those public methods be private? What other bad smells do you detect?
  3. The business logic and GUI are not well separated. Revise the code.
  4. How does this program terminate?
  5. Redo the implementation of whoseTurn as a renamed itIsXsTurn Boolean. Does this make the code better or worse?
  6. This problem was used as a Lab of CEG 4180 OOProg + Design. Do the Undo/Redo.
  7. Should documentation be embedded in the source code file itself? Perhaps following javadoc suggestions?

Copyright © 2015 www.wright.edu/~pmateti