add comments + make some variables final + converted String in AbstractPlayer#toString to StringBuilder
This commit is contained in:
parent
5bdc772f29
commit
ec69ea8d0c
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -20,7 +20,7 @@ public class Ship {
|
||||
/**
|
||||
* ship full coordinates calculate thank to base coordinates, direction and size
|
||||
*/
|
||||
Pair<Integer,Integer>[] fullCoords;
|
||||
final Pair<Integer,Integer>[] fullCoords;
|
||||
private Direction direction;
|
||||
/**
|
||||
* if true the ship is destroyed
|
||||
|
@ -16,8 +16,12 @@ import java.util.ArrayList;
|
||||
*/
|
||||
public abstract class AbstractPlayer implements Player {
|
||||
|
||||
ArrayList<Ship> ships = new ArrayList<>();
|
||||
ArrayList<Triplet<Integer,Integer,Boolean>> moves = new ArrayList<>();
|
||||
final ArrayList<Ship> 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<Triplet<Integer,Integer,Boolean>> 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<Integer,Integer,Boolean> move){
|
||||
moves.add(move);
|
||||
|
@ -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<Ship> 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<Integer, Integer> 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<Integer, Integer> pair) {
|
||||
if(ship.isDrown())
|
||||
|
@ -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);
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user