From c45d8a93ae55854d8e5add47e57ad4ecc928c752 Mon Sep 17 00:00:00 2001 From: Arthur <78031901+Arthur7770@users.noreply.github.com> Date: Sat, 27 Mar 2021 19:43:36 +0100 Subject: [PATCH] Adding some modifications + please if you find ho to fix Terminal.java toString() method, do it --- src/battleship/Main.java | 1 + src/battleship/model/Game.java | 55 +++++++++++++++++++++++++ src/battleship/model/Ship.java | 11 +++++ src/battleship/model/player/Human.java | 22 +++++++++- src/battleship/model/player/Player.java | 2 +- src/battleship/model/player/Random.java | 1 - src/battleship/view/Terminal.java | 33 +++++++++++++++ src/battleship/view/View.java | 34 +-------------- src/battleship/view/Window.java | 5 +++ 9 files changed, 129 insertions(+), 35 deletions(-) diff --git a/src/battleship/Main.java b/src/battleship/Main.java index 20ae9ee..2de0852 100644 --- a/src/battleship/Main.java +++ b/src/battleship/Main.java @@ -31,6 +31,7 @@ public class Main { e.printStackTrace(); System.exit(1); } + game.Play(view); } private static void parseArgs(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { diff --git a/src/battleship/model/Game.java b/src/battleship/model/Game.java index 584356a..55aa1f9 100644 --- a/src/battleship/model/Game.java +++ b/src/battleship/model/Game.java @@ -1,6 +1,9 @@ package battleship.model; import battleship.model.player.Player; +import battleship.utils.Pair; +import battleship.utils.Triplet; +import battleship.view.View; public class Game { @@ -9,6 +12,7 @@ public class Game { public Game(Player[] players) { this.players = players; + this.currentPlayer = players[0]; } public Player getCurrentPlayer(){ @@ -24,6 +28,57 @@ public class Game { otherPlayer.isItDrown(ship); } } + public Player getWinner(){ + int cpt = 0; + for(Ship ship : players[0].getShips()){ + if(!ship.hasDrown()) + break; + else + cpt ++; + } + if(cpt == 5) + return players[1]; + cpt = 0; + for(Ship ship : players[1].getShips()){ + if(!ship.hasDrown()) + break; + else + cpt ++; + } + if(cpt == 5) + return players[0]; + return null; + + } + public void move(Pair move){ + boolean bool = false; + Player player = (currentPlayer == players[1])? players[0] : players[1]; + for (Ship ship : player.getShips()) { + for(Pair pair : ship.getCoordsArray()){ + if ((pair.getRight().equals(move.getRight())) && (pair.getLeft().equals(move.getLeft()))) { + bool = true; + break; + } + } + } + currentPlayer.addMove(new Triplet<>(move.getLeft(),move.getRight(),bool)); + + } + public void setShips(Player player){ + //TODO a method that place the ships according to the decision of the players + } + public Player Play(View view){ + setShips(players[0]); + setShips(players[1]); + while(getWinner() == null){ + System.out.println(view); + Pair move = currentPlayer.chooseMove(); + move(move); + changeCurrentPlayer(); + checkDrownedShips(); + } + return getWinner(); + } } diff --git a/src/battleship/model/Ship.java b/src/battleship/model/Ship.java index 680831c..8e3a25b 100644 --- a/src/battleship/model/Ship.java +++ b/src/battleship/model/Ship.java @@ -31,5 +31,16 @@ public class Ship { public Pair getCoords(){ return this.coords; } + public Pair[] getCoordsArray(){ + Pair[] tab = new Pair[size]; + for(int i = 0; i(coords.getLeft()+i* direction.getLeft(),coords.getRight()+i* direction.getRight()); + } + } + return tab; + + + } } diff --git a/src/battleship/model/player/Human.java b/src/battleship/model/player/Human.java index 1e29a9e..882014d 100644 --- a/src/battleship/model/player/Human.java +++ b/src/battleship/model/player/Human.java @@ -1,12 +1,32 @@ package battleship.model.player; import battleship.utils.Pair; +import battleship.utils.Triplet; + +import java.util.Scanner; public class Human extends Player { @Override public Pair chooseMove() { - return null; + int x = -1, y = -1; + Scanner scanner = new Scanner(System.in); + 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(); + } + return new Pair<>(x,y); + } + public boolean areValid(int x,int y){ + if(x <0 || x >10 || y <0 || y >10) + return false; + for(Triplet move : moves){ + if((move.getLeft() == x) && (move.getMiddle() == y) ) + return false; + } + return true; } } diff --git a/src/battleship/model/player/Player.java b/src/battleship/model/player/Player.java index ced1c1e..e622df2 100644 --- a/src/battleship/model/player/Player.java +++ b/src/battleship/model/player/Player.java @@ -65,7 +65,7 @@ public abstract class Player { } } } - return null; + return validMovesList; } diff --git a/src/battleship/model/player/Random.java b/src/battleship/model/player/Random.java index e623826..7342e8d 100644 --- a/src/battleship/model/player/Random.java +++ b/src/battleship/model/player/Random.java @@ -7,7 +7,6 @@ public class Random extends Player { @Override public Pair chooseMove() { java.util.Random rand = new java.util.Random(); - // Pair index = validMoves().get(rand.nextInt(validMoves().size())); return validMoves().get(rand.nextInt(validMoves().size())); } } diff --git a/src/battleship/view/Terminal.java b/src/battleship/view/Terminal.java index db2adff..d4726dc 100644 --- a/src/battleship/view/Terminal.java +++ b/src/battleship/view/Terminal.java @@ -1,6 +1,9 @@ package battleship.view; import battleship.model.Game; +import battleship.utils.Triplet; + +import java.util.ArrayList; public class Terminal extends View { @@ -8,4 +11,34 @@ public class Terminal extends View { public Terminal(Game game) { super(game); } + + @Override + public String toString(){ + ArrayList> moves = game.currentPlayer.getMoves(); + String chain = "A vous de joueur "+game.currentPlayer+"\n+ - - - - - - - - - - +\n"; + for(int i = 0; i<10;i++){ + chain += "|"; + for(int y = 0;y<10;y++){ + if(moves.isEmpty()) + chain += " _"; + else { + for (Triplet ships : moves) { + if (i == ships.getLeft() && y == ships.getMiddle()) { + if (ships.getRight() == true) + chain += " !"; + else + chain += " ."; + + } else + chain += " _"; + } + } + + } + chain += " |\n"; + } + chain += "+ - - - - - - - - - - +\n"; + return chain; + } + } diff --git a/src/battleship/view/View.java b/src/battleship/view/View.java index 3026886..aef0387 100644 --- a/src/battleship/view/View.java +++ b/src/battleship/view/View.java @@ -1,44 +1,14 @@ package battleship.view; import battleship.model.Game; -import battleship.utils.Triplet; - -import java.util.ArrayList; public abstract class View { - private final Game game; + protected final Game game; public View(Game game) { this.game = game; } - @Override - public String toString(){ - - - 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"; - } - chain += "+ - - - - - - - - - - +\n"; - return chain; - } + public abstract String toString(); } diff --git a/src/battleship/view/Window.java b/src/battleship/view/Window.java index d0ff33f..666246c 100644 --- a/src/battleship/view/Window.java +++ b/src/battleship/view/Window.java @@ -7,4 +7,9 @@ public class Window extends View { public Window(Game game) { super(game); } + + @Override + public String toString() { + return null; + } }