Add View.placeShipRandomly

This commit is contained in:
Quentin Legot 2021-04-06 09:55:02 +02:00
parent 5098d46e5c
commit f2607f1268
3 changed files with 50 additions and 26 deletions

View File

@ -73,8 +73,8 @@ public class Game {
} }
public Player Play(View view){ public Player Play(View view){
//view.setShips(players[0]); view.setShips(players[0]);
//view.setShips(players[1]); view.setShips(players[1]);
while(getWinner() == null) { while(getWinner() == null) {
System.out.println(view); System.out.println(view);
Pair<Integer,Integer> move = currentPlayer.chooseMove(); Pair<Integer,Integer> move = currentPlayer.chooseMove();

View File

@ -3,6 +3,7 @@ package battleship.view;
import battleship.model.Direction; import battleship.model.Direction;
import battleship.model.Game; import battleship.model.Game;
import battleship.model.Ship; import battleship.model.Ship;
import battleship.model.player.Human;
import battleship.model.player.Player; import battleship.model.player.Player;
import battleship.utils.Pair; import battleship.utils.Pair;
@ -21,37 +22,46 @@ public class Terminal extends View {
System.out.println("Joueur " + player.getId() + ", placez vos navires"); System.out.println("Joueur " + player.getId() + ", placez vos navires");
int x, y; int x, y;
String dir; String dir;
for(int i : ships) { if(player instanceof Human) {
boolean valid = false; for(int i : ships) {
Ship ship = new Ship(new Pair<>(-1, -1), i, Direction.DEFAULT); boolean valid = false;
while (!player.setShips(ship)) { Ship ship = new Ship(new Pair<>(-1, -1), i, Direction.DEFAULT);
if (valid) { while (!player.setShips(ship)) {
System.out.println("Erreur"); if (valid) {
} System.out.println("Erreur");
valid = true; }
System.out.println("Placement du bateau de longueur " + ship.getSize()); valid = true;
System.out.println("Veuillez indiquer la coordonée x de votre bateau"); System.out.println("Placement du bateau de longueur " + ship.getSize());
x = scanner.nextInt(); System.out.println("Veuillez indiquer la coordonée x de votre bateau");
System.out.println("Veuillez indiquer la coordonée y de votre bateau"); x = scanner.nextInt();
y = scanner.nextInt(); System.out.println("Veuillez indiquer la coordonée y de votre bateau");
ship.setCoords(new Pair<>(x, y)); y = scanner.nextInt();
boolean validDirection = false; ship.setCoords(new Pair<>(x, y));
while (!validDirection) { boolean validDirection = false;
System.out.println("Veuillez indiquer la direction de placement de votre bateau (d droite, h haut, b bas, g gauche)"); while (!validDirection) {
dir = scanner.next().toUpperCase(); System.out.println("Veuillez indiquer la direction de placement de votre bateau (d droite, h haut, b bas, g gauche)");
for (Direction direction : Direction.values()) { dir = scanner.next().toUpperCase();
if (direction.getKeyword() != null && direction.getKeyword().equals(dir)) { for (Direction direction : Direction.values()) {
ship.setDirection(direction); if (direction.getKeyword() != null && direction.getKeyword().equals(dir)) {
System.out.println(direction); ship.setDirection(direction);
validDirection = true; System.out.println(direction);
break; validDirection = true;
break;
}
} }
} }
} }
} }
} else {
// Random
placeShipRandomly(player);
} }
} }
@Override @Override
public void displayBoard() { public void displayBoard() {
System.out.println(toString()); System.out.println(toString());

View File

@ -1,10 +1,14 @@
package battleship.view; package battleship.view;
import battleship.model.Direction;
import battleship.model.Game; import battleship.model.Game;
import battleship.model.Ship;
import battleship.model.player.Player; import battleship.model.player.Player;
import battleship.utils.Pair;
import battleship.utils.Triplet; import battleship.utils.Triplet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
public abstract class View { public abstract class View {
@ -21,6 +25,16 @@ public abstract class View {
public abstract void displayBoard(); public abstract void displayBoard();
protected void placeShipRandomly(Player player) {
Random rand = new Random();
for(int i : ships) {
Ship ship = null;
while(ship == null || !player.setShips(ship)) {
ship = new Ship(new Pair<>(rand.nextInt(10), rand.nextInt(10)), i, Direction.values()[rand.nextInt(Direction.values().length)]);
}
}
}
@Override @Override
public String toString() { public String toString() {
ArrayList<Triplet<Integer,Integer,Boolean>> moves = game.currentPlayer.getMoves(); ArrayList<Triplet<Integer,Integer,Boolean>> moves = game.currentPlayer.getMoves();