added replay function and clean code

master
Cemal Odabasioglu 2022-06-05 23:46:06 +02:00
parent 83abebacdc
commit 89e1461810
6 changed files with 86 additions and 161 deletions

View File

@ -20,9 +20,7 @@ public class LaticeApplicationConsole {
ArrayList<Tile> listOfTile = new ArrayList<Tile>();
//System.out.println("Hello Latice !");
/*
for (Color color : Color.values()) {
for (Shape shape : Shape.values()) {
Tile tile = new Tile(color, shape);
@ -32,68 +30,7 @@ public class LaticeApplicationConsole {
}
}
System.out.println("-----------------");
System.out.println("Notre Deck :");
Deck deck = new Deck(listOfTile);
deck.displayListTile();
System.out.println("-----------------");
Rack rack = new Rack(deck);
System.out.println("-----------------");
deck.displayListTile();
System.out.println("-----------------");
GameBoard board = new GameBoard();
board.displayGameBoard();
System.out.println("-----------------");
board.setGridBoard(" NV ", 4, 4);
board.displayGameBoard();
System.out.println("-----------------");
Score scorePlayer1 = new Score();
Score scorePlayer2 = new Score();
Player player1 = new Player("player1", scorePlayer1);
Player player2 = new Player("player2", scorePlayer2);
System.out.println(player1.getName() + " a " + scorePlayer1.getScore() +" points");
System.out.println(player2.getName() + " a " + scorePlayer2.getScore() +" points");
rack.displayRack();
*/
for (Color color : Color.values()) {
for (Shape shape : Shape.values()) {
Tile tile = new Tile(color, shape);
System.out.println(color.getStringColor() + shape.getStringShape()+ ".png");
listOfTile.add(tile);
}
}
System.out.println("-----------------");
//System.out.println("Notre Deck :");
//Deck deck1 = new Deck(listOfTile);
//Deck deck2 = new Deck(listOfTile);
//deck1.displayListTile();
System.out.println("-----------------");
//Rack rack1 = new Rack(deck1);
//Rack rack2 = new Rack(deck2);
//Score scorePlayer1 = new Score();
//Score scorePlayer2 = new Score();
//Player player1 = new Player("player1", scorePlayer1);
//Player player2 = new Player("player2", scorePlayer2);
System.out.println("-----------------");
//GameBoard board = new GameBoard();
//board.displayGameBoard();
System.out.println("-----------------");
//System.out.println(player1.getName() + " a " + scorePlayer1.getScore() +" points");
//System.out.println(player2.getName() + " a " + scorePlayer2.getScore() +" points");
//rack1.displayRack();
System.out.println("Hello Latice !");
@ -171,7 +108,7 @@ public class LaticeApplicationConsole {
case 2:
if (player.getScore()>=3) {
//Donner une action supplémentaire et enlever 3 points au joueur
//Buy another action and remove 2 points from score
player.Play(play, board, 0);
player.diffScore(2);
}else {

View File

@ -58,11 +58,10 @@ import latice.model.window.PlayerFX;
public class LaticeApplicationWindow extends Application {
Image image = new Image("backgroundLatice.png");
ImageView imageView = new ImageView(image);
//lists for tiles
ArrayList<Tile> listRackTile;
ArrayList<Image> listRackImage;
ArrayList<Tile> listOfTile = new ArrayList<Tile>();
@ -72,8 +71,6 @@ public class LaticeApplicationWindow extends Application {
//settings players
//public Player player1;
//public Player player2;
public Player player;
// informations of the player
@ -84,23 +81,20 @@ public class LaticeApplicationWindow extends Application {
Button changeButton;
Button buyActionButton;
//setting the referee to check rules, the GameBoard where the tile are placed and the Rectangle to put the image tile in the plateau
//setting the referee to check rules, the GameBoard where the tile are placed and the Rectangle to put the image tile in the game board
Rectangle[][] rect;
GameBoard board;
Rules referee;
//setting to know if the tile is well dropped with the check of rules or not
//To start, it's false
Boolean tileDropped = false;
Paint tileOnRect;
//tile is free when it the first tile put but tile is payable after
//tile is free when it's the first tile put but tile is payable after
Boolean freeOrPayableTile = true;
//borderPane layout
public static BorderPane borderPane = new BorderPane();
//StackPane for background image + BorderPane onto it
@ -116,6 +110,8 @@ public class LaticeApplicationWindow extends Application {
int confirmBtnClickedCount;
Parent loader;
static Scene menu;
public static void main(String[] args) {
Application.launch(args);
@ -126,8 +122,9 @@ public class LaticeApplicationWindow extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent loader = FXMLLoader.load(getClass().getResource("../view/MainScreen.fxml"));
Scene menu = new Scene(loader, 1280, 720);
//setting main menu screen
loader = FXMLLoader.load(getClass().getResource("../view/MainScreen.fxml"));
menu = new Scene(loader, Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT);
MainScreenController MSC = new MainScreenController();
@ -143,29 +140,40 @@ public class LaticeApplicationWindow extends Application {
}
public void startGame(Stage stage, StackPane parentStackPaneStock, Player player1, Player player2, Object menuBorderPane) {
public void startGame(Stage stage, StackPane parentStackPaneStock, Player player1, Player player2, Object menuBorderPane, Boolean isRestart) {
//changing layouts to start the game
parentStackPane = parentStackPaneStock;
StackPane root = getRootLayout();
root.translateYProperty().set(stage.getHeight());
System.out.println(parentStackPane);
System.out.println(parentStackPaneStock);
if (parentStackPane.getChildren().contains(root)) {
if (isRestart) {
//if we're restarting the game then do some settings
System.out.println("Voici le menu : " + menu);
Constant.START = true;
stage.setScene(menu);
parentStackPane.getChildren().remove(root);
}
parentStackPane.getChildren().add(root);
//settings for the animation
Timeline timeline = new Timeline();
KeyValue kv = new KeyValue(root.translateYProperty(), 0, Interpolator.EASE_IN);
KeyFrame kf = new KeyFrame(Duration.seconds(1), kv);
timeline.getKeyFrames().add(kf);
//when the animation is finished we're removing the main screen
timeline.setOnFinished(t -> {
parentStackPane.getChildren().add(root);
parentStackPane.getChildren().remove(menuBorderPane);
});
timeline.play();
}else {
//else if it's the first time we're launching it
root.translateYProperty().set(stage.getHeight());
parentStackPane.getChildren().add(root);
//settings for the animation
Timeline timeline = new Timeline();
KeyValue kv = new KeyValue(root.translateYProperty(), 0, Interpolator.EASE_IN);
KeyFrame kf = new KeyFrame(Duration.seconds(1), kv);
timeline.getKeyFrames().add(kf);
//when the animation is finished we're removing the main screen
timeline.setOnFinished(t -> {
parentStackPane.getChildren().remove(menuBorderPane);
});
timeline.play();
}
@ -177,12 +185,14 @@ public class LaticeApplicationWindow extends Application {
title.setFont(new Font(30));
topVbox.getChildren().add(title);
topVbox.setAlignment(Pos.CENTER);
//error label for displaying errors
ErrorLabel.setFont(new Font(20));
ErrorLabel.setTextFill(Constant.realColor.RED);
topVbox.getChildren().add(ErrorLabel);
borderPane.setTop(topVbox);
//Image
//background image
Pane pane = new Pane();
BackgroundImage myBG= new BackgroundImage(image,
BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER,
@ -234,7 +244,11 @@ public class LaticeApplicationWindow extends Application {
//----------- group all players in a HBox to display -----------//
HBox infoPlayers = new HBox();
if (isRestart) {
infoPlayers.getChildren().removeAll(infoPlayer1, infoPlayer2);
}
infoPlayers.getChildren().addAll(infoPlayer1, infoPlayer2);
HBox.setMargin(infoPlayer1, new Insets(50,0,0,55));
HBox.setMargin(infoPlayer2, new Insets(50,0,0,55));
infoPlayers.setSpacing(850);
@ -264,7 +278,6 @@ public class LaticeApplicationWindow extends Application {
System.out.println("-----------------");
Rack rack = new Rack(deck);
System.out.println("-----------------");
//deck.displayListTile();
//Player
@ -296,6 +309,7 @@ public class LaticeApplicationWindow extends Application {
buyActionButton.setPrefHeight(Constant.ACTION_BUTTONS_HEIGHT);
confirmButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
//confirming the end of the round and switching round
@Override
public void handle(MouseEvent arg0) {
@ -306,7 +320,6 @@ public class LaticeApplicationWindow extends Application {
try {
switchToGameFinishedScreen();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
@ -314,22 +327,9 @@ public class LaticeApplicationWindow extends Application {
player.getRack().displayRack();
player.getRack().updateRack();
//HBox newRackImage = rack2.createTileImage();
//rackImage.getChildren().clear();
//listRackTile = rack2.getListRackTile();
//System.out.println(listRackTile);
player.getRack().displayRack();
//Setting drag n drop on tiles
//setDragnDropOnRectangles(rect, board, referee, player);
//making names colored in red to make the players know which player's round is
if (confirmBtnClickedCount%2 == 0) {
playerFX.setFillName(Constant.realColor.BLACK);
player = player1;
@ -354,16 +354,20 @@ public class LaticeApplicationWindow extends Application {
}
private void switchToGameFinishedScreen() throws IOException {
//switching to game finished screen if the game finishes
System.out.println("confirmBtnClickedCount : " + confirmBtnClickedCount);
if (confirmBtnClickedCount>=20) {
if (confirmBtnClickedCount>=2) {
Parent loader = FXMLLoader.load(getClass().getResource("../view/GameFinishedScreen.fxml"));
Scene gameFinishedScreenScene = new Scene(loader, 1280, 720);
Scene gameFinishedScreenScene = new Scene(loader, Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT);
if (player1.getNumberOfTilesRemaining() < player2.getNumberOfTilesRemaining()) {
// if player 1 has less tiles then he wins
GameFinishedScreenController.staticNameWinner.setText(player1.getName());
GameFinishedScreenController.staticNameLooser.setText(player2.getName());
}else {
//if player 2 has less tiles then he wins
GameFinishedScreenController.staticNameWinner.setText(player2.getName());
GameFinishedScreenController.staticNameLooser.setText(player1.getName());
}
stage.setScene(gameFinishedScreenScene);
@ -377,35 +381,20 @@ public class LaticeApplicationWindow extends Application {
});
//With Image
//Rack player.getRack() = new Rack(deck);
rackImage = player.getRack().createTileImage();
//Adding lists to Arraylists
listRackTile = player.getRack().getListRackTile();
System.out.println(listRackTile);
listRackImage = player.getRack().getRackTileImage();
System.out.println("listTileImge : " + listRackImage);
//HBox rackTile = rack2.createTileImage();
System.out.println("listTileImge : " + listRackImage);
changeButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent arg0) {
//player.getRack().changeRack();
//HBox newRackImage = rack2.createTileImage();
//rackImage.getChildren().clear();
//listRackTile = rack2.getListRackTile();
//System.out.println(listRackTile);
////// for the actual player //////
if (player.getScore() > 1) {
System.out.println("Changing Rack");
@ -462,19 +451,12 @@ public class LaticeApplicationWindow extends Application {
}
});
//HBox rackTile = rack2.createTileImage();
rackImage.getChildren().addAll(confirmButton, changeButton, buyActionButton);
setDragnDropOnRack(rackImage, player);
System.out.println();
borderPane.setBottom(rackImage);
//------------------------------------------------------------------------
//Setting drag n drop on tiles
@ -484,12 +466,7 @@ public class LaticeApplicationWindow extends Application {
ImagePattern imagePattern = new ImagePattern(listRackImage.get(getIndexTileClicked()));
//------------------------------------------------------------------------
//###################### creating all rectangles and DragnDrop ######################//
//RectangleFX rectFX = new RectangleFX();
//rectFX.createRectangle(root, pane);
//rectFX.dragnDropOnAllRectangles(player1, indexTileClicked, validateBtnClickedCount);
//rectFX.dragnDropOnAllRectangles(player2, indexTileClicked, validateBtnClickedCount);
//------------------------------------------------------------------------
//Setting drag & drop on rectangles
setDragnDropOnRectangles(rect, board, referee, player);
@ -497,8 +474,15 @@ public class LaticeApplicationWindow extends Application {
//--------------------------------------------------------------------------------------
//------if it's à restart game then remove old childrens------------------------------------------------------------
if (root.getChildren().contains(infoPlayers)) {
root.getChildren().remove(infoPlayers);
}
if (root.getChildren().contains(borderPane)) {
root.getChildren().remove(borderPane);
}
//adding new childrens
root.getChildren().addAll(infoPlayers, borderPane);
@ -524,7 +508,7 @@ public class LaticeApplicationWindow extends Application {
if (arg0.getDragboard().hasString()){
Dragboard dragboard = arg0.getDragboard();
//not put the virtual image drag on the case because an image is already into it
//not putting the virtual image drag on the case because an image is already into it
player.getRack().getListRackTile().get(indexTileClicked).setPosition(new Position(a,b));
if ( referee.checkPositionRule(board, player.getRack().getListRackTile().get(indexTileClicked)) == true ) {
rect[a][b].setFill(new ImagePattern(player.getRack().getRackTileImage().get(indexTileClicked)));
@ -578,7 +562,8 @@ public class LaticeApplicationWindow extends Application {
}else {
if (Constant.START) {
if (rect[a][b] == rect[4][4]) {
if (rect[a][b] == rect[Constant.MOON_BOX_X][Constant.MOON_BOX_Y]) {
//if its's the center box (moon box)
System.out.println("MOON valid placement");
board.setGridBoardTile(player.getRack().getListRackTile().get(indexTileClicked), a, b);
@ -596,18 +581,18 @@ public class LaticeApplicationWindow extends Application {
}
}else {
//if it's not the first round of the game
System.out.println("OK3");
System.out.println("Règle effectué");
player.getRack().getListRackTile().get(indexTileClicked).setPosition(new Position(a,b));
//verify if a tile is already placed
if ( referee.checkPositionRule(board, player.getRack().getListRackTile().get(indexTileClicked)) == false ) {
ErrorLabel.setText("Error ! The tile can't be placed here because there is already a tile placed");
rect[a][b].setFill(tileOnRect);
}else {
//if there is not already a tile
int nbr = referee.neighborRule(board , player.getRack().getListRackTile().get(indexTileClicked));
if (nbr == 0) {
@ -638,7 +623,7 @@ public class LaticeApplicationWindow extends Application {
freeOrPayableTile = false;
System.out.println("tuile posé!");
//Sun rule
if (referee.sunRule(board, player.getRack().getListRackTile().get(indexTileClicked))) {
System.out.println("Vous avez gagné 2 points en mettant votre tuile sur un soleil");
playerFX.setAddScore(player, 2);
@ -649,13 +634,6 @@ public class LaticeApplicationWindow extends Application {
}
}
arg0.consume();
}

View File

@ -23,12 +23,15 @@ public class GameFinishedScreenController implements Initializable{
private Label nameWinner;
public static Label staticNameWinner;
public static Label staticNameLooser = new Label();
// Event Listener on Button[#replayBtn].onMouseClicked
@FXML
public void replayBtnClicked(MouseEvent event) {
MainScreenController MSC = new MainScreenController();
MSC.startGameInstruction();
MSC.player1 = MSC.instanciatePlayer(staticNameWinner.getText());
MSC.player2 = MSC.instanciatePlayer(staticNameLooser.getText());
MSC.startGameInstruction(true);
System.out.println("replayBtnClicked");
}
// Event Listener on Button[#quitBtn].onMouseClicked

View File

@ -124,9 +124,10 @@ public class MainScreenController extends LaticeApplicationWindow{
nameInputStage.show();
}
public void startGameInstruction() {
public void startGameInstruction(Boolean isRestart) {
startGame(mainStage, parentStackPaneStock, player1, player2, menuBorderPane);
startGame(mainStage, parentStackPaneStock, player1, player2, menuBorderPane, isRestart);
}
/*public void startGame(Stage stage) {

View File

@ -53,7 +53,7 @@ public class PlayerNameInputController {
if(nomJoueur2.getText().length() < 3 || nomJoueur2.getText().length() > 16) {
label2.setVisible(true);
}
else {
if ((nomJoueur1.getText().length() >= 3 && nomJoueur1.getText().length() <= 16 ) && (nomJoueur2.getText().length() >= 3 && nomJoueur2.getText().length() <= 16)) {
Stage nameInputStage = (Stage) ((Node) event.getTarget()).getScene().getWindow();
//setting player names
@ -72,7 +72,7 @@ public class PlayerNameInputController {
mainScreenController.player2 = mainScreenController.instanciatePlayer(name2);
//PlayerFX.displayPlayers(root ,laticeApplicationWindow.player1, laticeApplicationWindow.player2);
mainScreenController.startGameInstruction();
mainScreenController.startGameInstruction(false);
}
}

View File

@ -4,6 +4,12 @@ public class Constant {
//for the start of the game
public static Boolean START = true;
public static final int SCREEN_HEIGHT = 720;
public static final int SCREEN_WIDTH = 1280;
public static final int MOON_BOX_Y = 4;
public static final int MOON_BOX_X = 4;
//for creating rectangles
public static final int NUMBER_OF_BOX_ON_ONE_LINE = 9;