FX My Life XII: Super Serial

The Serializable Interface

public class TttController implements java.io.Serializable
case "save":
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("tic-tac-toe files (*.ttt)", "*.ttt");
fileChooser.getExtensionFilters().add(extFilter);
file = fileChooser.showSaveDialog(Main.me.stage);
if (file != null) {
try {
FileOutputStream fileOut = new FileOutputStream(file);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(this);
out.close();
fileOut.close();
} catch (IOException i) {
i.printStackTrace();
}
}
break;
case "load":
file = getFilename(true);
if (file != null)
try {
FileInputStream fileIn = new FileInputStream(file);
ObjectInputStream in = new ObjectInputStream(fileIn);
var t = (TttController) in.readObject();
in.close();
fileIn.close();

this.state = t.state;
this.winner = t.winner;
this.gameOver = t.gameOver;
} catch (IOException | ClassNotFoundException i) {
i.printStackTrace();
return;
}
break;

Well, Duh

The castle leads to Gehennom. Watch out, Medusa is waiting for you.
@FXML
public GridPane xobin;
@FXML
public transient ImageView X;
@FXML
public transient ImageView O;
@FXML
public GridPane board;
@FXML
public VBox outerGroup;
@FXML
public Group innerGroup;
@FXML
public TextField player1;
@FXML
public TextField player2;
@FXML
public Label p1label;
@FXML
public Label p2Label;
@FXML
public Label p1Score;
@FXML
public Label p2Score;
@FXML
public ImageView XoverO;

private String[][] state = new String[3][3];
private String winner;
private boolean gameOver = false;
private boolean playerOneIsX = true;
private Line line = new Line();
private int playerOneWins = 0;
private int playerTwoWins = 0;
private int Ties = 0;
state = new String[3][3];
winner = "";
innerGroup.getChildren().remove(line);
gameOver = false;
for (int i = board.getChildren().size() - 1; i > 0; i--) board.getChildren().remove(i);
case "new":
game.newGame();
break;
case "load":
package fxgames;

import java.io.Serializable;

public class TicTacToe implements Serializable {
private String[][] state = new String[3][3];
private String winner;
private boolean gameOver = false;
private boolean playerOneIsX = true;
private int playerOneWins = 0;
private int playerTwoWins = 0;
private int Ties = 0;

public TicTacToe() {
newGame();
}

public void newGame() {
state = new String[3][3];
winner = "";
gameOver = false;
}
}
public void checkVictoryCondition() {
check(0, 0, 0, 1);
check(1, 0, 0, 1);
check(2, 0, 0, 1);
check(0, 0, 1, 0);
check(0, 1, 1, 0);
check(0, 2, 1, 0);
check(0, 0, 1, 1);
check(2, 0, -1, 1);
}
private String bstr(int x, int y, int xd, int yd) {
return state[y][x] + state[y + yd][x + xd] + state[y + yd + yd][x + xd + xd];
}

public String check(int x, int y, int xd, int yd) {
if (winner == "") {
String s = bstr(x, y, xd, yd);
if (s.equals("OOO") || s.equals("XXX")) {
winner = s.substring(0, 1);
}
}
return winner;
}

public void checkVictoryCondition() {
check(0, 0, 0, 1);
check(1, 0, 0, 1);
check(2, 0, 0, 1);
check(0, 0, 1, 0);
check(0, 1, 1, 0);
check(0, 2, 1, 0);
check(0, 0, 1, 1);
check(2, 0, -1, 1);
}
package fxgames;

import java.io.Serializable;

