mirror of
https://github.com/Cpt-Adok/SNAKE.git
synced 2026-01-25 11:34:06 +00:00
programme basique fini
This commit is contained in:
@@ -10,9 +10,10 @@ import Objects.Snake;
|
||||
* personnage jouable.
|
||||
*/
|
||||
public class Personnage {
|
||||
private int n;
|
||||
public static int n;
|
||||
private int round;
|
||||
private int size = 0;
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* <p> la liste de toute les coordonnées en fonction de N. Si N = 2,
|
||||
@@ -33,11 +34,11 @@ public class Personnage {
|
||||
* @param coordinate est la variable qui contient les coordonnées
|
||||
* qui sont placé par la suite dans {@link #coordinate}[0]}
|
||||
*/
|
||||
protected Personnage(int n, int[] coordinate) {
|
||||
protected Personnage(String name, int[] coordinate) {
|
||||
this.coordinate = new ArrayList<int[]>();
|
||||
this.coordinate.add(coordinate);
|
||||
|
||||
this.n = n;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,13 +119,19 @@ public class Personnage {
|
||||
* @param mouvements le mouvement utilisé pour deplacer le serpent
|
||||
*/
|
||||
public void moveSnake(Mouvements mouvements) {
|
||||
mouvements.editCoordinate(this.coordinate.get(0));
|
||||
|
||||
int[] oldHeadPosition = getPrimaryCoordinate().clone();
|
||||
|
||||
for(int i = this.coordinate.size() - 1; i>1; i--) {
|
||||
int[] value = this.coordinate.get(i-1);
|
||||
this.coordinate.set(i, value);
|
||||
for (int i = this.coordinate.size() - 1; i > 0; i--) {
|
||||
this.coordinate.set(i, this.coordinate.get(i - 1).clone());
|
||||
}
|
||||
mouvements.editCoordinate(oldHeadPosition);
|
||||
this.coordinate.set(0, oldHeadPosition);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Mouvements getMouvement(Integer keys) {
|
||||
switch (keys) {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package Characters;
|
||||
|
||||
public class Players extends Personnage {
|
||||
public Players(int[] coordinate, int n) {
|
||||
super(n, coordinate);
|
||||
public Players(String name, int[] coordinate) {
|
||||
super(name, coordinate);
|
||||
}
|
||||
|
||||
public Integer changeCoordinate(String input) {
|
||||
|
||||
@@ -2,8 +2,8 @@ package Characters;
|
||||
|
||||
public class Robot extends Personnage {
|
||||
|
||||
protected Robot(int[] coordinate, int n) {
|
||||
super(n, coordinate);
|
||||
protected Robot(String name, int[] coordinate) {
|
||||
super(name, coordinate);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -44,6 +44,10 @@ public class Map {
|
||||
this.largeur = largeur;
|
||||
|
||||
this.grid = new Object[this.longueur][this.largeur];
|
||||
|
||||
this.ObjectItems = new ArrayList<>();
|
||||
this.coordinateItems = new ArrayList<>();
|
||||
|
||||
this.fillGrid();
|
||||
}
|
||||
|
||||
@@ -51,8 +55,9 @@ public class Map {
|
||||
* <p> cette fonction clear toute la grille qui
|
||||
* contient le jeu.
|
||||
*/
|
||||
public void clearMap() {
|
||||
public void clearMap(boolean edges) {
|
||||
this.fillGrid();
|
||||
if(edges) this.addEdges();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -96,6 +101,7 @@ public class Map {
|
||||
randomize(objects[i], value);
|
||||
}
|
||||
randomize(objects[lengthObjects], number);
|
||||
placeObjects();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,12 +130,11 @@ public class Map {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isGamefinishedImpassable(int key, Personnage personnage) {
|
||||
int[] coordinate = personnage.getMouvement(key).getCoordinate();
|
||||
private boolean isGamefinishedImpassable(Personnage personnage) {
|
||||
int[] playerCoordinate = personnage.getPrimaryCoordinate();
|
||||
|
||||
int y = coordinate[1] + playerCoordinate[1];
|
||||
int x = coordinate[0] + playerCoordinate[0];
|
||||
int y = playerCoordinate[1];
|
||||
int x = playerCoordinate[0];
|
||||
|
||||
Object grid = this.getGrid()[y][x];
|
||||
|
||||
@@ -147,7 +152,8 @@ public class Map {
|
||||
if (isOutX || isOutY) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
System.out.println(isGamefinishedImpassable(personnage));
|
||||
return isGamefinishedImpassable(personnage);
|
||||
}
|
||||
|
||||
private void fillGrid() {
|
||||
@@ -158,17 +164,24 @@ public class Map {
|
||||
}
|
||||
}
|
||||
|
||||
private void randomize(Object item, int number) {
|
||||
private void randomize(Object object, int number) {
|
||||
Random random = new Random();
|
||||
|
||||
for(int i = 0; i<number; i++) {
|
||||
int x = random.nextInt(1, this.grid[0].length);
|
||||
int y = random.nextInt(1, this.grid.length);
|
||||
for (int i = 0; i<number; i++) {
|
||||
int x = random.nextInt(this.grid[0].length);
|
||||
int y = random.nextInt(this.grid.length);
|
||||
|
||||
if(!(getGrid()[y][x] instanceof Items)) {this.coordinateItems.add(new int[]{y, x}); this.ObjectItems.add(grid);}
|
||||
else if(!(getGrid()[y][x] instanceof Snake)) {this.coordinateItems.add(new int[]{y, x}); this.ObjectItems.add(grid);}
|
||||
else if(!(getGrid()[y][x] instanceof Fruits)) {this.coordinateItems.add(new int[]{y, x}); this.ObjectItems.add(grid);}
|
||||
else i--;
|
||||
if(!((getGrid()[y][x] instanceof Snake) || (getGrid()[y][x] instanceof Fruits))) {
|
||||
if ((Items)getGrid()[y][x] == Items.VOID) {
|
||||
this.coordinateItems.add(new int[] {x, y});
|
||||
this.ObjectItems.add(object);
|
||||
} else {
|
||||
i--;
|
||||
}
|
||||
}
|
||||
else {
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,31 +1,23 @@
|
||||
import Characters.Personnage;
|
||||
import Characters.Players;
|
||||
import Display.Terminal;
|
||||
import Display.TerminalDisplay;
|
||||
import Environnement.Map;
|
||||
import Objects.Fruits;
|
||||
import Objects.Items;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
int N = 2;
|
||||
Personnage.n = 2;
|
||||
|
||||
Map map = new Map(30, 30);
|
||||
|
||||
Personnage[] personnages = new Personnage[] {
|
||||
new Players(new int[] {1, 1}, N),
|
||||
new Players(new int[] {8, 8}, N)
|
||||
new Players("d", new int[] {1, 1}),
|
||||
new Players("e", new int[] {28, 28})
|
||||
};
|
||||
|
||||
Map map = new Map(10, 10);
|
||||
// map.addEdges();
|
||||
|
||||
Terminal.run(personnages, map, N);
|
||||
|
||||
// Players player = new Players(new int[]{1, 1}, 1);
|
||||
// player.increaseSnake(player.keepLatestCoordinate());
|
||||
// player.moveCoordinate(0x77);
|
||||
|
||||
// for(int[] value : player.getCoordinate()) {
|
||||
// for (int valueInt : value) {
|
||||
// System.out.print(valueInt + " ");
|
||||
// }
|
||||
// System.out.println();
|
||||
// }
|
||||
Terminal.edges = true;
|
||||
new Terminal(map, personnages);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,37 @@
|
||||
package Display;
|
||||
|
||||
import java.io.IOError;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Scanner;
|
||||
|
||||
import Characters.Personnage;
|
||||
import Characters.Players;
|
||||
import Characters.Robot;
|
||||
import Environnement.Map;
|
||||
import Objects.Fruits;
|
||||
import Objects.Items;
|
||||
import Objects.Snake;
|
||||
import Characters.*;
|
||||
import Environnement.*;
|
||||
import Objects.*;
|
||||
|
||||
public class Terminal {
|
||||
private static Scanner scanner = new Scanner(System.in);
|
||||
private static String name = new String();
|
||||
|
||||
private static Scanner scanner;
|
||||
private static Map map;
|
||||
|
||||
private static Personnage[] personnages;
|
||||
|
||||
public static boolean edges = false;
|
||||
|
||||
public Terminal(Map m, Personnage[] personnage) {
|
||||
scanner = new Scanner(System.in);
|
||||
personnages = personnage;
|
||||
map = m;
|
||||
|
||||
run();
|
||||
if (edges) map.addEdges();
|
||||
}
|
||||
|
||||
private static void placePersonnages(Personnage[] personnages) {
|
||||
for(Personnage personnage : personnages) {
|
||||
map.placePersonnages(personnage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <p> Cette fonction est uniquement destiné pour la classe
|
||||
* Players pour recuperer l'input dans le terminal.
|
||||
@@ -35,60 +51,19 @@ public class Terminal {
|
||||
return input.intValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* <p> print toute la map.
|
||||
* @param map
|
||||
*/
|
||||
private static void printMap(Map map) {
|
||||
Object[][] mapObjects = map.getGrid();
|
||||
|
||||
for (int i = 0; i<mapObjects.length; i++) {
|
||||
for(int k = 0; k<mapObjects[0].length; k++) {
|
||||
if (mapObjects[i][k] instanceof Items) System.out.print(((Items)mapObjects[i][k]).getName() + " ");
|
||||
if (mapObjects[i][k] instanceof Fruits) System.out.print(((Fruits)mapObjects[i][k]).getName() + " ");
|
||||
if (mapObjects[i][k] instanceof Snake) System.out.print(((Snake)mapObjects[i][k]).getName() + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
private static void printPersonnages(Personnage[] personnages) {
|
||||
for(int i = 0; i<personnages.length; i++) {
|
||||
int[] coordinate = personnages[i].getPrimaryCoordinate();
|
||||
System.out.println("Joueur " + (i+1) + " sont aux coordonnées : {" + coordinate[0] + "," + coordinate[1] + "}");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* cette fonction clear le terminal et le remet en
|
||||
* haut a gauche de la ligne.
|
||||
*/
|
||||
private static void clearTerminal() {
|
||||
System.out.println("\u001b[2J \u001b[H");
|
||||
}
|
||||
|
||||
private static void placePersonnages(Personnage[] personnages) {
|
||||
for(Personnage personnage : personnages) {
|
||||
map.placePersonnages(personnage);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean playerRound(Players player) {
|
||||
printMap(map);
|
||||
TerminalDisplay.printMap(map, personnages);
|
||||
// TerminalDisplay.printMapType(map);
|
||||
|
||||
int[] latestCoordinate = player.keepLatestCoordinate();
|
||||
int input = getInput(scanner, player);
|
||||
player.moveCoordinate(input);
|
||||
|
||||
if(map.isGameOver(input, player)) {
|
||||
System.out.println("GameOver");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(map.isGameOver(input, player)) {TerminalDisplay.clearTerminal(); System.out.println("GameOver"); return false;}
|
||||
if(player.isIncreaseSize()) player.increaseSnake(latestCoordinate);
|
||||
|
||||
clearTerminal();
|
||||
map.clearMap();
|
||||
TerminalDisplay.clearTerminal();
|
||||
map.clearMap(edges);
|
||||
player.incrementRound();
|
||||
return true;
|
||||
}
|
||||
@@ -111,29 +86,25 @@ public class Terminal {
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void sleepTerminal(long value) {
|
||||
try {Thread.sleep(value);}
|
||||
catch (InterruptedException e){ e.printStackTrace();}
|
||||
}
|
||||
|
||||
public static void run(Personnage[] personnages, Map m, int n) {
|
||||
private static void run() {
|
||||
TerminalDisplay.clearTerminal();
|
||||
if (edges) map.addEdges();
|
||||
boolean isNotGameOver = true;
|
||||
map = m;
|
||||
|
||||
int i = 0;
|
||||
|
||||
// place les personnages dans la grille.
|
||||
placePersonnages(personnages);
|
||||
// print dans le terminal.
|
||||
printPersonnages(personnages);
|
||||
|
||||
while(isNotGameOver) {
|
||||
for (int i = 0; i<personnages.length; i++) {
|
||||
System.out.println("\nJoueur " + (i+1) + " :");
|
||||
isNotGameOver = instancePersonnage(personnages[i]);
|
||||
if (isNotGameOver) placePersonnages(personnages);
|
||||
for (i = 0; i<personnages.length; i++) {
|
||||
Personnage personnage = personnages[i];
|
||||
|
||||
System.out.println("\nJoueur " + (i+1) + " : " + personnage.getName());
|
||||
isNotGameOver = instancePersonnage(personnage);
|
||||
if(isNotGameOver) placePersonnages(personnages);
|
||||
else break;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("bien joué!");
|
||||
System.out.println("Le joueur " + (i+1) + " à perdu !");
|
||||
}
|
||||
}
|
||||
|
||||
132
src/display/TerminalDisplay.java
Normal file
132
src/display/TerminalDisplay.java
Normal file
@@ -0,0 +1,132 @@
|
||||
package Display;
|
||||
|
||||
import Objects.Fruits;
|
||||
import Objects.Items;
|
||||
import Objects.Snake;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import Characters.Personnage;
|
||||
import Environnement.Map;
|
||||
|
||||
public class TerminalDisplay {
|
||||
public static void printMap(Map map, Personnage[] personnages) {
|
||||
Object[][] mapObjects = map.getGrid();
|
||||
|
||||
for (int i = 0; i<mapObjects.length; i++) {
|
||||
for(int k = 0; k<mapObjects[0].length; k++) {
|
||||
Object object = mapObjects[i][k];
|
||||
|
||||
if(object instanceof Items) printItems((Items)object, mapObjects, k, i);
|
||||
if(object instanceof Fruits) printFruits((Fruits)object, i, k);
|
||||
if(object instanceof Snake) printSnake((Snake)object, personnages, i, k);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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");
|
||||
}
|
||||
|
||||
|
||||
public static void printMapType(Map map) {
|
||||
for (Object[] object : map.getGrid()) {
|
||||
for (Object value : object) {
|
||||
System.out.print(value.toString() + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
private static void printItems(Items item, Object[][] map, int x, int y) {
|
||||
switch (item) {
|
||||
case WALL: printWall(map, x, y); break;
|
||||
case VOID: System.out.print(" "); break;
|
||||
}
|
||||
}
|
||||
|
||||
private static void printSnake(Snake item, Personnage[] personnages, int x, int y) {
|
||||
switch (item) {
|
||||
case BODY: System.out.print(" \uF353 "); break;
|
||||
case HEAD: printHead(personnages, x, y); break;
|
||||
}
|
||||
}
|
||||
|
||||
private static void printFruits(Fruits item, int x, int y) {
|
||||
switch (item) {
|
||||
case FRAISE: System.out.print(" \uF353 ");break;
|
||||
}
|
||||
}
|
||||
|
||||
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.getPrimaryCoordinate();
|
||||
|
||||
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(" \u21E8 ");return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static Personnage searchSnake(Personnage[] personnages, int x, int y) {
|
||||
for (Personnage personnage : personnages) {
|
||||
int[] primaryCoordinate = personnage.getPrimaryCoordinate();
|
||||
|
||||
int primaryY = primaryCoordinate[0];
|
||||
int primaryX = primaryCoordinate[1];
|
||||
|
||||
if (primaryX == x && primaryY == y) {
|
||||
return personnage;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void printWall(Object[][] map, int x, int y) {
|
||||
// UP DOWN LEFT RIGHT
|
||||
boolean[] isWall = new boolean[4];
|
||||
|
||||
if (y > 0) isWall[0] = map[y - 1][x] == Items.WALL; else isWall[0] = false;
|
||||
if (y < map.length - 1) isWall[1] = map[y + 1][x] == Items.WALL; else isWall[1] = false;
|
||||
if (x > 0) isWall[2] = map[y][x - 1] == Items.WALL; else isWall[2] = false;
|
||||
if (x < map[0].length - 1) isWall[3] = map[y][x + 1] == Items.WALL; else isWall[3] = false;
|
||||
|
||||
if (isWall[0] && isWall[1] && isWall[2] && isWall[3]) {System.out.print("\u2550\u256C\u2550");return;}
|
||||
else if (isWall[0] && isWall[1] && isWall[3]) {System.out.print("\u2560");return;}
|
||||
else if (isWall[0] && isWall[1] && isWall[2]) {System.out.print("\u2563");return;}
|
||||
else if (isWall[0] && isWall[2] && isWall[3]) {System.out.print("\u2550\u2569\u2550");return;}
|
||||
else if (isWall[1] && isWall[2] && isWall[3]) {System.out.print("\u2550\u2566\u2550");return;}
|
||||
else if (isWall[0] && isWall[1]) {System.out.print("\u2551");return;}
|
||||
else if (isWall[2] && isWall[3]) {System.out.print("\u2550\u2550\u2550");return;}
|
||||
else if (isWall[0] && isWall[2]) {System.out.print("\u255D ");return;}
|
||||
else if (isWall[0] && isWall[3]) {System.out.print("\u255A");return;}
|
||||
else if (isWall[1] && isWall[2]) {System.out.print("\u2557 ");return;}
|
||||
else if (isWall[1] && isWall[3]) {System.out.print("\u2554");return;}
|
||||
else {System.out.print("\u2550\u256C\u2550");return;}
|
||||
}
|
||||
}
|
||||
@@ -1,151 +0,0 @@
|
||||
package Display;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
import Characters.Personnage;
|
||||
import Characters.Players;
|
||||
import Characters.Robot;
|
||||
import Environnement.Map;
|
||||
import Objects.Fruits;
|
||||
import Objects.Items;
|
||||
import Objects.Snake;
|
||||
|
||||
public class Test {
|
||||
private static Scanner scanner = new Scanner(System.in);
|
||||
private static String name = new String();
|
||||
|
||||
/**
|
||||
* cette fonction clear le terminal et le remet en
|
||||
* haut a gauche de la ligne.
|
||||
*/
|
||||
private static void clearTerminal() {
|
||||
System.out.println("\u001b[2J \u001b[H");
|
||||
}
|
||||
|
||||
private static int getInput(Scanner scanner, Players player) {
|
||||
String value = new String();
|
||||
Integer input = null;
|
||||
|
||||
do {
|
||||
value = scanner.nextLine();
|
||||
input = player.changeCoordinate(value);
|
||||
} while(player.getMouvement(input) == null);
|
||||
|
||||
return input.intValue();
|
||||
}
|
||||
|
||||
private static void printMap(Map map) {
|
||||
Object[][] mapObjects = map.getGrid();
|
||||
for (int i = 0; i<mapObjects.length; i++) {
|
||||
for(int k = 0; k<mapObjects[0].length; k++) {
|
||||
if (mapObjects[i][k] instanceof Items) System.out.print(((Items)mapObjects[i][k]).getName() + " ");
|
||||
if (mapObjects[i][k] instanceof Snake) System.out.print(((Snake)mapObjects[i][k]).getName() + " ");
|
||||
if (mapObjects[i][k] instanceof Fruits) System.out.print(((Fruits)mapObjects[i][k]).getName() + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
private static void playerRound(Players player) {
|
||||
int input = 0;
|
||||
}
|
||||
|
||||
private static void robotRound(Robot robot) {
|
||||
|
||||
}
|
||||
|
||||
private static void instancePersonnage(Personnage personnage) {
|
||||
if (personnage instanceof Players) {
|
||||
// tour du Player
|
||||
Players player = (Players)personnage;
|
||||
playerRound(player);
|
||||
}
|
||||
|
||||
if (personnage instanceof Robot) {
|
||||
// tour du robot
|
||||
Robot robot = (Robot)personnage;
|
||||
robotRound(robot);
|
||||
}
|
||||
}
|
||||
|
||||
private static void placePersonnages(Personnage[] personnages, Map map) {
|
||||
for(Personnage personnage : personnages) {
|
||||
map.placePersonnages(personnage);
|
||||
}
|
||||
}
|
||||
|
||||
public static void run(Personnage[] personnages, Map map, int n) {
|
||||
while (true) {
|
||||
for (int i = 0; i<personnages.length; i++) {
|
||||
Personnage personnage = personnages[i];
|
||||
|
||||
// if (personnage instanceof Players) {
|
||||
// Players player = (Players)personnage;
|
||||
// int input = 0;
|
||||
// printMap(map);
|
||||
// input = getInput(scanner, player);
|
||||
|
||||
|
||||
// player.moveCoordinate(input);
|
||||
|
||||
// // clearTerminal();
|
||||
// map.clearMap();
|
||||
|
||||
// if(map.isGameOver(input, personnage)) {
|
||||
// System.out.println("le Joueur " + i+1 + " à perdu.");
|
||||
// return;
|
||||
// }
|
||||
// map.placePersonnages(personnage);
|
||||
|
||||
// }
|
||||
|
||||
if (personnage instanceof Players) {
|
||||
Players player = (Players)personnage;
|
||||
}
|
||||
|
||||
if (personnage instanceof Robot) {
|
||||
Robot robot = (Robot)personnage;
|
||||
// tour du bot.
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Scanner scanner = new Scanner(System.in);
|
||||
|
||||
// int input = 0;
|
||||
// Players player = (Players)personnages[0];
|
||||
// int[] value = player.keepLatestCoordinate();
|
||||
|
||||
// map.placePersonnages(player);
|
||||
// input = getInput(scanner, player);
|
||||
// if (map.isGameOver(input, player)) {
|
||||
// System.out.println("pas bien");
|
||||
// return;
|
||||
// }
|
||||
// player.moveCoordinate(input);
|
||||
// player.increaseSnake(value);
|
||||
|
||||
// map.placePersonnages(player);
|
||||
|
||||
// if (map.isGameOver(input, player)) {
|
||||
// System.out.println("pas bien");
|
||||
// return;
|
||||
// }
|
||||
|
||||
// printMap(map);
|
||||
// map.clearMap();
|
||||
|
||||
// Players players = (Players)personnages[0];
|
||||
// map.placePersonnages(players);
|
||||
// printMap(map);
|
||||
// map.clearMap();
|
||||
// int[] value = players.keepLatestCoordinate();
|
||||
// System.out.println(value[0] + " " + value[1]);
|
||||
// players.moveCoordinate(getInput(scanner, players));
|
||||
// players.moveToLatestCoordinate(value);
|
||||
// System.out.println(value[0] + " " + value[1]);
|
||||
// map.placePersonnages(players);
|
||||
// printMap(map);
|
||||
}
|
||||
}
|
||||
145
src/display/Test2.java
Normal file
145
src/display/Test2.java
Normal file
@@ -0,0 +1,145 @@
|
||||
package Display;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
import Characters.Personnage;
|
||||
import Characters.Players;
|
||||
import Characters.Robot;
|
||||
import Environnement.Map;
|
||||
import Objects.Fruits;
|
||||
import Objects.Items;
|
||||
import Objects.Snake;
|
||||
|
||||
public class Test2 {
|
||||
private static Scanner scanner = new Scanner(System.in);
|
||||
private static String name = new String();
|
||||
|
||||
private static Map map;
|
||||
|
||||
/**
|
||||
* <p> Cette fonction est uniquement destiné pour la classe
|
||||
* Players pour recuperer l'input dans le terminal.
|
||||
* @param scanner
|
||||
* @param player
|
||||
* @return il retourne int qui est le char en ascii
|
||||
*/
|
||||
private static int getInput(Scanner scanner, Players player) {
|
||||
String value = new String();
|
||||
Integer input = null;
|
||||
|
||||
do {
|
||||
value = scanner.nextLine();
|
||||
input = player.changeCoordinate(value);
|
||||
}while(player.getMouvement(input) == null);
|
||||
|
||||
return input.intValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* <p> print toute la map.
|
||||
* @param map
|
||||
*/
|
||||
private static void printMap(Map map) {
|
||||
Object[][] mapObjects = map.getGrid();
|
||||
|
||||
for (int i = 0; i<mapObjects.length; i++) {
|
||||
for(int k = 0; k<mapObjects[0].length; k++) {
|
||||
if (mapObjects[i][k] instanceof Items) System.out.print(((Items)mapObjects[i][k]).getName() + " ");
|
||||
if (mapObjects[i][k] instanceof Fruits) System.out.print(((Fruits)mapObjects[i][k]).getName() + " ");
|
||||
if (mapObjects[i][k] instanceof Snake) System.out.print(((Snake)mapObjects[i][k]).getName() + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
private static void printWall(Map map) {
|
||||
Object[][] mapObjects = map.getGrid();
|
||||
|
||||
// for
|
||||
}
|
||||
|
||||
private static void printPersonnages(Personnage[] personnages) {
|
||||
for(int i = 0; i<personnages.length; i++) {
|
||||
int[] coordinate = personnages[i].getPrimaryCoordinate();
|
||||
System.out.println("Joueur " + (i+1) + " sont aux coordonnées : {" + coordinate[0] + "," + coordinate[1] + "}");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* cette fonction clear le terminal et le remet en
|
||||
* haut a gauche de la ligne.
|
||||
*/
|
||||
private static void clearTerminal() {
|
||||
System.out.println("\u001b[2J \u001b[H");
|
||||
}
|
||||
|
||||
private static void placePersonnages(Personnage[] personnages) {
|
||||
for(Personnage personnage : personnages) {
|
||||
map.placePersonnages(personnage);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean playerRound(Players player) {
|
||||
printMap(map);
|
||||
|
||||
int[] latestCoordinate = player.keepLatestCoordinate();
|
||||
int input = getInput(scanner, player);
|
||||
player.moveCoordinate(input);
|
||||
|
||||
if(map.isGameOver(input, player)) {
|
||||
System.out.println("GameOver");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(player.isIncreaseSize()) player.increaseSnake(latestCoordinate);
|
||||
|
||||
clearTerminal();
|
||||
// map.clearMap();
|
||||
player.incrementRound();
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean robotRound(Robot robot) {
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean instancePersonnage(Personnage personnage) {
|
||||
if (personnage instanceof Players) {
|
||||
// tour du Player
|
||||
return playerRound((Players)personnage);
|
||||
}
|
||||
|
||||
if (personnage instanceof Robot) {
|
||||
// tour du robot
|
||||
return robotRound((Robot)personnage);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void sleepTerminal(long value) {
|
||||
try {Thread.sleep(value);}
|
||||
catch (InterruptedException e){ e.printStackTrace();}
|
||||
}
|
||||
|
||||
public static void run(Personnage[] personnages, Map m, int n) {
|
||||
boolean isNotGameOver = true;
|
||||
map = m;
|
||||
|
||||
// place les personnages dans la grille.
|
||||
placePersonnages(personnages);
|
||||
// print dans le terminal.
|
||||
printPersonnages(personnages);
|
||||
|
||||
while(isNotGameOver) {
|
||||
for (int i = 0; i<personnages.length; i++) {
|
||||
System.out.println("\nJoueur " + (i+1) + " :");
|
||||
isNotGameOver = instancePersonnage(personnages[i]);
|
||||
if (isNotGameOver) placePersonnages(personnages);
|
||||
else break;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("bien joué!");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user