Skip to content

Commit

Permalink
Bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
alessionossa committed Apr 30, 2023
1 parent e288ed2 commit 6fd05c9
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 23 deletions.
49 changes: 48 additions & 1 deletion Battleship/GUI/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,33 @@
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>17.0.1</version>
<!-- <scope>compile</scope>-->
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>17.0.1</version>
<!-- <scope>compile</scope>-->
</dependency>


<!-- <dependency>-->
<!-- <groupId>org.openjfx</groupId>-->
<!-- <artifactId>javafx-graphics</artifactId>-->
<!-- <version>17.0.1</version>-->
<!-- <classifier>win</classifier>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.openjfx</groupId>-->
<!-- <artifactId>javafx-graphics</artifactId>-->
<!-- <version>17.0.1</version>-->
<!-- <classifier>linux</classifier>-->
<!-- </dependency>-->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>17.0.1</version>
<!-- <classifier>mac</classifier>-->
</dependency>

<dependency>
Expand Down Expand Up @@ -76,9 +98,34 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<release>17</release>
<release>17</release>
</configuration>
</plugin>

<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-shade-plugin</artifactId>-->
<!-- <version>3.2.4</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <phase>package</phase>-->
<!-- <goals>-->
<!-- <goal>shade</goal>-->
<!-- </goals>-->
<!-- <configuration>-->
<!-- <shadedArtifactAttached>true</shadedArtifactAttached>-->
<!-- <shadedClassifierName>with-dependencies</shadedClassifierName>-->
<!-- <transformers>-->
<!-- <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">-->
<!-- <mainClass>com.galactica.gui.controller.JarMain</mainClass>-->
<!-- </transformer>-->
<!-- <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>-->
<!-- </transformers>-->
<!-- </configuration>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->

</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ public void initialize() {

continueButton.setDisable(true);

Platform.runLater(this::displayShips);
Platform.runLater(this.currentPlayerGridContainer::updateShots);
Platform.runLater(this.opponentGridContainer::updateShots);
Platform.runLater(() -> {
this.displayShips();
this.currentPlayerGridContainer.updateShots();
this.opponentGridContainer.updateShots();
});
}

private void displayShips() {
System.out.println("Player turn " + gameModel.getPlayerTurn());
for (Ship ship: gameModel.getCurrentPlayer().getShips()) {
currentPlayerGridContainer.showShipImageView(ship);
}
Expand Down Expand Up @@ -149,6 +150,8 @@ private boolean checkWin() {
}

private void updateWeapons() {
weaponListView.setDisable(!canShoot);

weaponListView.getSelectionModel().clearSelection();

List<Weapon> weapons = gameModel.getCurrentPlayer().getWeapons().stream()
Expand All @@ -159,7 +162,7 @@ private void updateWeapons() {
}

private boolean isValidCoordinateForShooting(Weapon weapon, int columnIndex, int rowIndex) {
if (!canShoot) {
if (!canShoot || selectedWeapon == null) {
return false;
} else if (selectedWeapon instanceof Laser &&
(rowIndex == 0 || columnIndex == 0) &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.galactica.gui.controller;

public class JarMain {

public static void main(String[] args) {
Main.main(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import javafx.fxml.FXML;

public class WinController {
@FXML
private ImageView imageview;

@FXML
private Label messageLabel;
Expand All @@ -21,8 +19,6 @@ public class WinController {
}

public void initialize() {
Image i = new Image(new File("/assets/Cat.gif").toURI().toString());
imageview.setImage(i);

this.messageLabel.setText(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class GridContainer extends AnchorPane {

private final HashMap<Tile, ImageView> holesImages = new HashMap<>();

private final HashMap<Ship, ImageView> shipImages = new HashMap<>();
private final HashMap<Integer, ImageView> shipImages = new HashMap<>();

public GridContainer() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getClassLoader().getResource("grid_container.fxml"));
Expand Down Expand Up @@ -150,8 +150,13 @@ public void updateShots() {
setHoleImageViewForTile(tile, tileView);
} else if (tile.getShip() != null){
if (tile.getShip().isSunk()) {
ImageView shipImageView = this.shipImages.get(tile.getShip());
shipImageView.setVisible(true);
ImageView shipImageView = this.shipImages.get(tile.getShip().getIdentifier());
if (shipImageView == null) {
System.out.println("Missing image for " + tile.getShip() + " at " + columnIndex + ", "+ rowIndex);
} else {
System.out.println("Image present for " + tile.getShip() + " at " + columnIndex + ", "+ rowIndex);
shipImageView.setVisible(true);
}
// shipImageView.setOpacity(1.0);
// shipImageView.toFront();
}
Expand All @@ -170,6 +175,7 @@ private void setHoleImageViewForTile(Tile tile, StackPane tileView) {
ImageView holeImageView = holesImages.get(tile);

if (holeImageView == null) {
System.out.println("Creating a new hole image");
Image holeImage = new Image(getClass().getResource("/assets/hole.png").toExternalForm());

holeImageView = new ImageView(holeImage);
Expand All @@ -196,7 +202,7 @@ public ImageView showShipImageView(Ship ship) {

if (shipImageView == null) {
shipImageView = getShipImageView(ship);
this.shipImages.put(ship, shipImageView);
this.shipImages.put(ship.getIdentifier(), shipImageView);

Coordinate shipOriginCoordinate = ship.getCoordinate();
int xIndex = grid.convertXToMatrixIndex(shipOriginCoordinate.getX()) + 1;
Expand Down
20 changes: 11 additions & 9 deletions Battleship/GUI/src/main/resources/win-view.fxml
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1">
<padding>
<Insets top="50.0" />
</padding>
<top>
<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1">
<VBox alignment="CENTER">
<Label fx:id="messageLabel" text="YOU HAVE LASSO'D THE GALAXY AND SAVED THE CAT" textAlignment="CENTER" BorderPane.alignment="CENTER">
<font>
<Font size="36.0" />
<Font size="30.0" />
</font>
</Label>
</top>
</BorderPane>

<ImageView preserveRatio="true">
<image>
<Image url="@assets/Cat.gif" />
</image></ImageView>
</VBox>
</StackPane>

0 comments on commit 6fd05c9

Please sign in to comment.