public class TicTacToe implements Serializable {
public String[][] state = new String[3][3];
public String winner;
public boolean playerOneIsX;
public String playerOneName;
public int playerOneWins;
public String playerTwoName;
public int playerTwoWins;
public int ties;

public int vx;
public int vy;
public int vxd;
public int vyd;

public TicTacToe() {
resetGame();
}

public void newGame() {
state = new String[3][3];
winner = "";
}

public void resetGame() {
newGame();
playerOneIsX = true;
playerOneName = "";
playerOneWins = 0;
playerTwoName = "";
playerTwoWins = 0;
ties = 0;
}

private String bstr(int x, int y, int xd, int yd) {
return state[y][x] + state[y + yd][x + xd] + state[y + yd + yd][x + xd + xd];
}

public String check(int x, int y, int xd, int yd) {
if (winner == "") {
String s = bstr(x, y, xd, yd);
vx = x;
vy = y;
vxd = xd;
vyd = yd;
if (s.equals("OOO") || s.equals("XXX")) {
winner = s.substring(0, 1);
}
}
return winner;
}

public void checkVictoryCondition() {
check(0, 0, 0, 1);
check(1, 0, 0, 1);
check(2, 0, 0, 1);
check(0, 0, 1, 0);
check(0, 1, 1, 0);
check(0, 2, 1, 0);
check(0, 0, 1, 1);
check(2, 0, -1, 1);
}

}
package fxgames;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Group;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.input.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Line;
import javafx.stage.FileChooser;

import java.io.*;

