Move setShips from Game to View + fix Direction.java + improved Player.setShips to check if there is no boat at the location where we want to place another

This commit is contained in:
Quentin Legot 2021-03-28 15:09:44 +02:00
parent 19bd363503
commit 13a48c9d4b
7 changed files with 106 additions and 80 deletions

View File

@ -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<Integer, Integer> direction;

View File

@ -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<Integer,Integer> move = currentPlayer.chooseMove();
move(move);

View File

@ -1,6 +1,6 @@
package battleship.model;
import battleship.utils.*;
import battleship.utils.Pair;
public class Ship {

View File

@ -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;
}
}

View File

@ -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<Integer,Integer> 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<Triplet<Integer,Integer,Boolean>> 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<Integer, Integer, Boolean> 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());
}
}

View File

@ -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<Triplet<Integer,Integer,Boolean>> 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<Integer, Integer, Boolean> 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;
}
}

View File

@ -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() {
}
}