add chooseMove support for windows ( crosses doesn't display correctly )
This commit is contained in:
parent
abc02f94e7
commit
122200a391
@ -21,10 +21,12 @@ public class WindowKeyboardListener implements KeyListener {
|
|||||||
if(requestInput) {
|
if(requestInput) {
|
||||||
if(e.getKeyChar() != KeyEvent.CHAR_UNDEFINED)
|
if(e.getKeyChar() != KeyEvent.CHAR_UNDEFINED)
|
||||||
keyTyped = e.getKeyChar();
|
keyTyped = e.getKeyChar();
|
||||||
if(e.getKeyCode() != KeyEvent.VK_UNDEFINED)
|
if(e.getKeyCode() != KeyEvent.VK_UNDEFINED) {
|
||||||
System.out.println(e.getKeyCode());
|
System.out.println(e.getKeyCode());
|
||||||
keyTypedArrow = e.getKeyCode();
|
keyTypedArrow = e.getKeyCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package battleship.model;
|
package battleship.model;
|
||||||
|
|
||||||
import battleship.utils.Pair;
|
import battleship.utils.Pair;
|
||||||
|
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.awt.event.KeyListener;
|
|
||||||
|
|
||||||
public enum Direction {
|
public enum Direction {
|
||||||
|
|
||||||
|
@ -26,6 +26,10 @@ public class Game {
|
|||||||
return this.currentPlayer == players[0] ? players[1] : players[0];
|
return this.currentPlayer == players[0] ? players[1] : players[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Player getOtherPlayer(Player player) {
|
||||||
|
return this.currentPlayer == player ? getOtherPlayer() : currentPlayer;
|
||||||
|
}
|
||||||
|
|
||||||
public void changeCurrentPlayer(){
|
public void changeCurrentPlayer(){
|
||||||
currentPlayer = getOtherPlayer();
|
currentPlayer = getOtherPlayer();
|
||||||
}
|
}
|
||||||
@ -68,11 +72,6 @@ public class Game {
|
|||||||
try {
|
try {
|
||||||
view.setShips(players[0]);
|
view.setShips(players[0]);
|
||||||
view.setShips(players[1]);
|
view.setShips(players[1]);
|
||||||
} catch (InterruptedException e) {
|
|
||||||
System.out.println("Une erreur est survenue");
|
|
||||||
e.printStackTrace();
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
Player winner = null;
|
Player winner = null;
|
||||||
while (winner == null) {
|
while (winner == null) {
|
||||||
System.out.println("Au tour du joueur " + currentPlayer.getId());
|
System.out.println("Au tour du joueur " + currentPlayer.getId());
|
||||||
@ -83,6 +82,11 @@ public class Game {
|
|||||||
winner = getWinner();
|
winner = getWinner();
|
||||||
}
|
}
|
||||||
view.displayWinner(winner);
|
view.displayWinner(winner);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
System.out.println("Une erreur est survenue");
|
||||||
|
e.printStackTrace();
|
||||||
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -58,7 +58,7 @@ public abstract class AbstractPlayer implements Player {
|
|||||||
if(x < 0 || x > 10 || y < 0 || y > 10)
|
if(x < 0 || x > 10 || y < 0 || y > 10)
|
||||||
return false;
|
return false;
|
||||||
for(Triplet<Integer,Integer,Boolean> move : moves){
|
for(Triplet<Integer,Integer,Boolean> move : moves){
|
||||||
if((move.getLeft() == x) && (move.getMiddle() == y) )
|
if(move.getLeft() == x && move.getMiddle() == y)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -116,7 +116,7 @@ public abstract class AbstractView implements View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Pair<Integer, Integer> chooseMove(Player player) {
|
public Pair<Integer, Integer> chooseMove(Player player) throws InterruptedException {
|
||||||
return player.chooseMove();
|
return player.chooseMove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,14 +62,17 @@ public class Terminal extends AbstractView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Pair<Integer, Integer> chooseMove(Player player) {
|
public Pair<Integer, Integer> chooseMove(Player player) throws InterruptedException {
|
||||||
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
|
||||||
|
// (erreur de notre part aperçu lors du passage à une fenetre swing)
|
||||||
System.out.println("Veuillez indiquer la coordonée x de votre coup");
|
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();
|
y = scanner.nextInt();
|
||||||
|
System.out.println("Veuillez indiquer la coordonée y de votre coup");
|
||||||
|
x = scanner.nextInt();
|
||||||
|
|
||||||
}
|
}
|
||||||
return new Pair<>(x,y);
|
return new Pair<>(x,y);
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ public interface View {
|
|||||||
|
|
||||||
void displayBoard();
|
void displayBoard();
|
||||||
|
|
||||||
Pair<Integer,Integer> chooseMove(Player player);
|
Pair<Integer,Integer> chooseMove(Player player) throws InterruptedException;
|
||||||
|
|
||||||
void displayWinner(Player winner);
|
void displayWinner(Player winner);
|
||||||
}
|
}
|
||||||
|
@ -8,10 +8,12 @@ import battleship.model.Ship;
|
|||||||
import battleship.model.player.Human;
|
import battleship.model.player.Human;
|
||||||
import battleship.model.player.Player;
|
import battleship.model.player.Player;
|
||||||
import battleship.utils.Pair;
|
import battleship.utils.Pair;
|
||||||
|
import battleship.utils.Triplet;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.awt.geom.RoundRectangle2D;
|
||||||
|
|
||||||
public class Window extends AbstractView {
|
public class Window extends AbstractView {
|
||||||
|
|
||||||
@ -81,7 +83,7 @@ public class Window extends AbstractView {
|
|||||||
private String waitingForKeyboardInput() throws InterruptedException {
|
private String waitingForKeyboardInput() throws InterruptedException {
|
||||||
keyboardComponent.requestInput = true;
|
keyboardComponent.requestInput = true;
|
||||||
while(true) {
|
while(true) {
|
||||||
Thread.sleep(32);
|
Thread.sleep(25);
|
||||||
if(keyboardComponent.keyTyped != KeyEvent.CHAR_UNDEFINED) {
|
if(keyboardComponent.keyTyped != KeyEvent.CHAR_UNDEFINED) {
|
||||||
keyboardComponent.requestInput = false;
|
keyboardComponent.requestInput = false;
|
||||||
String value = String.valueOf(keyboardComponent.keyTyped).toUpperCase();
|
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 {
|
private Pair<Integer, Integer> waitingForMouseInput(Player player) throws InterruptedException {
|
||||||
mouseComponent.requestInput = true;
|
mouseComponent.requestInput = true;
|
||||||
while(true) {
|
while(true) {
|
||||||
Thread.sleep(32);
|
Thread.sleep(25);
|
||||||
if(mouseComponent.playerIdLastInput != 0) {
|
if(mouseComponent.playerIdLastInput != 0) {
|
||||||
if(player.getId() == mouseComponent.playerIdLastInput) {
|
if(player.getId() == mouseComponent.playerIdLastInput) {
|
||||||
mouseComponent.requestInput = false;
|
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);
|
JOptionPane.showMessageDialog(frame, message);
|
||||||
|
if(exitOnClose)
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void openDialog(String message) {
|
||||||
|
openDialog(message, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -122,14 +130,27 @@ public class Window extends AbstractView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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
|
@Override
|
||||||
public void displayWinner(Player winner) {
|
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 {
|
class Draw extends JPanel {
|
||||||
@ -141,20 +162,22 @@ public class Window extends AbstractView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void paintComponent(Graphics g) {
|
public void paintComponent(Graphics g) {
|
||||||
g.drawString(upperTitle, (int) (window.width /2 - (upperTitle.length() * 2.5)), 50);
|
super.paintComponent(g);
|
||||||
g.drawString(upperSubTitle, (int) (window.width / 2 - (upperSubTitle.length() * 2.5)), 65);
|
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 width = window.width;
|
||||||
int height = window.height;
|
int height = window.height;
|
||||||
int initialHeight = height / 12;
|
int initialHeight = height / 12;
|
||||||
int initialWidth = width / 23;
|
int initialWidth = width / 23;
|
||||||
for(int abscisse = initialWidth; abscisse < width; abscisse += initialWidth) {
|
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)
|
if(abscisse == initialWidth * 11)
|
||||||
abscisse += initialWidth;
|
abscisse += initialWidth;
|
||||||
}
|
}
|
||||||
for(int ordonnee = initialHeight * 2; ordonnee < height + 1; ordonnee += initialHeight) {
|
for(int ordonnee = initialHeight * 2; ordonnee < height + 1; ordonnee += initialHeight) {
|
||||||
g.drawLine(initialWidth, ordonnee, initialWidth * 11, ordonnee);
|
g2d.drawLine(initialWidth, ordonnee, initialWidth * 11, ordonnee);
|
||||||
g.drawLine(initialWidth * 13, ordonnee, width - 4, ordonnee);
|
g2d.drawLine(initialWidth * 13, ordonnee, width - 4, ordonnee);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int i = 1; i < 3; ++i) {
|
for(int i = 1; i < 3; ++i) {
|
||||||
@ -164,13 +187,12 @@ public class Window extends AbstractView {
|
|||||||
int y1 = initialHeight * 2;
|
int y1 = initialHeight * 2;
|
||||||
int shipWidth = initialWidth;
|
int shipWidth = initialWidth;
|
||||||
int shipHeight = initialHeight;
|
int shipHeight = initialHeight;
|
||||||
System.out.println(ship);
|
|
||||||
switch(ship.getDirection()) {
|
switch(ship.getDirection()) {
|
||||||
case DOWN:
|
case DOWN:
|
||||||
x1 += initialWidth * ship.getCoords().getRight();
|
x1 += initialWidth * ship.getCoords().getRight();
|
||||||
y1 += initialHeight * ship.getCoords().getLeft();
|
y1 += initialHeight * ship.getCoords().getLeft();
|
||||||
shipHeight = initialHeight * ship.getSize();
|
shipHeight = initialHeight * ship.getSize();
|
||||||
g.setColor(new Color(255, 0, 0));
|
g.setColor(new Color(0, 255, 255));
|
||||||
break;
|
break;
|
||||||
case UP:
|
case UP:
|
||||||
x1 += initialWidth * ship.getCoords().getRight();
|
x1 += initialWidth * ship.getCoords().getRight();
|
||||||
@ -191,7 +213,30 @@ public class Window extends AbstractView {
|
|||||||
g.setColor(new Color(0, 0, 255));
|
g.setColor(new Color(0, 0, 255));
|
||||||
break;
|
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);
|
System.out.println(window);
|
||||||
|
Loading…
Reference in New Issue
Block a user