Last active
June 19, 2022 08:58
-
-
Save Wqrld/edb1823222fa3a1b3fc11fceaf95ba76 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.application.Application; | |
import javafx.event.Event; | |
import javafx.event.EventHandler; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
import javafx.scene.layout.Pane; | |
import javafx.scene.paint.Color; | |
import javafx.scene.shape.Line; | |
import javafx.scene.shape.Rectangle; | |
import javafx.scene.text.Text; | |
import javafx.scene.text.TextAlignment; | |
import javafx.stage.Stage; | |
enum Player { | |
BLUE, | |
RED | |
} | |
enum State { | |
ACTIVE, | |
FINISHED | |
} | |
class Block extends Rectangle { | |
private Color color; | |
public Block(double x, double y, double width, double height){ | |
super(x, y, width, height); | |
color = Color.WHITE; | |
} | |
public Color getColor(){ | |
return color; | |
} | |
public void setColor(Color c){ | |
color = c; | |
this.setFill(c); | |
} | |
} | |
public class Main extends Application { | |
State state = State.ACTIVE; | |
Player active = Player.BLUE; | |
Text t = new Text(200, 50, "T3 game"); | |
Block array[][] = new Block[3][3]; // rows, columns | |
private boolean checkWinner() { | |
for (int row = 0; row <= 2; row++) { | |
if (array[row][0].getColor() != Color.WHITE && array[row][0].getColor() == array[row][1].getColor() && array[row][1].getColor() == array[row][2].getColor()) { | |
// colors match for this row | |
return true; | |
} | |
} | |
for (int column = 0; column <= 2; column++) { | |
if (array[0][column].getColor() != Color.WHITE && array[0][column].getColor() == array[1][column].getColor() && array[1][column].getColor() == array[2][column].getColor()) { | |
// colors match for this column | |
return true; | |
} | |
} | |
//diagonals | |
//topleft, bottom right | |
if (array[0][0].getColor() != Color.WHITE && array[0][0].getColor() == array[1][1].getColor() && array[1][1].getColor() == array[2][2].getColor()) { | |
// colors match for this column | |
return true; | |
} | |
//topright, bottom left | |
else if (array[0][2].getColor() != Color.WHITE && array[0][2].getColor() == array[1][1].getColor() && array[1][1].getColor() == array[2][0].getColor()) { | |
// colors match for this column | |
return true; | |
} | |
return false; | |
} | |
private boolean checkTie(){ | |
int filledCount = 0; | |
for (int y = 0; y <= 2; y++) { | |
for (int x = 0; x <= 2; x++) { | |
if(array[y][x].getColor() != Color.WHITE){ | |
filledCount++; | |
} | |
} | |
} | |
return filledCount == 9 ? true : false; | |
} | |
// Game logic | |
EventHandler clickHandler = e -> { | |
// Resetting the playing field | |
if (state == State.FINISHED) { | |
state = State.ACTIVE; | |
for (int y = 0; y <= 2; y++) { | |
for (int x = 0; x <= 2; x++) { | |
array[y][x].setColor(Color.WHITE); | |
} | |
} | |
t.setText("T3 game"); | |
} | |
Block target = (Block) e.getTarget(); | |
if(target.getColor() != Color.WHITE){ | |
return; | |
} | |
if (active == Player.BLUE) { | |
target.setColor(Color.BLUE); | |
active = Player.RED; | |
} else if (active == Player.RED) { | |
target.setColor(Color.RED); | |
active = Player.BLUE; | |
} | |
if (checkWinner()) { | |
t.setText(target.getColor() == Color.RED ? "Red has won!" : "Blue has won!"); | |
state = State.FINISHED; | |
}else if(checkTie()){ | |
t.setText("TIE!"); | |
state = State.FINISHED; | |
} | |
}; | |
@Override | |
public void start(Stage primaryStage) throws Exception { | |
Pane p = new Pane(); | |
primaryStage.setTitle("Hello World"); | |
t.setStyle("-fx-font: 24 arial"); | |
p.getChildren().add(t); | |
for (int y = 0; y <= 2; y++) { | |
for (int x = 0; x <= 2; x++) { | |
Block r = new Block(x * 100 + 100, y * 100 + 100, 100, 100); | |
r.setColor(Color.WHITE); | |
r.setStroke(Color.DARKGRAY); | |
r.setOnMouseClicked(clickHandler); | |
array[y][x] = r; | |
p.getChildren().add(r); | |
} | |
} | |
primaryStage.setScene(new Scene(p, 500, 500)); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment