diff --git a/src/battleship/model/Game.java b/src/battleship/model/Game.java index 55aa1f9..b976dda 100644 --- a/src/battleship/model/Game.java +++ b/src/battleship/model/Game.java @@ -5,16 +5,27 @@ import battleship.utils.Pair; import battleship.utils.Triplet; import battleship.view.View; +import java.util.Scanner; + public class Game { public Player[] players; public Player currentPlayer; + private int[] ships = new int[5]; public Game(Player[] players) { this.players = players; this.currentPlayer = players[0]; + players[0].setId(1); + players[1].setId(2); + ships[0] = 5; + ships[1] = 4; + ships[2] = 3; + ships[3] = 3; + ships[4] = 2; } + public Player getCurrentPlayer(){ return this.currentPlayer; } @@ -65,7 +76,45 @@ public class Game { } public void setShips(Player player){ - //TODO a method that place the ships according to the decision of the players + Ship ship = new Ship(new Pair<>(0,0),2,new Pair<>(-1,-1)); + Scanner scanner = new Scanner(System.in); + int x,y; + String dir = "AB"; + Pair pair = new Pair<>(null,null); + for(int i : ships){ + while(!player.setShips(ship)){ + System.out.println("Placement du bateau de longueur "+i +" :\n"); + 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)); + while(!dir.equals("D") || !dir.equals("H") || !dir.equals("B") || !dir.equals("G")){ + System.out.println("Veuillez indiquer la direction de placement de votre bateau (d droite, h haut, b bas, g gauche)"); + dir = scanner.nextLine(); + System.out.println(dir); + dir = (String.valueOf(dir.substring(0,1))).toUpperCase(); + + } + switch (dir){ + case "D": + ship.setDirection(new Pair<>(1,0)); + break; + case "H": + ship.setDirection(new Pair<>(0,1)); + break; + case "B": + ship.setDirection(new Pair<>(0,-1)); + break; + case "G": + ship.setDirection(new Pair<>(-1,0)); + break; + } + + } + } + + } public Player Play(View view){ setShips(players[0]); diff --git a/src/battleship/model/Ship.java b/src/battleship/model/Ship.java index 8e3a25b..305728c 100644 --- a/src/battleship/model/Ship.java +++ b/src/battleship/model/Ship.java @@ -4,9 +4,9 @@ import battleship.utils.*; public class Ship { - private final Pair coords; + private Pair coords; private final int size; - private final Pair direction; + private Pair direction; // (0,-1) bas // (0,1) haut // (1,0) droite // (-1,0) gauche private boolean isDrown; @@ -16,6 +16,14 @@ public class Ship { this.direction = direction; isDrown = false; } + public void setDirection(Pair d){ + this.direction = d; + } + public void setCoords(Pair c){ + this.coords = c; + + } + public void setDrown(){ isDrown = true; } diff --git a/src/battleship/model/player/Human.java b/src/battleship/model/player/Human.java index 882014d..3386d79 100644 --- a/src/battleship/model/player/Human.java +++ b/src/battleship/model/player/Human.java @@ -8,6 +8,7 @@ import java.util.Scanner; public class Human extends Player { + @Override public Pair chooseMove() { int x = -1, y = -1; @@ -18,6 +19,7 @@ public class Human extends Player { System.out.println("Veuillez indiquer la coordonée y de votre coup"); y = scanner.nextInt(); } + scanner.close(); return new Pair<>(x,y); } public boolean areValid(int x,int y){ @@ -29,4 +31,9 @@ public class Human extends Player { } return true; } + @Override + public String toString(){ + return "Human " +id; + } + } diff --git a/src/battleship/model/player/Player.java b/src/battleship/model/player/Player.java index e622df2..88251e6 100644 --- a/src/battleship/model/player/Player.java +++ b/src/battleship/model/player/Player.java @@ -5,19 +5,26 @@ import battleship.utils.Pair; import battleship.utils.Triplet; import java.util.ArrayList; -import java.util.Arrays; public abstract class Player { protected ArrayList ships = new ArrayList<>(); protected ArrayList> moves = new ArrayList<>(); + protected int id; public Player(){ - setShips(); } - public void setShips(Ship... ships){ - this.ships.addAll(Arrays.asList(ships)); + public boolean setShips(Ship ship){ + int x,y; + for(int i = 0; i 9 ||x<0||y>9||y<0) + return false; + } + this.ships.add(ship); + return true; } /** @@ -68,6 +75,9 @@ public abstract class Player { return validMovesList; } + public void setId(int i ){ + id = i; + } } diff --git a/src/battleship/model/player/Random.java b/src/battleship/model/player/Random.java index 7342e8d..6ba0f8c 100644 --- a/src/battleship/model/player/Random.java +++ b/src/battleship/model/player/Random.java @@ -9,4 +9,8 @@ public class Random extends Player { java.util.Random rand = new java.util.Random(); return validMoves().get(rand.nextInt(validMoves().size())); } + @Override + public String toString(){ + return "Random " +id; + } } diff --git a/src/battleship/view/Terminal.java b/src/battleship/view/Terminal.java index d4726dc..d04199b 100644 --- a/src/battleship/view/Terminal.java +++ b/src/battleship/view/Terminal.java @@ -1,6 +1,8 @@ package battleship.view; import battleship.model.Game; +import battleship.model.Ship; +import battleship.utils.Pair; import battleship.utils.Triplet; import java.util.ArrayList; @@ -12,28 +14,31 @@ public class Terminal extends View { super(game); } + public String setShip(Ship ship, int x, int y, Pair direction){ + String chain = "+ - - - - - - - - - - +\n"; + return chain; + } + @Override public String toString(){ ArrayList> moves = game.currentPlayer.getMoves(); - String chain = "A vous de joueur "+game.currentPlayer+"\n+ - - - - - - - - - - +\n"; + 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()) - chain += " _"; - else { + if(!moves.isEmpty()) { for (Triplet ships : moves) { if (i == ships.getLeft() && y == ships.getMiddle()) { - if (ships.getRight() == true) + if (ships.getRight()) chain += " !"; else chain += " ."; - } else + }else chain += " _"; } - } - + }else + chain += " _"; } chain += " |\n"; }