add comments + make some variables final + converted String in AbstractPlayer#toString to StringBuilder

This commit is contained in:
Quentin Legot 2021-05-07 12:22:32 +02:00
parent 5bdc772f29
commit ec69ea8d0c
6 changed files with 26 additions and 30 deletions

View File

@ -1,21 +1,14 @@
package battleship.control; package battleship.control;
import battleship.view.Window;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.KeyListener; import java.awt.event.KeyListener;
public class WindowKeyboardListener implements KeyListener { public class WindowKeyboardListener implements KeyListener {
private final Window window;
public boolean requestInput = false; public boolean requestInput = false;
public char keyTyped = KeyEvent.CHAR_UNDEFINED; public char keyTyped = KeyEvent.CHAR_UNDEFINED;
public int keyTypedArrow = KeyEvent.VK_UNDEFINED; public int keyTypedArrow = KeyEvent.VK_UNDEFINED;
public WindowKeyboardListener(Window window) {
this.window = window;
}
@Override @Override
public void keyTyped(KeyEvent e) { public void keyTyped(KeyEvent e) {
if(requestInput) { if(requestInput) {

View File

@ -10,7 +10,7 @@ import battleship.view.View;
*/ */
public class Game { public class Game {
public Player[] players; public final Player[] players;
public Player currentPlayer; public Player currentPlayer;
public Game(Player[] players) { public Game(Player[] players) {

View File

@ -20,7 +20,7 @@ public class Ship {
/** /**
* ship full coordinates calculate thank to base coordinates, direction and size * ship full coordinates calculate thank to base coordinates, direction and size
*/ */
Pair<Integer,Integer>[] fullCoords; final Pair<Integer,Integer>[] fullCoords;
private Direction direction; private Direction direction;
/** /**
* if true the ship is destroyed * if true the ship is destroyed

View File

@ -16,8 +16,12 @@ import java.util.ArrayList;
*/ */
public abstract class AbstractPlayer implements Player { public abstract class AbstractPlayer implements Player {
ArrayList<Ship> ships = new ArrayList<>(); final ArrayList<Ship> ships = new ArrayList<>();
ArrayList<Triplet<Integer,Integer,Boolean>> moves = 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<Triplet<Integer,Integer,Boolean>> moves = new ArrayList<>();
public int id; public int id;
public boolean setShips(Ship ship) { 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... * add {@code move} to the {@link AbstractPlayer#moves} list
* @param move
*/ */
public void addMove(Triplet<Integer,Integer,Boolean> move){ public void addMove(Triplet<Integer,Integer,Boolean> move){
moves.add(move); moves.add(move);

View File

@ -17,7 +17,7 @@ import java.util.ArrayList;
*/ */
public abstract class AbstractView implements View { public abstract class AbstractView implements View {
protected Game game; protected final Game game;
public AbstractView(Game game) { public AbstractView(Game game) {
this.game = game; this.game = game;
@ -33,14 +33,14 @@ public abstract class AbstractView implements View {
} }
public String toString(boolean debug) { public String toString(boolean debug) {
String chain = ""; StringBuilder chain = new StringBuilder();
for(int u = 0; u < 2; ++u) { for(int u = 0; u < 2; ++u) {
Player player = game.players[u]; Player player = game.players[u];
ArrayList<Ship> ships = game.players[u].getShips(); ArrayList<Ship> ships = game.players[u].getShips();
chain += "Joueur " + player.getId() + " :\n"; chain.append("Joueur ").append(player.getId()).append(" :\n");
chain += "+ 0 1 2 3 4 5 6 7 8 9 +\n"; chain.append("+ 0 1 2 3 4 5 6 7 8 9 +\n");
for(int x = 0; x < 10; ++x) { for(int x = 0; x < 10; ++x) {
chain += x; chain.append(x);
for(int y = 0; y < 10; ++y) { for(int y = 0; y < 10; ++y) {
Pair<Integer, Integer> pair = new Pair<>(x, y); Pair<Integer, Integer> pair = new Pair<>(x, y);
boolean isPosition = false; boolean isPosition = false;
@ -49,32 +49,32 @@ public abstract class AbstractView implements View {
isPosition = true; isPosition = true;
int result = isPositionDrowned(game.players[u == 0 ? 1 : 0], ship, pair); int result = isPositionDrowned(game.players[u == 0 ? 1 : 0], ship, pair);
if(result == 1) { if(result == 1) {
chain += " X"; chain.append(" X");
} else if (result == 2){ } else if (result == 2){
chain += " !"; chain.append(" !");
} else if(debug || game.getCurrentPlayer() == player) { } else if(debug || game.getCurrentPlayer() == player) {
chain += " ."; chain.append(" .");
} else { } else {
chain += " _"; chain.append(" _");
} }
break; break;
} }
} }
if(!isPosition) { if(!isPosition) {
if(isPositionDrowned(game.players[u == 0 ? 1 : 0], pair) == 2) { if(isPositionDrowned(game.players[u == 0 ? 1 : 0], pair) == 2) {
chain += " ?"; chain.append(" ?");
} else { } 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 * ask player for keyboard input and parse it into one of {@link Direction} value
* @return Direction depending of player input * @return Direction depending of player input
* @throws InterruptedException see {@link Window#getDirectionFromChar()} * @throws InterruptedException caused by {@link Window#getKeyInput()}
* @see Window#getDirectionFromChar() * @see Window#getDirectionFromChar()
*/ */
protected Direction getDirectionFromChar() throws InterruptedException { 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 ship check if this ship at this position has been hit
* @param pair coords * @param pair coords
* @return 1 if ship fully drowned, 2 if only damaged, 0 if not * @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<Integer, Integer> pair) { private int isPositionDrowned(Player other, Ship ship, Pair<Integer, Integer> pair) {
if(ship.isDrown()) if(ship.isDrown())

View File

@ -47,7 +47,7 @@ public class Window extends AbstractView {
frame.setVisible(true); frame.setVisible(true);
this.mouseComponent = new WindowMouseListener(this); this.mouseComponent = new WindowMouseListener(this);
frame.addMouseListener(mouseComponent); frame.addMouseListener(mouseComponent);
this.keyboardComponent = new WindowKeyboardListener(this); this.keyboardComponent = new WindowKeyboardListener();
frame.addKeyListener(keyboardComponent); frame.addKeyListener(keyboardComponent);
} }