ADD method checkPostionRule in Rules | implementation of

checkPositionRule in LaticeApplicationWindow to avoid putting a tile on
a tile
master
Mathis 2022-06-03 22:59:40 +02:00
parent acd757b978
commit 2927f59f8a
2 changed files with 55 additions and 41 deletions

View File

@ -35,6 +35,7 @@ import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane; import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import javafx.scene.paint.ImagePattern; import javafx.scene.paint.ImagePattern;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle; import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font; import javafx.scene.text.Font;
import javafx.scene.text.Text; import javafx.scene.text.Text;
@ -95,6 +96,7 @@ public class LaticeApplicationWindow extends Application {
//To start, it's false //To start, it's false
Boolean tileDropped = false; Boolean tileDropped = false;
Paint tileOnRect;
//borderPane layout //borderPane layout
public static BorderPane borderPane = new BorderPane(); public static BorderPane borderPane = new BorderPane();
@ -440,7 +442,7 @@ public class LaticeApplicationWindow extends Application {
//------------------------------------------------------------------------ //------------------------------------------------------------------------
//###################### creating all rectangles and DragnDrop ######################// //###################### creating all rectangles and DragnDrop ######################//
//ectangleFX rectFX = new RectangleFX(); //RectangleFX rectFX = new RectangleFX();
//rectFX.createRectangle(root, pane); //rectFX.createRectangle(root, pane);
//rectFX.dragnDropOnAllRectangles(player1, indexTileClicked, validateBtnClickedCount); //rectFX.dragnDropOnAllRectangles(player1, indexTileClicked, validateBtnClickedCount);
//rectFX.dragnDropOnAllRectangles(player2, indexTileClicked, validateBtnClickedCount); //rectFX.dragnDropOnAllRectangles(player2, indexTileClicked, validateBtnClickedCount);
@ -450,19 +452,6 @@ public class LaticeApplicationWindow extends Application {
setDragnDropOnRectangles(rect, board, referee, player); setDragnDropOnRectangles(rect, board, referee, player);
//rules / referee implementaion
//this.transition(namePlayer1, namePlayer2);
//root.setLeft(namePlayer1);
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
@ -470,7 +459,6 @@ public class LaticeApplicationWindow extends Application {
root.getChildren().addAll(infoPlayers, borderPane); root.getChildren().addAll(infoPlayers, borderPane);
//Règles
} }
@ -481,14 +469,23 @@ public class LaticeApplicationWindow extends Application {
int a = i; int a = i;
int b = j; int b = j;
rect[a][b].setOnDragEntered(new EventHandler<DragEvent>() { rect[a][b].setOnDragEntered(new EventHandler<DragEvent>() {
@Override @Override
public void handle(DragEvent arg0) { public void handle(DragEvent arg0) {
tileOnRect = rect[a][b].getFill();
if (arg0.getDragboard().hasString()){ if (arg0.getDragboard().hasString()){
Dragboard dragboard = arg0.getDragboard(); Dragboard dragboard = arg0.getDragboard();
rect[a][b].setFill(new ImagePattern(player.getRack().getRackTileImage().get(getIndexTileClicked()))); //not put 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)));
}
} }
arg0.consume(); arg0.consume();
} }
@ -499,7 +496,8 @@ public class LaticeApplicationWindow extends Application {
@Override @Override
public void handle(DragEvent arg0) { public void handle(DragEvent arg0) {
if (arg0.isDropCompleted() == false) { if (arg0.isDropCompleted() == false) {
rect[a][b].setFill(Constant.realColor.TRANSPARENT);
rect[a][b].setFill(tileOnRect);
} }
arg0.consume(); arg0.consume();
} }
@ -509,6 +507,7 @@ public class LaticeApplicationWindow extends Application {
rect[a][b].setOnDragOver(new EventHandler <DragEvent>() { rect[a][b].setOnDragOver(new EventHandler <DragEvent>() {
@Override @Override
public void handle(DragEvent arg0) { public void handle(DragEvent arg0) {
arg0.acceptTransferModes(TransferMode.ANY); arg0.acceptTransferModes(TransferMode.ANY);
arg0.consume(); arg0.consume();
} }
@ -520,23 +519,23 @@ public class LaticeApplicationWindow extends Application {
System.out.println("entered"); System.out.println("entered");
Dragboard dragboard = arg0.getDragboard(); Dragboard dragboard = arg0.getDragboard();
System.out.println("OK2"); System.out.println("OK2");
rect[a][b].setFill(new ImagePattern(player.getRack().getRackTileImage().get(getIndexTileClicked()))); rect[a][b].setFill(new ImagePattern(player.getRack().getRackTileImage().get(getIndexTileClicked())));
arg0.setDropCompleted(true); arg0.setDropCompleted(true);
System.out.println("OK"); System.out.println("OK");
//assocRectangleTile.put(rect[a][b], listRackTile.get(getIndexTileClicked()));
//System.out.println(assocRectangleTile.toString());
ErrorLabel.setText(""); ErrorLabel.setText("");
if (Constant.START) { if (Constant.START) {
if (rect[a][b] == rect[4][4]) { if (rect[a][b] == rect[4][4]) {
System.out.println("MOON valid placement"); System.out.println("MOON valid placement");
//assocRectangleTile.put(rect[a][b], listRackTile.get(getIndexTileClicked()));
board.setGridBoardTile(player.getRack().getListRackTile().get(getIndexTileClicked()), a, b); board.setGridBoardTile(player.getRack().getListRackTile().get(indexTileClicked), a, b);
tileDropped = true; tileDropped = true;
Constant.START = false; Constant.START = false;
}else { }else {
ErrorLabel.setText("Error ! Please place the first tile on the moon"); ErrorLabel.setText("Error ! Please place the first tile on the moon");
//removing all tiles from gameboard //removing all tiles from gameboard
@ -549,30 +548,37 @@ public class LaticeApplicationWindow extends Application {
}else { }else {
System.out.println("OK3"); System.out.println("OK3");
System.out.println("Règle effectué"); System.out.println("Règle effectué");
player.getRack().getListRackTile().get(getIndexTileClicked()).setPosition(new Position(a,b)); player.getRack().getListRackTile().get(indexTileClicked).setPosition(new Position(a,b));
int nbr = referee.neighborRule(board , player.getRack().getListRackTile().get(getIndexTileClicked()));
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 {
int nbr = referee.neighborRule(board , player.getRack().getListRackTile().get(indexTileClicked));
if (nbr == 0) { if (nbr == 0) {
ErrorLabel.setText("Error! the tile isn't place next to another tile or there is no correspondance with the neighbor tiles !!"); ErrorLabel.setText("Error ! The tile isn't place next to another tile or there is no correspondance with the neighbor tiles !!");
rect[a][b].setFill(Constant.realColor.TRANSPARENT); rect[a][b].setFill(Constant.realColor.TRANSPARENT);
}else { }else {
if (nbr == 2) { if (nbr == 2) {
System.out.println("Vous avez gagné 1 point"); System.out.println("Vous avez gagné 1 point");
playerFX.setAddScore(player, 1); playerFX.setAddScore(player, 1);
}else if (nbr == 3) { }else if (nbr == 3) {
System.out.println("Vous avez gagné 2 points"); System.out.println("Vous avez gagné 2 points");
playerFX.setAddScore(player, 2); playerFX.setAddScore(player, 2);
}else if (nbr == 4) { }else if (nbr == 4) {
System.out.println("Vous avez gagné 4 points"); System.out.println("Vous avez gagné 4 points");
playerFX.setAddScore(player, 4); playerFX.setAddScore(player, 4);
} }
//assocRectangleTile.put(rect[a][b], listRackTile.get(getIndexTileClicked()));
board.setGridBoardTile(player.getRack().getListRackTile().get(getIndexTileClicked()), a, b); board.setGridBoardTile(player.getRack().getListRackTile().get(getIndexTileClicked()), a, b);
tileDropped = true; tileDropped = true;
System.out.println("tuile posé!"); System.out.println("tuile posé!");
}
} }

View File

@ -22,6 +22,14 @@ public class Rules {
} }
} }
public Boolean checkPositionRule(GameBoard board, Tile tile) {
if (board.getGridBoardTile()[tile.getPositionRow()][tile.getPositionColumn()] == null) {
return true;
}else {
return false;
}
}
public Integer neighborRule(GameBoard board, Tile tile) { public Integer neighborRule(GameBoard board, Tile tile) {