diff --git a/src/battleship/model/Direction.java b/src/battleship/model/Direction.java index a66556b..d79420f 100644 --- a/src/battleship/model/Direction.java +++ b/src/battleship/model/Direction.java @@ -4,10 +4,10 @@ import battleship.utils.Pair; public enum Direction { - RIGHT(new Pair<>(1, 0), "D"), - LEFT(new Pair<>(-1,0), "G"), - UP(new Pair<>(0,-1), "H"), - DOWN(new Pair<>(0,1), "B"), + RIGHT(new Pair<>(0, 1), "D"), + LEFT(new Pair<>(0,-1), "G"), + UP(new Pair<>(-1,0), "H"), + DOWN(new Pair<>(1,0), "B"), DEFAULT(new Pair<>(-1,-1), null); private final Pair direction; diff --git a/src/battleship/model/Game.java b/src/battleship/model/Game.java index 366f6ee..9d4f190 100644 --- a/src/battleship/model/Game.java +++ b/src/battleship/model/Game.java @@ -5,8 +5,6 @@ import battleship.utils.Pair; import battleship.utils.Triplet; import battleship.view.View; -import java.util.Scanner; - public class Game { public Player[] players; @@ -73,46 +71,11 @@ public class Game { currentPlayer.addMove(new Triplet<>(move.getLeft(),move.getRight(),bool)); } - public void setShips(Player player){ - Scanner scanner = new Scanner(System.in); - int x, y; - String dir; - for(int i : ships){ - boolean valid = false; - Ship ship = new Ship(new Pair<>(0,0), i, Direction.DEFAULT); - while(!player.setShips(ship)) { - if(valid) { - System.out.println("Erreur"); - } - valid = true; - System.out.println("Placement du bateau de longueur "+ ship.getSize()); - System.out.println("Veuillez indiquer la coordonée x de votre bateau"); - x = scanner.nextInt(); - System.out.println("Veuillez indiquer la coordonée y de votre bateau"); - y = scanner.nextInt(); - ship.setCoords(new Pair<>(x, y)); - boolean validDirection = false; - while(!validDirection){ - System.out.println("Veuillez indiquer la direction de placement de votre bateau (d droite, h haut, b bas, g gauche)"); - dir = scanner.next().toUpperCase(); - System.out.println(dir); - for(Direction direction : Direction.values()) { - if(direction.getKeyword() != null && direction.getKeyword().equals(dir)) { - ship.setDirection(direction); - validDirection = true; - break; - } - } - } - } - } - - } public Player Play(View view){ - setShips(players[0]); - setShips(players[1]); - while(getWinner() == null){ + view.setShips(players[0]); + view.setShips(players[1]); + while(getWinner() == null) { System.out.println(view); Pair move = currentPlayer.chooseMove(); move(move); diff --git a/src/battleship/model/Ship.java b/src/battleship/model/Ship.java index 921aa10..ef6e7e5 100644 --- a/src/battleship/model/Ship.java +++ b/src/battleship/model/Ship.java @@ -1,6 +1,6 @@ package battleship.model; -import battleship.utils.*; +import battleship.utils.Pair; public class Ship { diff --git a/src/battleship/model/player/Player.java b/src/battleship/model/player/Player.java index c2b2a50..cdf072d 100644 --- a/src/battleship/model/player/Player.java +++ b/src/battleship/model/player/Player.java @@ -13,12 +13,19 @@ public abstract class Player { protected int id; public boolean setShips(Ship ship){ - int x, y; for(int i = 0; i < ship.getSize(); i++){ - x = ship.getCoords().getLeft() + i * ship.getDirection().getDirection().getLeft(); - y = ship.getCoords().getRight()+ i * ship.getDirection().getDirection().getRight(); + int x = ship.getCoords().getLeft() + i * ship.getDirection().getDirection().getLeft(); + int y = ship.getCoords().getRight()+ i * ship.getDirection().getDirection().getRight(); if(x > 9 || x < 0 || y > 9 || y < 0) return false; + for(Ship ship1 : this.ships) { + for (int j = 0; j < ship1.getSize(); j++) { + int x1 = ship1.getCoords().getLeft() + i * ship1.getDirection().getDirection().getLeft(); + int y1 = ship1.getCoords().getRight() + i * ship1.getDirection().getDirection().getRight(); + if (x1 == x && y1 == y) + return false; + } + } } this.ships.add(ship); return true; @@ -78,5 +85,8 @@ public abstract class Player { id = i; } + public int getId() { + return id; + } } diff --git a/src/battleship/view/Terminal.java b/src/battleship/view/Terminal.java index d04199b..ece0807 100644 --- a/src/battleship/view/Terminal.java +++ b/src/battleship/view/Terminal.java @@ -1,49 +1,60 @@ package battleship.view; +import battleship.model.Direction; import battleship.model.Game; import battleship.model.Ship; +import battleship.model.player.Player; import battleship.utils.Pair; -import battleship.utils.Triplet; -import java.util.ArrayList; +import java.util.Scanner; public class Terminal extends View { + private Scanner scanner = new Scanner(System.in); public Terminal(Game game) { super(game); } - public String setShip(Ship ship, int x, int y, Pair direction){ - String chain = "+ - - - - - - - - - - +\n"; - return chain; + @Override + public void setShips(Player player) { + System.out.println("Joueur " + player.getId() + ", placez vos navires"); + int x, y; + String dir; + for(int i : ships) { + boolean valid = false; + Ship ship = new Ship(new Pair<>(-1, -1), i, Direction.DEFAULT); + while (!player.setShips(ship)) { + if (valid) { + System.out.println("Erreur"); + } + valid = true; + System.out.println("Placement du bateau de longueur " + ship.getSize()); + System.out.println("Veuillez indiquer la coordonée x de votre bateau"); + x = scanner.nextInt(); + System.out.println("Veuillez indiquer la coordonée y de votre bateau"); + y = scanner.nextInt(); + ship.setCoords(new Pair<>(x, y)); + boolean validDirection = false; + while (!validDirection) { + System.out.println("Veuillez indiquer la direction de placement de votre bateau (d droite, h haut, b bas, g gauche)"); + dir = scanner.next().toUpperCase(); + for (Direction direction : Direction.values()) { + if (direction.getKeyword() != null && direction.getKeyword().equals(dir)) { + ship.setDirection(direction); + System.out.println(direction); + validDirection = true; + break; + } + } + } + } + } } @Override - public String toString(){ - ArrayList> moves = game.currentPlayer.getMoves(); - String chain = "A vous de joueur "+game.currentPlayer.toString()+"\n+ - - - - - - - - - - +\n"; - for(int i = 0; i<10;i++){ - chain += "|"; - for(int y = 0;y<10;y++){ - if(!moves.isEmpty()) { - for (Triplet ships : moves) { - if (i == ships.getLeft() && y == ships.getMiddle()) { - if (ships.getRight()) - chain += " !"; - else - chain += " ."; - - }else - chain += " _"; - } - }else - chain += " _"; - } - chain += " |\n"; - } - chain += "+ - - - - - - - - - - +\n"; - return chain; + public void displayBoard() { + System.out.println(toString()); } } diff --git a/src/battleship/view/View.java b/src/battleship/view/View.java index aef0387..04de367 100644 --- a/src/battleship/view/View.java +++ b/src/battleship/view/View.java @@ -1,14 +1,50 @@ package battleship.view; import battleship.model.Game; +import battleship.model.player.Player; +import battleship.utils.Triplet; + +import java.util.ArrayList; public abstract class View { protected final Game game; + protected final int[] ships = { 5, 4, 3, 3, 2}; public View(Game game) { this.game = game; } - public abstract String toString(); + + public abstract void setShips(Player player); + + public abstract void displayBoard(); + + @Override + public String toString() { + ArrayList> moves = game.currentPlayer.getMoves(); + String chain = "A vous de joueur "+game.currentPlayer.toString()+ "\n+ - - - - - - - - - - +\n"; + for(int i = 0; i<10;i++){ + chain += "|"; + for(int y = 0;y<10;y++){ + if(!moves.isEmpty()) { + for (Triplet ships : moves) { + if (i == ships.getLeft() && y == ships.getMiddle()) { + if (ships.getRight()) + chain += " !"; + else + chain += " ."; + + }else + chain += " _"; + } + }else + chain += " _"; + } + chain += " |\n"; + } + chain += "+ - - - - - - - - - - +\n"; + return chain; + } + } diff --git a/src/battleship/view/Window.java b/src/battleship/view/Window.java index 666246c..2ee5343 100644 --- a/src/battleship/view/Window.java +++ b/src/battleship/view/Window.java @@ -1,6 +1,7 @@ package battleship.view; import battleship.model.Game; +import battleship.model.player.Player; public class Window extends View { @@ -9,7 +10,12 @@ public class Window extends View { } @Override - public String toString() { - return null; + public void setShips(Player player) { + + } + + @Override + public void displayBoard() { + } }