Move all Scanner input to a new class in control package

This commit is contained in:
Quentin Legot 2021-04-30 14:18:04 +02:00
parent 424d969598
commit 90557d5a67
4 changed files with 81 additions and 27 deletions

View File

@ -0,0 +1,16 @@
package battleship.control;
import java.util.Scanner;
public class TerminalKeyboardListener {
private final Scanner scanner;
public TerminalKeyboardListener(Scanner scanner) {
this.scanner = scanner;
}
public String keyboardInput() {
return scanner.next();
}
}

View File

@ -119,4 +119,8 @@ public abstract class AbstractView implements View {
public Pair<Integer, Integer> chooseMove(Player player) throws InterruptedException {
return player.chooseMove();
}
protected abstract Pair<Integer, Integer> mouseInput(Player player) throws InterruptedException;
protected abstract String keyboardInput() throws InterruptedException;
}

View File

@ -1,5 +1,6 @@
package battleship.view;
import battleship.control.TerminalKeyboardListener;
import battleship.model.Direction;
import battleship.model.Game;
import battleship.model.Ship;
@ -11,10 +12,14 @@ import java.util.Scanner;
public class Terminal extends AbstractView {
public static Scanner scanner = new Scanner(System.in);
public static Scanner scanner = null;
private final TerminalKeyboardListener keyboardComponent;
public Terminal(Game game) {
super(game);
if(scanner == null)
scanner = new Scanner(System.in);
keyboardComponent = new TerminalKeyboardListener(scanner);
}
@Override
@ -29,25 +34,33 @@ public class Terminal extends AbstractView {
@Override
public void setShips(Player player) throws InterruptedException {
System.out.println("Joueur " + player.getId() + ", placez vos navires");
setUpperText("Joueur " + player.getId() + ", placez vos navires");
int x, y;
if(player instanceof Human) {
for(int i : shipsSize) {
boolean valid = false;
Ship ship = new Ship(new Pair<>(-1, -1), i, Direction.DEFAULT);
Pair<Integer, Integer> defaultPos = new Pair<>(-1, -1);
Ship ship = new Ship(defaultPos, i, Direction.DEFAULT);
while (!player.setShips(ship)) {
if (valid) {
System.out.println("Erreur");
try {
if (valid) {
setUpperText("Erreur");
}
valid = true;
setUpperText("Placement du bateau de longueur " + ship.getSize());
setUpperText("Veuillez indiquer la coordonée x de votre bateau");
x = keyboardInputInteger();
setUpperText("Veuillez indiquer la coordonée y de votre bateau");
y = keyboardInputInteger();
ship.setCoords(new Pair<>(x, y));
ship.setDirection(getDirectionFromChar());
ship.recalculateFullCoords();
} catch(NumberFormatException e) {
// Pour être sur qu'il ne passera pas la boucle
ship.setCoords(defaultPos);
ship.setDirection(Direction.DEFAULT);
}
valid = true;
System.out.println("Placement du bateau de longueur " + ship.getSize());
System.out.println("Veuillez indiquer la coordonée x de votre bateau");
x = scanner.nextInt();
System.out.println("Veuillez indiquer la coordonée y de votre bateau");
y = scanner.nextInt();
ship.setCoords(new Pair<>(x, y));
ship.setDirection(getDirectionFromChar());
ship.recalculateFullCoords();
}
}
} else {
@ -66,13 +79,17 @@ public class Terminal extends AbstractView {
if(player instanceof Human) {
int x = -1, y = -1;
while(!player.areValid(x, y)) {
// y correspond à l'ordonnée mais est stocké comme étant l'abscisse
// (erreur de notre part aperçu lors du passage à une fenetre swing)
System.out.println("Veuillez indiquer la coordonée x de votre coup");
y = scanner.nextInt();
System.out.println("Veuillez indiquer la coordonée y de votre coup");
x = scanner.nextInt();
try {
// y correspond à l'ordonnée mais est stocké comme étant l'abscisse
// (erreur de notre part aperçu lors du passage à une fenetre swing)
setUpperText("Veuillez indiquer la coordonée x de votre coup");
y = keyboardInputInteger();
setUpperText("Veuillez indiquer la coordonée y de votre coup");
x = keyboardInputInteger();
} catch (NumberFormatException ignored) {
x = -1;
y = -1;
}
}
return new Pair<>(x,y);
}
@ -80,10 +97,25 @@ public class Terminal extends AbstractView {
}
@Override
protected Pair<Integer, Integer> mouseInput(Player player) {
return null;
}
@Override
protected String keyboardInput() {
return keyboardComponent.keyboardInput();
}
protected int keyboardInputInteger() throws NumberFormatException {
return Integer.parseInt(keyboardComponent.keyboardInput());
}
@Override
public void displayWinner(Player winner) {
displayBoard();
System.out.println("Le joueur " + winner.getId() + " a gagné");
setUpperText("Le joueur " + winner.getId() + " a gagné");
scanner.close();
}
}

View File

@ -43,7 +43,7 @@ public class Window extends AbstractView {
@Override
protected String getKeyInput() throws InterruptedException {
return waitingForKeyboardInput();
return keyboardInput();
}
@Override
@ -63,7 +63,7 @@ public class Window extends AbstractView {
openDialog("Erreur de placement, votre navire se superpose avec un autre, ou la direction donnée n'est pas valide");
upperTitle = "joueur " + player.getId() + ", Placez votre premier navire de taille " + i + " à l'aide de la souris";
ship.setCoords(waitingForMouseInput(player));
ship.setCoords(mouseInput(player));
upperTitle = "joueur " + player.getId() + ", Choisissez la direction de votre navire avec le clavier";
upperSubTitle = "H, B, G, D pour respectivement Haut, Bas, Gauche, Droite";
frame.repaint();
@ -80,7 +80,8 @@ public class Window extends AbstractView {
}
}
private String waitingForKeyboardInput() throws InterruptedException {
@Override
protected String keyboardInput() throws InterruptedException {
keyboardComponent.requestInput = true;
while(true) {
Thread.sleep(25);
@ -93,7 +94,8 @@ public class Window extends AbstractView {
}
}
private Pair<Integer, Integer> waitingForMouseInput(Player player) throws InterruptedException {
@Override
protected Pair<Integer, Integer> mouseInput(Player player) throws InterruptedException {
mouseComponent.requestInput = true;
while(true) {
Thread.sleep(25);
@ -140,7 +142,7 @@ public class Window extends AbstractView {
if(valid)
openDialog("Erreur de placement, ce coup a déjà été effectué");
valid = true;
coords = waitingForMouseInput(game.getOtherPlayer(player));
coords = mouseInput(game.getOtherPlayer(player));
}
return coords;
}