add chooseMove support for windows ( crosses doesn't display correctly )

This commit is contained in:
Quentin Legot 2021-04-29 19:34:13 +02:00
parent abc02f94e7
commit 122200a391
8 changed files with 90 additions and 36 deletions

View File

@ -21,10 +21,12 @@ public class WindowKeyboardListener implements KeyListener {
if(requestInput) {
if(e.getKeyChar() != KeyEvent.CHAR_UNDEFINED)
keyTyped = e.getKeyChar();
if(e.getKeyCode() != KeyEvent.VK_UNDEFINED)
if(e.getKeyCode() != KeyEvent.VK_UNDEFINED) {
System.out.println(e.getKeyCode());
keyTypedArrow = e.getKeyCode();
}
}
}
@Override

View File

@ -1,8 +1,8 @@
package battleship.model;
import battleship.utils.Pair;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public enum Direction {

View File

@ -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,17 +68,12 @@ 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]);
} catch (InterruptedException e) {
System.out.println("Une erreur est survenue");
e.printStackTrace();
System.exit(1);
}
Player winner = null;
while(winner == null) {
while (winner == null) {
System.out.println("Au tour du joueur " + currentPlayer.getId());
view.displayBoard();
move(view.chooseMove(currentPlayer));
@ -83,6 +82,11 @@ public class Game {
winner = getWinner();
}
view.displayWinner(winner);
} catch (InterruptedException e) {
System.out.println("Une erreur est survenue");
e.printStackTrace();
System.exit(1);
}
}
}

View File

@ -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<Integer,Integer,Boolean> move : moves){
if((move.getLeft() == x) && (move.getMiddle() == y) )
if(move.getLeft() == x && move.getMiddle() == y)
return false;
}
return true;

View File

@ -116,7 +116,7 @@ public abstract class AbstractView implements View {
}
@Override
public Pair<Integer, Integer> chooseMove(Player player) {
public Pair<Integer, Integer> chooseMove(Player player) throws InterruptedException {
return player.chooseMove();
}
}

View File

@ -62,14 +62,17 @@ public class Terminal extends AbstractView {
}
@Override
public Pair<Integer, Integer> chooseMove(Player player) {
public Pair<Integer, Integer> 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);
}

View File

@ -11,7 +11,7 @@ public interface View {
void displayBoard();
Pair<Integer,Integer> chooseMove(Player player);
Pair<Integer,Integer> chooseMove(Player player) throws InterruptedException;
void displayWinner(Player winner);
}

View File

@ -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<Integer, Integer> 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<Integer, Integer> chooseMove(Player player) {
public Pair<Integer, Integer> chooseMove(Player player) throws InterruptedException {
setUpperText("Joueur " + player.getId() + " cliquer sur l'emplacement ou vous souhaitez tirer");
frame.repaint();
if(player instanceof Human) {
Pair<Integer, Integer> 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<Integer, Integer, Boolean> 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);