Adding some modifications + please if you find ho to fix Terminal.java toString() method, do it

This commit is contained in:
Arthur 2021-03-27 19:43:36 +01:00
parent 1ffc7ded74
commit c45d8a93ae
9 changed files with 129 additions and 35 deletions

View File

@ -31,6 +31,7 @@ public class Main {
e.printStackTrace(); e.printStackTrace();
System.exit(1); System.exit(1);
} }
game.Play(view);
} }
private static void parseArgs(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { private static void parseArgs(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {

View File

@ -1,6 +1,9 @@
package battleship.model; package battleship.model;
import battleship.model.player.Player; import battleship.model.player.Player;
import battleship.utils.Pair;
import battleship.utils.Triplet;
import battleship.view.View;
public class Game { public class Game {
@ -9,6 +12,7 @@ public class Game {
public Game(Player[] players) { public Game(Player[] players) {
this.players = players; this.players = players;
this.currentPlayer = players[0];
} }
public Player getCurrentPlayer(){ public Player getCurrentPlayer(){
@ -24,6 +28,57 @@ public class Game {
otherPlayer.isItDrown(ship); 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<Integer,Integer> move){
boolean bool = false;
Player player = (currentPlayer == players[1])? players[0] : players[1];
for (Ship ship : player.getShips()) {
for(Pair<Integer,Integer> 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<Integer,Integer> move = currentPlayer.chooseMove();
move(move);
changeCurrentPlayer();
checkDrownedShips();
}
return getWinner();
}
} }

View File

@ -31,5 +31,16 @@ public class Ship {
public Pair<Integer,Integer> getCoords(){ public Pair<Integer,Integer> getCoords(){
return this.coords; return this.coords;
} }
public Pair<Integer, Integer>[] getCoordsArray(){
Pair<Integer,Integer>[] tab = new Pair[size];
for(int i = 0; i<size;i++){
for(int y = 0;y<size;y++){
tab[i] = new Pair<>(coords.getLeft()+i* direction.getLeft(),coords.getRight()+i* direction.getRight());
}
}
return tab;
}
} }

View File

@ -1,12 +1,32 @@
package battleship.model.player; package battleship.model.player;
import battleship.utils.Pair; import battleship.utils.Pair;
import battleship.utils.Triplet;
import java.util.Scanner;
public class Human extends Player { public class Human extends Player {
@Override @Override
public Pair<Integer,Integer> chooseMove() { public Pair<Integer,Integer> 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<Integer,Integer,Boolean> move : moves){
if((move.getLeft() == x) && (move.getMiddle() == y) )
return false;
}
return true;
} }
} }

View File

@ -65,7 +65,7 @@ public abstract class Player {
} }
} }
} }
return null; return validMovesList;
} }

View File

@ -7,7 +7,6 @@ public class Random extends Player {
@Override @Override
public Pair<Integer,Integer> chooseMove() { public Pair<Integer,Integer> chooseMove() {
java.util.Random rand = new java.util.Random(); java.util.Random rand = new java.util.Random();
// Pair<Integer, Integer> index = validMoves().get(rand.nextInt(validMoves().size()));
return validMoves().get(rand.nextInt(validMoves().size())); return validMoves().get(rand.nextInt(validMoves().size()));
} }
} }

View File

@ -1,6 +1,9 @@
package battleship.view; package battleship.view;
import battleship.model.Game; import battleship.model.Game;
import battleship.utils.Triplet;
import java.util.ArrayList;
public class Terminal extends View { public class Terminal extends View {
@ -8,4 +11,34 @@ public class Terminal extends View {
public Terminal(Game game) { public Terminal(Game game) {
super(game); super(game);
} }
@Override
public String toString(){
ArrayList<Triplet<Integer,Integer,Boolean>> 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<Integer, Integer, Boolean> ships : moves) {
if (i == ships.getLeft() && y == ships.getMiddle()) {
if (ships.getRight() == true)
chain += " !";
else
chain += " .";
} else
chain += " _";
}
}
}
chain += " |\n";
}
chain += "+ - - - - - - - - - - +\n";
return chain;
}
} }

View File

@ -1,44 +1,14 @@
package battleship.view; package battleship.view;
import battleship.model.Game; import battleship.model.Game;
import battleship.utils.Triplet;
import java.util.ArrayList;
public abstract class View { public abstract class View {
private final Game game; protected final Game game;
public View(Game game) { public View(Game game) {
this.game = game; this.game = game;
} }
@Override public abstract String toString();
public String toString(){
ArrayList<Triplet<Integer,Integer,Boolean>> 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<Integer, Integer, Boolean> ships : player){
if(i == ships.getLeft()&& y == ships.getMiddle()){
if(ships.getRight() == true){
chain += " !";
}
else
chain += " .";
}
else
chain += " ";
}
}
chain += " |\n";
}
chain += "+ - - - - - - - - - - +\n";
return chain;
}
} }

View File

@ -7,4 +7,9 @@ public class Window extends View {
public Window(Game game) { public Window(Game game) {
super(game); super(game);
} }
@Override
public String toString() {
return null;
}
} }