Fix toString not displaying ships (but need debugging)
This commit is contained in:
parent
eddd1940f8
commit
3919fcfea8
@ -23,6 +23,10 @@ public class Game {
|
|||||||
return this.currentPlayer;
|
return this.currentPlayer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Player getOtherPlayer() {
|
||||||
|
return this.currentPlayer == players[0] ? players[1] : players[0];
|
||||||
|
}
|
||||||
|
|
||||||
public void changeCurrentPlayer(){
|
public void changeCurrentPlayer(){
|
||||||
currentPlayer = (currentPlayer == players[1])? players[0] : players[1];
|
currentPlayer = (currentPlayer == players[1])? players[0] : players[1];
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ public abstract class Player {
|
|||||||
protected int id;
|
protected int id;
|
||||||
protected final int[] bato = { 5, 4, 3, 3, 2};
|
protected final int[] bato = { 5, 4, 3, 3, 2};
|
||||||
|
|
||||||
public boolean setShips(Ship ship){
|
public boolean setShips(Ship ship) {
|
||||||
for(int i = 0; i < ship.getSize(); i++){
|
for(int i = 0; i < ship.getSize(); i++){
|
||||||
int x = ship.getCoords().getLeft() + i * ship.getDirection().getDirection().getLeft();
|
int x = ship.getCoords().getLeft() + i * ship.getDirection().getDirection().getLeft();
|
||||||
int y = ship.getCoords().getRight()+ i * ship.getDirection().getDirection().getRight();
|
int y = ship.getCoords().getRight()+ i * ship.getDirection().getDirection().getRight();
|
||||||
@ -44,7 +44,7 @@ public abstract class Player {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isItDrown(Ship ship){
|
public boolean isItDrown(Ship ship) {
|
||||||
int cpt = 0;
|
int cpt = 0;
|
||||||
for(Triplet<Integer,Integer,Boolean> move : moves){
|
for(Triplet<Integer,Integer,Boolean> move : moves){
|
||||||
for(int i = 1; i <= ship.getSize(); i++){
|
for(int i = 1; i <= ship.getSize(); i++){
|
||||||
@ -75,7 +75,7 @@ public abstract class Player {
|
|||||||
public ArrayList<Pair<Integer,Integer>> validMoves() {
|
public ArrayList<Pair<Integer,Integer>> validMoves() {
|
||||||
ArrayList<Pair<Integer,Integer>> validMovesList = new ArrayList<>();
|
ArrayList<Pair<Integer,Integer>> validMovesList = new ArrayList<>();
|
||||||
for(Integer i = 0; i<10;i++){
|
for(Integer i = 0; i<10;i++){
|
||||||
for(Integer y = 0;y<10;y++){
|
for(Integer y = 0;y<10;y++) {
|
||||||
if(!moves.contains(new Triplet<>(i,y,true)) ||!moves.contains(new Triplet<>(i,y,false))){
|
if(!moves.contains(new Triplet<>(i,y,true)) ||!moves.contains(new Triplet<>(i,y,false))){
|
||||||
validMovesList.add(new Pair<>(i,y));
|
validMovesList.add(new Pair<>(i,y));
|
||||||
}
|
}
|
||||||
@ -95,8 +95,8 @@ public abstract class Player {
|
|||||||
public void placeShipRandomly() {
|
public void placeShipRandomly() {
|
||||||
Random rand = new Random();
|
Random rand = new Random();
|
||||||
for(int i : bato) {
|
for(int i : bato) {
|
||||||
Ship ship = null;
|
Ship ship = new Ship(new Pair<>(-1, -1), i, Direction.DEFAULT);
|
||||||
while(ship == null || !setShips(ship)) {
|
while(!setShips(ship)) {
|
||||||
ship = new Ship(new Pair<>(rand.nextInt(10), rand.nextInt(10)), i, Direction.values()[rand.nextInt(Direction.values().length)]);
|
ship = new Ship(new Pair<>(rand.nextInt(10), rand.nextInt(10)), i, Direction.values()[rand.nextInt(Direction.values().length)]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,13 @@
|
|||||||
package battleship.view;
|
package battleship.view;
|
||||||
|
|
||||||
import battleship.model.Direction;
|
|
||||||
import battleship.model.Game;
|
import battleship.model.Game;
|
||||||
import battleship.model.Ship;
|
import battleship.model.Ship;
|
||||||
import battleship.model.player.Player;
|
import battleship.model.player.Player;
|
||||||
import battleship.utils.Pair;
|
import battleship.utils.Pair;
|
||||||
import battleship.utils.Triplet;
|
import battleship.utils.Triplet;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Random;
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
|
||||||
|
|
||||||
public abstract class View {
|
public abstract class View {
|
||||||
|
|
||||||
@ -23,31 +21,36 @@ public abstract class View {
|
|||||||
|
|
||||||
public abstract void setShips(Player player);
|
public abstract void setShips(Player player);
|
||||||
|
|
||||||
public abstract void displayBoard();
|
public abstract void displayBoard(/*Graphics g*/);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
// String chain = "A vous de joueur "+game.currentPlayer.toString()+ "\n+ - - - - - - - - - - +\n";
|
// String chain = "A vous de joueur "+game.currentPlayer.toString()+ "\n+ - - - - - - - - - - +\n";
|
||||||
String chain = "";
|
String chain = "";
|
||||||
for(int u = 0; u < 2; ++u) {
|
for(int u = 0; u < 2; ++u) {
|
||||||
ArrayList<Triplet<Integer,Integer,Boolean>> moves = game.players[u].getMoves();
|
Player player = game.players[u];
|
||||||
|
ArrayList<Ship> ships = game.players[u].getShips();
|
||||||
chain += "Player " + (u + 1) + " :\n";
|
chain += "Player " + (u + 1) + " :\n";
|
||||||
chain += "+ - - - - - - - - - - +\n";
|
chain += "+ - - - - - - - - - - +\n";
|
||||||
for(int i = 0; i < 10;++i) {
|
for(int i = 0; i < 10;++i) {
|
||||||
chain += "|";
|
chain += "|";
|
||||||
for(int y = 0;y < 10; ++y) {
|
for(int y = 0;y < 10; ++y) {
|
||||||
if(!moves.isEmpty()) {
|
Pair<Integer, Integer> pair = new Pair<>(i, y);
|
||||||
for(Triplet<Integer, Integer, Boolean> move : moves) {
|
if(!ships.isEmpty()) {
|
||||||
if(move.getLeft() == i && move.getMiddle() == y) {
|
boolean isPosition = false;
|
||||||
if (move.getRight())
|
for(Ship ship : ships) {
|
||||||
|
if(isShipPosition(ship, pair)) {
|
||||||
|
isPosition = true;
|
||||||
|
if(isPositionDrowned(game.players[u == 0 ? 1 : 0], ship, pair)) {
|
||||||
chain += " !";
|
chain += " !";
|
||||||
else
|
|
||||||
chain += " .";
|
|
||||||
} else {
|
} else {
|
||||||
chain += " _";
|
chain += " .";
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if(!isPosition)
|
||||||
|
chain += " _";
|
||||||
} else {
|
} else {
|
||||||
chain += " _";
|
chain += " _";
|
||||||
}
|
}
|
||||||
@ -60,4 +63,31 @@ public abstract class View {
|
|||||||
return chain;
|
return chain;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isShipPosition(Ship ship, Pair<Integer, Integer> pair) {
|
||||||
|
if(ship.getCoords().equals(pair))
|
||||||
|
return true;
|
||||||
|
for(int a = 0; a < ship.getSize(); ++a) {
|
||||||
|
if(new Pair<>(ship.getCoords().getLeft() + a * ship.getDirection().getDirection().getLeft(), ship.getCoords().getRight() + a * ship.getDirection().getDirection().getRight()).equals(pair)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private boolean isPositionDrowned(Player other, Pair<Integer, Integer> pair) {
|
||||||
|
for(Triplet<Integer, Integer, Boolean> move : other.getMoves()) {
|
||||||
|
if(move.getRight() && pair.getLeft().equals(move.getLeft()) && pair.getRight().equals(move.getMiddle())) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isPositionDrowned(Player other, Ship ship, Pair<Integer, Integer> pair) {
|
||||||
|
if(ship.isDrown())
|
||||||
|
return true;
|
||||||
|
return isPositionDrowned(other, pair);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,9 +3,8 @@ package battleship.view;
|
|||||||
import battleship.model.Game;
|
import battleship.model.Game;
|
||||||
import battleship.model.player.Player;
|
import battleship.model.player.Player;
|
||||||
|
|
||||||
import java.awt.Graphics;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
public class Window extends View {
|
public class Window extends View {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user