mirror of
https://github.com/Cpt-Adok/SNAKE.git
synced 2026-01-25 15:34:07 +00:00
changement de plein de trucs (manque juste a regler un probleme)
This commit is contained in:
@@ -22,17 +22,17 @@ public enum Mouvements {
|
||||
|
||||
/**
|
||||
* GAUCHE prend comme coordonnée (1, 0) pour se déplacer.
|
||||
* @param x = 1
|
||||
* @param y = 0
|
||||
*/
|
||||
GAUCHE(1, 0),
|
||||
|
||||
/**
|
||||
* @DROITE prend comme coordonnée (-1, 0) pour se déplacer.
|
||||
* @param x = -1
|
||||
* @param y = 0
|
||||
*/
|
||||
DROITE(-1, 0);
|
||||
GAUCHE(-1, 0),
|
||||
|
||||
/**
|
||||
* @DROITE prend comme coordonnée (-1, 0) pour se déplacer.
|
||||
* @param x = 1
|
||||
* @param y = 0
|
||||
*/
|
||||
DROITE(1, 0);
|
||||
|
||||
private final int deltaX;
|
||||
private final int deltaY;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package Characters;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import Objects.Snake;
|
||||
|
||||
@@ -12,7 +13,6 @@ public class Personnage {
|
||||
private int n;
|
||||
private int round;
|
||||
private int size = 0;
|
||||
private Snake head = Snake.HEAD;
|
||||
|
||||
/**
|
||||
* <p> la liste de toute les coordonnées en fonction de N. Si N = 2,
|
||||
@@ -28,11 +28,10 @@ public class Personnage {
|
||||
* <p> le constructor definie un arrayList pour {@link #coordinate}
|
||||
* et defini n.
|
||||
*
|
||||
* @param name est le nom du personnage.
|
||||
* @param n est une variable qui contient le nombre de tour avant
|
||||
* l'augmentation de taille.
|
||||
* @param coordinate est la variable qui contient les coordonnées
|
||||
* qui sont placé par la suite dans {@link #coordinate}[0]
|
||||
* qui sont placé par la suite dans {@link #coordinate}[0]}
|
||||
*/
|
||||
protected Personnage(int n, int[] coordinate) {
|
||||
this.coordinate = new ArrayList<int[]>();
|
||||
@@ -42,8 +41,8 @@ public class Personnage {
|
||||
}
|
||||
|
||||
/**
|
||||
* cette fonction retourne la premiere coordonnée de la liste {@link
|
||||
* #coordinate} qui la tête du personnage
|
||||
* <p> cette fonction retourne la premiere coordonnée de la liste
|
||||
* {@link #coordinate} qui la tête du personnage.
|
||||
* @return la tête du personnage.
|
||||
*/
|
||||
public int[] getPrimaryCoordinate() {
|
||||
@@ -51,8 +50,8 @@ public class Personnage {
|
||||
}
|
||||
|
||||
/**
|
||||
* cette fonction retourne toute la liste {@link #coordinate} de
|
||||
* coordonnée du serpent.
|
||||
* <p> cette fonction retourne toute la liste
|
||||
* {@link #coordinate} de coordonnée du serpent.
|
||||
* @return toute la liste des coordonnées du serpent
|
||||
*/
|
||||
public ArrayList<int[]> getCoordinate() {
|
||||
@@ -95,12 +94,12 @@ public class Personnage {
|
||||
/**
|
||||
* <p> cette fonction est très pratique aggrandir le serpent
|
||||
* car elle garde la derniere coordonnée et si on la fusionne
|
||||
* avec {@link #moveToLatestCoordinate()}, on peut l'utiliser
|
||||
* avec {@link #increaseSnake()}, on peut l'utiliser
|
||||
* ajouter la coordonnée pour justement l'aggrandir.
|
||||
* @return garde la derniere coordonnée du serpent (sa queue)
|
||||
*/
|
||||
public int[] keepLatestCoordinate() {
|
||||
return this.coordinate.get(getCoordinate().size()-1);
|
||||
return this.coordinate.get(getCoordinate().size()-1).clone();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,7 +108,7 @@ public class Personnage {
|
||||
* le serpent.
|
||||
* @param coordinate ajout de la nouvelle coordonnée
|
||||
*/
|
||||
public void moveToLatestCoordinate(int[] coordinate) {
|
||||
public void increaseSnake(int[] coordinate) {
|
||||
this.coordinate.add(coordinate);
|
||||
}
|
||||
|
||||
@@ -118,19 +117,23 @@ public class Personnage {
|
||||
* serpent.
|
||||
* @param mouvements le mouvement utilisé pour deplacer le serpent
|
||||
*/
|
||||
protected void moveSnake(Mouvements mouvements) {
|
||||
for (int[] coordinate : this.coordinate) {
|
||||
mouvements.editCoordinate(coordinate);
|
||||
public void moveSnake(Mouvements mouvements) {
|
||||
mouvements.editCoordinate(this.coordinate.get(0));
|
||||
|
||||
for(int i = this.coordinate.size() - 1; i>1; i--) {
|
||||
int[] value = this.coordinate.get(i-1);
|
||||
this.coordinate.set(i, value);
|
||||
}
|
||||
}
|
||||
|
||||
public Mouvements getMouvement(int keys) {
|
||||
public Mouvements getMouvement(Integer keys) {
|
||||
switch (keys) {
|
||||
case 0x77: return Mouvements.HAUT; // w
|
||||
case 0x73: return Mouvements.BAS; // s
|
||||
case 0x61: return Mouvements.GAUCHE; // a
|
||||
case 0x64: return Mouvements.DROITE; // d
|
||||
default: return null;
|
||||
case 0x77: return Mouvements.HAUT; // w
|
||||
case 0x73: return Mouvements.BAS; // s
|
||||
case 0x61: return Mouvements.GAUCHE; // a
|
||||
case 0x64: return Mouvements.DROITE; // d
|
||||
case null: return null;
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,15 +110,44 @@ public class Map {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isGamefinished(int key, Personnage personnage) {
|
||||
public void placePersonnages(Personnage personnage) {
|
||||
int index = 0;
|
||||
|
||||
for (int[] coordinate : personnage.getCoordinate()) {
|
||||
if (index == 0) {
|
||||
this.grid[coordinate[1]][coordinate[0]] = Snake.HEAD;
|
||||
|
||||
} else {
|
||||
this.grid[coordinate[1]][coordinate[0]] = Snake.BODY;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isGamefinishedImpassable(int key, Personnage personnage) {
|
||||
int[] coordinate = personnage.getMouvement(key).getCoordinate();
|
||||
int[] playerCoordinate = personnage.getPrimaryCoordinate();
|
||||
|
||||
|
||||
int y = coordinate[1] + playerCoordinate[1];
|
||||
int x = coordinate[0] + playerCoordinate[0];
|
||||
|
||||
return this.getGrid()[y][x] instanceof Snake;
|
||||
Object grid = this.getGrid()[y][x];
|
||||
|
||||
if (grid instanceof Snake) return ((Snake)grid).getEffects() == Effects.IMPASSABLE;
|
||||
else if (grid instanceof Items) return ((Items)grid).getEffects() == Effects.IMPASSABLE;
|
||||
else return false;
|
||||
}
|
||||
|
||||
public boolean isGameOver(int key, Personnage personnage) {
|
||||
int[] personnageCoordinate = personnage.getPrimaryCoordinate();
|
||||
|
||||
boolean isOutX = personnageCoordinate[0] < 0 || this.grid[0].length - 1 < personnageCoordinate[0];
|
||||
boolean isOutY = personnageCoordinate[1] < 0 || this.grid.length - 1 < personnageCoordinate[1];
|
||||
|
||||
if (isOutX || isOutY) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void fillGrid() {
|
||||
|
||||
@@ -1,31 +1,31 @@
|
||||
import Characters.*;
|
||||
import Characters.Personnage;
|
||||
import Characters.Players;
|
||||
import Display.Terminal;
|
||||
import Environnement.Map;
|
||||
|
||||
public class Main {
|
||||
// public static void print(Players player) {
|
||||
// for (int[] coordinate : player.getCoordinate()) {
|
||||
// for(int value : coordinate) {
|
||||
// System.out.print(value + " ");
|
||||
// }
|
||||
// System.out.println();
|
||||
// }
|
||||
// System.out.println();
|
||||
// }
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Players player = new Players(new int[] {-1, 1}, 2);
|
||||
int N = 2;
|
||||
|
||||
// while(true) {
|
||||
// int[] value = player.keepLatestCoordinate();
|
||||
Personnage[] personnages = new Personnage[] {
|
||||
new Players(new int[] {1, 1}, N),
|
||||
new Players(new int[] {8, 8}, N)
|
||||
};
|
||||
|
||||
// player.incrementRound();
|
||||
// print(player);
|
||||
// if (player.isIncreaseSize()) player.moveToLatestCoordinate(value);
|
||||
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.run(new Personnage[]{
|
||||
new Players(new int[] {1, 1}, 2)
|
||||
}, new Map(10, 10), 2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,15 +2,63 @@ package Display;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
import Characters.Mouvements;
|
||||
import Characters.Personnage;
|
||||
import Characters.Players;
|
||||
import Characters.Robot;
|
||||
import Environnement.Map;
|
||||
import Objects.Fruits;
|
||||
import Objects.Items;
|
||||
import Objects.Snake;
|
||||
|
||||
public class Terminal {
|
||||
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 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.
|
||||
@@ -19,24 +67,73 @@ public class Terminal {
|
||||
System.out.println("\u001b[2J \u001b[H");
|
||||
}
|
||||
|
||||
private static int getInput(Scanner scanner, Players player) {
|
||||
String value = new String();
|
||||
int input = 0;
|
||||
|
||||
do {
|
||||
value = scanner.nextLine();
|
||||
} while(player.getMouvement(input = player.changeCoordinate(value)) == null);
|
||||
|
||||
return input;
|
||||
private static void placePersonnages(Personnage[] personnages) {
|
||||
for(Personnage personnage : personnages) {
|
||||
map.placePersonnages(personnage);
|
||||
}
|
||||
}
|
||||
|
||||
private static void printMap(Map map) {
|
||||
Object[][] mapObjects = map.getGrid();
|
||||
}
|
||||
|
||||
public static void run(Personnage[] personnages, Map map, int n) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
private static boolean playerRound(Players player) {
|
||||
printMap(map);
|
||||
scanner.close();
|
||||
|
||||
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é!");
|
||||
}
|
||||
}
|
||||
|
||||
151
src/display/Test.java
Normal file
151
src/display/Test.java
Normal file
@@ -0,0 +1,151 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user