Skip to content

Commit

Permalink
Deplacement isCollision et nbrMaxRoom dans strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinBeauchet committed Mar 19, 2021
1 parent 2f45f8c commit f1407b8
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 188 deletions.
2 changes: 1 addition & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import Model.Entitys.BasicPlayer;import Model.Map.Cell;import Model.Map.Etage;import Model.Map.Map;import Model.Utils.Affichage;import Model.Utils.Position;import Model.Utils.Procedure;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.Random;public class Main { private static long seed; private static Map map; private static BasicPlayer player; private static Etage etage; public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); init(reader); testAstar(); affichage(); System.getProperty("os.name"); while (true) { Tour(reader.readLine()); } } public static void testAstar() { Position posPlayer = player.getPosition(); Position posTarget = Procedure.getAccesibleRandomPosition(false, etage); ArrayList<Position> chemin = etage.Astar(posPlayer, posTarget, Etage.PathType.DIAGONAL); for (Position p : chemin) { etage.Cells.get(p.getY()).get(p.getX()).updateCell(true, Cell.CellType.PATH); } System.out.println("Taille chemin : " + chemin.size()); etage.Cells.get(posTarget.getY()).get(posTarget.getX()).updateCell(true, Cell.CellType.TRAP_ROOM); } private static void init(BufferedReader reader) throws IOException { System.out.print("Numero seed ? "); long seed_value; try{ seed_value = Long.parseLong(reader.readLine()); } catch (NumberFormatException e){ seed_value =new Random().nextLong(); } seed=seed_value; Procedure.setSeed(seed); map = new Map(); etage = map.getCurrent(); player = map.getPlayer(); } private static void Tour(String input){ switch (input) { case "z" , "\u001B[A" -> player.moveUp(); case "q" , "\u001B[D" -> player.moveLeft(); case "s" , "\u001B[B" -> player.moveDown(); case "d" , "\u001B[C" -> player.moveRight(); case "exit" -> System.exit(0); default -> System.out.println("Wrong key:"+input); } switch (etage.get(map.getPlayer().getPosition()).getType()){ case UP : map.UP(); break; case DOWN : map.DOWN(); break; case TRAP_ROOM : map.TRAP_ROOM(); break; default: break; } etage=map.getCurrent(); affichage(); } private static void affichage(){ System.out.print(Affichage.CLEAR); System.out.println(seed); System.out.println(etage); System.out.print(Affichage.RESET+"Etage n°"+(map.getIndexCurrent()+1)); System.out.println("\n\n\n\n\n\n\n\n\n\n\n"); System.out.print("Enter your key: "); }}
import Model.Entitys.BasicPlayer;import Model.Map.Cell;import Model.Map.Etage;import Model.Map.Map;import Model.Utils.Affichage;import Model.Utils.Position;import Model.Utils.Procedure;import Model.Utils.Tools;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.Random;public class Main { private static long seed; private static Map map; private static BasicPlayer player; private static Etage etage; public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); init(reader); testAstar(); affichage(); System.getProperty("os.name"); while (true) { Tour(reader.readLine()); } } public static void testAstar() { Position posPlayer = player.getPosition(); Position posTarget = Procedure.getAccesibleRandomPosition(false, etage); for (int i = 0; i < 2; i++) { Etage test = etage.copyOf(); ArrayList<Position> chemin = Tools.Astar(test,posPlayer, posTarget, i); for (Position p : chemin) { test.get(p).updateCell(true, Cell.CellType.PATH); } System.out.println("Taille chemin : " + chemin.size()); test.get(posTarget).updateCell(true, Cell.CellType.SPECIAL); System.out.println(test); } } private static void init(BufferedReader reader) throws IOException { System.out.print("Numero seed ? "); long seed_value; try{ seed_value = Long.parseLong(reader.readLine()); } catch (NumberFormatException e){ seed_value =new Random().nextLong(); } seed=seed_value; Procedure.setSeed(seed); map = new Map(); etage = map.getCurrent(); player = map.getPlayer(); } private static void Tour(String input){ switch (input) { case "z" , "\u001B[A" -> player.moveUp(); case "q" , "\u001B[D" -> player.moveLeft(); case "s" , "\u001B[B" -> player.moveDown(); case "d" , "\u001B[C" -> player.moveRight(); case "exit" -> System.exit(0); default -> System.out.println("Wrong key:"+input); } switch (etage.get(map.getPlayer().getPosition()).getType()){ case UP : map.UP(); break; case DOWN : map.DOWN(); break; case TRAP_ROOM : map.TRAP_ROOM(); break; default: break; } etage=map.getCurrent(); affichage(); } private static void affichage(){ System.out.print(Affichage.CLEAR); System.out.println(seed); System.out.println(etage); System.out.print(Affichage.RESET+"Etage n°"+(map.getIndexCurrent()+1)); System.out.println("\n\n\n\n\n\n\n\n\n\n\n"); System.out.print("Enter your key: "); }}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/Model/Map/Cell.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public enum CellType {
CHEST("\uD83D\uDCB0",Affichage.YELLOW+Affichage.BOLD+"$"),
UP("\uD83D\uDC4D",Affichage.BLUE+Affichage.BOLD+"^"),
DOWN("\uD83D\uDC4E",Affichage.BLUE+Affichage.BOLD+"v"),
TRAP_ROOM(Affichage.BLUE+"X"), ;
TRAP_ROOM(Affichage.BLUE+"X"),
SPECIAL(Affichage.YELLOW+"X");

private String string;

Expand Down Expand Up @@ -60,7 +61,8 @@ public boolean isReserved(){
return type.equals(CellType.UP)
|| type.equals(CellType.DOWN)
|| type.equals(CellType.CHEST)
|| type.equals(CellType.TRAP_ROOM);
|| type.equals(CellType.TRAP_ROOM)
|| type.equals(CellType.SPECIAL);
}

public CellType getType() {
Expand All @@ -81,7 +83,9 @@ public void setEntity(Entity entity) {
}

public Cell copyOf(){
return new Cell(isAccesible, type);
Cell cell = new Cell(isAccesible, type);
cell.Entity=Entity;
return cell;
}

@Override
Expand Down
119 changes: 12 additions & 107 deletions src/main/java/Model/Map/Etage.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public String toString() {
}

public void addRoom(Room r, Position p) {
if (!isCollision(r, p)) {
if (!r.isCollision(this,p)) {
for (int y = 0; y < r.getHeigth(); y++) {
for (int x = 0; x < r.getWidth(); x++) {
this.set(p.getX() + x, p.getY() + y, r.get(x, y));
Expand All @@ -61,16 +61,6 @@ public void addEntity(Entity e) {
Entitys.add(e);
}

public boolean isCollision(Room r, Position p) {
for (int y = 0; y < r.getHeigth(); y++) {
for (int x = 0; x < r.getWidth(); x++) {
if (this.get(p.getX() + x, p.getY() + y).getType() != Cell.CellType.VOID
) return true;
}
}
return false;
}

public Cell get(int x, int y) {
return Cells.get(y).get(x);
}
Expand All @@ -83,6 +73,17 @@ public void set(int x, int y, Cell c) {
Cells.get(y).set(x, c);
}

public Etage copyOf(){
Etage etage = new Etage(getWidth(), getHeigth());
for (int y = 0; y < etage.getHeigth(); y++) {
for (int x = 0; x < etage.getWidth(); x++) {
etage.Cells.get(y).set(x,get(x,y).copyOf());
}
}
etage.Rooms=Rooms;
etage.Entitys=Entitys;
return etage;
}

public int getWidth() {
return Width;
Expand Down Expand Up @@ -166,100 +167,4 @@ public void RoomFusion(){
}
}

public ArrayList<Position> Astar(Position depart, Position arrive, PathType pathType){

ArrayList<Noeud> closedList = new ArrayList<>();
PriorityQueue<Noeud> openList = new PriorityQueue<>(Comparator.comparingDouble(Noeud::getScore));

openList.add(new Noeud(depart.getX(),depart.getY()));

while(openList.size()>0){
Noeud u = openList.poll();

//Current == arrive
if(u.equals(arrive)) {
ArrayList<Position> chemin = new ArrayList<>();
chemin.add(u.copyOf());
Noeud current = u;
while(current.cameFrom!=null){
current=current.cameFrom;
chemin.add(current.copyOf());
}
return chemin;
}

// Génération des voisins
ArrayList<Noeud> voisins = new ArrayList<>();
if (pathType.equals(PathType.CROSS)) addStandardNeighboors(voisins, u);
else if (pathType.equals(PathType.DIAGONAL)) addDiagoNeighboors(voisins, u);

/* Filtrage pour obtenir un voisin valide (dans la map, accessible...) */
voisins = voisins.stream().filter(p -> ((p.getX() >= 0 && p.getY() >= 0 && p.getX() < getWidth() && p.getY() < getHeigth()) && get(p.getX(),p.getY()).isAccesible())).collect(Collectors.toCollection(ArrayList::new));

//Parcous voisins
for(Noeud n : voisins){
n.cameFrom=u;
n.cout = u.cout + n.Distance(u);
n.heuristique = n.Distance(arrive);

if (!(closedList.contains(n)) || n.getScore() < u.getScore()){
if(openList.contains(n)){
openList.remove(n);
openList.add(n);
}
else {
openList.add(n);
}
}
}
closedList.add(u);
}
return new ArrayList<>();
}

private void addStandardNeighboors(ArrayList<Noeud> voisins, Noeud u) {
voisins.add(new Noeud(u.getX() - 1,u.getY()));
voisins.add(new Noeud(u.getX() + 1,u.getY()));
voisins.add(new Noeud(u.getX(),u.getY() - 1));
voisins.add(new Noeud(u.getX(),u.getY() + 1));
}

private void addDiagoNeighboors(ArrayList<Noeud> voisins, Noeud u) {
voisins.add(new Noeud(u.getX() - 1,u.getY() - 1));
voisins.add(new Noeud(u.getX() - 1,u.getY() + 1));
voisins.add(new Noeud(u.getX() + 1,u.getY() + 1));
voisins.add(new Noeud(u.getX() + 1,u.getY() - 1));
}

public enum PathType {
CROSS, DIAGONAL;
}

class Noeud extends Position{
public double heuristique=0;
public double cout=0;
public Noeud cameFrom=null;

public Noeud(int x, int y) {
super(x, y);
}

@Override
public double Distance(Position pos) {
return (int)(super.Distance(pos)*10);
}

private double getScore(){
return cout+heuristique;
}

public Noeud copyOf(){
Noeud noeud = new Noeud(this.getX(), this.getY());
noeud.cout=this.cout;
noeud.cameFrom=this.cameFrom;
noeud.heuristique=this.heuristique;
return noeud;
}
}

}
55 changes: 11 additions & 44 deletions src/main/java/Model/Map/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@
public class Room extends Etage implements Comparable<Room> {
private final int nbrMaxMobPerRoom;
private Position AbsolutePos=null;
private RoomStrategy strategy;

private Room(int Width, int Height, int nbrMaxMobPerRoom) {
super(Width,Height);
public Room(int width, int height,int nbrMaxMobPerRoom,RoomStrategy strategy){
super(width,height);
this.nbrMaxMobPerRoom=nbrMaxMobPerRoom;
this.strategy=strategy;
strategy.composeRoom(this);
}

public Room(int width, int height,int nbrMaxMobPerRoom,RoomStrategy r){
this(width,height,nbrMaxMobPerRoom);
r.composeRoom(this);
public boolean isCollision(Etage etage,Position pos){
return strategy.isCollision(etage,this,pos);
}

public int getNbrMaxRoom() {
return strategy.getNbrMaxRoom();
}

public void setAbsolutePos(Position pos){
Expand All @@ -29,45 +35,6 @@ public int getNbrMaxMobPerRoom() {
return nbrMaxMobPerRoom;
}

/**
* Compares this object with the specified object for order. Returns a
* negative integer, zero, or a positive integer as this object is less
* than, equal to, or greater than the specified object.
*
* <p>The implementor must ensure
* {@code sgn(x.compareTo(y)) == -sgn(y.compareTo(x))}
* for all {@code x} and {@code y}. (This
* implies that {@code x.compareTo(y)} must throw an exception iff
* {@code y.compareTo(x)} throws an exception.)
*
* <p>The implementor must also ensure that the relation is transitive:
* {@code (x.compareTo(y) > 0 && y.compareTo(z) > 0)} implies
* {@code x.compareTo(z) > 0}.
*
* <p>Finally, the implementor must ensure that {@code x.compareTo(y)==0}
* implies that {@code sgn(x.compareTo(z)) == sgn(y.compareTo(z))}, for
* all {@code z}.
*
* <p>It is strongly recommended, but <i>not</i> strictly required that
* {@code (x.compareTo(y)==0) == (x.equals(y))}. Generally speaking, any
* class that implements the {@code Comparable} interface and violates
* this condition should clearly indicate this fact. The recommended
* language is "Note: this class has a natural ordering that is
* inconsistent with equals."
*
* <p>In the foregoing description, the notation
* {@code sgn(}<i>expression</i>{@code )} designates the mathematical
* <i>signum</i> function, which is defined to return one of {@code -1},
* {@code 0}, or {@code 1} according to whether the value of
* <i>expression</i> is negative, zero, or positive, respectively.
*
* @param o the object to be compared.
* @return a negative integer, zero, or a positive integer as this object
* is less than, equal to, or greater than the specified object.
* @throws NullPointerException if the specified object is null
* @throws ClassCastException if the specified object's type prevents it
* from being compared to this object.
*/
@Override
public int compareTo(Room o) {
return (int) (this.getAbsolutePos().Distance(new Position(0,0)) - o.getAbsolutePos().Distance(new Position(0,0)));
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/Model/Map/RoomFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
import Model.Map.Strategy.MarchandRoomStrategy;
import Model.Map.Strategy.NormalRoomStrategy;
import Model.Map.Strategy.TresorRoomStrategy;
import Model.Utils.Procedure;

public class RoomFactory {
public enum roomType {
TRESOR,NORMAL,BOSS,MARCHAND,REPOS,TRAP
}

public Room getNewMarchandRoom(){
return new Room(5,5,0,new MarchandRoomStrategy());
private static Room getNewMarchandRoom(){
return new Room(5,5,0,new MarchandRoomStrategy());
}

private static Room getNewNormalRoom(){
return Procedure.getRandomRoom(5,15,5,new NormalRoomStrategy());
}

public Room getNewRoom(roomType r, int width , int height, int nbrMaxMob){
switch (r){
case MARCHAND -> {
return getNewMarchandRoom();
}
switch (r) {
case TRESOR -> {
return new Room(width,height,nbrMaxMob,new TresorRoomStrategy());
}
Expand All @@ -26,4 +29,15 @@ public Room getNewRoom(roomType r, int width , int height, int nbrMaxMob){
}
}
}

public static Room getNewRoom(roomType r){
switch (r){
case MARCHAND -> {
return getNewMarchandRoom();
}
default -> {
return getNewNormalRoom();
}
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/Model/Map/Strategy/LabyRoomStrategy.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package Model.Map.Strategy;

import Model.Map.Etage;
import Model.Map.Room;
import Model.Utils.Position;

public class LabyRoomStrategy extends RoomStrategy {

@Override
public void composeRoom(Room r) {

}

@Override
public boolean isCollision(Etage etage, Room room, Position pos) {
return super.isCollision(etage,room,pos,0);
}

}
7 changes: 7 additions & 0 deletions src/main/java/Model/Map/Strategy/MarchandRoomStrategy.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Model.Map.Strategy;

import Model.Map.Cell;
import Model.Map.Etage;
import Model.Map.Room;
import Model.Utils.Position;
import Model.Utils.Procedure;
Expand All @@ -15,4 +16,10 @@ public void composeRoom(Room r) {
Position p2 = Procedure.getAccesibleRandomPosition(false, r);
r.get(p2).updateCell(true, Cell.CellType.DOWN);
}

@Override
public boolean isCollision(Etage etage, Room room, Position pos) {
return super.isCollision(etage,room,pos,0);
}

}
12 changes: 12 additions & 0 deletions src/main/java/Model/Map/Strategy/NormalRoomStrategy.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package Model.Map.Strategy;


import Model.Map.Etage;
import Model.Utils.Affichage;
import Model.Map.Cell;
import Model.Map.Room;
import Model.Utils.Position;

public class NormalRoomStrategy extends RoomStrategy{
@Override
Expand All @@ -13,4 +15,14 @@ public void composeRoom(Room r) {
type.setString(Affichage.GREEN+'.');
r.fillMap(new Cell(true, type));
}

@Override
public boolean isCollision(Etage etage, Room room, Position pos) {
return super.isCollision(etage, room, pos,0);
}

@Override
public int getNbrMaxRoom() {
return 8;
}
}
18 changes: 18 additions & 0 deletions src/main/java/Model/Map/Strategy/RoomStrategy.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
package Model.Map.Strategy;

import Model.Map.Cell;
import Model.Map.Etage;
import Model.Map.Room;
import Model.Utils.Position;

public abstract class RoomStrategy{

public void composeRoom(Room r){
r.fillMap(new Cell(true, Cell.CellType.NORMAL));
}

protected boolean isCollision(Etage etage,Room room,Position pos,int DistanceMin){
for (int y = 0; y < room.getHeigth()+DistanceMin*2; y++) {
for (int x = 0; x < room.getWidth()+DistanceMin*2; x++) {
if (etage.get(Math.max(Math.min(pos.getX() + x - DistanceMin, etage.getWidth()-1), 0), Math.max(Math.min(pos.getY() + y - DistanceMin, etage.getHeigth()-1),0)).getType() != Cell.CellType.VOID){
return true;
}
}
}
return false;
}

public abstract boolean isCollision(Etage etage,Room room,Position pos);

public int getNbrMaxRoom() {
return 8;
}
}
Loading

0 comments on commit f1407b8

Please sign in to comment.