mirror of
https://github.com/Cpt-Adok/SNAKE.git
synced 2026-01-25 17:34:08 +00:00
Construction (#2)
* Add files via upload * correction * changement de dossiers * changement de beaucoup de class et ajout de Mouvement * ajout de plein de fonction et suppression des anciennes classes * ajout de plein d'autre fonction * changement de plein de fonction * Ajout et correction * deplacer les fichiers * changement de plein de trucs (manque juste a regler un probleme) * programme basique fini * mise en place d'un runner pour windows (UTF-8) * correction de plein de chose dans le programme --------- Co-authored-by: Cpt-Adok <126670243+Cpt-Adok@users.noreply.github.com>
This commit is contained in:
152
src/display/Display.java
Normal file
152
src/display/Display.java
Normal file
@@ -0,0 +1,152 @@
|
||||
package Display;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import Environnements.Map;
|
||||
import Objets.Items;
|
||||
import Personnages.Personnage;
|
||||
import Personnages.Snake;
|
||||
|
||||
public class Display {
|
||||
/**
|
||||
* cette fonction clear le terminal et le remet en
|
||||
* haut a gauche de la ligne.
|
||||
*/
|
||||
public static void clearTerminal() {
|
||||
System.out.println("\u001b[2J \u001b[H");
|
||||
}
|
||||
|
||||
/**
|
||||
* cette fonction dessine la grille avec des noms
|
||||
* pour chaque items.
|
||||
*/
|
||||
public static void printMap(Object[][] map) {
|
||||
for (Object[] object : map) {
|
||||
for (Object value : object) {
|
||||
System.out.print(value.toString() + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* cette fonction print le snake dans le terminal avec des
|
||||
* caracteres speciaux, (du utf-8).
|
||||
* @param map
|
||||
* @param personnages
|
||||
*/
|
||||
public static void printMap(Object[][] map, Personnage[] personnages) {
|
||||
for (int i = 0; i<map.length; i++) {
|
||||
for(int k = 0; k<map[0].length; k++) {
|
||||
Object object = map[i][k];
|
||||
|
||||
if(object instanceof Items) printItems((Items)object, map, k, i);
|
||||
if(object instanceof Snake) printSnake((Snake)object, personnages, k, i);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
public static void printInformation(int round, int playerIndex, Personnage personnage) {
|
||||
int[] coordinate = personnage.getHeadCoordinate();
|
||||
|
||||
System.out.println(
|
||||
"Round : " + round + " | N : " + Personnage.n +
|
||||
"\n Joueur " + (playerIndex+1) + " : " + personnage.getName() + " (" +
|
||||
coordinate[0]+", "+ coordinate[1] +") | size : " + personnage.getSize()
|
||||
);
|
||||
}
|
||||
|
||||
private static void printSnake(Snake item, Personnage[] personnages, int x, int y) {
|
||||
switch (item) {
|
||||
case BODY: System.out.print(" \u25A1 "); break;
|
||||
case HEAD: printHead(personnages, x, y); break;
|
||||
}
|
||||
}
|
||||
|
||||
private static void printItems(Items item, Object[][] map, int x, int y) {
|
||||
switch (item) {
|
||||
case FRAISE: System.out.print(" \uF353 "); break;
|
||||
|
||||
case WALL: printWall(map, x, y); break;
|
||||
case VOID: System.out.print(" "); break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void printWall(Object[][] map, int x, int y) {
|
||||
// TOP BOT LEFT RIGHT
|
||||
boolean[] position = new boolean[] {
|
||||
(y > 0) ? map[y - 1][x] == Items.WALL : false,
|
||||
(y < map.length - 1) ? map[y+1][x] == Items.WALL : false,
|
||||
(x > 0) ? map[y][x - 1] == Items.WALL : false,
|
||||
(x < map[y].length - 1) ? map[y][x + 1] == Items.WALL : false,
|
||||
};
|
||||
|
||||
System.out.print(whichWall(position));
|
||||
}
|
||||
|
||||
private static String whichWall(boolean[] isWall) {
|
||||
String positionWall = new String();
|
||||
|
||||
if (isWall[0] && isWall[1] && isWall[2] && isWall[3]) positionWall = "\u2550\u256C\u2550";
|
||||
|
||||
else if (isWall[0] && isWall[1] && isWall[3]) positionWall = "\u2560";
|
||||
else if (isWall[0] && isWall[1] && isWall[2]) positionWall = "\u2563";
|
||||
else if (isWall[0] && isWall[2] && isWall[3]) positionWall = "\u2550\u2569\u2550";
|
||||
else if (isWall[1] && isWall[2] && isWall[3]) positionWall = "\u2550\u2566\u2550";
|
||||
|
||||
else if (isWall[0] && isWall[1]) positionWall = "\u2551";
|
||||
else if (isWall[2] && isWall[3]) positionWall = "\u2550\u2550\u2550";
|
||||
else if (isWall[0] && isWall[2]) positionWall = "\u255D ";
|
||||
else if (isWall[0] && isWall[3]) positionWall = "\u255A";
|
||||
else if (isWall[1] && isWall[2]) positionWall = "\u2557 ";
|
||||
else if (isWall[1] && isWall[3]) positionWall = "\u2554";
|
||||
|
||||
else positionWall = "\u2550\u256C\u2550";
|
||||
|
||||
return positionWall;
|
||||
}
|
||||
|
||||
private static void printHead(Personnage[] personnages, int x, int y) {
|
||||
Personnage personnage = searchSnake(personnages, x, y);
|
||||
|
||||
if (personnage != null) {
|
||||
ArrayList<int[]> personnageCoordinate = personnage.getCoordinate();
|
||||
|
||||
int[] primaryCoordinate = personnage.getHeadCoordinate();
|
||||
|
||||
if (personnageCoordinate.size() > 1) {
|
||||
int[] secondCoordinate = personnageCoordinate.get(1);
|
||||
|
||||
// UP DOWN LEFT RIGHT
|
||||
boolean[] isHead = new boolean[] {
|
||||
primaryCoordinate[0] == secondCoordinate[0] && primaryCoordinate[1] > secondCoordinate[1], // UP
|
||||
primaryCoordinate[0] == secondCoordinate[0] && primaryCoordinate[1] < secondCoordinate[1], // DOWN
|
||||
primaryCoordinate[1] == secondCoordinate[1] && primaryCoordinate[0] > secondCoordinate[0], // LEFT
|
||||
primaryCoordinate[1] == secondCoordinate[1] && primaryCoordinate[0] < secondCoordinate[0] // RIGHT
|
||||
};
|
||||
|
||||
if (isHead[0]) {System.out.print(" \u21E9 ");return;}
|
||||
else if (isHead[1]) {System.out.print(" \u21E7 ");return;}
|
||||
else if (isHead[2]) {System.out.print("\u21E8 ");return;}
|
||||
else if (isHead[3]) {System.out.print(" \u21E6");return;}
|
||||
}
|
||||
}
|
||||
System.out.print(" \u25CF ");
|
||||
}
|
||||
|
||||
private static Personnage searchSnake(Personnage[] personnages, int x, int y) {
|
||||
for (Personnage personnage : personnages) {
|
||||
int[] primaryCoordinate = personnage.getHeadCoordinate();
|
||||
|
||||
int primaryY = primaryCoordinate[1] + 1;
|
||||
int primaryX = primaryCoordinate[0] + 1;
|
||||
|
||||
if (primaryX == x && primaryY == y) {
|
||||
return personnage;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
146
src/display/Terminal.java
Normal file
146
src/display/Terminal.java
Normal file
@@ -0,0 +1,146 @@
|
||||
package Display;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
import Environnements.Map;
|
||||
import Objets.Effects;
|
||||
import Objets.Items;
|
||||
import Personnages.Mouvements;
|
||||
import Personnages.Personnage;
|
||||
import Personnages.Player;
|
||||
import Personnages.Robot;
|
||||
|
||||
public class Terminal {
|
||||
private int round = 0;
|
||||
|
||||
private static Map map;
|
||||
private static Personnage[] personnages;
|
||||
|
||||
private static Scanner scanner;
|
||||
|
||||
|
||||
public Terminal(Map m, Personnage[] personnage) {
|
||||
scanner = new Scanner(System.in);
|
||||
personnages = personnage;
|
||||
map = m;
|
||||
|
||||
run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Cette fonction est uniquement destiné pour la classe
|
||||
* Players pour recuperer l'input dans le terminal.
|
||||
* @param player
|
||||
* @return il retourne int qui est le char en ascii
|
||||
*/
|
||||
private static int getInput(Player player) {
|
||||
String value = new String();
|
||||
Integer input = null;
|
||||
|
||||
do {
|
||||
value = scanner.nextLine();
|
||||
input = changeCoordinate(value);
|
||||
}while(player.getMouvement(input) == null);
|
||||
|
||||
return input.intValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* transforme le String en prennant le premier char et
|
||||
* le mets en ascii dans la classe Integer.
|
||||
* @param input
|
||||
* @return
|
||||
*/
|
||||
private static Integer changeCoordinate(String input) {
|
||||
if (input.length() > 0) {
|
||||
return (int)input.charAt(0);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* place tout les personnages dans la fonction {@link #map.placePersonnages()}
|
||||
* @param personnages
|
||||
*/
|
||||
private static void placePersonnages(Personnage[] personnages) {
|
||||
for(Personnage personnage : personnages) {
|
||||
map.placePersonnages(personnage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* cette fonction est spécialement conçu pour gerer le gameplay du joueur.
|
||||
* @param player
|
||||
* @return
|
||||
*/
|
||||
private static boolean playerRound(Player player) {
|
||||
int input = getInput(player);
|
||||
player.moveCoordinate(input);
|
||||
|
||||
int[] coordinate = player.getHeadCoordinate();
|
||||
if(map.isGameOver(player.getHeadCoordinate()) || player.applyEffects(map.getEffect(coordinate))) return true;
|
||||
|
||||
player.increaseRound();
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* cette fonction est spécialement conçu pour gerer le gameplay du robot.
|
||||
* @param player
|
||||
* @return
|
||||
*/
|
||||
private static boolean robotRound(Robot robot) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* cette fonction cherche si le personnage mis en paramètre
|
||||
* est un {@link Player} ou un {@link Robot}.
|
||||
* @param player
|
||||
* @return
|
||||
*/
|
||||
private static boolean instancePersonnage(Personnage personnage) {
|
||||
if (personnage instanceof Player) {
|
||||
// tour du Player
|
||||
return playerRound((Player)personnage);
|
||||
}
|
||||
|
||||
if (personnage instanceof Robot) {
|
||||
// tour du robot
|
||||
return robotRound((Robot)personnage);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* la fonction principal qui lance tout le jeu en terminal.
|
||||
*/
|
||||
private void run() {
|
||||
boolean isGameOver = false;
|
||||
int i = 0;
|
||||
|
||||
while(!isGameOver) {
|
||||
for (i = 0; i<personnages.length; i++) {
|
||||
Personnage personnage = personnages[i];
|
||||
Display.clearTerminal();
|
||||
|
||||
map.placeObjects();
|
||||
placePersonnages(personnages);
|
||||
|
||||
Display.printInformation(this.round, i, personnage);
|
||||
|
||||
map.placeObjects();
|
||||
Display.printMap(map.addEdges(), personnages);
|
||||
|
||||
isGameOver = instancePersonnage(personnage);
|
||||
if(!isGameOver) placePersonnages(personnages);
|
||||
else break;
|
||||
|
||||
map.clearMap();
|
||||
}
|
||||
this.round++;
|
||||
}
|
||||
System.out.println("GAMEOVER \nLe joueur " + (i+1) + " à perdu !");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user