Adding some modifications + please if you find ho to fix Terminal.java toString() method, do it
This commit is contained in:
parent
1ffc7ded74
commit
c45d8a93ae
@ -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 {
|
||||
|
@ -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<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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -31,5 +31,16 @@ public class Ship {
|
||||
public Pair<Integer,Integer> getCoords(){
|
||||
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;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<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;
|
||||
}
|
||||
}
|
||||
|
@ -65,7 +65,7 @@ public abstract class Player {
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return validMovesList;
|
||||
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,6 @@ public class Random extends Player {
|
||||
@Override
|
||||
public Pair<Integer,Integer> chooseMove() {
|
||||
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()));
|
||||
}
|
||||
}
|
||||
|
@ -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<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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<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;
|
||||
}
|
||||
public abstract String toString();
|
||||
}
|
||||
|
@ -7,4 +7,9 @@ public class Window extends View {
|
||||
public Window(Game game) {
|
||||
super(game);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user