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){
view.setShips(players[0]);
view.setShips(players[1]);
//view.setShips(players[0]);
//view.setShips(players[1]);
while(getWinner() == null) {
System.out.println(view);
Pair<Integer,Integer> move = currentPlayer.chooseMove();

View File

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

View File

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

View File

@ -23,20 +23,22 @@ public abstract class View {
@Override
public String toString() {
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++){
chain += "|";
chain += i+1;
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())
if(ships.getRight())
chain += " !";
else
chain += " .";
}else
chain += " _";
}
}else
chain += " _";

View File

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