diff --git a/MavenChess/MavenChess/src/main/java/controller/Main.java b/MavenChess/MavenChess/src/main/java/controller/Main.java index 0725fe2e02b4988cbafdb1d17fca499228cf5bd2..008b9952c903a684fcda5efa37a47eed3fee5a0f 100644 --- a/MavenChess/MavenChess/src/main/java/controller/Main.java +++ b/MavenChess/MavenChess/src/main/java/controller/Main.java @@ -6,9 +6,23 @@ import javafx.scene.input.MouseEvent; import javafx.scene.input.ScrollEvent; import javafx.stage.Stage; import model.Board; +import model.Position; import view.*; +import java.util.ArrayList; + public class Main extends Application { + + private CustomCamera camera; + private Group group; + private CustomScene scene; + private CustomMaterial material; + private CustomLight light; + private Custom3dModel model; + private Desk desk; + + private Board board; + private BoardView boardView; @Override public void start(Stage stage) { @@ -19,31 +33,31 @@ public class Main extends Application { stage.setTitle("Chess3D - Tuna Acikbas & Olivier Pillods"); // title // init - camera - CustomCamera camera = new CustomCamera(1, 1000, 70); // create a camera and some settings + camera = new CustomCamera(1, 1000, 70); // create a camera and some settings // init - scene, main container - Group group = new Group(); // will contain all the elements "to show" on scene - CustomScene scene = new CustomScene(group, camera.getCamera(), SceneAntialiasing.BALANCED); + group = new Group(); // will contain all the elements "to show" on scene + scene = new CustomScene(group, camera.getCamera(), SceneAntialiasing.BALANCED); // init - materials and shaders - CustomMaterial material = new CustomMaterial(); // prepare all the 3D materials (shaders) + material = new CustomMaterial(); // prepare all the 3D materials (shaders) // init - all lights - CustomLight light = new CustomLight(); // setups different custom lights + light = new CustomLight(); // setups different custom lights light.addAllToScene(group); // adds all the lights to scene // init - import all 3D models from .obj files - Custom3dModel model = new Custom3dModel(); + model = new Custom3dModel(); // init - creates a wooden desk - Desk desk = new Desk(material); + desk = new Desk(material); desk.addAllToScene(group); // adds the desk to scene // init - creates a (model) list of all pieces - Board board = new Board(); + board = new Board(); // init - creates a (visual) chessboard of 8x8, and all (visual) 3d pieces - BoardView boardView = new BoardView(model, material, board); + boardView = new BoardView(model, material, board); boardView.addAllToScene(group); // adds the board and the pieces to scene // event -------------------------------------------------------------- @@ -58,14 +72,14 @@ public class Main extends Application { for(int n = 0 ; n < 64 ; n++) { PieceView piece = boardView.getPiece(n); if(piece != null) { - piece.getObj().addEventHandler(MouseEvent.MOUSE_CLICKED, event -> boardView.clickPiece(material, piece, board)); + piece.getObj().addEventHandler(MouseEvent.MOUSE_CLICKED, event -> clickPiece(piece)); } } // event - click on (visual) "colored tiles" to move the selected piece there for(int n = 0 ; n < 64 ; n++) { TileView tile = boardView.getTile(n); - tile.getObj().addEventHandler(MouseEvent.MOUSE_CLICKED, event -> boardView.clickTile(material, model, tile, board)); + tile.getObj().addEventHandler(MouseEvent.MOUSE_CLICKED, event -> clickTile(tile)); } // start -------------------------------------------------------------- @@ -78,4 +92,102 @@ public class Main extends Application { public static void main(String[] args) { launch(); } + + public void clickPiece(PieceView clickedPiece) { + // clicking a piece will ask her available moves + Position position = clickedPiece.getPosition(); + board.setSelected(position); + boardView.resetColor(material); + boardView.setColor(material, position, CustomMaterial.SELECTED_TILE); + ArrayList<Position> moves = board.getPiece(position).getAvailableMoves(board); + + // shows all available moves on the tiles, with colors, each tile-color having a meaning + for (Position move : moves) { + if(board.isFree(move)) { + boardView.setColor(material, move, CustomMaterial.MOVABLE_TILE); + } else if(board.isEnemy(move, position)) { + boardView.setColor(material, move, CustomMaterial.ATTACK_TILE); + } else { + boardView.setColor(material, move, CustomMaterial.SPECIAL_TILE); // any special move + } + // PROMOTION (attacking or not) + if(board.isPawn(position) && (move.getY() == 8 || move.getY() == 1)) { + boardView.setColor(material, move, CustomMaterial.SPECIAL_TILE); + } + } + } + + public void clickTile(TileView clickedTile) { + int state = clickedTile.getState(); + Position selected = board.getSelected(); + Position arrival = clickedTile.getPosition(); + if(state != TileView.NORMAL && state != TileView.SELECTED) { + // --- ALL SPECIAL MOVES --- + if(state == TileView.SPECIAL) { + if(board.isPawn(selected)) { + // --- PROMOTION --- + if(arrival.getY() == 8 || arrival.getY() == 1) { + // --- ATTACK (while doing promotion) --- + if (!board.isFree(arrival)) { + boardView.killPiece(arrival); // place piece on graveyard (visual) + } + // Promotion to Queen (by default, Queen for now) + board.mutationOfSelectedPiece(Board.QUEEN); // (on model) + boardView.mutationPiece(model, selected, Custom3dModel.QUEEN); // (on view) + // --- MOVE (while doing promotion) --- + board.movePiece(selected, arrival); // move piece (on model) + boardView.movePiece(selected, arrival); // move piece (on 3d view) + } + // --- PRISE EN PASSANT --- + else { + // PRE : if it's not a promotion, then it's "prise en passant", because of pawn rules + // we don't need to test y-position + } + } + // --- CASTLING (from king) --- + if(board.isKing(selected)) { + // PRE (1) : the arrival piece CAN ONLY BE ROOK, because of king rules + // PRE (2) : we don't need to test if rook !hasBeenMoved(), because of king rules + // PRE (3) : we don't need to test if king !hasBeenMoved(), because of king rules + int x = Board.D; // king x arrival, for west-rook + if(arrival.getX() == Board.H) { + x = Board.F; // king x arrival, for east-rook + } + Position kingArrival = new Position(x, selected.getY()); + board.movePiece(selected, kingArrival); // move king (on model) + boardView.movePiece(selected, kingArrival); // move king (on 3d view) + board.movePiece(arrival, selected); // move rook (on model) + boardView.movePiece(arrival, selected); // move rook (on 3d view) + } + // --- CASTLING (from rook) --- + else if(board.isRook(selected)) { + // PRE (1) : the arrival piece CAN ONLY BE KING, because of rook rules + // PRE (2) : we don't need to test if king !hasBeenMoved(), because of rook rules + // PRE (3) : we don't need to test if rook !hasBeenMoved(), because of rook rules + int x = Board.D; // king x arrival, for west-rook + if (selected.getX() == Board.H) { + x = Board.F; // king x arrival, for east-rook + } + Position kingArrival = new Position(x, arrival.getY()); + board.movePiece(arrival, kingArrival); // move king (on model) + boardView.movePiece(arrival, kingArrival); // move king (on 3d view) + board.movePiece(selected, arrival); // move rook (on model) + boardView.movePiece(selected, arrival); // move rook (on 3d view) + } + } + // --- STANDARD MOVES --- + else { + // --- ATTACK --- + if(state == TileView.ATTACK) { + boardView.killPiece(arrival); // place piece on graveyard (visual) + } + // --- MOVE --- + board.movePiece(selected, arrival); // move piece (on model) + boardView.movePiece(selected, arrival); // move piece (on 3d view) + } + + } + // --- Resets board coloring, doing any action or not --- + boardView.resetColor(material); + } } \ No newline at end of file diff --git a/MavenChess/MavenChess/src/main/java/view/BoardView.java b/MavenChess/MavenChess/src/main/java/view/BoardView.java index 2439a13b49818a78ef67acd870a45e988a1ee62c..dc006f208c1d134bc0eef33287dbe04e4f95b4ad 100644 --- a/MavenChess/MavenChess/src/main/java/view/BoardView.java +++ b/MavenChess/MavenChess/src/main/java/view/BoardView.java @@ -4,8 +4,6 @@ import javafx.scene.Group; import javafx.scene.shape.*; import model.*; -import java.util.ArrayList; - public class BoardView { public static final int BOARD_SIZE = 16, CENTERED_ORIGIN = -BOARD_SIZE*7/2, BOARD_HEIGHT = -5, GRAVEYARD_HEIGHT = -1; @@ -123,7 +121,7 @@ public class BoardView { piece.getObj().setTranslateY(GRAVEYARD_HEIGHT); // place at right height (Y axis) on table } - private void resetColor(CustomMaterial material) { + public void resetColor(CustomMaterial material) { for(int n = 0, j = 1 ; j <= 8 ; j++) { for (int i = 1; i <= 8; i++, n++) { // resets board with black and white - bottom left (first case) is black, then alternates @@ -137,7 +135,7 @@ public class BoardView { } } - private void setColor(CustomMaterial material, Position position, int type) { + public void setColor(CustomMaterial material, Position position, int type) { int id = position.getID(); // set the new color-type (visually) getTileObj(id).setMaterial(material.get(type)); @@ -151,105 +149,7 @@ public class BoardView { } } - public void clickPiece(CustomMaterial material, PieceView clickedPiece, Board board) { - // clicking a piece will ask her available moves - Position position = clickedPiece.getPosition(); - board.setSelected(position); - resetColor(material); - setColor(material, position, CustomMaterial.SELECTED_TILE); - ArrayList<Position> moves = board.getPiece(position).getAvailableMoves(board); - - // shows all available moves on the tiles, with colors, each tile-color having a meaning - for (Position move : moves) { - if(board.isFree(move)) { - setColor(material, move, CustomMaterial.MOVABLE_TILE); - } else if(board.isEnemy(move, position)) { - setColor(material, move, CustomMaterial.ATTACK_TILE); - } else { - setColor(material, move, CustomMaterial.SPECIAL_TILE); // any special move - } - // PROMOTION (attacking or not) - if(board.isPawn(position) && (move.getY() == 8 || move.getY() == 1)) { - setColor(material, move, CustomMaterial.SPECIAL_TILE); - } - } - } - - public void clickTile(CustomMaterial material, Custom3dModel model, TileView clickedTile, Board board) { - int state = clickedTile.getState(); - Position selected = board.getSelected(); - Position arrival = clickedTile.getPosition(); - if(state != TileView.NORMAL && state != TileView.SELECTED) { - // --- ALL SPECIAL MOVES --- - if(state == TileView.SPECIAL) { - if(board.isPawn(selected)) { - // --- PROMOTION --- - if(arrival.getY() == 8 || arrival.getY() == 1) { - // --- ATTACK (while doing promotion) --- - if (!board.isFree(arrival)) { - killPiece(arrival); // place piece on graveyard (visual) - } - // Promotion to Queen (by default, Queen for now) - board.mutationOfSelectedPiece(Board.QUEEN); // (on model) - mutationPiece(model, selected, Custom3dModel.QUEEN); // (on view) - // --- MOVE (while doing promotion) --- - board.movePiece(selected, arrival); // move piece (on model) - movePiece(selected, arrival); // move piece (on 3d view) - } - // --- PRISE EN PASSANT --- - else { - // PRE : if it's not a promotion, then it's "prise en passant", because of pawn rules - // we don't need to test y-position - } - } - // --- CASTLING (from king) --- - if(board.isKing(selected)) { - // PRE (1) : the arrival piece CAN ONLY BE ROOK, because of king rules - // PRE (2) : we don't need to test if rook !hasBeenMoved(), because of king rules - // PRE (3) : we don't need to test if king !hasBeenMoved(), because of king rules - int x = Board.D; // king x arrival, for west-rook - if(arrival.getX() == Board.H) { - x = Board.F; // king x arrival, for east-rook - } - Position kingArrival = new Position(x, selected.getY()); - board.movePiece(selected, kingArrival); // move king (on model) - movePiece(selected, kingArrival); // move king (on 3d view) - board.movePiece(arrival, selected); // move rook (on model) - movePiece(arrival, selected); // move rook (on 3d view) - } - // --- CASTLING (from rook) --- - else if(board.isRook(selected)) { - // PRE (1) : the arrival piece CAN ONLY BE KING, because of rook rules - // PRE (2) : we don't need to test if king !hasBeenMoved(), because of rook rules - // PRE (3) : we don't need to test if rook !hasBeenMoved(), because of rook rules - int x = Board.D; // king x arrival, for west-rook - if (selected.getX() == Board.H) { - x = Board.F; // king x arrival, for east-rook - } - Position kingArrival = new Position(x, arrival.getY()); - board.movePiece(arrival, kingArrival); // move king (on model) - movePiece(arrival, kingArrival); // move king (on 3d view) - board.movePiece(selected, arrival); // move rook (on model) - movePiece(selected, arrival); // move rook (on 3d view) - } - } - // --- STANDARD MOVES --- - else { - // --- ATTACK --- - if(state == TileView.ATTACK) { - killPiece(arrival); // place piece on graveyard (visual) - } - // --- MOVE --- - board.movePiece(selected, arrival); // move piece (on model) - movePiece(selected, arrival); // move piece (on 3d view) - } - - } - // --- Resets board coloring, doing any action or not --- - resetColor(material); - } - - private void movePiece(Position origin, Position arrival) { + public void movePiece(Position origin, Position arrival) { PieceView piece = getPiece(origin.getID()); piece.setPosition(arrival); // changes position to arrival setPiece(piece); // copies the piece from origin ID to arrival ID @@ -257,7 +157,7 @@ public class BoardView { updatePiece(arrival); // updates 3d model position (visual) } - private void killPiece(Position p) { // graveyards are specified as negative x + public void killPiece(Position p) { // graveyards are specified as negative x PieceView piece = getPiece(p.getID()); if(piece.getColor() == Piece.WHITE){ white_death_count++; @@ -269,7 +169,7 @@ public class BoardView { updateGraveyardPiece(piece); } - private void mutationPiece(Custom3dModel model, Position selected, int type) { + public void mutationPiece(Custom3dModel model, Position selected, int type) { // for view side, we can't "overwrite" the piece, we need to "edit" it, // otherwise the old 3d model would stay on the scene MeshView obj = getPieceObj(selected.getID()); diff --git a/MavenChess/MavenChess/target/classes/controller/Main.class b/MavenChess/MavenChess/target/classes/controller/Main.class index 26a5078555bb03c979e9421431ca6b3186969f9e..a58fe7ddbb1a5d7404aac4d5172a66aeafdf4666 100644 Binary files a/MavenChess/MavenChess/target/classes/controller/Main.class and b/MavenChess/MavenChess/target/classes/controller/Main.class differ diff --git a/MavenChess/MavenChess/target/classes/view/BoardView.class b/MavenChess/MavenChess/target/classes/view/BoardView.class index 8bfa2b308389f4a7da41ccdd5dd8a3e5ce86e819..13aa03119faf5d4aef19c4100a993ea3c4332aaf 100644 Binary files a/MavenChess/MavenChess/target/classes/view/BoardView.class and b/MavenChess/MavenChess/target/classes/view/BoardView.class differ