diff --git a/src/battleship/model/Game.java b/src/battleship/model/Game.java index 21b1dc0..3efd11d 100644 --- a/src/battleship/model/Game.java +++ b/src/battleship/model/Game.java @@ -23,6 +23,10 @@ public class Game { return this.currentPlayer; } + public Player getOtherPlayer() { + return this.currentPlayer == players[0] ? players[1] : players[0]; + } + public void changeCurrentPlayer(){ currentPlayer = (currentPlayer == players[1])? players[0] : players[1]; } diff --git a/src/battleship/model/player/Player.java b/src/battleship/model/player/Player.java index 86b11d1..a663c57 100644 --- a/src/battleship/model/player/Player.java +++ b/src/battleship/model/player/Player.java @@ -15,7 +15,7 @@ public abstract class Player { protected int id; protected final static int[] bato = { 5, 4, 3, 3, 2}; - public boolean setShips(Ship ship){ + public boolean setShips(Ship ship) { for(int i = 0; i < ship.getSize(); i++){ int x = ship.getCoords().getLeft() + i * ship.getDirection().getDirection().getLeft(); int y = ship.getCoords().getRight()+ i * ship.getDirection().getDirection().getRight(); @@ -44,7 +44,7 @@ public abstract class Player { return this; } - public boolean isItDrown(Ship ship){ + public boolean isItDrown(Ship ship) { int cpt = 0; for(Triplet move : moves){ for(int i = 1; i <= ship.getSize(); i++){ @@ -75,7 +75,7 @@ public abstract class Player { public ArrayList> validMoves() { ArrayList> validMovesList = new ArrayList<>(); for(Integer i = 0; i<10;i++){ - for(Integer y = 0;y<10;y++){ + for(Integer y = 0;y<10;y++) { if(!moves.contains(new Triplet<>(i,y,true)) ||!moves.contains(new Triplet<>(i,y,false))){ validMovesList.add(new Pair<>(i,y)); } @@ -92,11 +92,11 @@ public abstract class Player { return id; } - public static void placeShipRandomly(Player player) { + public void placeShipRandomly() { Random rand = new Random(); for(int i : bato) { - Ship ship = null; - while(ship == null || !player.setShips(ship)) { + Ship ship = new Ship(new Pair<>(-1, -1), i, Direction.DEFAULT); + while(!setShips(ship)) { ship = new Ship(new Pair<>(rand.nextInt(10), rand.nextInt(10)), i, Direction.values()[rand.nextInt(Direction.values().length)]); } } diff --git a/src/battleship/view/Terminal.java b/src/battleship/view/Terminal.java index 100de66..6096dfe 100644 --- a/src/battleship/view/Terminal.java +++ b/src/battleship/view/Terminal.java @@ -54,14 +54,12 @@ public class Terminal extends View { } } else { // Random - Player.placeShipRandomly(player); + player.placeShipRandomly(); } } - - @Override public void displayBoard() { System.out.println(toString()); diff --git a/src/battleship/view/View.java b/src/battleship/view/View.java index d35a50d..5021d90 100644 --- a/src/battleship/view/View.java +++ b/src/battleship/view/View.java @@ -1,6 +1,5 @@ package battleship.view; -import battleship.model.Direction; import battleship.model.Game; import battleship.model.Ship; import battleship.model.player.Player; @@ -8,9 +7,8 @@ import battleship.utils.Pair; import battleship.utils.Triplet; import java.awt.Graphics; +import java.awt.*; import java.util.ArrayList; -import java.util.Random; -import java.util.concurrent.atomic.AtomicInteger; public abstract class View { @@ -24,33 +22,36 @@ public abstract class View { public abstract void setShips(Player player); - public abstract void displayBoard(); - public abstract void displayBoard(Graphics g); @Override public String toString() { // String chain = "A vous de joueur "+game.currentPlayer.toString()+ "\n+ - - - - - - - - - - +\n"; String chain = ""; for(int u = 0; u < 2; ++u) { - ArrayList> moves = game.players[u].getMoves(); + Player player = game.players[u]; + ArrayList ships = game.players[u].getShips(); chain += "Player " + (u + 1) + " :\n"; chain += "+ - - - - - - - - - - +\n"; for(int i = 0; i < 10;++i) { chain += "|"; for(int y = 0;y < 10; ++y) { - if(!moves.isEmpty()) { - for(Triplet move : moves) { - if(move.getLeft() == i && move.getMiddle() == y) { - if (move.getRight()) + Pair pair = new Pair<>(i, y); + if(!ships.isEmpty()) { + boolean isPosition = false; + for(Ship ship : ships) { + if(isShipPosition(ship, pair)) { + isPosition = true; + if(isPositionDrowned(game.players[u == 0 ? 1 : 0], ship, pair)) { chain += " !"; - else + } else { chain += " ."; - } else { - chain += " _"; + } + break; } - break; } + if(!isPosition) + chain += " _"; } else { chain += " _"; } @@ -62,5 +63,32 @@ public abstract class View { return chain; } + + private boolean isShipPosition(Ship ship, Pair pair) { + if(ship.getCoords().equals(pair)) + return true; + for(int a = 0; a < ship.getSize(); ++a) { + if(new Pair<>(ship.getCoords().getLeft() + a * ship.getDirection().getDirection().getLeft(), ship.getCoords().getRight() + a * ship.getDirection().getDirection().getRight()).equals(pair)) { + return true; + } + } + return false; + } + + + private boolean isPositionDrowned(Player other, Pair pair) { + for(Triplet move : other.getMoves()) { + if(move.getRight() && pair.getLeft().equals(move.getLeft()) && pair.getRight().equals(move.getMiddle())) { + return true; + } + } + return false; + } + + private boolean isPositionDrowned(Player other, Ship ship, Pair pair) { + if(ship.isDrown()) + return true; + return isPositionDrowned(other, pair); + } } diff --git a/src/battleship/view/Window.java b/src/battleship/view/Window.java index 3aee201..8ae8b81 100644 --- a/src/battleship/view/Window.java +++ b/src/battleship/view/Window.java @@ -7,8 +7,8 @@ import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; - import javax.swing.*; +import java.awt.*; public class Window extends View { @@ -30,10 +30,6 @@ public class Window extends View { } - @Override - public void displayBoard(Graphics g) { - - } class Draw extends JPanel { public void paintComponent(Graphics g) { /*JTextArea area = new JTextArea();