mirror of
https://github.com/Cpt-Adok/SNAKE.git
synced 2026-01-25 12:34:07 +00:00
ajout de Channel
This commit is contained in:
@@ -102,10 +102,9 @@ public class Map {
|
|||||||
public Grid[][] getInverseGrid() {
|
public Grid[][] getInverseGrid() {
|
||||||
Grid[][] grid = getGrid();
|
Grid[][] grid = getGrid();
|
||||||
Grid[][] inverseGrid = new Grid[this.largeur][this.longueur];
|
Grid[][] inverseGrid = new Grid[this.largeur][this.longueur];
|
||||||
int k = 0;
|
|
||||||
|
|
||||||
for (int i = grid.length; i> 0; --i) {
|
for (int i = grid.length - 1; i >= 0; i--) {
|
||||||
inverseGrid[k++] = inverseGrid[i];
|
inverseGrid[i] = grid[grid.length - 1 - i];
|
||||||
}
|
}
|
||||||
|
|
||||||
return inverseGrid;
|
return inverseGrid;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import connexion.Channel;
|
||||||
import environnements.*;
|
import environnements.*;
|
||||||
import game.Terminal;
|
import game.Terminal;
|
||||||
import personnages.*;
|
import personnages.*;
|
||||||
@@ -7,15 +8,17 @@ public class Main {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Personnage.n = 2;
|
Personnage.n = 2;
|
||||||
|
|
||||||
|
Map map = new Map(20, 20);
|
||||||
|
|
||||||
Personnage[] personnages = new Personnage[] {
|
Personnage[] personnages = new Personnage[] {
|
||||||
new Player(new int[] {0, 0}, "Philippe Etchebest"),
|
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.addObjects(Item.FRAISE, 0, 0);
|
||||||
// map.addObjectsRandomize(new Item[] {Item.FRAISE}, 1);
|
// map.addObjectsRandomize(new Item[] {Item.FRAISE}, 1);
|
||||||
|
|
||||||
new Terminal(map, personnages).run();
|
new Terminal(map, personnages).run("24");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -175,5 +175,5 @@ public abstract class Personnage {
|
|||||||
if(round > 0 && n > 0) if (round%n == 0) this.coordinate.add(coordinate);
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package personnages;
|
|||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
import connexion.Channel;
|
||||||
import environnements.Map;
|
import environnements.Map;
|
||||||
import types.*;
|
import types.*;
|
||||||
|
|
||||||
@@ -81,10 +82,12 @@ public class Player extends Personnage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean round(Map map) {
|
public boolean round(Map map, String channel) {
|
||||||
this.moveCoordinate(this.getInput());
|
int keys = this.getInput();
|
||||||
|
this.moveCoordinate(keys);
|
||||||
|
|
||||||
int[] coordinate = this.getHeadCoordinate();
|
int[] coordinate = this.getHeadCoordinate();
|
||||||
|
if (channel != null) Channel.envoyerMessage(getMouvement(keys));
|
||||||
if(map.isGameOver(coordinate) || this.applyEffects(map.getEffect(coordinate))) return true;
|
if(map.isGameOver(coordinate) || this.applyEffects(map.getEffect(coordinate))) return true;
|
||||||
map.deleteItems(coordinate);
|
map.deleteItems(coordinate);
|
||||||
|
|
||||||
|
|||||||
@@ -1,24 +1,48 @@
|
|||||||
package connexion;
|
package connexion;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import environnements.*;
|
import environnements.*;
|
||||||
|
import types.Item;
|
||||||
import types.Mouvement;
|
import types.Mouvement;
|
||||||
import personnages.Personnage;
|
import personnages.Personnage;
|
||||||
|
|
||||||
public class Channel extends Personnage {
|
public class Channel extends Personnage {
|
||||||
private Reseau reseau;
|
private Reseau reseau;
|
||||||
|
private static Reseau adversaire;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
public Channel(Object[][] map, String channel) {
|
|
||||||
super(new int [] {19,19});
|
|
||||||
reseau = new Reseau(channel);
|
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
|
* Méthode permettant l'envoi de message, grâce à celle de Réseau
|
||||||
* @param String contenant la direction voulue
|
* @param String contenant la direction voulue
|
||||||
**/
|
**/
|
||||||
public void envoyerMessage(String s) {
|
public static void envoyerMessage(Mouvement mouvement) {
|
||||||
reseau.sendContent(s);
|
adversaire.sendContent(conversionString(mouvement));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -34,7 +58,7 @@ public class Channel extends Personnage {
|
|||||||
* @param String s
|
* @param String s
|
||||||
* @return Mouvement
|
* @return Mouvement
|
||||||
*/
|
*/
|
||||||
public Mouvement conversion(String s){
|
public Mouvement conversionMouvement(String s){
|
||||||
if (s.equals("U") || s.equals("u")){
|
if (s.equals("U") || s.equals("u")){
|
||||||
return Mouvement.HAUT;
|
return Mouvement.HAUT;
|
||||||
}else if (s.equals("D") || s.equals("d")){
|
}else if (s.equals("D") || s.equals("d")){
|
||||||
@@ -47,6 +71,18 @@ public class Channel extends Personnage {
|
|||||||
return null;
|
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
|
* Cette méthode est commune à toutes les sous-classes de personnages
|
||||||
@@ -55,13 +91,16 @@ public class Channel extends Personnage {
|
|||||||
* @param map
|
* @param map
|
||||||
* @return boolean qui indique si le Personnage est vivant ou pas.
|
* @return boolean qui indique si le Personnage est vivant ou pas.
|
||||||
*/
|
*/
|
||||||
// @Override
|
@Override
|
||||||
public boolean round(Map map){
|
public boolean round(Map map, String channel){
|
||||||
int[] coordinate=this.getHeadCoordinate();
|
int[] coordinate=this.getHeadCoordinate();
|
||||||
this.moveSnake(conversion(recupererMessage()));
|
|
||||||
|
this.moveSnake(conversionMouvement(recupererMessage()));
|
||||||
|
|
||||||
if (map.isGameOver(coordinate) || this.applyEffects(map.getEffect(coordinate))){
|
if (map.isGameOver(coordinate) || this.applyEffects(map.getEffect(coordinate))){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
map.deleteItems(coordinate);
|
map.deleteItems(coordinate);
|
||||||
this.increaseRound();
|
this.increaseRound();
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ public class Terminal {
|
|||||||
this.map = map;
|
this.map = map;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run(String channel) {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
@@ -32,7 +32,7 @@ public class Terminal {
|
|||||||
Display.printInformation(i++, personnage);
|
Display.printInformation(i++, personnage);
|
||||||
Display.printMap(map.addEdges());
|
Display.printMap(map.addEdges());
|
||||||
|
|
||||||
if(personnage.round(map)) {
|
if(personnage.round(map, channel)) {
|
||||||
Display.clearTerminal();
|
Display.clearTerminal();
|
||||||
System.out.println(personnage.getName() + " à perdu!");
|
System.out.println(personnage.getName() + " à perdu!");
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user