diff --git a/Screenshots/GameBoard.JPG b/Screenshots/GameBoard.JPG new file mode 100644 index 0000000..97f2f36 Binary files /dev/null and b/Screenshots/GameBoard.JPG differ diff --git a/Screenshots/ResultPage.JPG b/Screenshots/ResultPage.JPG new file mode 100644 index 0000000..994f96f Binary files /dev/null and b/Screenshots/ResultPage.JPG differ diff --git a/Screenshots/WelcomePage.JPG b/Screenshots/WelcomePage.JPG new file mode 100644 index 0000000..a6d502a Binary files /dev/null and b/Screenshots/WelcomePage.JPG differ diff --git a/module-info.java b/module-info.java new file mode 100644 index 0000000..037b345 --- /dev/null +++ b/module-info.java @@ -0,0 +1,7 @@ +module RuralBankingManagement { + + requires javafx.fxml; + requires javafx.controls; + + opens com.akgarg.bankingsystem; +} \ No newline at end of file diff --git a/src/TicTacToeJavaFX/GameBoard.fxml b/src/TicTacToeJavaFX/GameBoard.fxml new file mode 100644 index 0000000..63bf065 --- /dev/null +++ b/src/TicTacToeJavaFX/GameBoard.fxml @@ -0,0 +1,27 @@ + + + +
+ + + + + + + + + + + + + + + + + +
+ + + +
\ No newline at end of file diff --git a/src/TicTacToeJavaFX/GameBoardController.java b/src/TicTacToeJavaFX/GameBoardController.java new file mode 100644 index 0000000..af229f0 --- /dev/null +++ b/src/TicTacToeJavaFX/GameBoardController.java @@ -0,0 +1,317 @@ +package TicTacToeJavaFX; + +import javafx.event.EventHandler; +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.input.MouseEvent; +import javafx.scene.paint.Paint; +import javafx.scene.text.Font; +import javafx.stage.Stage; + +import java.io.IOException; + +public class GameBoardController { + + private String playerOneName, playerTwoName; + private int turnsCount = 0; + private String playerOneSymbol; + private String playerTwoSymbol; + private String playerOneColor, playerTwoColor; + + @FXML + private Button btn1; + @FXML + private Button btn2; + @FXML + private Button btn3; + @FXML + private Button btn4; + @FXML + private Button btn5; + @FXML + private Button btn6; + @FXML + private Button btn7; + @FXML + private Button btn8; + @FXML + private Button btn9; + + // this method set the width and height of the buttons which are going to be visible on the game board + private void setButtonDimensions(double width, double height) { + btn1.setPrefWidth(width / 3); + btn2.setPrefWidth(width / 3); + btn3.setPrefWidth(width / 3); + btn4.setPrefWidth(width / 3); + btn5.setPrefWidth(width / 3); + btn6.setPrefWidth(width / 3); + btn7.setPrefWidth(width / 3); + btn8.setPrefWidth(width / 3); + btn9.setPrefWidth(width / 3); + + btn1.setPrefHeight(height / 3); + btn2.setPrefHeight(height / 3); + btn3.setPrefHeight(height / 3); + btn4.setPrefHeight(height / 3); + btn5.setPrefHeight(height / 3); + btn6.setPrefHeight(height / 3); + btn7.setPrefHeight(height / 3); + btn8.setPrefHeight(height / 3); + btn9.setPrefHeight(height / 3); + } + + public void initialize() { + setButtonDimensions(Main.STAGE_DEFAULT_WIDTH, Main.STAGE_DEFAULT_HEIGHT); + setButtonsFont(Main.BUTTON_DEFAULT_FONT_SIZE); + + if (WelcomeScreenController.getPlayerOneName().equals("")) { + playerOneName = "Player 1"; + } else { + playerOneName = WelcomeScreenController.getPlayerOneName(); + } + + if (WelcomeScreenController.getPlayerTwoName().equals("")) { + playerTwoName = "Player 2"; + } else { + playerTwoName = WelcomeScreenController.getPlayerTwoName(); + } + + playerOneSymbol = WelcomeScreenController.getPlayerOneSymbol(); + playerTwoSymbol = WelcomeScreenController.getPlayerTwoSymbol(); + + playerOneColor = WelcomeScreenController.getPlayerOneColor(); + playerTwoColor = WelcomeScreenController.getPlayerTwoColor(); + + btn1.setOnMouseClicked(new EventHandler() { + @Override + public void handle(MouseEvent event) { + if (turnsCount % 2 == 0 && btn1.getText().equals("")) { + turnsCount++; + btn1.setText(playerOneSymbol); + btn1.setTextFill(Paint.valueOf(playerOneColor)); + } else if (turnsCount % 2 != 0 && btn1.getText().equals("")) { + turnsCount++; + btn1.setText(playerTwoSymbol); + btn1.setTextFill(Paint.valueOf(playerTwoColor)); + } + resultTest(Main.getPrimaryStage(), btn1.getText()); + } + }); + + btn2.setOnMouseClicked(new EventHandler() { + @Override + public void handle(MouseEvent event) { + if (turnsCount % 2 == 0 && btn2.getText().equals("")) { + turnsCount++; + btn2.setText(playerOneSymbol); + btn2.setTextFill(Paint.valueOf(playerOneColor)); + } else if (turnsCount % 2 != 0 && btn2.getText().equals("")) { + turnsCount++; + btn2.setText(playerTwoSymbol); + btn2.setTextFill(Paint.valueOf(playerTwoColor)); + } + resultTest(Main.getPrimaryStage(), btn2.getText()); + } + }); + + btn3.setOnMouseClicked(new EventHandler() { + @Override + public void handle(MouseEvent event) { + if (turnsCount % 2 == 0 && btn3.getText().equals("")) { + turnsCount++; + btn3.setText(playerOneSymbol); + btn3.setTextFill(Paint.valueOf(playerOneColor)); + } else if (turnsCount % 2 != 0 && btn3.getText().equals("")) { + turnsCount++; + btn3.setText(playerTwoSymbol); + btn3.setTextFill(Paint.valueOf(playerTwoColor)); + } + resultTest(Main.getPrimaryStage(), btn3.getText()); + } + }); + + btn4.setOnMouseClicked(new EventHandler() { + @Override + public void handle(MouseEvent event) { + if (turnsCount % 2 == 0 && btn4.getText().equals("")) { + turnsCount++; + btn4.setText(playerOneSymbol); + btn4.setTextFill(Paint.valueOf(playerOneColor)); + } else if (turnsCount % 2 != 0 && btn4.getText().equals("")) { + turnsCount++; + btn4.setText(playerTwoSymbol); + btn4.setTextFill(Paint.valueOf(playerTwoColor)); + } + resultTest(Main.getPrimaryStage(), btn4.getText()); + } + }); + + btn5.setOnMouseClicked(new EventHandler() { + @Override + public void handle(MouseEvent event) { + if (turnsCount % 2 == 0 && btn5.getText().equals("")) { + turnsCount++; + btn5.setText(playerOneSymbol); + btn5.setTextFill(Paint.valueOf(playerOneColor)); + } else if (turnsCount % 2 != 0 && btn5.getText().equals("")) { + turnsCount++; + btn5.setText(playerTwoSymbol); + btn5.setTextFill(Paint.valueOf(playerTwoColor)); + } + resultTest(Main.getPrimaryStage(), btn5.getText()); + } + }); + + btn6.setOnMouseClicked(new EventHandler() { + @Override + public void handle(MouseEvent event) { + if (turnsCount % 2 == 0 && btn6.getText().equals("")) { + turnsCount++; + btn6.setText(playerOneSymbol); + btn6.setTextFill(Paint.valueOf(playerOneColor)); + } else if (turnsCount % 2 != 0 && btn6.getText().equals("")) { + turnsCount++; + btn6.setText(playerTwoSymbol); + btn6.setTextFill(Paint.valueOf(playerTwoColor)); + } + resultTest(Main.getPrimaryStage(), btn6.getText()); + } + }); + + btn7.setOnMouseClicked(new EventHandler() { + @Override + public void handle(MouseEvent event) { + if (turnsCount % 2 == 0 && btn7.getText().equals("")) { + turnsCount++; + btn7.setText(playerOneSymbol); + btn7.setTextFill(Paint.valueOf(playerOneColor)); + } else if (turnsCount % 2 != 0 && btn7.getText().equals("")) { + turnsCount++; + btn7.setText(playerTwoSymbol); + btn7.setTextFill(Paint.valueOf(playerTwoColor)); + } + resultTest(Main.getPrimaryStage(), btn7.getText()); + } + }); + + btn8.setOnMouseClicked(new EventHandler() { + @Override + public void handle(MouseEvent event) { + if (turnsCount % 2 == 0 && btn8.getText().equals("")) { + turnsCount++; + btn8.setText(playerOneSymbol); + btn8.setTextFill(Paint.valueOf(playerOneColor)); + } else if (turnsCount % 2 != 0 && btn8.getText().equals("")) { + turnsCount++; + btn8.setText(playerTwoSymbol); + btn8.setTextFill(Paint.valueOf(playerTwoColor)); + } + resultTest(Main.getPrimaryStage(), btn8.getText()); + } + }); + + btn9.setOnMouseClicked(new EventHandler() { + @Override + public void handle(MouseEvent event) { + if (turnsCount % 2 == 0 && btn9.getText().equals("")) { + turnsCount++; + btn9.setText(playerOneSymbol); + btn9.setTextFill(Paint.valueOf(playerOneColor)); + } else if (turnsCount % 2 != 0 && btn9.getText().equals("")) { + turnsCount++; + btn9.setText(playerTwoSymbol); + btn9.setTextFill(Paint.valueOf(playerTwoColor)); + } + resultTest(Main.getPrimaryStage(), btn9.getText()); + } + }); + } + + // this method set the font size of the text which will be visible on the button (either 'O' or 'X') + private void setButtonsFont(double font) { + Font font1 = new Font(font); + btn1.setFont(font1); + btn2.setFont(font1); + btn3.setFont(font1); + btn4.setFont(font1); + btn5.setFont(font1); + btn6.setFont(font1); + btn7.setFont(font1); + btn8.setFont(font1); + btn9.setFont(font1); + } + + // this method check the all possible conditions of winning of a player and + // also check for draw case when all blocks are filled with the data provided by the players playing game + private void resultTest(Stage primaryStage, String buttonText) { + if (btn1.getText().equals(btn2.getText()) && btn2.getText().equals(btn3.getText()) && + !btn1.getText().equals("") && !btn2.getText().equals("") && !btn3.getText().equals("")) { + System.out.println("Game Over"); + gameOverDialog(primaryStage, buttonText); + } else if (btn1.getText().equals(btn4.getText()) && btn4.getText().equals(btn7.getText()) && + !btn1.getText().equals("") && !btn4.getText().equals("") && !btn7.getText().equals("")) { + System.out.println("Game Over"); + gameOverDialog(primaryStage, buttonText); + } else if (btn3.getText().equals(btn6.getText()) && btn6.getText().equals(btn9.getText()) && + !btn3.getText().equals("") && !btn6.getText().equals("") && !btn9.getText().equals("")) { + System.out.println("Game Over"); + gameOverDialog(primaryStage, buttonText); + } else if (btn7.getText().equals(btn8.getText()) && btn8.getText().equals(btn9.getText()) && + !btn7.getText().equals("") && !btn8.getText().equals("") && !btn9.getText().equals("")) { + System.out.println("Game Over"); + gameOverDialog(primaryStage, buttonText); + } else if (btn3.getText().equals(btn5.getText()) && btn5.getText().equals(btn7.getText()) && + !btn3.getText().equals("") && !btn5.getText().equals("") && !btn7.getText().equals("")) { + System.out.println("Game Over"); + gameOverDialog(primaryStage, buttonText); + } else if (btn1.getText().equals(btn5.getText()) && btn5.getText().equals(btn9.getText()) && + !btn1.getText().equals("") && !btn5.getText().equals("") && !btn9.getText().equals("")) { + System.out.println("Game Over"); + gameOverDialog(primaryStage, buttonText); + } else if (btn2.getText().equals(btn5.getText()) && btn5.getText().equals(btn8.getText()) && + !btn2.getText().equals("") && !btn5.getText().equals("") && !btn8.getText().equals("")) { + System.out.println("Game Over"); + gameOverDialog(primaryStage, buttonText); + } else if (btn4.getText().equals(btn5.getText()) && btn5.getText().equals(btn6.getText()) && + !btn4.getText().equals("") && !btn5.getText().equals("") && !btn6.getText().equals("")) { + System.out.println("Game Over"); + gameOverDialog(primaryStage, buttonText); + } else { + if (!btn1.getText().equals("") && !btn2.getText().equals("") && !btn3.getText().equals("") && !btn4.getText().equals("") && !btn5.getText().equals("") && + !btn6.getText().equals("") && !btn7.getText().equals("") && !btn8.getText().equals("") && !btn9.getText().equals("")) { + System.out.println("Game Draw"); + gameOverDialog(primaryStage, "Draw"); + } + } + } + + // this method is used to display the final result of the game + // this method will be called only when either any of player wins the game + // or game get draw. In other cases, this method will not be called by resultTest method + private void gameOverDialog(Stage primaryStage, String buttonText) { + Parent root = null; + FXMLLoader fxmlLoader = new FXMLLoader(); + fxmlLoader.setLocation(getClass().getResource("GameResult.fxml")); + GameResultController gameResultController = null; + + try { + primaryStage.setScene(new Scene(fxmlLoader.load(), Main.STAGE_DEFAULT_WIDTH, Main.STAGE_DEFAULT_HEIGHT)); + gameResultController = fxmlLoader.getController(); + + if (buttonText.equals("Draw")) { + gameResultController.setGameResultText("Draw", ""); + } else { + if (playerOneSymbol.equals(buttonText)) { + gameResultController.setGameResultText("win", playerOneName); + } else { + gameResultController.setGameResultText("win", playerTwoName); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/TicTacToeJavaFX/GameResult.fxml b/src/TicTacToeJavaFX/GameResult.fxml new file mode 100644 index 0000000..0b2c101 --- /dev/null +++ b/src/TicTacToeJavaFX/GameResult.fxml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + +
+ + + + + +
diff --git a/src/TicTacToeJavaFX/GameResultController.java b/src/TicTacToeJavaFX/GameResultController.java new file mode 100644 index 0000000..3d7aaa3 --- /dev/null +++ b/src/TicTacToeJavaFX/GameResultController.java @@ -0,0 +1,76 @@ +package TicTacToeJavaFX; + +import javafx.event.EventHandler; +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.input.MouseEvent; +import javafx.scene.text.Font; +import javafx.scene.text.FontWeight; + +import java.io.IOException; + +public class GameResultController { + + @FXML + private Label congratsLabel; + @FXML + private Label winnerName; + @FXML + private Label gameOverLabel; + @FXML + private Label complementLabel; + @FXML + private Button playAgainButton; + @FXML + private Button exitGameButton; + + // this method dynamically update the content of the final result scene of the game according to the game result + void setGameResultText(String gameResultText1, String winner) { + if (gameResultText1.equals("Draw")) { + gameOverLabel.setFont(Font.font("Algerian", FontWeight.BOLD, 45)); + congratsLabel.setFont(new Font(35)); + winnerName.setFont(new Font(30)); + congratsLabel.setVisible(false); + complementLabel.setVisible(false); + winnerName.setText("Game Draw"); + } else { + gameOverLabel.setFont(Font.font("Algerian", FontWeight.BOLD, 45)); + congratsLabel.setFont(new Font(35)); + winnerName.setFont(new Font(25)); + congratsLabel.setVisible(true); + complementLabel.setVisible(true); + + if (winner.length() > 30) { + winnerName.setText(winner.substring(0, 30)); + } else { + winnerName.setText(winner); + } + + winnerName.setWrapText(true); + } + } + + public void initialize() { + playAgainButton.setOnMouseClicked(new EventHandler() { + @Override + public void handle(MouseEvent event) { + try { + Main.getPrimaryStage().setScene(new Scene + (FXMLLoader.load(getClass().getResource("WelcomeScreen.fxml")), Main.STAGE_DEFAULT_WIDTH, Main.STAGE_DEFAULT_HEIGHT)); + } catch (IOException exception) { + exception.printStackTrace(); + } + } + }); + + exitGameButton.setOnMouseClicked(new EventHandler() { + @Override + public void handle(MouseEvent event) { + Main.getPrimaryStage().close(); + } + }); + } +} diff --git a/src/TicTacToeJavaFX/Main.java b/src/TicTacToeJavaFX/Main.java new file mode 100644 index 0000000..b2b6ee1 --- /dev/null +++ b/src/TicTacToeJavaFX/Main.java @@ -0,0 +1,36 @@ +package TicTacToeJavaFX; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.stage.Stage; + +public class Main extends Application { + + static final int STAGE_DEFAULT_WIDTH = 300; + static final int STAGE_DEFAULT_HEIGHT = 300; + static final int BUTTON_DEFAULT_FONT_SIZE = 40; + + private static Stage stage; + + // method used to retrieve the primaryStage + static Stage getPrimaryStage() { + return stage; + } + + public static void main(String[] args) { + launch(args); + } + + @Override + public void start(Stage primaryStage) throws Exception { + stage = primaryStage; + Parent root = FXMLLoader.load(getClass().getResource("WelcomeScreen.fxml")); + primaryStage.setTitle("Tic Tac Toe"); + Scene scene = new Scene(root, STAGE_DEFAULT_WIDTH, STAGE_DEFAULT_HEIGHT); + primaryStage.setScene(scene); + primaryStage.setResizable(false); + primaryStage.show(); + } +} diff --git a/src/TicTacToeJavaFX/WelcomeScreen.fxml b/src/TicTacToeJavaFX/WelcomeScreen.fxml new file mode 100644 index 0000000..b8787d2 --- /dev/null +++ b/src/TicTacToeJavaFX/WelcomeScreen.fxml @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/TicTacToeJavaFX/WelcomeScreenController.java b/src/TicTacToeJavaFX/WelcomeScreenController.java new file mode 100644 index 0000000..b7d7615 --- /dev/null +++ b/src/TicTacToeJavaFX/WelcomeScreenController.java @@ -0,0 +1,121 @@ +package TicTacToeJavaFX; + +import javafx.event.ActionEvent; +import javafx.event.EventHandler; +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.scene.control.*; +import javafx.scene.paint.Color; +import javafx.scene.text.Font; + +import java.io.IOException; + +public class WelcomeScreenController { + + private static final Font font = new Font(14); + private static String playerOneName; + private static String playerTwoName; + private static String playerOneSymbol; + private static String playerTwoSymbol; + private static String playerOneColor; + private static String playerTwoColor; + + private ToggleGroup toggleGroup; + @FXML + private Label playerOneNameLabel; + @FXML + private TextField playerOneNameField; + + @FXML + private Label playerTwoNameLabel; + @FXML + private TextField playerTwoNameField; + + @FXML + private Label playerOneSymbolLabel; + @FXML + private RadioButton crossSymbol; + @FXML + private RadioButton zeroSymbol; + + @FXML + private Label playerOneColorLabel; + @FXML + private ColorPicker playerOneColorPicker; + + @FXML + private Label playerTwoColorLabel; + @FXML + private ColorPicker playerTwoColorPicker; + + @FXML + private Button startGameButton; + + static String getPlayerOneName() { + return playerOneName; + } + + static String getPlayerTwoName() { + return playerTwoName; + } + + static String getPlayerOneSymbol() { + return playerOneSymbol; + } + + static String getPlayerTwoSymbol() { + return playerTwoSymbol; + } + + static String getPlayerOneColor() { + return playerOneColor; + } + + static String getPlayerTwoColor() { + return playerTwoColor; + } + + public void initialize() { + toggleGroup = new ToggleGroup(); + crossSymbol.setToggleGroup(toggleGroup); + zeroSymbol.setToggleGroup(toggleGroup); + + playerOneNameLabel.setFont(font); + playerTwoNameLabel.setFont(font); + playerOneSymbolLabel.setFont(font); + playerOneColorLabel.setFont(font); + playerTwoColorLabel.setFont(font); + + playerOneColorPicker.setValue(Color.BLACK); + playerTwoColorPicker.setValue(Color.BLACK); + + startGameButton.setOnAction(new EventHandler() { + @Override + public void handle(ActionEvent actionEvent) { + playerOneName = playerOneNameField.getText(); + playerTwoName = playerTwoNameField.getText(); + + if (crossSymbol.isSelected()) { + playerOneSymbol = "X"; + playerTwoSymbol = "O"; + } else { + playerOneSymbol = "O"; + playerTwoSymbol = "X"; + } + + playerOneColor = playerOneColorPicker.getValue().toString(); + playerTwoColor = playerTwoColorPicker.getValue().toString(); + + try { + Parent root = FXMLLoader.load(getClass().getResource("GameBoard.fxml")); + Scene scene = new Scene(root, Main.STAGE_DEFAULT_WIDTH, Main.STAGE_DEFAULT_HEIGHT); + Main.getPrimaryStage().setScene(scene); + } catch (IOException e) { + e.printStackTrace(); + } + } + }); + } +} diff --git a/src/module-info.java b/src/module-info.java new file mode 100644 index 0000000..7f08896 --- /dev/null +++ b/src/module-info.java @@ -0,0 +1,7 @@ +module SampleJavaFX { + + requires javafx.fxml; + requires javafx.controls; + + opens TicTacToeJavaFX; +} \ No newline at end of file