ajout de Channel

This commit is contained in:
2024-05-23 16:46:56 +02:00
parent 71cb378256
commit 0e0a1c3ff4
6 changed files with 68 additions and 24 deletions

View File

@@ -102,10 +102,9 @@ public class Map {
public Grid[][] getInverseGrid() {
Grid[][] grid = getGrid();
Grid[][] inverseGrid = new Grid[this.largeur][this.longueur];
int k = 0;
for (int i = grid.length; i> 0; --i) {
inverseGrid[k++] = inverseGrid[i];
for (int i = grid.length - 1; i >= 0; i--) {
inverseGrid[i] = grid[grid.length - 1 - i];
}
return inverseGrid;

View File

@@ -1,3 +1,4 @@
import connexion.Channel;
import environnements.*;
import game.Terminal;
import personnages.*;
@@ -7,15 +8,17 @@ public class Main {
public static void main(String[] args) {
Personnage.n = 2;
Map map = new Map(20, 20);
Personnage[] personnages = new Personnage[] {
new Player(new int[] {0, 0}, "Philippe Etchebest"),
new Player(new int[] {19, 19}, "Luke Skywalker")
new Channel(map, "23", "24")
};
Map map = new Map(20, 20);
// map.addObjects(Item.FRAISE, 0, 0);
// map.addObjectsRandomize(new Item[] {Item.FRAISE}, 1);
new Terminal(map, personnages).run();
new Terminal(map, personnages).run("24");
}
}

View File

@@ -175,5 +175,5 @@ public abstract class Personnage {
if(round > 0 && n > 0) if (round%n == 0) this.coordinate.add(coordinate);
}
public abstract boolean round(Map map);
public abstract boolean round(Map map, String channel);
}

View File

@@ -2,6 +2,7 @@ package personnages;
import java.util.Scanner;
import connexion.Channel;
import environnements.Map;
import types.*;
@@ -81,10 +82,12 @@ public class Player extends Personnage {
}
@Override
public boolean round(Map map) {
this.moveCoordinate(this.getInput());
public boolean round(Map map, String channel) {
int keys = this.getInput();
this.moveCoordinate(keys);
int[] coordinate = this.getHeadCoordinate();
if (channel != null) Channel.envoyerMessage(getMouvement(keys));
if(map.isGameOver(coordinate) || this.applyEffects(map.getEffect(coordinate))) return true;
map.deleteItems(coordinate);

View File

@@ -1,24 +1,48 @@
package connexion;
import java.util.Arrays;
import environnements.*;
import types.Item;
import types.Mouvement;
import personnages.Personnage;
public class Channel extends Personnage {
private Reseau reseau;
private static Reseau adversaire;
public Channel(Object[][] map, String channel) {
super(new int [] {19,19});
reseau=new Reseau(channel);
private Map map;
public Channel(Map map, String channel, String autreChannel) {
super(new int [] {map.getGrid()[0].length, map.getGrid().length});
this.map = map;
this.name = autreChannel;
reseau = new Reseau(channel);
adversaire = new Reseau(autreChannel);
}
public Grid[][] getInverseGridChannel() {
Grid[][] grid = map.getInverseGrid();
Grid[][] inverseGrid = new Grid[grid.length][grid[0].length];
for (int j = grid.length - 1; j >= 0; j--) {
for (int i = grid[j].length - 1; i >= 0; i--) {
inverseGrid[j][i] = grid[j][grid[j].length - 1 - i];
}
}
return inverseGrid;
}
/**
* Méthode permettant l'envoi de message, grâce à celle de Réseau
* @param String contenant la direction voulue
**/
public void envoyerMessage(String s) {
reseau.sendContent(s);
public static void envoyerMessage(Mouvement mouvement) {
adversaire.sendContent(conversionString(mouvement));
}
/**
@@ -34,7 +58,7 @@ public class Channel extends Personnage {
* @param String s
* @return Mouvement
*/
public Mouvement conversion(String s){
public Mouvement conversionMouvement(String s){
if (s.equals("U") || s.equals("u")){
return Mouvement.HAUT;
}else if (s.equals("D") || s.equals("d")){
@@ -47,6 +71,18 @@ public class Channel extends Personnage {
return null;
}
private static String conversionString(Mouvement mouvement){
if (mouvement == Mouvement.HAUT) {
return "U";
} else if (mouvement == Mouvement.BAS) {
return "D";
} else if (mouvement == Mouvement.GAUCHE) {
return "L";
} else if (mouvement == Mouvement.DROITE) {
return "R";
}
return null;
}
/**
* Cette méthode est commune à toutes les sous-classes de personnages
@@ -55,13 +91,16 @@ public class Channel extends Personnage {
* @param map
* @return boolean qui indique si le Personnage est vivant ou pas.
*/
// @Override
public boolean round(Map map){
int [] coordinate=this.getHeadCoordinate();
this.moveSnake(conversion(recupererMessage()));
@Override
public boolean round(Map map, String channel){
int[] coordinate=this.getHeadCoordinate();
this.moveSnake(conversionMouvement(recupererMessage()));
if (map.isGameOver(coordinate) || this.applyEffects(map.getEffect(coordinate))){
return true;
}
map.deleteItems(coordinate);
this.increaseRound();
return false;

View File

@@ -19,7 +19,7 @@ public class Terminal {
this.map = map;
}
public void run() {
public void run(String channel) {
int i = 0;
while(true) {
@@ -32,7 +32,7 @@ public class Terminal {
Display.printInformation(i++, personnage);
Display.printMap(map.addEdges());
if(personnage.round(map)) {
if(personnage.round(map, channel)) {
Display.clearTerminal();
System.out.println(personnage.getName() + " à perdu!");
return;