diff --git a/src/battleship/control/WindowMouseListener.java b/src/battleship/control/WindowMouseListener.java index dfe9c78..296ccd8 100644 --- a/src/battleship/control/WindowMouseListener.java +++ b/src/battleship/control/WindowMouseListener.java @@ -9,6 +9,9 @@ import java.awt.event.MouseListener; public class WindowMouseListener implements MouseListener { private final Window window; + public boolean requestInput = false; + public Pair lastInput = null; + public int playerIdLastInput = 0; public WindowMouseListener(Window view) { this.window = view; @@ -16,25 +19,24 @@ public class WindowMouseListener implements MouseListener { @Override public void mouseClicked(MouseEvent e) { - int x = e.getX() - 7; - int y = e.getY() - 30; - int initialHeight = window.height / 12; - int initialWidth = window.width / 23; - if(y >= initialHeight * 2 && y <= window.height) { - y -= initialHeight * 2; - if(x >= initialWidth && x <= initialWidth * 11) { - x -= initialWidth; - System.out.println("Player 1"); - Pair location = new Pair<>(y / initialHeight, x / initialWidth); - System.out.println(location); - } else if(x >= initialHeight * 13 && x <= window.width) { - x -= initialWidth * 13; - System.out.println("Player 2"); - Pair location = new Pair<>(y / initialHeight, x / initialWidth); - System.out.println("location: " + location); + if(requestInput) { + int x = e.getX() - 7; + int y = e.getY() - 30; + int initialHeight = window.height / 12; + int initialWidth = window.width / 23; + if(y >= initialHeight * 2 && y <= window.height) { + y -= initialHeight * 2; + if(x >= initialWidth && x <= initialWidth * 11) { + x -= initialWidth; + lastInput = new Pair<>(y / initialHeight, x / initialWidth); + playerIdLastInput = 1; + } else if(x >= initialHeight * 13 && x <= window.width) { + x -= initialWidth * 13; + lastInput = new Pair<>(y / initialHeight, x / initialWidth); + playerIdLastInput = 2; + } } } - System.out.println("(" + x + ", " + y + ")"); } @Override diff --git a/src/battleship/model/Game.java b/src/battleship/model/Game.java index 2ebf1df..6e4c869 100644 --- a/src/battleship/model/Game.java +++ b/src/battleship/model/Game.java @@ -65,8 +65,14 @@ public class Game { } public void Play(AbstractView view){ - view.setShips(players[0]); - view.setShips(players[1]); + try { + view.setShips(players[0]); + view.setShips(players[1]); + } catch (InterruptedException e) { + System.out.println("Une erreur est survenue"); + e.printStackTrace(); + System.exit(1); + } Player winner = null; while(winner == null) { System.out.println("Au tour du joueur " + currentPlayer.getId()); diff --git a/src/battleship/view/AbstractView.java b/src/battleship/view/AbstractView.java index e64e048..05e412a 100644 --- a/src/battleship/view/AbstractView.java +++ b/src/battleship/view/AbstractView.java @@ -71,7 +71,7 @@ public abstract class AbstractView implements View { } @Override - public void setShips(Player player) { + public void setShips(Player player) throws InterruptedException { player.placeShips(); } diff --git a/src/battleship/view/Terminal.java b/src/battleship/view/Terminal.java index af9d4e4..2bd2016 100644 --- a/src/battleship/view/Terminal.java +++ b/src/battleship/view/Terminal.java @@ -18,7 +18,7 @@ public class Terminal extends AbstractView { } @Override - public void setShips(Player player) { + public void setShips(Player player) throws InterruptedException { System.out.println("Joueur " + player.getId() + ", placez vos navires"); int x, y; String dir; @@ -61,7 +61,7 @@ public class Terminal extends AbstractView { @Override public void displayBoard() { - System.out.println(toString()); + System.out.println(this); } @Override diff --git a/src/battleship/view/View.java b/src/battleship/view/View.java index 06641d2..f012577 100644 --- a/src/battleship/view/View.java +++ b/src/battleship/view/View.java @@ -7,7 +7,7 @@ public interface View { int[] shipsSize = { 5, 4, 3, 3, 2}; - void setShips(Player player); + void setShips(Player player) throws InterruptedException; void displayBoard(); diff --git a/src/battleship/view/Window.java b/src/battleship/view/Window.java index 9efb945..9d5c14a 100644 --- a/src/battleship/view/Window.java +++ b/src/battleship/view/Window.java @@ -16,6 +16,7 @@ public class Window extends AbstractView { public final int height = 600; public final int width = 1200; + private final WindowMouseListener mouseComponent; String upperText = ""; public Window(Game game) { @@ -26,16 +27,20 @@ public class Window extends AbstractView { frame.setContentPane(new Draw(this)); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setVisible(true); - frame.addMouseListener(new WindowMouseListener(this)); + this.mouseComponent = new WindowMouseListener(this); + frame.addMouseListener(mouseComponent); } @Override - public void setShips(Player player) { + 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 } } else { @@ -43,7 +48,23 @@ public class Window extends AbstractView { } } - @Override + private Pair waitingForMouseInput(Player player) throws InterruptedException { + mouseComponent.requestInput = true; + while(true) { + Thread.sleep(33); + if(mouseComponent.playerIdLastInput != 0) { + if(player.getId() == mouseComponent.playerIdLastInput) { + return mouseComponent.lastInput; + } else { + JOptionPane.showMessageDialog(frame, "Vous avez cliquer sur une zone de jeu qui n'est pas la votre"); + mouseComponent.playerIdLastInput = 0; + } + } + + } + } + + @Override public void displayBoard() { frame.paintComponents(frame.getGraphics()); }