Adding a few changes
This commit is contained in:
parent
33ab1c131a
commit
de37c534c0
@ -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 <Human/Random> <Human/Random> <Int/Taille de la grille> [nogui]");
|
||||
System.out.println("java -jar battleship <Human/Random> <Human/Random> [nogui]");
|
||||
System.out.println("<param>: paramètre obligatoire");
|
||||
System.out.println("[param]: paramètre optionnel");
|
||||
System.exit(2);
|
||||
@ -38,7 +38,7 @@ public class Main {
|
||||
ArrayList<Pair<String, Class<? extends Player>>> 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<String, Class<? extends Player>> 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);
|
||||
|
@ -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);
|
||||
|
@ -8,16 +8,16 @@ public class Ship {
|
||||
private final int size;
|
||||
private final Pair<Integer,Integer> direction;
|
||||
// (0,-1) bas // (0,1) haut // (1,0) droite // (-1,0) gauche
|
||||
private Boolean isDrown;
|
||||
private boolean isDrown;
|
||||
|
||||
public Ship(Pair<Integer, Integer> coords, int size,Pair<Integer,Integer> 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;
|
||||
|
@ -1,12 +1,12 @@
|
||||
package battleship.model.player;
|
||||
|
||||
import battleship.utils.Triplet;
|
||||
import battleship.utils.Pair;
|
||||
|
||||
public class Human extends Player {
|
||||
|
||||
|
||||
@Override
|
||||
public Triplet<Integer,Integer,Boolean> chooseMove() {
|
||||
public Pair<Integer,Integer> chooseMove() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -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<Integer,Integer,Boolean> chooseMove();
|
||||
public abstract Pair<Integer,Integer> chooseMove();
|
||||
|
||||
public ArrayList<Pair<Integer,Integer>> validMoves(){
|
||||
ArrayList<Pair<Integer,Integer>> 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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,11 +1,13 @@
|
||||
package battleship.model.player;
|
||||
|
||||
import battleship.utils.Triplet;
|
||||
import battleship.utils.Pair;
|
||||
|
||||
public class Random extends Player{
|
||||
|
||||
@Override
|
||||
public Triplet<Integer,Integer,Boolean> chooseMove() {
|
||||
return null;
|
||||
public Pair<Integer,Integer> chooseMove() {
|
||||
Random rand = new Random();
|
||||
int index = validMoves().get(rand.nextInt(validMoves().size()));
|
||||
return validMoves().get(index);
|
||||
}
|
||||
}
|
||||
|
@ -15,22 +15,30 @@ public abstract class View {
|
||||
}
|
||||
@Override
|
||||
public String toString(){
|
||||
ArrayList<Triplet<Integer,Integer,Boolean>> player1 = game.players[0].getMoves();
|
||||
ArrayList<Triplet<Integer,Integer,Boolean>> player2 = game.players[1].getMoves();
|
||||
String chain = "";
|
||||
for(int i = 0; i<game.size;i++){
|
||||
for(int y = 0;y<game.size;y++){
|
||||
for(Triplet<Integer, Integer, Boolean> ships1 : player1){
|
||||
if(i == ships1.getLeft()&& y == ships1.getMiddle()){
|
||||
//chain +=
|
||||
|
||||
|
||||
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 += " !";
|
||||
}
|
||||
}
|
||||
for(Triplet<Integer, Integer, Boolean> ships2 : player2){
|
||||
else
|
||||
chain += " .";
|
||||
|
||||
}
|
||||
else
|
||||
chain += " ";
|
||||
}
|
||||
|
||||
}
|
||||
// Not finished yet
|
||||
chain += " |\n";
|
||||
}
|
||||
chain += "+ - - - - - - - - - - +\n";
|
||||
return chain;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user