Move all Scanner input to a new class in control package
This commit is contained in:
parent
424d969598
commit
90557d5a67
16
src/battleship/control/TerminalKeyboardListener.java
Normal file
16
src/battleship/control/TerminalKeyboardListener.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
@ -119,4 +119,8 @@ public abstract class AbstractView implements View {
|
|||||||
public Pair<Integer, Integer> chooseMove(Player player) throws InterruptedException {
|
public Pair<Integer, Integer> chooseMove(Player player) throws InterruptedException {
|
||||||
return player.chooseMove();
|
return player.chooseMove();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected abstract Pair<Integer, Integer> mouseInput(Player player) throws InterruptedException;
|
||||||
|
|
||||||
|
protected abstract String keyboardInput() throws InterruptedException;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package battleship.view;
|
package battleship.view;
|
||||||
|
|
||||||
|
import battleship.control.TerminalKeyboardListener;
|
||||||
import battleship.model.Direction;
|
import battleship.model.Direction;
|
||||||
import battleship.model.Game;
|
import battleship.model.Game;
|
||||||
import battleship.model.Ship;
|
import battleship.model.Ship;
|
||||||
@ -11,10 +12,14 @@ import java.util.Scanner;
|
|||||||
|
|
||||||
public class Terminal extends AbstractView {
|
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) {
|
public Terminal(Game game) {
|
||||||
super(game);
|
super(game);
|
||||||
|
if(scanner == null)
|
||||||
|
scanner = new Scanner(System.in);
|
||||||
|
keyboardComponent = new TerminalKeyboardListener(scanner);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -29,25 +34,33 @@ public class Terminal extends AbstractView {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setShips(Player player) throws InterruptedException {
|
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;
|
int x, y;
|
||||||
if(player instanceof Human) {
|
if(player instanceof Human) {
|
||||||
for(int i : shipsSize) {
|
for(int i : shipsSize) {
|
||||||
boolean valid = false;
|
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)) {
|
while (!player.setShips(ship)) {
|
||||||
if (valid) {
|
try {
|
||||||
System.out.println("Erreur");
|
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 {
|
} else {
|
||||||
@ -66,13 +79,17 @@ public class Terminal extends AbstractView {
|
|||||||
if(player instanceof Human) {
|
if(player instanceof Human) {
|
||||||
int x = -1, y = -1;
|
int x = -1, y = -1;
|
||||||
while(!player.areValid(x, y)) {
|
while(!player.areValid(x, y)) {
|
||||||
// y correspond à l'ordonnée mais est stocké comme étant l'abscisse
|
try {
|
||||||
// (erreur de notre part aperçu lors du passage à une fenetre swing)
|
// y correspond à l'ordonnée mais est stocké comme étant l'abscisse
|
||||||
System.out.println("Veuillez indiquer la coordonée x de votre coup");
|
// (erreur de notre part aperçu lors du passage à une fenetre swing)
|
||||||
y = scanner.nextInt();
|
setUpperText("Veuillez indiquer la coordonée x de votre coup");
|
||||||
System.out.println("Veuillez indiquer la coordonée y de votre coup");
|
y = keyboardInputInteger();
|
||||||
x = scanner.nextInt();
|
setUpperText("Veuillez indiquer la coordonée y de votre coup");
|
||||||
|
x = keyboardInputInteger();
|
||||||
|
} catch (NumberFormatException ignored) {
|
||||||
|
x = -1;
|
||||||
|
y = -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return new Pair<>(x,y);
|
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
|
@Override
|
||||||
public void displayWinner(Player winner) {
|
public void displayWinner(Player winner) {
|
||||||
displayBoard();
|
displayBoard();
|
||||||
System.out.println("Le joueur " + winner.getId() + " a gagné");
|
setUpperText("Le joueur " + winner.getId() + " a gagné");
|
||||||
|
scanner.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ public class Window extends AbstractView {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getKeyInput() throws InterruptedException {
|
protected String getKeyInput() throws InterruptedException {
|
||||||
return waitingForKeyboardInput();
|
return keyboardInput();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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");
|
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";
|
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";
|
upperTitle = "joueur " + player.getId() + ", Choisissez la direction de votre navire avec le clavier";
|
||||||
upperSubTitle = "H, B, G, D pour respectivement Haut, Bas, Gauche, Droite";
|
upperSubTitle = "H, B, G, D pour respectivement Haut, Bas, Gauche, Droite";
|
||||||
frame.repaint();
|
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;
|
keyboardComponent.requestInput = true;
|
||||||
while(true) {
|
while(true) {
|
||||||
Thread.sleep(25);
|
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;
|
mouseComponent.requestInput = true;
|
||||||
while(true) {
|
while(true) {
|
||||||
Thread.sleep(25);
|
Thread.sleep(25);
|
||||||
@ -140,7 +142,7 @@ public class Window extends AbstractView {
|
|||||||
if(valid)
|
if(valid)
|
||||||
openDialog("Erreur de placement, ce coup a déjà été effectué");
|
openDialog("Erreur de placement, ce coup a déjà été effectué");
|
||||||
valid = true;
|
valid = true;
|
||||||
coords = waitingForMouseInput(game.getOtherPlayer(player));
|
coords = mouseInput(game.getOtherPlayer(player));
|
||||||
}
|
}
|
||||||
return coords;
|
return coords;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user