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:
Arthur 2021-03-28 13:55:59 +02:00
parent c45d8a93ae
commit be296d3946
6 changed files with 98 additions and 15 deletions

View File

@ -5,16 +5,27 @@ import battleship.utils.Pair;
import battleship.utils.Triplet;
import battleship.view.View;
import java.util.Scanner;
public class Game {
public Player[] players;
public Player currentPlayer;
private int[] ships = new int[5];
public Game(Player[] players) {
this.players = players;
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(){
return this.currentPlayer;
}
@ -65,7 +76,45 @@ public class Game {
}
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){
setShips(players[0]);

View File

@ -4,9 +4,9 @@ import battleship.utils.*;
public class Ship {
private final Pair<Integer, Integer> coords;
private Pair<Integer, Integer> coords;
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
private boolean isDrown;
@ -16,6 +16,14 @@ public class Ship {
this.direction = direction;
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(){
isDrown = true;
}

View File

@ -8,6 +8,7 @@ import java.util.Scanner;
public class Human extends Player {
@Override
public Pair<Integer,Integer> chooseMove() {
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");
y = scanner.nextInt();
}
scanner.close();
return new Pair<>(x,y);
}
public boolean areValid(int x,int y){
@ -29,4 +31,9 @@ public class Human extends Player {
}
return true;
}
@Override
public String toString(){
return "Human " +id;
}
}

View File

@ -5,19 +5,26 @@ import battleship.utils.Pair;
import battleship.utils.Triplet;
import java.util.ArrayList;
import java.util.Arrays;
public abstract class Player {
protected ArrayList<Ship> ships = new ArrayList<>();
protected ArrayList<Triplet<Integer,Integer,Boolean>> moves = new ArrayList<>();
protected int id;
public Player(){
setShips();
}
public void setShips(Ship... ships){
this.ships.addAll(Arrays.asList(ships));
public boolean setShips(Ship ship){
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;
}
public void setId(int i ){
id = i;
}
}

View File

@ -9,4 +9,8 @@ public class Random extends Player {
java.util.Random rand = new java.util.Random();
return validMoves().get(rand.nextInt(validMoves().size()));
}
@Override
public String toString(){
return "Random " +id;
}
}

View File

@ -1,6 +1,8 @@
package battleship.view;
import battleship.model.Game;
import battleship.model.Ship;
import battleship.utils.Pair;
import battleship.utils.Triplet;
import java.util.ArrayList;
@ -12,19 +14,22 @@ public class Terminal extends View {
super(game);
}
public String setShip(Ship ship, int x, int y, Pair<Integer,Integer> direction){
String chain = "+ - - - - - - - - - - +\n";
return chain;
}
@Override
public String toString(){
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++){
chain += "|";
for(int y = 0;y<10;y++){
if(moves.isEmpty())
chain += " _";
else {
if(!moves.isEmpty()) {
for (Triplet<Integer, Integer, Boolean> ships : moves) {
if (i == ships.getLeft() && y == ships.getMiddle()) {
if (ships.getRight() == true)
if (ships.getRight())
chain += " !";
else
chain += " .";
@ -32,8 +37,8 @@ public class Terminal extends View {
}else
chain += " _";
}
}
}else
chain += " _";
}
chain += " |\n";
}