diff --git a/src/battleship/control/WindowKeyboardListener.java b/src/battleship/control/WindowKeyboardListener.java index 6409616..d5134eb 100644 --- a/src/battleship/control/WindowKeyboardListener.java +++ b/src/battleship/control/WindowKeyboardListener.java @@ -21,9 +21,11 @@ public class WindowKeyboardListener implements KeyListener { if(requestInput) { if(e.getKeyChar() != KeyEvent.CHAR_UNDEFINED) keyTyped = e.getKeyChar(); - if(e.getKeyCode() != KeyEvent.VK_UNDEFINED) - System.out.println(e.getKeyCode()); + if(e.getKeyCode() != KeyEvent.VK_UNDEFINED) { + System.out.println(e.getKeyCode()); keyTypedArrow = e.getKeyCode(); + } + } } diff --git a/src/battleship/model/Direction.java b/src/battleship/model/Direction.java index c41f770..f9b63fd 100644 --- a/src/battleship/model/Direction.java +++ b/src/battleship/model/Direction.java @@ -1,8 +1,8 @@ package battleship.model; import battleship.utils.Pair; + import java.awt.event.KeyEvent; -import java.awt.event.KeyListener; public enum Direction { diff --git a/src/battleship/model/Game.java b/src/battleship/model/Game.java index 6e4c869..810bc8e 100644 --- a/src/battleship/model/Game.java +++ b/src/battleship/model/Game.java @@ -26,6 +26,10 @@ public class Game { return this.currentPlayer == players[0] ? players[1] : players[0]; } + public Player getOtherPlayer(Player player) { + return this.currentPlayer == player ? getOtherPlayer() : currentPlayer; + } + public void changeCurrentPlayer(){ currentPlayer = getOtherPlayer(); } @@ -64,25 +68,25 @@ public class Game { currentPlayer.addMove(new Triplet<>(move, bool)); } - public void Play(AbstractView view){ + public void Play(AbstractView view) { try { view.setShips(players[0]); view.setShips(players[1]); + Player winner = null; + while (winner == null) { + System.out.println("Au tour du joueur " + currentPlayer.getId()); + view.displayBoard(); + move(view.chooseMove(currentPlayer)); + checkDrownedShips(); + changeCurrentPlayer(); + winner = getWinner(); + } + view.displayWinner(winner); } 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()); - view.displayBoard(); - move(view.chooseMove(currentPlayer)); - checkDrownedShips(); - changeCurrentPlayer(); - winner = getWinner(); - } - view.displayWinner(winner); - } + } } diff --git a/src/battleship/model/player/AbstractPlayer.java b/src/battleship/model/player/AbstractPlayer.java index 233c0ed..bd39075 100644 --- a/src/battleship/model/player/AbstractPlayer.java +++ b/src/battleship/model/player/AbstractPlayer.java @@ -55,10 +55,10 @@ public abstract class AbstractPlayer implements Player { } public boolean areValid(int x, int y){ - if(x <0 || x >10 || y <0 || y >10) + if(x < 0 || x > 10 || y < 0 || y > 10) return false; for(Triplet move : moves){ - if((move.getLeft() == x) && (move.getMiddle() == y) ) + if(move.getLeft() == x && move.getMiddle() == y) return false; } return true; diff --git a/src/battleship/view/AbstractView.java b/src/battleship/view/AbstractView.java index 5cc1a4d..6ab1ce8 100644 --- a/src/battleship/view/AbstractView.java +++ b/src/battleship/view/AbstractView.java @@ -116,7 +116,7 @@ public abstract class AbstractView implements View { } @Override - public Pair chooseMove(Player player) { + public Pair chooseMove(Player player) throws InterruptedException { return player.chooseMove(); } } diff --git a/src/battleship/view/Terminal.java b/src/battleship/view/Terminal.java index 8629ab3..268ae78 100644 --- a/src/battleship/view/Terminal.java +++ b/src/battleship/view/Terminal.java @@ -62,14 +62,17 @@ public class Terminal extends AbstractView { } @Override - public Pair chooseMove(Player player) { + public Pair chooseMove(Player player) throws InterruptedException { 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"); - x = scanner.nextInt(); - System.out.println("Veuillez indiquer la coordonée y de votre coup"); y = scanner.nextInt(); + System.out.println("Veuillez indiquer la coordonée y de votre coup"); + x = scanner.nextInt(); + } return new Pair<>(x,y); } diff --git a/src/battleship/view/View.java b/src/battleship/view/View.java index f012577..232cc86 100644 --- a/src/battleship/view/View.java +++ b/src/battleship/view/View.java @@ -11,7 +11,7 @@ public interface View { void displayBoard(); - Pair chooseMove(Player player); + Pair chooseMove(Player player) throws InterruptedException; void displayWinner(Player winner); } diff --git a/src/battleship/view/Window.java b/src/battleship/view/Window.java index 6a8cad7..2a5155f 100644 --- a/src/battleship/view/Window.java +++ b/src/battleship/view/Window.java @@ -8,10 +8,12 @@ import battleship.model.Ship; import battleship.model.player.Human; import battleship.model.player.Player; import battleship.utils.Pair; +import battleship.utils.Triplet; import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; +import java.awt.geom.RoundRectangle2D; public class Window extends AbstractView { @@ -81,7 +83,7 @@ public class Window extends AbstractView { private String waitingForKeyboardInput() throws InterruptedException { keyboardComponent.requestInput = true; while(true) { - Thread.sleep(32); + Thread.sleep(25); if(keyboardComponent.keyTyped != KeyEvent.CHAR_UNDEFINED) { keyboardComponent.requestInput = false; String value = String.valueOf(keyboardComponent.keyTyped).toUpperCase(); @@ -94,7 +96,7 @@ public class Window extends AbstractView { private Pair waitingForMouseInput(Player player) throws InterruptedException { mouseComponent.requestInput = true; while(true) { - Thread.sleep(32); + Thread.sleep(25); if(mouseComponent.playerIdLastInput != 0) { if(player.getId() == mouseComponent.playerIdLastInput) { mouseComponent.requestInput = false; @@ -112,8 +114,14 @@ public class Window extends AbstractView { } } - public void openDialog(String message) { + public void openDialog(String message, boolean exitOnClose) { JOptionPane.showMessageDialog(frame, message); + if(exitOnClose) + System.exit(0); + } + + public void openDialog(String message) { + openDialog(message, false); } @Override @@ -122,14 +130,27 @@ public class Window extends AbstractView { } @Override - public Pair chooseMove(Player player) { + public Pair chooseMove(Player player) throws InterruptedException { + setUpperText("Joueur " + player.getId() + " cliquer sur l'emplacement ou vous souhaitez tirer"); + frame.repaint(); + if(player instanceof Human) { + Pair coords = new Pair<>(-1, -1); + boolean valid = false; + while(!player.areValid(coords.getLeft(), coords.getRight())) { + if(valid) + openDialog("Erreur de placement, ce coup a déjà été effectué"); + valid = true; + coords = waitingForMouseInput(game.getOtherPlayer(player)); + } + return coords; + } + return super.chooseMove(player); - return null; } @Override public void displayWinner(Player winner) { - // TODO: 07/04/2021 afficher un dialog + openDialog("Le joueur " + winner.getId() + " a gagné(e)", true); } class Draw extends JPanel { @@ -141,20 +162,22 @@ public class Window extends AbstractView { } public void paintComponent(Graphics g) { - g.drawString(upperTitle, (int) (window.width /2 - (upperTitle.length() * 2.5)), 50); - g.drawString(upperSubTitle, (int) (window.width / 2 - (upperSubTitle.length() * 2.5)), 65); + super.paintComponent(g); + Graphics2D g2d = (Graphics2D)g; + g2d.drawString(upperTitle, (int) (window.width /2 - (upperTitle.length() * 2.5)), 50); + g2d.drawString(upperSubTitle, (int) (window.width / 2 - (upperSubTitle.length() * 2.5)), 65); int width = window.width; int height = window.height; int initialHeight = height / 12; int initialWidth = width / 23; for(int abscisse = initialWidth; abscisse < width; abscisse += initialWidth) { - g.drawLine(abscisse, initialHeight * 2, abscisse, height); + g2d.drawLine(abscisse, initialHeight * 2, abscisse, height); if(abscisse == initialWidth * 11) abscisse += initialWidth; } for(int ordonnee = initialHeight * 2; ordonnee < height + 1; ordonnee += initialHeight) { - g.drawLine(initialWidth, ordonnee, initialWidth * 11, ordonnee); - g.drawLine(initialWidth * 13, ordonnee, width - 4, ordonnee); + g2d.drawLine(initialWidth, ordonnee, initialWidth * 11, ordonnee); + g2d.drawLine(initialWidth * 13, ordonnee, width - 4, ordonnee); } for(int i = 1; i < 3; ++i) { @@ -164,13 +187,12 @@ public class Window extends AbstractView { int y1 = initialHeight * 2; int shipWidth = initialWidth; int shipHeight = initialHeight; - System.out.println(ship); switch(ship.getDirection()) { case DOWN: x1 += initialWidth * ship.getCoords().getRight(); y1 += initialHeight * ship.getCoords().getLeft(); shipHeight = initialHeight * ship.getSize(); - g.setColor(new Color(255, 0, 0)); + g.setColor(new Color(0, 255, 255)); break; case UP: x1 += initialWidth * ship.getCoords().getRight(); @@ -191,7 +213,30 @@ public class Window extends AbstractView { g.setColor(new Color(0, 0, 255)); break; } - g.fillRect(x1, y1, shipWidth, shipHeight); + g2d.fillRect(x1, y1, shipWidth, shipHeight); + } + } + for(int i = 1; i < 3; ++i) { + Player player = game.players[i-1]; + int halfBoxSizeWidth = initialWidth / 2; + int halfBoxSizeHeight = initialHeight / 2; + int sqrt = (int) Math.sqrt(initialHeight * initialHeight + initialWidth * initialWidth); + for(Triplet move : player.getMoves()) { + int x1 = (i == 1 ? initialWidth * 13 : initialWidth) + initialWidth * move.getMiddle(); + int y1 = initialHeight * 2 + initialHeight * move.getLeft(); + RoundRectangle2D rect = new RoundRectangle2D.Float(x1, y1, initialWidth / 4f, sqrt, 15, 15); + if(move.getRight()) { + g.setColor(new Color(255, 0, 0)); + } else { + g.setColor(new Color(0, 123, 255)); + } + g2d.rotate(Math.toRadians(45), x1, y1); + g2d.fill(rect); + // g2d.rotate(Math.toRadians(-45), x1, y1); + g2d.rotate(Math.toRadians(-90), x1 + halfBoxSizeWidth, y1 + halfBoxSizeHeight); + g2d.fill(rect); + g2d.rotate(Math.toRadians(90), x1 + halfBoxSizeWidth, y1 + halfBoxSizeHeight); + g2d.rotate(Math.toRadians(-45), x1, y1); } } System.out.println(window);