From ec69ea8d0cabd92e015c6fa4613aa515269b0a55 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Fri, 7 May 2021 12:22:32 +0200 Subject: [PATCH] add comments + make some variables final + converted String in AbstractPlayer#toString to StringBuilder --- .../control/WindowKeyboardListener.java | 7 ---- src/battleship/model/Game.java | 2 +- src/battleship/model/Ship.java | 2 +- .../model/player/AbstractPlayer.java | 11 ++++--- src/battleship/view/AbstractView.java | 32 +++++++++---------- src/battleship/view/Window.java | 2 +- 6 files changed, 26 insertions(+), 30 deletions(-) diff --git a/src/battleship/control/WindowKeyboardListener.java b/src/battleship/control/WindowKeyboardListener.java index d5134eb..04555a9 100644 --- a/src/battleship/control/WindowKeyboardListener.java +++ b/src/battleship/control/WindowKeyboardListener.java @@ -1,21 +1,14 @@ package battleship.control; -import battleship.view.Window; - import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class WindowKeyboardListener implements KeyListener { - private final Window window; public boolean requestInput = false; public char keyTyped = KeyEvent.CHAR_UNDEFINED; public int keyTypedArrow = KeyEvent.VK_UNDEFINED; - public WindowKeyboardListener(Window window) { - this.window = window; - } - @Override public void keyTyped(KeyEvent e) { if(requestInput) { diff --git a/src/battleship/model/Game.java b/src/battleship/model/Game.java index 17091cb..a0d52a6 100644 --- a/src/battleship/model/Game.java +++ b/src/battleship/model/Game.java @@ -10,7 +10,7 @@ import battleship.view.View; */ public class Game { - public Player[] players; + public final Player[] players; public Player currentPlayer; public Game(Player[] players) { diff --git a/src/battleship/model/Ship.java b/src/battleship/model/Ship.java index 92e196b..00fcf76 100644 --- a/src/battleship/model/Ship.java +++ b/src/battleship/model/Ship.java @@ -20,7 +20,7 @@ public class Ship { /** * ship full coordinates calculate thank to base coordinates, direction and size */ - Pair[] fullCoords; + final Pair[] fullCoords; private Direction direction; /** * if true the ship is destroyed diff --git a/src/battleship/model/player/AbstractPlayer.java b/src/battleship/model/player/AbstractPlayer.java index f3a1ad6..11ec9c0 100644 --- a/src/battleship/model/player/AbstractPlayer.java +++ b/src/battleship/model/player/AbstractPlayer.java @@ -16,8 +16,12 @@ import java.util.ArrayList; */ public abstract class AbstractPlayer implements Player { - ArrayList ships = new ArrayList<>(); - ArrayList> moves = new ArrayList<>(); + final ArrayList ships = new ArrayList<>(); + /** + * reference every shot on the opponent board, left and middle side of the Triplet reference the coordinates and the + * right side if this move hit or not an opponent ship + */ + final ArrayList> moves = new ArrayList<>(); public int id; public boolean setShips(Ship ship) { @@ -38,8 +42,7 @@ public abstract class AbstractPlayer implements Player { } /** - * La methode retourne son objet afin d'avoir la possibilité de faire Player.addMove().addMove().etc... - * @param move + * add {@code move} to the {@link AbstractPlayer#moves} list */ public void addMove(Triplet move){ moves.add(move); diff --git a/src/battleship/view/AbstractView.java b/src/battleship/view/AbstractView.java index 8923055..bcf2a26 100644 --- a/src/battleship/view/AbstractView.java +++ b/src/battleship/view/AbstractView.java @@ -17,7 +17,7 @@ import java.util.ArrayList; */ public abstract class AbstractView implements View { - protected Game game; + protected final Game game; public AbstractView(Game game) { this.game = game; @@ -33,14 +33,14 @@ public abstract class AbstractView implements View { } public String toString(boolean debug) { - String chain = ""; + StringBuilder chain = new StringBuilder(); for(int u = 0; u < 2; ++u) { Player player = game.players[u]; ArrayList ships = game.players[u].getShips(); - chain += "Joueur " + player.getId() + " :\n"; - chain += "+ 0 1 2 3 4 5 6 7 8 9 +\n"; + chain.append("Joueur ").append(player.getId()).append(" :\n"); + chain.append("+ 0 1 2 3 4 5 6 7 8 9 +\n"); for(int x = 0; x < 10; ++x) { - chain += x; + chain.append(x); for(int y = 0; y < 10; ++y) { Pair pair = new Pair<>(x, y); boolean isPosition = false; @@ -49,32 +49,32 @@ public abstract class AbstractView implements View { isPosition = true; int result = isPositionDrowned(game.players[u == 0 ? 1 : 0], ship, pair); if(result == 1) { - chain += " X"; + chain.append(" X"); } else if (result == 2){ - chain += " !"; + chain.append(" !"); } else if(debug || game.getCurrentPlayer() == player) { - chain += " ."; + chain.append(" ."); } else { - chain += " _"; + chain.append(" _"); } break; } } if(!isPosition) { if(isPositionDrowned(game.players[u == 0 ? 1 : 0], pair) == 2) { - chain += " ?"; + chain.append(" ?"); } else { - chain += " _"; + chain.append(" _"); } } } - chain += " |\n"; + chain.append(" |\n"); } - chain += "+ - - - - - - - - - - +\n"; + chain.append("+ - - - - - - - - - - +\n"); } - return chain; + return chain.toString(); } /** @@ -92,7 +92,7 @@ public abstract class AbstractView implements View { /** * ask player for keyboard input and parse it into one of {@link Direction} value * @return Direction depending of player input - * @throws InterruptedException see {@link Window#getDirectionFromChar()} + * @throws InterruptedException caused by {@link Window#getKeyInput()} * @see Window#getDirectionFromChar() */ protected Direction getDirectionFromChar() throws InterruptedException { @@ -138,7 +138,7 @@ public abstract class AbstractView implements View { * @param ship check if this ship at this position has been hit * @param pair coords * @return 1 if ship fully drowned, 2 if only damaged, 0 if not - * @see + * @see AbstractView#isPositionDrowned(Player, Pair) */ private int isPositionDrowned(Player other, Ship ship, Pair pair) { if(ship.isDrown()) diff --git a/src/battleship/view/Window.java b/src/battleship/view/Window.java index b3b9b75..604544d 100644 --- a/src/battleship/view/Window.java +++ b/src/battleship/view/Window.java @@ -47,7 +47,7 @@ public class Window extends AbstractView { frame.setVisible(true); this.mouseComponent = new WindowMouseListener(this); frame.addMouseListener(mouseComponent); - this.keyboardComponent = new WindowKeyboardListener(this); + this.keyboardComponent = new WindowKeyboardListener(); frame.addKeyListener(keyboardComponent); }