From 8e2e2a09d30998bc36c01e22bbfda0e48ecf2339 Mon Sep 17 00:00:00 2001 From: Mathis Date: Mon, 30 May 2022 19:35:34 +0200 Subject: [PATCH 1/2] ADD new rules | ADD method changeRack | Change 'Fl' -> 'f' for class Shape | ADD getPositionRow, getPositionColumn in Tile | CREATE Constant 'BLUE' in GameBoard --- .../application/LaticeApplicationConsole.java | 108 +++++++++++++++++- src/main/java/latice/model/GameBoard.java | 5 +- src/main/java/latice/model/Player.java | 36 ++++-- src/main/java/latice/model/Rack.java | 35 +++++- src/main/java/latice/model/Rules.java | 102 ++++++++++++++++- src/main/java/latice/model/Shape.java | 2 +- src/main/java/latice/model/Tile.java | 8 +- 7 files changed, 274 insertions(+), 22 deletions(-) diff --git a/src/main/java/latice/application/LaticeApplicationConsole.java b/src/main/java/latice/application/LaticeApplicationConsole.java index 5c2291b..d9042ac 100644 --- a/src/main/java/latice/application/LaticeApplicationConsole.java +++ b/src/main/java/latice/application/LaticeApplicationConsole.java @@ -1,13 +1,16 @@ package latice.application; import java.util.ArrayList; +import java.util.Objects; import java.util.Scanner; +import java.util.regex.Pattern; import latice.model.Color; import latice.model.Deck; import latice.model.GameBoard; import latice.model.Player; import latice.model.Rack; +import latice.model.Rules; import latice.model.Score; import latice.model.Shape; import latice.model.Tile; @@ -91,7 +94,7 @@ public class LaticeApplicationConsole { //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 !"); System.out.println("-----------------"); @@ -110,14 +113,109 @@ public class LaticeApplicationConsole { GameBoard board = new GameBoard(); board.displayGameBoard(); - + System.out.println(Objects.equals(board.getGridBoard()[1][0], Tile.class)); Scanner play = new Scanner(System.in); + Player player; + Boolean round; + Tile tile = null; + Boolean start = true; + Boolean freeTile; + Rules arbitre = new Rules(); - for(int i = 0; i < 10; i++) { + for(int i = 0; i < 20; i++) { + round = true; + freeTile = true; - player1.Play(play,board); - player2.Play(play,board); + if (i%2 == 0) { + player = player1; + }else { + player = player2; + } + while (round) { + System.out.println("c'est à votre tour de jouer " + player.getName() +"!"); + System.out.println("Vous avez " + player.getScore() +", donc que voulez-vous faire ?\n" + + " 1. Jouer une Tuile (à partir de la deuxième tuile jouée, cela coûtera 2 points)\n" + + " 2. Acheter une action supplémentaire\n" + + " 3. Changer le Rack et passer(coûte 3 points)\n" + + " 4. Passer\n"); + + int choiceMenu = Integer.parseInt(play.next()); + switch(choiceMenu) { + case 1: //if (arbitre.checkScore(freeTile)){ + //System.out.println("Vous n'avez pas assez de points pour jouer un nouvelle tuile"); + //}else { + Boolean rulesCheck = false; + + while (rulesCheck == false) { + + tile = player.Play(play,board,i); + rulesCheck = arbitre.arbitration(player, board, tile, start); + + }; + + if (i == 0) { + start = false; + } + + board.setGridBoard(" "+tile.getShapeConsole()+tile.getColorConsole()+" ", tile.getPositionRow(), tile.getPositionColumn()); + + player.getRack().removeTile(tile); + board.displayGameBoard(); + //} + break; + + + case 2: + + case 3: player.getRack().changeRack(); + System.out.println("Votre rack à été changé avec succès !"); + + case 4: System.out.println("Votre tour est terminé " + player.getName() + " !"); + round = false; + break; + + default: throw new IllegalArgumentException("Veuillez choisir un nombre entre 1 et 4!"); + } + } + + + /* + if (PlayOrPass == 2) { + round = false; + System.out.println("Votre tour est terminé " + player.getName() + " !"); + } + + while (round) { + Boolean rulesCheck = false; + + System.out.println("c'est à votre tour de jouer " + player.getName() +"!"); + + while (rulesCheck == false) { + tile = player.Play(play,board,i); + rulesCheck = arbitre.arbitration(player, board, tile, START); + + }; + + if (i == 0) { + START = false; + } + + board.setGridBoard(" "+tile.getShapeConsole()+tile.getColorConsole()+" ", tile.getPositionRow(), tile.getPositionColumn()); + + player.getRack().removeTile(tile); + board.displayGameBoard(); + + + System.out.println(player.getName() + " ! Voulez-vous passer votre tour ou continuer à jouer ? 1.continuer ou 2.passer"); + int ContinueOrPass = Integer.parseInt(play.next()); + if (ContinueOrPass == 2) { + round = false; + System.out.println("Votre tour est terminé " + player.getName() + " !"); + } + } + + player.getRack().updateRack();*/ } diff --git a/src/main/java/latice/model/GameBoard.java b/src/main/java/latice/model/GameBoard.java index 9da6f67..6370415 100644 --- a/src/main/java/latice/model/GameBoard.java +++ b/src/main/java/latice/model/GameBoard.java @@ -4,6 +4,8 @@ public class GameBoard { private Integer DIMENSION = 9; public static final String SUN = " SU "; public static final String MOON = " MO "; + public static final String BLUE = " "; + private String[][] gridBoard; public GameBoard() { @@ -30,7 +32,7 @@ public class GameBoard { this.gridBoard[i][j] = SUN; }else { - this.gridBoard[i][j] = " "; + this.gridBoard[i][j] = BLUE; } if (j == 8) { @@ -67,6 +69,7 @@ public class GameBoard { if (j == 8) { System.out.println("|"); + } } } diff --git a/src/main/java/latice/model/Player.java b/src/main/java/latice/model/Player.java index 11dfacd..e2ec3fd 100644 --- a/src/main/java/latice/model/Player.java +++ b/src/main/java/latice/model/Player.java @@ -24,12 +24,28 @@ public class Player { return this.name; } + public Rack getRack() { + return this.rack; + } + public Integer getScore() { return this.score.getScore(); } - public void Play(Scanner play, GameBoard board) { - System.out.println("c'est à votre tour de jouer " + this.name +"!"); + public Integer addScore(Integer value) { + int newScore = this.score.getScore()+value; + this.score.setScore(newScore); + return this.score.getScore(); + } + + public Integer diffScore(Integer value) { + int newScore = this.score.getScore()-value; + this.score.setScore(newScore); + return this.score.getScore(); + } + + public Tile Play(Scanner play, GameBoard board, Integer start) { + if (this.getScore() == 0) { System.out.println("Vous avez " + this.getScore() + " point"); @@ -41,17 +57,23 @@ public class Player { System.out.print("Quel tuile voulez-vous jouez ? "); this.rack.displayRack(); - String tileToPlay = play.next(); + System.out.println(1); + Integer idTileToPlay = Integer.parseInt(play.next())-1; + Tile tileToPlay = this.rack.getListRackTile().get(idTileToPlay); System.out.print("Sur quelle ligne, voulez-vous placer la tuile ?"); int row = Integer.parseInt(play.next()); System.out.print("Sur quelle colonne, voulez-vous placer la tuile ?"); int column = Integer.parseInt(play.next()); - board.setGridBoard(" "+tileToPlay+" ", row, column); - this.rack.removeTile(tileToPlay); - board.displayGameBoard(); + tileToPlay.setPosition(new Position(row, column)); + return tileToPlay; - this.rack.updateRack(); + //.setGridBoard(" "+tileToPlay.getShapeConsole()+tileToPlay.getColorConsole()+" ", row, column); + //this.rack.removeTile(tileToPlay); + + //board.displayGameBoard(); + + //this.rack.updateRack(); } diff --git a/src/main/java/latice/model/Rack.java b/src/main/java/latice/model/Rack.java index 94a42e9..fc002f6 100644 --- a/src/main/java/latice/model/Rack.java +++ b/src/main/java/latice/model/Rack.java @@ -40,7 +40,6 @@ public class Rack { } public ArrayList getListRackTile() { - System.out.println(this.listRackTile); return this.listRackTile; } @@ -64,13 +63,14 @@ public class Rack { } - public void removeTile(String stringTile) { + public void removeTile(Tile tileToDelete) { int count = 0; int index = -1; System.out.println("taille : " + this.listRackTile.size()); + String strTileToDelete = tileToDelete.getShapeConsole()+tileToDelete.getColorConsole(); for (Tile tile : this.listRackTile) { System.out.println(count++); - if (stringTile.equals(tile.getShapeConsole() + tile.getColorConsole())) { + if (strTileToDelete.equals(tile.getShapeConsole()+tile.getColorConsole())) { index = this.listRackTile.indexOf(tile); System.out.println(index); System.out.println("tuile supprimé avec succès"); @@ -85,16 +85,41 @@ public class Rack { } + public void changeRack() { + + Tile tile; + int listRackTileSize = this.listRackTile.size(); + + for (int i = 0; i < listRackTileSize ; i++) { + + tile = this.listRackTile.get(0); + this.deck.getListTile().add(tile); + this.listRackTile.remove(0); + } + + for (int i = 0; i < listRackTileSize ; i++) { + int index = (int)(Math.random()*( ((this.deck.getListTile()).size()-1)-0+1)+0); //(int)(Math.random()*(max-min+1)+min); + + tile = (this.deck.getListTile()).get(index); + this.listRackTile.add(tile); + this.deck.getListTile().remove(index); + + } + + } + public void displayRack() { boolean success = false; + Integer tile_id = 1; System.out.print("rack : "); for (Tile tile : this.listRackTile) { if (success) { - System.out.print(", " + tile.getShapeConsole() + tile.getColorConsole()); + System.out.print(", " + tile_id + "." + tile.getShapeConsole() + tile.getColorConsole()); }else { - System.out.print(tile.getShapeConsole() + tile.getColorConsole()); + System.out.print(tile_id + "." + tile.getShapeConsole() + tile.getColorConsole()); success = true; } + tile_id = tile_id + 1; } System.out.println(); } diff --git a/src/main/java/latice/model/Rules.java b/src/main/java/latice/model/Rules.java index 152c2bf..ad1343b 100644 --- a/src/main/java/latice/model/Rules.java +++ b/src/main/java/latice/model/Rules.java @@ -1,11 +1,111 @@ package latice.model; +import java.util.Objects; +import java.util.regex.Pattern; + public class Rules { - private static boolean START = true; + //private static boolean START = true; public Rules() { // TODO Auto-generated constructor stub } + public Boolean moonRule(GameBoard board, Tile tile) { + if (GameBoard.MOON.equals(board.getGridBoard()[tile.getPositionRow()][tile.getPositionColumn()])) { + System.out.println("La première tuile se trouve bien sur la lune !"); + return true; + }else { + System.out.println("La première tuile doit être placé sur la lune, recommencez !"); + return false; + } + } + + public Integer neighborRule(GameBoard board, Tile tile) { + + Integer nbrNeighbor = 0; + String checkNeighbor = null; + Boolean checkCase = false; + + for(int i = 0; i < 2 ; i++) { + for(int j = -1; j < 2 ; j=j+2) { + if (i == 0) { + if (tile.getPositionColumn()+j >= 0 && tile.getPositionColumn()+j <= 9) { + checkNeighbor = board.getGridBoard()[tile.getPositionRow()][tile.getPositionColumn()+j]; + checkCase = true; + } + }else { + if (tile.getPositionRow()+j >= 0 && tile.getPositionRow()+j <= 9) { + checkNeighbor = board.getGridBoard()[tile.getPositionRow()+j][tile.getPositionColumn()]; + checkCase = true; + } + } + + if (checkCase) { + if (!(GameBoard.SUN.equals(checkNeighbor)) || !(GameBoard.BLUE.equals(checkNeighbor))) { + + System.out.println("Il y a une tuile"); + + if ( tile.getShapeConsole().equals(checkNeighbor.substring(1, 2)) || tile.getColorConsole().equals(checkNeighbor.substring(2, 3)) ) { + System.out.println("Il y a correspondance avec la tuile !"); + nbrNeighbor = nbrNeighbor + 1; + }else { + System.out.println("Il n'y a pas correspondance avec la tuile !"); + + } + } + } + } + } + + + return nbrNeighbor; + } + + public Boolean sunRule(GameBoard board, Tile tile) { + + Boolean sun; + + if (GameBoard.SUN.equals(board.getGridBoard()[tile.getPositionRow()][tile.getPositionColumn()])) { + sun = true; + }else { + sun = false; + } + + return sun; + } + + public Boolean arbitration(Player player, GameBoard board, Tile tile, Boolean start) { + + if (start == true){ + return this.moonRule(board, tile); + }else { + System.out.println("-----------------------------"); + if (this.sunRule(board, tile)){ + player.addScore(2); + } + int nbr = this.neighborRule(board, tile); + if (nbr == 0) { + System.out.println("l'emplacement où est posé la tuile n'a pas de voisin ou il n'y a pas de correspondance avec les voisins !"); + return false; + + }else { + if (nbr == 2) { + System.out.println("Vous avez gagné 1 point"); + player.addScore(1); + }else if (nbr == 3) { + System.out.println("Vous avez gagné 2 points"); + player.addScore(2); + }else if (nbr == 4) { + System.out.println("Vous avez gagné 4 points"); + player.addScore(4); + } + return true; + + } + + + } + + } } diff --git a/src/main/java/latice/model/Shape.java b/src/main/java/latice/model/Shape.java index 442ef31..dd385ec 100644 --- a/src/main/java/latice/model/Shape.java +++ b/src/main/java/latice/model/Shape.java @@ -3,7 +3,7 @@ package latice.model; public enum Shape { BIRD("bird", "B"), DOLPHIN("dolphin", "D"), - FLOWER("flower", "Fl"), + FLOWER("flower", "f"), FEATHER("feather", "F"), GECKO("gecko", "G"), TURTLE("turtle", "T"); diff --git a/src/main/java/latice/model/Tile.java b/src/main/java/latice/model/Tile.java index 0c98546..4f506d8 100644 --- a/src/main/java/latice/model/Tile.java +++ b/src/main/java/latice/model/Tile.java @@ -30,7 +30,11 @@ public class Tile { return this.shape.getStringShapeConsole(); } - public Position getPosition() { - return this.position; + public Integer getPositionRow() { + return this.position.getRow(); + } + + public Integer getPositionColumn() { + return this.position.getColumn(); } } From b2502ae030131b755d61db24dc133c9cc3a0a3e3 Mon Sep 17 00:00:00 2001 From: cemal Date: Mon, 30 May 2022 19:39:23 +0200 Subject: [PATCH 2/2] added player name input window --- .../application/LaticeApplicationWindow.java | 44 +++++++--- .../controller/MainScreenController.java | 80 +++++++++++++++---- .../controller/PlayerNameInputController.java | 39 +++++++++ .../java/latice/view/PlayerNameInput.fxml | 40 ++++++++++ 4 files changed, 176 insertions(+), 27 deletions(-) create mode 100644 src/main/java/latice/controller/PlayerNameInputController.java create mode 100644 src/main/java/latice/view/PlayerNameInput.fxml diff --git a/src/main/java/latice/application/LaticeApplicationWindow.java b/src/main/java/latice/application/LaticeApplicationWindow.java index 108a9ce..2bde023 100644 --- a/src/main/java/latice/application/LaticeApplicationWindow.java +++ b/src/main/java/latice/application/LaticeApplicationWindow.java @@ -16,6 +16,7 @@ import javafx.geometry.Pos; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.Button; +import javafx.scene.control.Label; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.ClipboardContent; @@ -38,6 +39,7 @@ import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.stage.Stage; import latice.controller.MainScreenController; +import latice.controller.PlayerNameInputController; import latice.model.Color; import latice.model.Deck; import latice.model.Rack; @@ -72,10 +74,18 @@ public class LaticeApplicationWindow extends Application { ArrayList listOfTile = new ArrayList(); Map assocRectangleTile = new HashMap(); static StackPane rootLayout; + private Label namePlayer1 = new Label(); + private Label namePlayer2 = new Label(); - public static int indexTileClicked; + //root layout + BorderPane root = new BorderPane(); + + //StackPane for background image + BorderPane root onto it + StackPane stackPane = new StackPane(); + static Stage primaryStageCopy; + @@ -92,11 +102,8 @@ public class LaticeApplicationWindow extends Application { Parent loader = FXMLLoader.load(getClass().getResource("../view/MainScreen.fxml")); Scene menu = new Scene(loader, 1280, 720); MainScreenController MSC = new MainScreenController(); + - //StackPane for background image + BorderPane root onto it - StackPane stackPane = new StackPane(); - //root layout - BorderPane root = new BorderPane(); //Title Text title = new Text("Latice"); @@ -276,10 +283,10 @@ public class LaticeApplicationWindow extends Application { } } - - + root.setLeft(namePlayer1); //-------------------------------------------------------------------------------------- + setPrimaryStage(primaryStage); setRootLayout(stackPane); stackPane.getChildren().add(root); @@ -302,9 +309,9 @@ public class LaticeApplicationWindow extends Application { public static int getIndexTileClicked() { return indexTileClicked; } - - - + public void setPrimaryStage(Stage primaryStage) { + this.primaryStageCopy = primaryStage; + } //Setter to set the mouse clicked tile @@ -312,6 +319,23 @@ public class LaticeApplicationWindow extends Application { LaticeApplicationWindow.indexTileClicked = indexTileClicked; } + //player names setters + public void setNamePlayer1(String namePlayer1) { + this.namePlayer1.setText(namePlayer1); + } + + public void setNamePlayer2(String namePlayer2) { + this.namePlayer2.setText(namePlayer2); + } + public void playerNamesEntered() { + System.out.println("entered playNamesEntered()" + namePlayer1 + " VS " + namePlayer2); + HBox scoreHbox = new HBox(); + scoreHbox.getChildren().add(namePlayer1); + scoreHbox.getChildren().add(namePlayer2); + Text working = new Text("Working"); + root.setLeft(working); + primaryStageCopy.show(); + } } diff --git a/src/main/java/latice/controller/MainScreenController.java b/src/main/java/latice/controller/MainScreenController.java index 99da2bb..2ebb3b7 100644 --- a/src/main/java/latice/controller/MainScreenController.java +++ b/src/main/java/latice/controller/MainScreenController.java @@ -1,12 +1,16 @@ package latice.controller; +import java.io.IOException; + import javafx.animation.Interpolator; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.application.Platform; import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; import javafx.scene.Node; +import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.input.MouseEvent; @@ -29,28 +33,22 @@ public class MainScreenController extends LaticeApplicationWindow{ private StackPane parentStackPane; @FXML private BorderPane menuBorderPane; + public static Stage mainStage; + private static StackPane parentStackPaneStock; // Event Listener on Rectangle[#playButton].onMouseClicked @FXML - public void playButtonClicked(MouseEvent event) { + public void playButtonClicked(MouseEvent event) throws IOException { + System.out.println(parentStackPane); + parentStackPaneStock = parentStackPane; System.out.println("playButtonClicked"); - Stage primaryStage = (Stage) ((Node) event.getTarget()).getScene().getWindow(); - StackPane root = getRootLayout(); - root.translateYProperty().set(primaryStage.getHeight()); - parentStackPane.getChildren().add(root); - - //parameters of 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(); + mainStage = (Stage) ((Node) event.getTarget()).getScene().getWindow(); + System.out.println(mainStage); + if (!PlayerNameInputController.btnClicked) { + playerNamesInput(event); + } } + // Event Listener on Rectangle[#rulesButton].onMouseClicked @FXML public void rulesButtonClicked(MouseEvent event) { @@ -86,4 +84,52 @@ public class MainScreenController extends LaticeApplicationWindow{ System.out.println("exitButtonClicked"); Platform.exit(); } + + public void playerNamesInput(MouseEvent event) throws IOException { + Parent loader = FXMLLoader.load(getClass().getResource("../view/PlayerNameInput.fxml")); + Scene nameInputScene = new Scene(loader, 600, 300); + Stage primaryStage = (Stage) ((Node) event.getTarget()).getScene().getWindow(); + + // New window (Stage) + Stage nameInputStage = new Stage(); + nameInputStage.setTitle("Names"); + nameInputStage.setScene(nameInputScene); + + // Specifies the modality for new window + nameInputStage.initModality(Modality.WINDOW_MODAL); + + // Specifies the owner window + nameInputStage.initOwner(primaryStage); + + // Set position of window + nameInputStage.setX(primaryStage.getX() + 300); + nameInputStage.setY(primaryStage.getY() + 175); + + nameInputStage.show(); + } + + public void startGameInstruction() { + startGame(mainStage); + } + + public void startGame(Stage stage) { + parentStackPane = parentStackPaneStock; + StackPane root = getRootLayout(); + root.translateYProperty().set(stage.getHeight()); + System.out.println(parentStackPane); + System.out.println(parentStackPaneStock); + parentStackPane.getChildren().add(root); + + //parameters of 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(); + } } diff --git a/src/main/java/latice/controller/PlayerNameInputController.java b/src/main/java/latice/controller/PlayerNameInputController.java new file mode 100644 index 0000000..6d181b4 --- /dev/null +++ b/src/main/java/latice/controller/PlayerNameInputController.java @@ -0,0 +1,39 @@ +package latice.controller; + +import javafx.application.Platform; +import javafx.fxml.FXML; +import javafx.scene.Node; +import javafx.scene.control.Button; + +import javafx.scene.control.TextField; +import javafx.scene.input.MouseEvent; +import javafx.stage.Stage; +import latice.application.LaticeApplicationWindow; + +public class PlayerNameInputController { + @FXML + private TextField nomJoueur1; + @FXML + private TextField nomJoueur2; + @FXML + public Button btnValid; + public static boolean btnClicked = false; + MainScreenController mainScreenController = new MainScreenController(); + LaticeApplicationWindow laticeApplicationWindow = new LaticeApplicationWindow(); + + @FXML + public void validBtnClicked(MouseEvent event) { + System.out.println("valid Button Clicked"); + Stage nameInputStage = (Stage) ((Node) event.getTarget()).getScene().getWindow(); + //setting player names + String name1 = nomJoueur1.getText(); + String name2 = nomJoueur2.getText(); + laticeApplicationWindow.setNamePlayer1(name1); + laticeApplicationWindow.setNamePlayer2(name2); + btnClicked = true; + nameInputStage.close(); + laticeApplicationWindow.playerNamesEntered(); + mainScreenController.startGameInstruction(); + } + +} diff --git a/src/main/java/latice/view/PlayerNameInput.fxml b/src/main/java/latice/view/PlayerNameInput.fxml new file mode 100644 index 0000000..294994e --- /dev/null +++ b/src/main/java/latice/view/PlayerNameInput.fxml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + +