This commit is contained in:
Antonin Boyon 2021-03-30 10:16:40 +02:00
commit 77d0c0946e
5 changed files with 18 additions and 8 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

@ -2,6 +2,7 @@ package battleship.model.player;
import battleship.utils.Pair; import battleship.utils.Pair;
import battleship.utils.Triplet; import battleship.utils.Triplet;
import battleship.view.Terminal;
import java.util.Scanner; import java.util.Scanner;
@ -12,14 +13,13 @@ 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;
Scanner scanner = new Scanner(System.in); Scanner scanner = Terminal.scanner;
while(!areValid(x,y)) { while(!areValid(x,y)) {
System.out.println("Veuillez indiquer la coordonée x de votre coup"); System.out.println("Veuillez indiquer la coordonée x de votre coup");
x = scanner.nextInt(); x = scanner.nextInt();
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){

View File

@ -10,7 +10,7 @@ import java.util.Scanner;
public class Terminal extends View { public class Terminal extends View {
private Scanner scanner = new Scanner(System.in); public static Scanner scanner = new Scanner(System.in);
public Terminal(Game game) { public Terminal(Game game) {
super(game); super(game);

View File

@ -23,9 +23,9 @@ public abstract class View {
@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.toString()+ "\n+ - - - - - - - - - - +\n"; String chain = "A vous de joueur "+game.currentPlayer.toString()+ "\n+ A B C D E F G H I J +\n";
for(int i = 0; i<10;i++){ for(int i = 0; i<10;i++){
chain += "|"; chain += i+1;
for(int y = 0;y<10;y++){ for(int y = 0;y<10;y++){
if(!moves.isEmpty()) { if(!moves.isEmpty()) {
for (Triplet<Integer, Integer, Boolean> ships : moves) { for (Triplet<Integer, Integer, Boolean> ships : moves) {
@ -35,8 +35,10 @@ public abstract class View {
else else
chain += " ."; chain += " .";
}else }else
chain += " _"; chain += " _";
} }
}else }else
chain += " _"; chain += " _";

View File

@ -3,10 +3,18 @@ package battleship.view;
import battleship.model.Game; import battleship.model.Game;
import battleship.model.player.Player; import battleship.model.player.Player;
import javax.swing.*;
public class Window extends View { public class Window extends View {
private JFrame frame;
public Window(Game game) { public Window(Game game) {
super(game); super(game);
this.frame = new JFrame("Battleship");
frame.setSize(600,400);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
} }
@Override @Override