From de37c534c075a361369f7baf1a670fd6398be4c6 Mon Sep 17 00:00:00 2001 From: Arthur <78031901+Arthur7770@users.noreply.github.com> Date: Sat, 27 Mar 2021 14:56:01 +0100 Subject: [PATCH] Adding a few changes --- src/battleship/Main.java | 10 ++++---- src/battleship/model/Game.java | 8 ++----- src/battleship/model/Ship.java | 8 +++---- src/battleship/model/player/Human.java | 4 ++-- src/battleship/model/player/Player.java | 18 ++++++++++++-- src/battleship/model/player/Random.java | 8 ++++--- src/battleship/view/View.java | 32 +++++++++++++++---------- 7 files changed, 54 insertions(+), 34 deletions(-) diff --git a/src/battleship/Main.java b/src/battleship/Main.java index 803abc1..20ae9ee 100644 --- a/src/battleship/Main.java +++ b/src/battleship/Main.java @@ -23,7 +23,7 @@ public class Main { parseArgs(args); } catch (NoSuchElementException e) { System.out.println("Pas assez d'arguments, veuillez donner des arguments de cette forme:"); - System.out.println("java -jar battleship [nogui]"); + System.out.println("java -jar battleship [nogui]"); System.out.println(": paramètre obligatoire"); System.out.println("[param]: paramètre optionnel"); System.exit(2); @@ -38,7 +38,7 @@ public class Main { ArrayList>> playerClass = new ArrayList<>(2); playerClass.add(new Pair<>("human", Human.class)); playerClass.add(new Pair<>("random", Random.class)); - if(args.length >= 3) { + if(args.length >= 2) { for(int i = 0; i < 2; ++i) { for (Pair> pair : playerClass) { if(args[i].equalsIgnoreCase(pair.getLeft())) { @@ -46,10 +46,10 @@ public class Main { } } } - game = new Game(players,Integer.parseInt(args[2])); - if(args.length >= 4) { + game = new Game(players); + if(args.length >= 3) { // arguments > 3 ignorés - if(args[3].equalsIgnoreCase("nogui")) + if(args[2].equalsIgnoreCase("nogui")) view = new Terminal(game); else view = new Window(game); diff --git a/src/battleship/model/Game.java b/src/battleship/model/Game.java index f24f1d3..584356a 100644 --- a/src/battleship/model/Game.java +++ b/src/battleship/model/Game.java @@ -6,11 +6,9 @@ public class Game { public Player[] players; public Player currentPlayer; - public int size; - public Game(Player[] players,int size) { + public Game(Player[] players) { this.players = players; - this.size = size; } public Player getCurrentPlayer(){ @@ -20,9 +18,7 @@ public class Game { currentPlayer = (currentPlayer == players[1])? players[0] : players[1]; } public void checkDrownedShips(){ - changeCurrentPlayer(); - Player otherPlayer = currentPlayer; - changeCurrentPlayer(); + Player otherPlayer = (currentPlayer == players[1])? players[0] : players[1]; for(Ship ship : currentPlayer.getShips()){ if(!ship.hasDrown()) otherPlayer.isItDrown(ship); diff --git a/src/battleship/model/Ship.java b/src/battleship/model/Ship.java index 2f4b01c..680831c 100644 --- a/src/battleship/model/Ship.java +++ b/src/battleship/model/Ship.java @@ -8,16 +8,16 @@ public class Ship { private final int size; private final Pair direction; // (0,-1) bas // (0,1) haut // (1,0) droite // (-1,0) gauche - private Boolean isDrown; + private boolean isDrown; public Ship(Pair coords, int size,Pair direction) { this.coords = coords; this.size = size; this.direction = direction; - this.isDrown = Boolean.FALSE; + isDrown = false; } - public void isDrown(){ - isDrown = Boolean.TRUE; + public void setDrown(){ + isDrown = true; } public Boolean hasDrown(){ return isDrown; diff --git a/src/battleship/model/player/Human.java b/src/battleship/model/player/Human.java index 8d6e61f..1e29a9e 100644 --- a/src/battleship/model/player/Human.java +++ b/src/battleship/model/player/Human.java @@ -1,12 +1,12 @@ package battleship.model.player; -import battleship.utils.Triplet; +import battleship.utils.Pair; public class Human extends Player { @Override - public Triplet chooseMove() { + public Pair chooseMove() { return null; } } diff --git a/src/battleship/model/player/Player.java b/src/battleship/model/player/Player.java index 551bb34..ced1c1e 100644 --- a/src/battleship/model/player/Player.java +++ b/src/battleship/model/player/Player.java @@ -1,6 +1,7 @@ package battleship.model.player; import battleship.model.Ship; +import battleship.utils.Pair; import battleship.utils.Triplet; import java.util.ArrayList; @@ -41,7 +42,7 @@ public abstract class Player { } } if(cpt == ship.getSize()) { - ship.isDrown(); + ship.setDrown(); return Boolean.TRUE; } return Boolean.FALSE; @@ -53,7 +54,20 @@ public abstract class Player { return this.moves; } - public abstract Triplet chooseMove(); + public abstract Pair chooseMove(); + + public ArrayList> validMoves(){ + ArrayList> validMovesList = new ArrayList<>(); + for(Integer i = 0; i<10;i++){ + for(Integer y = 0;y<10;y++){ + if(!moves.contains(new Triplet<>(i,y,true)) ||!moves.contains(new Triplet<>(i,y,false))){ + validMovesList.add(new Pair<>(i,y)); + } + } + } + return null; + + } } diff --git a/src/battleship/model/player/Random.java b/src/battleship/model/player/Random.java index d0f1423..b5c6e14 100644 --- a/src/battleship/model/player/Random.java +++ b/src/battleship/model/player/Random.java @@ -1,11 +1,13 @@ package battleship.model.player; -import battleship.utils.Triplet; +import battleship.utils.Pair; public class Random extends Player{ @Override - public Triplet chooseMove() { - return null; + public Pair chooseMove() { + Random rand = new Random(); + int index = validMoves().get(rand.nextInt(validMoves().size())); + return validMoves().get(index); } } diff --git a/src/battleship/view/View.java b/src/battleship/view/View.java index 0f0e3c9..3026886 100644 --- a/src/battleship/view/View.java +++ b/src/battleship/view/View.java @@ -15,22 +15,30 @@ public abstract class View { } @Override public String toString(){ - ArrayList> player1 = game.players[0].getMoves(); - ArrayList> player2 = game.players[1].getMoves(); - String chain = ""; - for(int i = 0; i ships1 : player1){ - if(i == ships1.getLeft()&& y == ships1.getMiddle()){ - //chain += - } - } - for(Triplet ships2 : player2){ + + ArrayList> player = game.currentPlayer.getMoves(); + String chain = "A vous de joueur "+game.currentPlayer+"\n+ - - - - - - - - - - +"; + for(int i = 0; i<10;i++){ + chain += "|"; + for(int y = 0;y<10;y++){ + for(Triplet ships : player){ + if(i == ships.getLeft()&& y == ships.getMiddle()){ + if(ships.getRight() == true){ + chain += " !"; + } + else + chain += " ."; + + } + else + chain += " "; } + } + chain += " |\n"; } - // Not finished yet + chain += "+ - - - - - - - - - - +\n"; return chain; } }