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:
parent
19bd363503
commit
13a48c9d4b
@ -4,10 +4,10 @@ import battleship.utils.Pair;
|
|||||||
|
|
||||||
public enum Direction {
|
public enum Direction {
|
||||||
|
|
||||||
RIGHT(new Pair<>(1, 0), "D"),
|
RIGHT(new Pair<>(0, 1), "D"),
|
||||||
LEFT(new Pair<>(-1,0), "G"),
|
LEFT(new Pair<>(0,-1), "G"),
|
||||||
UP(new Pair<>(0,-1), "H"),
|
UP(new Pair<>(-1,0), "H"),
|
||||||
DOWN(new Pair<>(0,1), "B"),
|
DOWN(new Pair<>(1,0), "B"),
|
||||||
DEFAULT(new Pair<>(-1,-1), null);
|
DEFAULT(new Pair<>(-1,-1), null);
|
||||||
|
|
||||||
private final Pair<Integer, Integer> direction;
|
private final Pair<Integer, Integer> direction;
|
||||||
|
@ -5,8 +5,6 @@ import battleship.utils.Pair;
|
|||||||
import battleship.utils.Triplet;
|
import battleship.utils.Triplet;
|
||||||
import battleship.view.View;
|
import battleship.view.View;
|
||||||
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class Game {
|
public class Game {
|
||||||
|
|
||||||
public Player[] players;
|
public Player[] players;
|
||||||
@ -73,46 +71,11 @@ public class Game {
|
|||||||
currentPlayer.addMove(new Triplet<>(move.getLeft(),move.getRight(),bool));
|
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){
|
public Player Play(View view){
|
||||||
setShips(players[0]);
|
view.setShips(players[0]);
|
||||||
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();
|
||||||
move(move);
|
move(move);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package battleship.model;
|
package battleship.model;
|
||||||
|
|
||||||
import battleship.utils.*;
|
import battleship.utils.Pair;
|
||||||
|
|
||||||
public class Ship {
|
public class Ship {
|
||||||
|
|
||||||
|
@ -13,12 +13,19 @@ public abstract class Player {
|
|||||||
protected int id;
|
protected int id;
|
||||||
|
|
||||||
public boolean setShips(Ship ship){
|
public boolean setShips(Ship ship){
|
||||||
int x, y;
|
|
||||||
for(int i = 0; i < ship.getSize(); i++){
|
for(int i = 0; i < ship.getSize(); i++){
|
||||||
x = ship.getCoords().getLeft() + i * ship.getDirection().getDirection().getLeft();
|
int x = ship.getCoords().getLeft() + i * ship.getDirection().getDirection().getLeft();
|
||||||
y = ship.getCoords().getRight()+ i * ship.getDirection().getDirection().getRight();
|
int y = ship.getCoords().getRight()+ i * ship.getDirection().getDirection().getRight();
|
||||||
if(x > 9 || x < 0 || y > 9 || y < 0)
|
if(x > 9 || x < 0 || y > 9 || y < 0)
|
||||||
return false;
|
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);
|
this.ships.add(ship);
|
||||||
return true;
|
return true;
|
||||||
@ -78,5 +85,8 @@ public abstract class Player {
|
|||||||
id = i;
|
id = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,49 +1,60 @@
|
|||||||
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.Ship;
|
||||||
|
import battleship.model.player.Player;
|
||||||
import battleship.utils.Pair;
|
import battleship.utils.Pair;
|
||||||
import battleship.utils.Triplet;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Terminal extends View {
|
public class Terminal extends View {
|
||||||
|
|
||||||
|
private Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
public Terminal(Game game) {
|
public Terminal(Game game) {
|
||||||
super(game);
|
super(game);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String setShip(Ship ship, int x, int y, Pair<Integer,Integer> direction){
|
@Override
|
||||||
String chain = "+ - - - - - - - - - - +\n";
|
public void setShips(Player player) {
|
||||||
return chain;
|
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
|
@Override
|
||||||
public String toString(){
|
public void displayBoard() {
|
||||||
ArrayList<Triplet<Integer,Integer,Boolean>> moves = game.currentPlayer.getMoves();
|
System.out.println(toString());
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,50 @@
|
|||||||
package battleship.view;
|
package battleship.view;
|
||||||
|
|
||||||
import battleship.model.Game;
|
import battleship.model.Game;
|
||||||
|
import battleship.model.player.Player;
|
||||||
|
import battleship.utils.Triplet;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public abstract class View {
|
public abstract class View {
|
||||||
|
|
||||||
|
|
||||||
protected final Game game;
|
protected final Game game;
|
||||||
|
protected final int[] ships = { 5, 4, 3, 3, 2};
|
||||||
|
|
||||||
public View(Game game) {
|
public View(Game game) {
|
||||||
this.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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package battleship.view;
|
package battleship.view;
|
||||||
|
|
||||||
import battleship.model.Game;
|
import battleship.model.Game;
|
||||||
|
import battleship.model.player.Player;
|
||||||
|
|
||||||
public class Window extends View {
|
public class Window extends View {
|
||||||
|
|
||||||
@ -9,7 +10,12 @@ public class Window extends View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public void setShips(Player player) {
|
||||||
return null;
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void displayBoard() {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user