From 33ab1c131af72cb145f41f6dec357cbd580afae1 Mon Sep 17 00:00:00 2001 From: Arthur <78031901+Arthur7770@users.noreply.github.com> Date: Sat, 27 Mar 2021 13:12:33 +0100 Subject: [PATCH] Adding getters to Ship.class, Adding a checker in Game.class and Starting the toString() method of View.class --- src/battleship/Main.java | 10 ++++----- src/battleship/model/Game.java | 23 +++++++++++++++++++- src/battleship/model/Ship.java | 28 +++++++++++++++++++++---- src/battleship/model/player/Human.java | 1 + src/battleship/model/player/Player.java | 26 +++++++++++++++++++++++ src/battleship/model/player/Random.java | 1 + src/battleship/view/View.java | 23 ++++++++++++++++++++ 7 files changed, 102 insertions(+), 10 deletions(-) diff --git a/src/battleship/Main.java b/src/battleship/Main.java index 20ae9ee..803abc1 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 >= 2) { + if(args.length >= 3) { 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); - if(args.length >= 3) { + game = new Game(players,Integer.parseInt(args[2])); + if(args.length >= 4) { // arguments > 3 ignorés - if(args[2].equalsIgnoreCase("nogui")) + if(args[3].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 51a036f..f24f1d3 100644 --- a/src/battleship/model/Game.java +++ b/src/battleship/model/Game.java @@ -5,8 +5,29 @@ import battleship.model.player.Player; public class Game { public Player[] players; + public Player currentPlayer; + public int size; - public Game(Player[] players) { + public Game(Player[] players,int size) { this.players = players; + this.size = size; } + + public Player getCurrentPlayer(){ + return this.currentPlayer; + } + public void changeCurrentPlayer(){ + currentPlayer = (currentPlayer == players[1])? players[0] : players[1]; + } + public void checkDrownedShips(){ + changeCurrentPlayer(); + Player otherPlayer = currentPlayer; + changeCurrentPlayer(); + 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 675c79f..2f4b01c 100644 --- a/src/battleship/model/Ship.java +++ b/src/battleship/model/Ship.java @@ -1,15 +1,35 @@ package battleship.model; -import battleship.utils.Pair; +import battleship.utils.*; public class Ship { - public Pair coords; - public int size; + private final Pair coords; + private final int size; + private final Pair direction; + // (0,-1) bas // (0,1) haut // (1,0) droite // (-1,0) gauche + private Boolean isDrown; - public Ship(Pair coords, int size) { + public Ship(Pair coords, int size,Pair direction) { this.coords = coords; this.size = size; + this.direction = direction; + this.isDrown = Boolean.FALSE; + } + public void isDrown(){ + isDrown = Boolean.TRUE; + } + public Boolean hasDrown(){ + return isDrown; + } + public int getSize(){ + return this.size; + } + public Pair getDirection(){ + return this.direction; + } + public Pair getCoords(){ + return this.coords; } } diff --git a/src/battleship/model/player/Human.java b/src/battleship/model/player/Human.java index b3fd5f6..8d6e61f 100644 --- a/src/battleship/model/player/Human.java +++ b/src/battleship/model/player/Human.java @@ -4,6 +4,7 @@ import battleship.utils.Triplet; public class Human extends Player { + @Override public Triplet chooseMove() { return null; diff --git a/src/battleship/model/player/Player.java b/src/battleship/model/player/Player.java index f2f2925..551bb34 100644 --- a/src/battleship/model/player/Player.java +++ b/src/battleship/model/player/Player.java @@ -28,6 +28,32 @@ public abstract class Player { moves.add(move); return this; } + public Boolean isItDrown(Ship ship){ + int cpt = 0; + for(Triplet move : moves){ + for(int i =1;i<=ship.getSize();i++){ + int x = ship.getCoords().getLeft()+i*ship.getDirection().getLeft(); + int y = ship.getCoords().getRight()+i*ship.getDirection().getRight(); + if((move.getLeft() == x) && (move.getMiddle() == y)){ + cpt += 1; + break; + } + } + } + if(cpt == ship.getSize()) { + ship.isDrown(); + return Boolean.TRUE; + } + return Boolean.FALSE; + } + public ArrayList getShips(){ + return this.ships; + } + public ArrayList> getMoves(){ + return this.moves; + } public abstract Triplet chooseMove(); + + } diff --git a/src/battleship/model/player/Random.java b/src/battleship/model/player/Random.java index d8651c8..d0f1423 100644 --- a/src/battleship/model/player/Random.java +++ b/src/battleship/model/player/Random.java @@ -3,6 +3,7 @@ package battleship.model.player; import battleship.utils.Triplet; public class Random extends Player{ + @Override public Triplet chooseMove() { return null; diff --git a/src/battleship/view/View.java b/src/battleship/view/View.java index 760acca..0f0e3c9 100644 --- a/src/battleship/view/View.java +++ b/src/battleship/view/View.java @@ -1,6 +1,9 @@ package battleship.view; import battleship.model.Game; +import battleship.utils.Triplet; + +import java.util.ArrayList; public abstract class View { @@ -10,4 +13,24 @@ public abstract class View { public View(Game game) { this.game = game; } + @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){ + + } + } + } + // Not finished yet + return chain; + } }