mirror of
https://github.com/Cpt-Adok/SNAKE.git
synced 2026-01-25 12:34:07 +00:00
changement de plein de fonction
This commit is contained in:
2
Makefile
2
Makefile
@@ -14,7 +14,7 @@ LIB_DIR = lib
|
|||||||
JAR = $(LIB_DIR)/*
|
JAR = $(LIB_DIR)/*
|
||||||
|
|
||||||
# main
|
# main
|
||||||
all: $(MAIN_FILE) run clean
|
all: clean $(MAIN_FILE) run clean
|
||||||
|
|
||||||
$(MAIN_FILE) : $(BIN_DIR)/$(MAIN_FILE).class
|
$(MAIN_FILE) : $(BIN_DIR)/$(MAIN_FILE).class
|
||||||
|
|
||||||
|
|||||||
171
res/ASCII-Table.svg
Normal file
171
res/ASCII-Table.svg
Normal file
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 1.5 MiB |
@@ -1,13 +1,14 @@
|
|||||||
package Environnements;
|
package environnements;
|
||||||
|
|
||||||
import java.lang.Object;
|
import java.lang.Object;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
import Item.Effects;
|
import item.Effects;
|
||||||
import Item.Items;
|
import item.Items;
|
||||||
|
|
||||||
public class Map {
|
public class Map {
|
||||||
private Object[][] grid;
|
private Object[][] grid;
|
||||||
|
private Object[][] emptyGrid;
|
||||||
private Random random;
|
private Random random;
|
||||||
|
|
||||||
public int longueur;
|
public int longueur;
|
||||||
@@ -20,15 +21,14 @@ public class Map {
|
|||||||
this.random = new Random();
|
this.random = new Random();
|
||||||
|
|
||||||
this.grid = new Object[this.largeur][this.longueur];
|
this.grid = new Object[this.largeur][this.longueur];
|
||||||
|
this.emptyGrid = new Object[this.largeur][this.longueur];
|
||||||
|
|
||||||
this.fillGrid();
|
this.fillGrid();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Items getCoordinate(int x, int y) {
|
public Items getCoordinate(int x, int y) {
|
||||||
if (x >= 0 && x < grid[0].length && y >= 0 && y < grid.length) {
|
if (x >= 0 && x < grid[0].length && y >= 0 && y < grid.length) {
|
||||||
Object coordinate = grid[y][x];
|
return (Items)grid[y][x];
|
||||||
if (coordinate instanceof Items) {
|
|
||||||
return ((Items)coordinate);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -38,9 +38,13 @@ public class Map {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isGameOver(int[] coordinate) {
|
public boolean isGameOver(int[] coordinate) {
|
||||||
if (getCoordinate(longueur, largeur) != null &&
|
Items item = getCoordinate(coordinate[0], coordinate[1]);
|
||||||
getCoordinate(longueur, largeur).getEffects() == Effects.IMPASSABLE) return true;
|
|
||||||
return false;
|
if (item != null) {
|
||||||
|
Effects effect = item.getEffects();
|
||||||
|
return effect == Effects.IMPASSABLE;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getStringGrid() {
|
public String getStringGrid() {
|
||||||
@@ -70,6 +74,10 @@ public class Map {
|
|||||||
return grid;
|
return grid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void cleanGrid() {
|
||||||
|
this.grid = this.emptyGrid;
|
||||||
|
}
|
||||||
|
|
||||||
public void ajoutBordure() {
|
public void ajoutBordure() {
|
||||||
for(int i = 0; i < this.grid.length; i++) {
|
for(int i = 0; i < this.grid.length; i++) {
|
||||||
for(int k = 0; k < this.grid[0].length; k++) {
|
for(int k = 0; k < this.grid[0].length; k++) {
|
||||||
@@ -88,6 +96,16 @@ public class Map {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addCoordinate(int[] coordinate, Items items) {
|
||||||
|
for(int i = 0; i < this.grid.length; i++) {
|
||||||
|
for(int k = 0; k < this.grid[0].length; k++) {
|
||||||
|
if(i == coordinate[1] && k == coordinate[0]) {
|
||||||
|
this.grid[i][k] = items;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void randomize(Items item, int number) {
|
private void randomize(Items item, int number) {
|
||||||
for(int i = 0; i<number; i++) {
|
for(int i = 0; i<number; i++) {
|
||||||
int x = this.random.nextInt(1, this.grid[0].length);
|
int x = this.random.nextInt(1, this.grid[0].length);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package Item;
|
package item;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Ceci est l'enumération où il y aura tout les effets disponible dans le projet.
|
* <p>Ceci est l'enumération où il y aura tout les effets disponible dans le projet.
|
||||||
@@ -26,5 +26,7 @@ public enum Effects {
|
|||||||
/**
|
/**
|
||||||
* <p> impossible à passer à travers.
|
* <p> impossible à passer à travers.
|
||||||
*/
|
*/
|
||||||
IMPASSABLE, Effects;
|
IMPASSABLE,
|
||||||
|
|
||||||
|
VOID;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package Item;
|
package item;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cette enumération contient tout les items à effets disponnible dans le jeu.
|
* Cette enumération contient tout les items à effets disponnible dans le jeu.
|
||||||
@@ -8,7 +8,8 @@ public enum Items {
|
|||||||
|
|
||||||
BODY("corps", Effects.IMPASSABLE),
|
BODY("corps", Effects.IMPASSABLE),
|
||||||
HEAD("tete", Effects.IMPASSABLE),
|
HEAD("tete", Effects.IMPASSABLE),
|
||||||
VOID("void", null),
|
|
||||||
|
VOID("void", Effects.VOID),
|
||||||
|
|
||||||
FRAISE("fraise", Effects.INVINCIBILITY),
|
FRAISE("fraise", Effects.INVINCIBILITY),
|
||||||
ORANGE("orange", Effects.POWER),
|
ORANGE("orange", Effects.POWER),
|
||||||
|
|||||||
@@ -1,20 +1,15 @@
|
|||||||
import java.lang.Object;
|
import environnements.Map;
|
||||||
import java.util.function.Supplier;
|
import personnages.Personnage;
|
||||||
|
|
||||||
import Environnements.Map;
|
|
||||||
import Item.Items;
|
|
||||||
import personnages.Player;
|
import personnages.Player;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
int n = 2;
|
int n = 2;
|
||||||
Map map = new Map(10, 10);
|
|
||||||
|
|
||||||
Player player1 = new Player(n, new int[]{1, 1});
|
Personnage[] players = new Personnage[] {
|
||||||
Player player2 = new Player(n, new int[]{9, 9});
|
new Player("player1", n, new int[]{2, 2})
|
||||||
|
};
|
||||||
|
|
||||||
while(!map.isGameOver(player1.getCoordinate()) || !map.isGameOver(player2.getCoordinate())) {
|
display.Terminal.run(players, new Map(10, 10), 2);
|
||||||
player1.changeCoordinate();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
63
src/display/Terminal.java
Normal file
63
src/display/Terminal.java
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
package display;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
import environnements.Map;
|
||||||
|
import item.Items;
|
||||||
|
import personnages.Personnage;
|
||||||
|
import personnages.Player;
|
||||||
|
|
||||||
|
public class Terminal {
|
||||||
|
private static void clearTerminal() {
|
||||||
|
System.out.println("\u001b[2J \u001b[H");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void showMap(Object[][] grid) {
|
||||||
|
for (int i = 0; i<grid.length; i++) {
|
||||||
|
for(int k = 0; k<grid[0].length; k++) {
|
||||||
|
System.out.print(((Items)grid[i][k]).getName() + " ");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void getInput(Scanner scanner, Player player) {
|
||||||
|
String value = new String();
|
||||||
|
|
||||||
|
do {
|
||||||
|
value = scanner.nextLine();
|
||||||
|
} while(!player.changeCoordinate(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isGameOver(Personnage[] personnages, Map map) {
|
||||||
|
boolean gameover = false;
|
||||||
|
for(Personnage personnage : personnages) {
|
||||||
|
if (gameover = map.isGameOver(personnage.getCoordinate())) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return gameover;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void run(Personnage[] personnages, Map map, long n) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
for(Personnage personnage : personnages) {
|
||||||
|
map.addCoordinate(personnage.getCoordinate(), Items.HEAD);
|
||||||
|
}
|
||||||
|
|
||||||
|
showMap(map.getGrid());
|
||||||
|
|
||||||
|
System.out.println(map.isGameOver(personnages[0].getCoordinate()));
|
||||||
|
|
||||||
|
while (!isGameOver(personnages, map)) {
|
||||||
|
for(Personnage personnage : personnages) {
|
||||||
|
getInput(scanner, (Player)personnage);
|
||||||
|
map.addCoordinate(((Player)personnage).getCoordinate(), Items.HEAD);
|
||||||
|
}
|
||||||
|
|
||||||
|
map.cleanGrid();
|
||||||
|
clearTerminal();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,8 +2,8 @@ package personnages;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import Item.Effects;
|
import item.Effects;
|
||||||
import Item.Items;
|
import item.Items;
|
||||||
|
|
||||||
public class Personnage {
|
public class Personnage {
|
||||||
/**
|
/**
|
||||||
@@ -18,6 +18,8 @@ public class Personnage {
|
|||||||
*/
|
*/
|
||||||
private int round;
|
private int round;
|
||||||
|
|
||||||
|
private String nom;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p> la liste de toute les coordonnées en fonction de N. Si N = 2,
|
* <p> la liste de toute les coordonnées en fonction de N. Si N = 2,
|
||||||
* tout les deux tours, la taille du serpent augmente de 1. Si N = 3,
|
* tout les deux tours, la taille du serpent augmente de 1. Si N = 3,
|
||||||
@@ -32,19 +34,28 @@ public class Personnage {
|
|||||||
* <p> le constructor definie un arrayList pour {@link #coordinate}
|
* <p> le constructor definie un arrayList pour {@link #coordinate}
|
||||||
* et defini n.
|
* et defini n.
|
||||||
*
|
*
|
||||||
|
* @param name est le nom du personnage.
|
||||||
* @param n est une variable qui contient le nombre de tour avant
|
* @param n est une variable qui contient le nombre de tour avant
|
||||||
* l'augmentation de taille.
|
* l'augmentation de taille.
|
||||||
* @param coordinate est la variable qui contient les coordonnées
|
* @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) {
|
protected Personnage(String name, int n, int[] coordinate) {
|
||||||
this.coordinate = new ArrayList<int[]>();
|
this.coordinate = new ArrayList<int[]>();
|
||||||
this.coordinate.add(coordinate);
|
this.coordinate.add(coordinate);
|
||||||
|
|
||||||
this.n = n;
|
this.n = n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return nom;
|
||||||
|
}
|
||||||
|
|
||||||
public int incrementRound() {
|
public int incrementRound() {
|
||||||
return ++this.round;
|
return ++this.round;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int[] getCoordinate() {
|
||||||
|
return coordinate.get(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package personnages;
|
package personnages;
|
||||||
|
|
||||||
|
import static java.util.Arrays.toString;
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -16,32 +18,24 @@ public class Player extends Personnage {
|
|||||||
* est représenté comme <strong>{x, y}</strong> et qui represente les
|
* est représenté comme <strong>{x, y}</strong> et qui represente les
|
||||||
* coordonnées de la tête du personnage.
|
* coordonnées de la tête du personnage.
|
||||||
*/
|
*/
|
||||||
public Player(int n, int[] coordinate) {
|
public Player(String name, int n, int[] coordinate) {
|
||||||
super(n, coordinate);
|
super(name, n, coordinate);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void changeCoordinate() {
|
public boolean changeCoordinate(String input) {
|
||||||
Scanner scanner = new Scanner(System.in);
|
if (input.length() > 0) {
|
||||||
String value;
|
return moveCoordinate(input.charAt(0));
|
||||||
|
}
|
||||||
do {
|
return false;
|
||||||
value = scanner.nextLine();
|
|
||||||
} while (!moveCoordinate((int)'w'));
|
|
||||||
|
|
||||||
scanner.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
public int[] getCoordinate() {
|
|
||||||
return coordinate.get(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean moveCoordinate(int keys) {
|
private boolean moveCoordinate(int keys) {
|
||||||
switch (keys) {
|
switch (keys) {
|
||||||
case 119: Mouvement.HAUT.editCoordinate(getCoordinate()); break;
|
case 0x77: Mouvement.HAUT.editCoordinate(getCoordinate()); break; // w
|
||||||
case 115: Mouvement.BAS.editCoordinate(getCoordinate()); break;
|
case 0x73: Mouvement.BAS.editCoordinate(getCoordinate()); break; // s
|
||||||
case 97: Mouvement.GAUCHE.editCoordinate(getCoordinate()); break;
|
case 0x61: Mouvement.GAUCHE.editCoordinate(getCoordinate()); break; // a
|
||||||
case 100: Mouvement.DROITE.editCoordinate(getCoordinate()); break;
|
case 0x64: Mouvement.DROITE.editCoordinate(getCoordinate()); break; // d
|
||||||
default: return false;
|
default: return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package personnages;
|
package personnages;
|
||||||
|
|
||||||
public class Robot extends Personnage {
|
public class Robot extends Personnage {
|
||||||
public Robot(int size, int[] coordinate) {
|
public Robot(String name, int size, int[] coordinate) {
|
||||||
super(size, coordinate);
|
super(name, size, coordinate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user