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,6 +22,7 @@ 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;
if(player instanceof Human) {
for(int i : ships) { for(int i : ships) {
boolean valid = false; boolean valid = false;
Ship ship = new Ship(new Pair<>(-1, -1), i, Direction.DEFAULT); Ship ship = new Ship(new Pair<>(-1, -1), i, Direction.DEFAULT);
@ -50,8 +52,16 @@ public class Terminal extends View {
} }
} }
} }
} 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();