There is an error in Game.java line 95, I don't understand why the program skip the scanner.nextLine
This commit is contained in:
parent
c45d8a93ae
commit
be296d3946
@ -5,16 +5,27 @@ 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;
|
||||||
public Player currentPlayer;
|
public Player currentPlayer;
|
||||||
|
private int[] ships = new int[5];
|
||||||
|
|
||||||
public Game(Player[] players) {
|
public Game(Player[] players) {
|
||||||
this.players = players;
|
this.players = players;
|
||||||
this.currentPlayer = players[0];
|
this.currentPlayer = players[0];
|
||||||
|
players[0].setId(1);
|
||||||
|
players[1].setId(2);
|
||||||
|
ships[0] = 5;
|
||||||
|
ships[1] = 4;
|
||||||
|
ships[2] = 3;
|
||||||
|
ships[3] = 3;
|
||||||
|
ships[4] = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Player getCurrentPlayer(){
|
public Player getCurrentPlayer(){
|
||||||
return this.currentPlayer;
|
return this.currentPlayer;
|
||||||
}
|
}
|
||||||
@ -65,7 +76,45 @@ public class Game {
|
|||||||
|
|
||||||
}
|
}
|
||||||
public void setShips(Player player){
|
public void setShips(Player player){
|
||||||
//TODO a method that place the ships according to the decision of the players
|
Ship ship = new Ship(new Pair<>(0,0),2,new Pair<>(-1,-1));
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
int x,y;
|
||||||
|
String dir = "AB";
|
||||||
|
Pair pair = new Pair<>(null,null);
|
||||||
|
for(int i : ships){
|
||||||
|
while(!player.setShips(ship)){
|
||||||
|
System.out.println("Placement du bateau de longueur "+i +" :\n");
|
||||||
|
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));
|
||||||
|
while(!dir.equals("D") || !dir.equals("H") || !dir.equals("B") || !dir.equals("G")){
|
||||||
|
System.out.println("Veuillez indiquer la direction de placement de votre bateau (d droite, h haut, b bas, g gauche)");
|
||||||
|
dir = scanner.nextLine();
|
||||||
|
System.out.println(dir);
|
||||||
|
dir = (String.valueOf(dir.substring(0,1))).toUpperCase();
|
||||||
|
|
||||||
|
}
|
||||||
|
switch (dir){
|
||||||
|
case "D":
|
||||||
|
ship.setDirection(new Pair<>(1,0));
|
||||||
|
break;
|
||||||
|
case "H":
|
||||||
|
ship.setDirection(new Pair<>(0,1));
|
||||||
|
break;
|
||||||
|
case "B":
|
||||||
|
ship.setDirection(new Pair<>(0,-1));
|
||||||
|
break;
|
||||||
|
case "G":
|
||||||
|
ship.setDirection(new Pair<>(-1,0));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
public Player Play(View view){
|
public Player Play(View view){
|
||||||
setShips(players[0]);
|
setShips(players[0]);
|
||||||
|
@ -4,9 +4,9 @@ import battleship.utils.*;
|
|||||||
|
|
||||||
public class Ship {
|
public class Ship {
|
||||||
|
|
||||||
private final Pair<Integer, Integer> coords;
|
private Pair<Integer, Integer> coords;
|
||||||
private final int size;
|
private final int size;
|
||||||
private final Pair<Integer,Integer> direction;
|
private Pair<Integer,Integer> direction;
|
||||||
// (0,-1) bas // (0,1) haut // (1,0) droite // (-1,0) gauche
|
// (0,-1) bas // (0,1) haut // (1,0) droite // (-1,0) gauche
|
||||||
private boolean isDrown;
|
private boolean isDrown;
|
||||||
|
|
||||||
@ -16,6 +16,14 @@ public class Ship {
|
|||||||
this.direction = direction;
|
this.direction = direction;
|
||||||
isDrown = false;
|
isDrown = false;
|
||||||
}
|
}
|
||||||
|
public void setDirection(Pair<Integer,Integer> d){
|
||||||
|
this.direction = d;
|
||||||
|
}
|
||||||
|
public void setCoords(Pair<Integer,Integer> c){
|
||||||
|
this.coords = c;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public void setDrown(){
|
public void setDrown(){
|
||||||
isDrown = true;
|
isDrown = true;
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import java.util.Scanner;
|
|||||||
public class Human extends Player {
|
public class Human extends Player {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Pair<Integer,Integer> chooseMove() {
|
public Pair<Integer,Integer> chooseMove() {
|
||||||
int x = -1, y = -1;
|
int x = -1, y = -1;
|
||||||
@ -18,6 +19,7 @@ public class Human extends Player {
|
|||||||
System.out.println("Veuillez indiquer la coordonée y de votre coup");
|
System.out.println("Veuillez indiquer la coordonée y de votre coup");
|
||||||
y = scanner.nextInt();
|
y = scanner.nextInt();
|
||||||
}
|
}
|
||||||
|
scanner.close();
|
||||||
return new Pair<>(x,y);
|
return new Pair<>(x,y);
|
||||||
}
|
}
|
||||||
public boolean areValid(int x,int y){
|
public boolean areValid(int x,int y){
|
||||||
@ -29,4 +31,9 @@ public class Human extends Player {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@Override
|
||||||
|
public String toString(){
|
||||||
|
return "Human " +id;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,19 +5,26 @@ import battleship.utils.Pair;
|
|||||||
import battleship.utils.Triplet;
|
import battleship.utils.Triplet;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
public abstract class Player {
|
public abstract class Player {
|
||||||
|
|
||||||
protected ArrayList<Ship> ships = new ArrayList<>();
|
protected ArrayList<Ship> ships = new ArrayList<>();
|
||||||
protected ArrayList<Triplet<Integer,Integer,Boolean>> moves = new ArrayList<>();
|
protected ArrayList<Triplet<Integer,Integer,Boolean>> moves = new ArrayList<>();
|
||||||
|
protected int id;
|
||||||
|
|
||||||
public Player(){
|
public Player(){
|
||||||
setShips();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setShips(Ship... ships){
|
public boolean setShips(Ship ship){
|
||||||
this.ships.addAll(Arrays.asList(ships));
|
int x,y;
|
||||||
|
for(int i = 0; i<ship.getSize();i++){
|
||||||
|
x = ship.getCoords().getLeft()+i* ship.getDirection().getLeft();
|
||||||
|
y = ship.getCoords().getRight()+i* ship.getDirection().getRight();
|
||||||
|
if(x > 9 ||x<0||y>9||y<0)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
this.ships.add(ship);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -68,6 +75,9 @@ public abstract class Player {
|
|||||||
return validMovesList;
|
return validMovesList;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
public void setId(int i ){
|
||||||
|
id = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,4 +9,8 @@ public class Random extends Player {
|
|||||||
java.util.Random rand = new java.util.Random();
|
java.util.Random rand = new java.util.Random();
|
||||||
return validMoves().get(rand.nextInt(validMoves().size()));
|
return validMoves().get(rand.nextInt(validMoves().size()));
|
||||||
}
|
}
|
||||||
|
@Override
|
||||||
|
public String toString(){
|
||||||
|
return "Random " +id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package battleship.view;
|
package battleship.view;
|
||||||
|
|
||||||
import battleship.model.Game;
|
import battleship.model.Game;
|
||||||
|
import battleship.model.Ship;
|
||||||
|
import battleship.utils.Pair;
|
||||||
import battleship.utils.Triplet;
|
import battleship.utils.Triplet;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -12,19 +14,22 @@ public class Terminal extends View {
|
|||||||
super(game);
|
super(game);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String setShip(Ship ship, int x, int y, Pair<Integer,Integer> direction){
|
||||||
|
String chain = "+ - - - - - - - - - - +\n";
|
||||||
|
return chain;
|
||||||
|
}
|
||||||
|
|
||||||
@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();
|
||||||
String chain = "A vous de joueur "+game.currentPlayer+"\n+ - - - - - - - - - - +\n";
|
String chain = "A vous de joueur "+game.currentPlayer.toString()+"\n+ - - - - - - - - - - +\n";
|
||||||
for(int i = 0; i<10;i++){
|
for(int i = 0; i<10;i++){
|
||||||
chain += "|";
|
chain += "|";
|
||||||
for(int y = 0;y<10;y++){
|
for(int y = 0;y<10;y++){
|
||||||
if(moves.isEmpty())
|
if(!moves.isEmpty()) {
|
||||||
chain += " _";
|
|
||||||
else {
|
|
||||||
for (Triplet<Integer, Integer, Boolean> ships : moves) {
|
for (Triplet<Integer, Integer, Boolean> ships : moves) {
|
||||||
if (i == ships.getLeft() && y == ships.getMiddle()) {
|
if (i == ships.getLeft() && y == ships.getMiddle()) {
|
||||||
if (ships.getRight() == true)
|
if (ships.getRight())
|
||||||
chain += " !";
|
chain += " !";
|
||||||
else
|
else
|
||||||
chain += " .";
|
chain += " .";
|
||||||
@ -32,8 +37,8 @@ public class Terminal extends View {
|
|||||||
}else
|
}else
|
||||||
chain += " _";
|
chain += " _";
|
||||||
}
|
}
|
||||||
}
|
}else
|
||||||
|
chain += " _";
|
||||||
}
|
}
|
||||||
chain += " |\n";
|
chain += " |\n";
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user