From 725a7b25d9d5d67d86761e23272525e91b3b34b7 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Tue, 27 Apr 2021 16:33:21 +0200 Subject: [PATCH] Implemented keyboard --- .../control/WindowKeyboardListener.java | 35 +++++++++ src/battleship/model/Ship.java | 2 - src/battleship/view/AbstractView.java | 18 +++++ src/battleship/view/Terminal.java | 25 +++---- src/battleship/view/Window.java | 75 ++++++++++++++++--- 5 files changed, 128 insertions(+), 27 deletions(-) create mode 100644 src/battleship/control/WindowKeyboardListener.java diff --git a/src/battleship/control/WindowKeyboardListener.java b/src/battleship/control/WindowKeyboardListener.java new file mode 100644 index 0000000..20c00b3 --- /dev/null +++ b/src/battleship/control/WindowKeyboardListener.java @@ -0,0 +1,35 @@ +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 WindowKeyboardListener(Window window) { + this.window = window; + } + + @Override + public void keyTyped(KeyEvent e) { + if(requestInput) { + if(e.getKeyChar() != KeyEvent.CHAR_UNDEFINED) + keyTyped = e.getKeyChar(); + } + } + + @Override + public void keyPressed(KeyEvent e) { + + } + + @Override + public void keyReleased(KeyEvent e) { + + } +} diff --git a/src/battleship/model/Ship.java b/src/battleship/model/Ship.java index 6ff2408..9a77311 100644 --- a/src/battleship/model/Ship.java +++ b/src/battleship/model/Ship.java @@ -4,8 +4,6 @@ import battleship.model.player.Player; import battleship.utils.Pair; import battleship.utils.Triplet; -import java.util.Arrays; - public class Ship { private Pair coords; diff --git a/src/battleship/view/AbstractView.java b/src/battleship/view/AbstractView.java index 05e412a..5cc1a4d 100644 --- a/src/battleship/view/AbstractView.java +++ b/src/battleship/view/AbstractView.java @@ -1,5 +1,6 @@ package battleship.view; +import battleship.model.Direction; import battleship.model.Game; import battleship.model.Ship; import battleship.model.player.Player; @@ -70,6 +71,23 @@ public abstract class AbstractView implements View { return false; } + protected Direction getDirectionFromChar() throws InterruptedException { + String dir; + while (true) { + setUpperText("Veuillez indiquer la direction de placement de votre bateau (d droite, h haut, b bas, g gauche)"); + dir = getKeyInput(); + for (Direction direction : Direction.values()) { + if (direction.getKeyword() != null && direction.getKeyword().equals(dir)) { + return direction; + } + } + } + } + + protected abstract String getKeyInput() throws InterruptedException; + + protected abstract void setUpperText(String s); + @Override public void setShips(Player player) throws InterruptedException { player.placeShips(); diff --git a/src/battleship/view/Terminal.java b/src/battleship/view/Terminal.java index 2bd2016..8629ab3 100644 --- a/src/battleship/view/Terminal.java +++ b/src/battleship/view/Terminal.java @@ -17,11 +17,20 @@ public class Terminal extends AbstractView { super(game); } + @Override + protected String getKeyInput() { + return scanner.next().toUpperCase(); + } + + @Override + protected void setUpperText(String s) { + System.out.println(s); + } + @Override public void setShips(Player player) throws InterruptedException { System.out.println("Joueur " + player.getId() + ", placez vos navires"); int x, y; - String dir; if(player instanceof Human) { for(int i : shipsSize) { boolean valid = false; @@ -37,19 +46,7 @@ public class Terminal extends AbstractView { System.out.println("Veuillez indiquer la coordonée y de votre bateau"); y = scanner.nextInt(); ship.setCoords(new Pair<>(x, y)); - boolean validDirection = false; - while (!validDirection) { - System.out.println("Veuillez indiquer la direction de placement de votre bateau (d droite, h haut, b bas, g gauche)"); - dir = scanner.next().toUpperCase(); - for (Direction direction : Direction.values()) { - if (direction.getKeyword() != null && direction.getKeyword().equals(dir)) { - ship.setDirection(direction); - System.out.println(direction); - validDirection = true; - break; - } - } - } + ship.setDirection(getDirectionFromChar()); ship.recalculateFullCoords(); } } diff --git a/src/battleship/view/Window.java b/src/battleship/view/Window.java index 9d5c14a..6a8cad7 100644 --- a/src/battleship/view/Window.java +++ b/src/battleship/view/Window.java @@ -1,6 +1,8 @@ package battleship.view; +import battleship.control.WindowKeyboardListener; import battleship.control.WindowMouseListener; +import battleship.model.Direction; import battleship.model.Game; import battleship.model.Ship; import battleship.model.player.Human; @@ -9,6 +11,7 @@ import battleship.utils.Pair; import javax.swing.*; import java.awt.*; +import java.awt.event.KeyEvent; public class Window extends AbstractView { @@ -17,7 +20,9 @@ public class Window extends AbstractView { public final int height = 600; public final int width = 1200; private final WindowMouseListener mouseComponent; - String upperText = ""; + private final WindowKeyboardListener keyboardComponent; + String upperTitle = ""; + String upperSubTitle = ""; public Window(Game game) { super(game); @@ -29,34 +34,77 @@ public class Window extends AbstractView { frame.setVisible(true); this.mouseComponent = new WindowMouseListener(this); frame.addMouseListener(mouseComponent); + this.keyboardComponent = new WindowKeyboardListener(this); + frame.addKeyListener(keyboardComponent); } + @Override + protected String getKeyInput() throws InterruptedException { + return waitingForKeyboardInput(); + } + + @Override + protected void setUpperText(String s) { + upperTitle = s; + } + @Override public void setShips(Player player) throws InterruptedException { if(player instanceof Human) { for(int i : shipsSize) { - upperText = "joueur " + player.getId() + ", Placez votre premier navire de taille " + i + " à l'aide de la souris"; - Pair coords = waitingForMouseInput(player); - upperText = "joueur " + player.getId() + ", Choisissez la direction de votre navire avec le clavier\n" + - "H, B, G, D pour respectivement Haut, Bas, Gauche, Droite"; - // TODO: 27/04/2021 implementer clavier - } + Ship ship = new Ship(new Pair<>(-1, -1), i, Direction.DEFAULT); + boolean valid = false; + while(!player.setShips(ship)) { + frame.repaint(); + if(valid) + 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)); + 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(); + ship.setDirection(getDirectionFromChar()); + ship.recalculateFullCoords(); + valid = true; + } + + } + upperTitle = ""; + upperSubTitle = ""; } else { super.setShips(player); } } + private String waitingForKeyboardInput() throws InterruptedException { + keyboardComponent.requestInput = true; + while(true) { + Thread.sleep(32); + if(keyboardComponent.keyTyped != KeyEvent.CHAR_UNDEFINED) { + keyboardComponent.requestInput = false; + String value = String.valueOf(keyboardComponent.keyTyped).toUpperCase(); + keyboardComponent.keyTyped = KeyEvent.CHAR_UNDEFINED; + return value; + } + } + } + private Pair waitingForMouseInput(Player player) throws InterruptedException { mouseComponent.requestInput = true; while(true) { - Thread.sleep(33); + Thread.sleep(32); if(mouseComponent.playerIdLastInput != 0) { if(player.getId() == mouseComponent.playerIdLastInput) { - return mouseComponent.lastInput; + mouseComponent.requestInput = false; + mouseComponent.playerIdLastInput = 0; + Pair value = mouseComponent.lastInput; + mouseComponent.lastInput = null; + System.out.println(value); + return value; } else { - JOptionPane.showMessageDialog(frame, "Vous avez cliquer sur une zone de jeu qui n'est pas la votre"); + openDialog("Vous avez cliquer sur une zone de jeu qui n'est pas la votre"); mouseComponent.playerIdLastInput = 0; } } @@ -64,6 +112,10 @@ public class Window extends AbstractView { } } + public void openDialog(String message) { + JOptionPane.showMessageDialog(frame, message); + } + @Override public void displayBoard() { frame.paintComponents(frame.getGraphics()); @@ -89,7 +141,8 @@ public class Window extends AbstractView { } public void paintComponent(Graphics g) { - g.drawString(upperText, (int) (window.width /2 - (upperText.length() * 2.5)), 50); + g.drawString(upperTitle, (int) (window.width /2 - (upperTitle.length() * 2.5)), 50); + g.drawString(upperSubTitle, (int) (window.width / 2 - (upperSubTitle.length() * 2.5)), 65); int width = window.width; int height = window.height; int initialHeight = height / 12;