patternjavaMinor
Ultimate Tic Tac Toe A.K.A. Tic Tactics
Viewed 0 times
toetictacultimatetactics
Problem
My attempt at this challenge., incorporating lessons from this question, which turned out into a much bigger project than I anticipated, but it works. I'll definitely be refactoring most of this in the future, maybe going the MVC route. I tried a lot of new things here and am curious which are good and which are far removed from such.
Concerns:
The code will follow the screenshot, with imports included to facilitate things.
The Winner enum
The Player enum
The Position enum
The Tic Tac Toe Square Class
```
import javafx.scene.control.Button;
public class TicTacToeSquare {
private Button button = new Button();
private final int SQUARE_LENGTH = 70;
TicTacToeSquare(TicTacticsGame game, TicTacToeBoard board, Position position) {
button.setMinSize(SQUARE_LENGTH, SQUARE_LENGTH);
button.setOnAction(e -> {
if (button.getText().isEmpty()) {
button.setText(game.getCurrentPlayer().toString());
but
Concerns:
- Clarity in variable and method names.
- Redundancies.
- Anything unnecessarily convoluted.
- General efficiency of this approach.
- Edge Cases- Again, everything works, but I can't help but think there's something I've not encompassed in the code.
- General improvements/feedback welcomed.
The code will follow the screenshot, with imports included to facilitate things.
The Winner enum
public enum Winner {
NONE(""),
X("-fx-color: darkred;"),
O("-fx-color: gold"),
TIE("-fx-color: orangered;");
private final String style;
Winner(String style) {
this.style = style;
}
public String getStyle() {
return style;
}
}The Player enum
public enum Player {
X("-fx-text-fill: darkred;"),
O("-fx-text-fill: gold;");
private final String style;
Player(String style) {
this.style = style;
}
public String getStyle() {
return style;
}
}The Position enum
public enum Position {
TOP_LEFT,
TOP_MIDDLE,
TOP_RIGHT,
MIDDLE_LEFT,
MIDDLE,
MIDDLE_RIGHT,
BOTTOM_LEFT,
BOTTOM_MIDDLE,
BOTTOM_RIGHT;
}The Tic Tac Toe Square Class
```
import javafx.scene.control.Button;
public class TicTacToeSquare {
private Button button = new Button();
private final int SQUARE_LENGTH = 70;
TicTacToeSquare(TicTacticsGame game, TicTacToeBoard board, Position position) {
button.setMinSize(SQUARE_LENGTH, SQUARE_LENGTH);
button.setOnAction(e -> {
if (button.getText().isEmpty()) {
button.setText(game.getCurrentPlayer().toString());
but
Solution
final KeywordThe
Game, Tic Tac Toe Board, Tic Tac Toe Board, all have fields which are assigned but never reassigned, private StringProperty xPlayer = new SimpleStringProperty("X player");
private StringProperty oPlayer = new SimpleStringProperty("O player");
private TicTacticsGame game;
private Button button = new Button();They could all use the
final keyword. Generally
The code is fairly easy to read, you follow most conventions. Overall a pleasant surprise to look at.
Code Snippets
private StringProperty xPlayer = new SimpleStringProperty("X player");
private StringProperty oPlayer = new SimpleStringProperty("O player");
private TicTacticsGame game;
private Button button = new Button();Context
StackExchange Code Review Q#101253, answer score: 3
Revisions (0)
No revisions yet.