public class TttController {

@FXML
public GridPane xobin;
@FXML
public transient ImageView X;
@FXML
public transient ImageView O;
@FXML
public GridPane board;
@FXML
public VBox outerGroup;
@FXML
public Group innerGroup;
@FXML
public TextField player1;
@FXML
public TextField player2;
@FXML
public Label p1label;
@FXML
public Label p2Label;
@FXML
public Label p1Score;
@FXML
public Label p2Score;
@FXML
public ImageView XoverO;

private Line line = new Line();
private TicTacToe game = new TicTacToe();

@FXML
public void initialize() {
NodeController.me.addHandler(outerGroup, (e) -> {
var id = ((Button) e.getTarget()).getId();
File file;
if (id != null) {
switch (id) {
case "new":
game.newGame();
break;
case "load":
file = getFilename(true);
if (file != null)
try {
FileInputStream fileIn = new FileInputStream(file);
ObjectInputStream in = new ObjectInputStream(fileIn);
game = (TicTacToe) in.readObject();
in.close();
fileIn.close();
} catch (IOException | ClassNotFoundException i) {
i.printStackTrace();
return;
}
break;
case "save":
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("tic-tac-toe files (*.ttt)", "*.ttt");
fileChooser.getExtensionFilters().add(extFilter);
file = fileChooser.showSaveDialog(Main.me.stage);
if (file != null) {
try {
FileOutputStream fileOut = new FileOutputStream(file);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(game);
out.close();
fileOut.close();
} catch (IOException i) {
i.printStackTrace();
}
}
break;
default:
System.out.println("what?");
}
}
});
}

public File getFilename(boolean mustExist) {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("tic-tac-toe files (*.ttt)", "*.ttt");
fileChooser.getExtensionFilters().add(extFilter);
if (mustExist) return fileChooser.showOpenDialog(Main.me.stage);
else return fileChooser.showSaveDialog(Main.me.stage);
}

public void drawVictorySlash() {
if (game.check(game.vx, game.vy, game.vxd, game.vyd) != "") {
var bw = board.getWidth();
var bh = board.getHeight();
var cw = bw / 3;
var ch = bh / 3;
var wxs = 0.0;
var wys = 0.0;
var wxe = bw;
var wye = bh;

innerGroup.getChildren().add(line);
line.getStyleClass().add("line");
if (game.vxd == 1 && game.vyd == 1) {
//don't actually have to do anything since these are the defaults, but lets leave the case in here
} else if (game.vxd == -1 && game.vyd == 1) {
wys = bh;
wye = 0;
} else if (game.vxd == 1) {
wys = (game.vy * ch) + (ch / 2);
wye = wys;
} else if (game.vyd == 1) {
wxs = (game.vx * cw) + (cw / 2);
wxe = wxs;
}
line.setStartX(wxs);
line.setStartY(wys);
line.setEndX(wxe);
line.setEndY(wye);
}
}

public void handleOnDragDetected(MouseEvent event) {
Dragboard db = X.startDragAndDrop(TransferMode.ANY);

ClipboardContent content = new ClipboardContent();
content.putString(((ImageView) event.getSource()).getId());
db.setContent(content);
}

public int getCoord(double width, double coord, int numberOfSections) {
long dim = Math.round(width / numberOfSections);
long border = dim;
int val = 0;
while (coord > border) {
border += dim;
val++;
}
return val;
}

public void handleOnDragOver(DragEvent event) {
int row = getCoord(board.getWidth(), event.getX(), board.getRowCount());
int column = getCoord(board.getHeight(), event.getY(), board.getColumnCount());

if (game.state[column][row] == null) {
if (event.getGestureSource() != board && event.getDragboard().hasString()) {
event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
}
}
event.consume();
}

public void handleOnDrop(DragEvent event) {
int row = getCoord(board.getWidth(), event.getX(), board.getRowCount());
int column = getCoord(board.getHeight(), event.getY(), board.getColumnCount());

ImageView xo = new ImageView();

if (event.getDragboard().getString().equals("X"))
xo.setImage(X.getImage());
else xo.setImage(O.getImage());

xo.setFitWidth(Math.round(board.getWidth() / 3));
xo.setFitHeight(Math.round(board.getHeight() / 3));
GridPane.setRowIndex(xo, column);
GridPane.setColumnIndex(xo, row);
board.getChildren().addAll(xo);

game.state[column][row] = event.getDragboard().getString();
game.checkVictoryCondition();

if (game.winner != "") {
System.out.println("GAME OVER! " + game.winner + " WINS!");
drawVictorySlash();
if (game.winner.equals("X") && game.playerOneIsX) {
game.playerOneWins++;
p1Score.setText(String.valueOf(game.playerOneWins));
} else {
game.playerTwoWins++;
p2Score.setText(String.valueOf(game.playerTwoWins));
}
}
}

public boolean handler(ActionEvent e) {
System.out.println(e);
return false;
}

public void nameChange(KeyEvent e) {
Label l = (e.getTarget() == player1) ? p1label : p2Label;
l.setText(((TextField) e.getTarget()).getText());
}

public void togglePlayerX() {
game.playerOneIsX = !game.playerOneIsX;

if (game.playerOneIsX) {
XoverO.setRotate(0);
} else {
XoverO.setRotate(180);
}
}
}
public void draw() {
innerGroup.getChildren().remove(line);
for (int i = board.getChildren().size() - 1; i > 0; i--) board.getChildren().remove(i);

for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++)
if (game.state[i][j] != null) {
ImageView xo = new ImageView();

if (game.state[i][j].equals("X"))
xo.setImage(X.getImage());
else if (game.state[i][j].equals("O"))
xo.setImage(O.getImage());
xo.setFitWidth(Math.round(board.getWidth() / 3));
xo.setFitHeight(Math.round(board.getHeight() / 3));
GridPane.setRowIndex(xo, i);
GridPane.setColumnIndex(xo, j);
board.getChildren().addAll(xo);
}
}
drawVictorySlash();
}
public void initialize() {
NodeController.me.addHandler(outerGroup, (e) -> {
var id = ((Button) e.getTarget()).getId();
File file;
if (id != null) {
switch (id) {
case "new":
game.newGame();
draw(); //<--Easy
break;
case "load":
public void handleOnDrop(DragEvent event) {
int row = getCoord(board.getWidth(), event.getX(), board.getRowCount());
int column = getCoord(board.getHeight(), event.getY(), board.getColumnCount());

game.state[column][row] = event.getDragboard().getString();
game.checkVictoryCondition();

draw();

if (game.winner != "") {
System.out.println("GAME OVER! " + game.winner + " WINS!");

if (game.winner.equals("X") && game.playerOneIsX) {
game.playerOneWins++;
p1Score.setText(String.valueOf(game.playerOneWins));
} else {
game.playerTwoWins++;
p2Score.setText(String.valueOf(game.playerTwoWins));
}
}
}
public void handleOnDrop(DragEvent event) {
int row = getCoord(board.getWidth(), event.getX(), board.getRowCount());
int column = getCoord(board.getHeight(), event.getY(), board.getColumnCount());

game.addPiece(event.getDragboard().getString(), column, row);
draw();
}
public void checkGameState() {
checkVictoryCondition();
if (winner != "") {
if (winner.equals("X") && playerOneIsX) {
playerOneWins++;
} else {
playerTwoWins++;
}
}
}

public void addPiece(String piece, int x, int y) {
state[y][x] = piece;
checkGameState();
}
                 board.getChildren().addAll(xo);
}
}
drawVictorySlash();

p1Score.setText(String.valueOf(game.playerOneWins));
p2Score.setText(String.valueOf(game.playerTwoWins));
}
public void togglePlayerX() {
game.playerOneIsX = !game.playerOneIsX;

if (game.playerOneIsX) {
XoverO.setRotate(0);
} else {
XoverO.setRotate(180);
}
}
public void togglePlayerX() {
game.togglePlayerX();
draw();
}
public void togglePlayerX() {
playerOneIsX = !playerOneIsX;
}
. . .
p2Score.setText(String.valueOf(game.playerTwoWins));
if (game.playerOneIsX) {
XoverO.setRotate(0);
} else {
XoverO.setRotate(180);
}
public void drawVictorySlash() {
if (game.check(game.vx, game.vy, game.vxd, game.vyd) != "") {
public void drawVictorySlash() {
if (game.winner != "") {
var bw = board.getWidth();
vc = game.getVictoryConditions();
if (vc.xd == 1 && vc.yd == 1) {
vc = game.getVictoryConditions();
if (vc[XD] == 1 && vc[YD] == 1) {
private String[][] state = new String[3][3];
public String get(int x, int y) {
return state[y][x];
}
public void draw() {
. . .
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++)
if (game.get(i, j) != null) {
ImageView xo = new ImageView();

if (game.get(i, j).equals("X"))
xo.setImage(X.getImage());
else if (game.get(i, j).equals("O"))
xo.setImage(O.getImage());
. . .
public void handleOnDragOver(DragEvent event) {
int row = getCoord(board.getWidth(), event.getX(), board.getRowCount());
int column = getCoord(board.getHeight(), event.getY(), board.getColumnCount());

if (game.get(row, column) == null) {
if (event.getGestureSource() != board && event.getDragboard().hasString()) {
event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
}
}
event.consume();
}
private String bstr(int x, int y, int xd, int yd) {
return state[x][y] + state[x + xd][y + yd] + state[x + xd + xd][y + yd + yd];
}
case "load":
file = getFilename(true);
if (file != null)
try {
FileInputStream fileIn = new FileInputStream(file);
ObjectInputStream in = new ObjectInputStream(fileIn);
game = (TicTacToe) in.readObject();
in.close();
fileIn.close();
draw();
} catch (IOException | ClassNotFoundException i) {
i.printStackTrace();
return;
}
break;
case "save":
file = getFilename(false);
if (file != null) {
try {
FileOutputStream fileOut = new FileOutputStream(file);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(game);
out.close();
fileOut.close();
} catch (IOException i) {
i.printStackTrace();
}
}
break;

--

--

--

I am a poor, wayfaring stranger, traveling through this world of woe.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

My experience in programming as a Gazan 16-year-old teenager.

Battle for Low-Code Custom Development: Linx vs Outsystems

Rsync project code to an EC2 instance

Set up multiple Firebase environments in Flutter

DB per feature in #Rails

What are Kubernetes and its relationship with Containers?

Basics before starting with Robotics — Part 6

7 Steps To Use Mobile DevOps Tools Efficiently

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Blake

Blake

I am a poor, wayfaring stranger, traveling through this world of woe.

More from Medium

A Week of Verdicts

Reviewing 2021

New Harvard Study Shows: When Water Runs Dry, Alcohol Flows like the Mississippi