game-client

Play TicTacToe and Reversi
Log | Files | Refs

TicTacToeSinglePlayerController.java (3782B)


      1 package nl.isygameclient.controllers.games.tictactoe;
      2 
      3 import com.jfoenix.controls.JFXButton;
      4 import com.jfoenix.controls.JFXComboBox;
      5 
      6 import java.util.ArrayList;
      7 import java.util.List;
      8 import java.util.stream.Stream;
      9 
     10 import javafx.event.ActionEvent;
     11 import javafx.fxml.FXML;
     12 import javafx.scene.control.Label;
     13 import javafx.scene.layout.GridPane;
     14 import nl.isygameclient.Window;
     15 import nl.isygameclient.controllers.games.GameController;
     16 import nl.isygameclient.models.Game;
     17 import nl.isygameclient.models.Player;
     18 import nl.isygameclient.models.PlayerManager;
     19 import nl.isygameclient.models.games.TicTacToe;
     20 import nl.isygameclient.util.StageHandler;
     21 import nl.isygameclient.util.Vector2D;
     22 
     23 public class TicTacToeSinglePlayerController implements GameController {
     24 
     25 	private Game game;
     26 	private final ArrayList<JFXButton> boardButtons = new ArrayList<>();
     27 
     28 	@FXML
     29 	protected Label currentPlayer;
     30 	@FXML
     31 	public Label gameOverText;
     32 
     33 	@FXML
     34 	protected GridPane grid;
     35 
     36 
     37 	@FXML
     38 	protected void initialize() {
     39 		initializeGame();
     40 		initializeBoard();
     41 		updateCurrentPlayerLabel();
     42 	}
     43 
     44 	private void  initializeGame() {
     45 		var players = new ArrayList<>(List.of(new Player("player1", "x") {
     46 			@Override
     47 			public Vector2D<Integer, Integer> onPlayerTurn() {
     48 				return null;
     49 			}
     50 		}, new Player("player2", "o") {
     51 			@Override
     52 			public Vector2D<Integer, Integer> onPlayerTurn() {
     53 				return null;
     54 			}
     55 		}));
     56 		var manager = new PlayerManager(0, players);
     57 		game = new TicTacToe(manager);
     58 
     59 	}
     60 
     61 
     62 	private void initializeBoard() {
     63 		var board = game.getBoard();
     64 		for (int y = 0; y < board.getWidth(); y++) {
     65 			for (int x = 0; x < board.getHeight(); x++) {
     66 				JFXButton button = new JFXButton();
     67 
     68 				var index = x + "," + y;
     69 				button.setId(index);
     70 				button.setMinSize(200.0, 200.0);
     71 				var styleClass = button.getStyleClass();
     72 				styleClass.add("ttt-button");
     73 				styleClass.add("display-large");
     74 				button.setOnAction((ActionEvent event) -> onMoveButtonClick(button));
     75 				boardButtons.add(button);
     76 				grid.add(button, x, y);
     77 			}
     78 		}
     79 	}
     80 
     81 	private void updateCurrentPlayerLabel() {
     82 		currentPlayer.setText(game.getPlayerManager().getCurrentPlayer().getPlayingAs());
     83 	}
     84 
     85 	private void onMoveButtonClick(JFXButton button) {
     86 		// Move
     87 		var id = button.getId().split(",");
     88 		var index = Stream.of(id).mapToInt(Integer::parseInt).toArray();
     89 		var manager = game.getPlayerManager();
     90 		var currentPlayer = manager.getCurrentPlayer();
     91 		if (game.isMoveValid(currentPlayer, new Vector2D<>(index[0], index[1]))) {
     92 			game.move(currentPlayer, new Vector2D<>(index[0], index[1]));
     93 			button.setText(currentPlayer.getPlayingAs());
     94 			updateCurrentPlayerLabel();
     95 		}
     96 
     97 		// Game Over
     98 		if (game.isGameOver()) {
     99 			onGameOver();
    100 		}
    101 	}
    102 
    103 	private void onGameOver() {
    104 		disableBoardButtons();
    105 		if (game.isDraw()) {
    106 			gameOverText.setText("Draw!");
    107 			gameOverText.setVisible(true);
    108 		} else {
    109 			var winner = game.getWinners().get(0).getPlayingAs().toUpperCase();
    110 			gameOverText.setText(String.format("%s, is the Winner!\n\n", winner));
    111 			gameOverText.setVisible(true);
    112 		}
    113 	}
    114 
    115 	private void clearBoardButtons() {
    116 		for (JFXButton button : boardButtons) {
    117 			button.setText("");
    118 		}
    119 	}
    120 
    121 	private void disableBoardButtons() {
    122 		for (JFXButton button : boardButtons) {
    123 			button.setDisable(true);
    124 		}
    125 	}
    126 
    127 	private void enableBoardButtons() {
    128 		for (JFXButton button : boardButtons) {
    129 			button.setDisable(false);
    130 		}
    131 	}
    132 
    133 	@Override
    134 	public void onNewGameButtonClick(ActionEvent e) {
    135 		// Make new Game
    136 		game.restart();
    137 
    138 		clearBoardButtons();
    139 		enableBoardButtons();
    140 		gameOverText.setVisible(false);
    141 	}
    142 
    143 	@Override
    144 	public void onMainMenuButtonClick(ActionEvent e) {
    145 		StageHandler.get().changeSceneOfStage(Window.GAME,"/nl/isygameclient/views/games/tictactoe/TicTacToeMainMenu.fxml");
    146 	}
    147 }