Add View.placeShipRandomly
This commit is contained in:
parent
5098d46e5c
commit
f2607f1268
@ -73,8 +73,8 @@ public class Game {
|
||||
}
|
||||
|
||||
public Player Play(View view){
|
||||
//view.setShips(players[0]);
|
||||
//view.setShips(players[1]);
|
||||
view.setShips(players[0]);
|
||||
view.setShips(players[1]);
|
||||
while(getWinner() == null) {
|
||||
System.out.println(view);
|
||||
Pair<Integer,Integer> move = currentPlayer.chooseMove();
|
||||
|
@ -3,6 +3,7 @@ package battleship.view;
|
||||
import battleship.model.Direction;
|
||||
import battleship.model.Game;
|
||||
import battleship.model.Ship;
|
||||
import battleship.model.player.Human;
|
||||
import battleship.model.player.Player;
|
||||
import battleship.utils.Pair;
|
||||
|
||||
@ -21,37 +22,46 @@ public class Terminal extends View {
|
||||
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;
|
||||
if(player instanceof Human) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Random
|
||||
placeShipRandomly(player);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void displayBoard() {
|
||||
System.out.println(toString());
|
||||
|
@ -1,10 +1,14 @@
|
||||
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.Random;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public abstract class View {
|
||||
@ -21,6 +25,16 @@ public abstract class View {
|
||||
|
||||
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
|
||||
public String toString() {
|
||||
ArrayList<Triplet<Integer,Integer,Boolean>> moves = game.currentPlayer.getMoves();
|
||||
|
Loading…
Reference in New Issue
Block a user