changement de plein de fonction

This commit is contained in:
2024-05-09 18:52:12 +02:00
parent 83ab978dda
commit 7831172941
10 changed files with 308 additions and 53 deletions

View File

@@ -14,7 +14,7 @@ LIB_DIR = lib
JAR = $(LIB_DIR)/*
# main
all: $(MAIN_FILE) run clean
all: clean $(MAIN_FILE) run clean
$(MAIN_FILE) : $(BIN_DIR)/$(MAIN_FILE).class

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

View File

@@ -1,13 +1,14 @@
package Environnements;
package environnements;
import java.lang.Object;
import java.util.Random;
import Item.Effects;
import Item.Items;
import item.Effects;
import item.Items;
public class Map {
private Object[][] grid;
private Object[][] emptyGrid;
private Random random;
public int longueur;
@@ -20,15 +21,14 @@ public class Map {
this.random = new Random();
this.grid = new Object[this.largeur][this.longueur];
this.emptyGrid = new Object[this.largeur][this.longueur];
this.fillGrid();
}
public Items getCoordinate(int x, int y) {
if (x >= 0 && x < grid[0].length && y >= 0 && y < grid.length) {
Object coordinate = grid[y][x];
if (coordinate instanceof Items) {
return ((Items)coordinate);
}
return (Items)grid[y][x];
}
return null;
}
@@ -38,9 +38,13 @@ public class Map {
}
public boolean isGameOver(int[] coordinate) {
if (getCoordinate(longueur, largeur) != null &&
getCoordinate(longueur, largeur).getEffects() == Effects.IMPASSABLE) return true;
return false;
Items item = getCoordinate(coordinate[0], coordinate[1]);
if (item != null) {
Effects effect = item.getEffects();
return effect == Effects.IMPASSABLE;
}
return true;
}
public String getStringGrid() {
@@ -70,6 +74,10 @@ public class Map {
return grid;
}
public void cleanGrid() {
this.grid = this.emptyGrid;
}
public void ajoutBordure() {
for(int i = 0; i < this.grid.length; i++) {
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) {
for(int i = 0; i<number; i++) {
int x = this.random.nextInt(1, this.grid[0].length);

View File

@@ -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.
@@ -26,5 +26,7 @@ public enum Effects {
/**
* <p> impossible à passer à travers.
*/
IMPASSABLE, Effects;
IMPASSABLE,
VOID;
}

View File

@@ -1,4 +1,4 @@
package Item;
package item;
/**
* Cette enumération contient tout les items à effets disponnible dans le jeu.
@@ -8,7 +8,8 @@ public enum Items {
BODY("corps", Effects.IMPASSABLE),
HEAD("tete", Effects.IMPASSABLE),
VOID("void", null),
VOID("void", Effects.VOID),
FRAISE("fraise", Effects.INVINCIBILITY),
ORANGE("orange", Effects.POWER),

View File

@@ -1,20 +1,15 @@
import java.lang.Object;
import java.util.function.Supplier;
import Environnements.Map;
import Item.Items;
import environnements.Map;
import personnages.Personnage;
import personnages.Player;
public class Main {
public static void main(String[] args) {
int n = 2;
Map map = new Map(10, 10);
Player player1 = new Player(n, new int[]{1, 1});
Player player2 = new Player(n, new int[]{9, 9});
Personnage[] players = new Personnage[] {
new Player("player1", n, new int[]{2, 2})
};
while(!map.isGameOver(player1.getCoordinate()) || !map.isGameOver(player2.getCoordinate())) {
player1.changeCoordinate();
}
display.Terminal.run(players, new Map(10, 10), 2);
}
}

63
src/display/Terminal.java Normal file
View 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();
}
}
}

View File

@@ -2,8 +2,8 @@ package personnages;
import java.util.ArrayList;
import Item.Effects;
import Item.Items;
import item.Effects;
import item.Items;
public class Personnage {
/**
@@ -18,6 +18,8 @@ public class Personnage {
*/
private int round;
private String nom;
/**
* <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,
@@ -32,19 +34,28 @@ 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]
*/
protected Personnage(int n, int[] coordinate) {
protected Personnage(String name, int n, int[] coordinate) {
this.coordinate = new ArrayList<int[]>();
this.coordinate.add(coordinate);
this.n = n;
}
public String getName() {
return nom;
}
public int incrementRound() {
return ++this.round;
}
public int[] getCoordinate() {
return coordinate.get(0);
}
}

View File

@@ -1,5 +1,7 @@
package personnages;
import static java.util.Arrays.toString;
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
* coordonnées de la tête du personnage.
*/
public Player(int n, int[] coordinate) {
super(n, coordinate);
public Player(String name, int n, int[] coordinate) {
super(name, n, coordinate);
}
public void changeCoordinate() {
Scanner scanner = new Scanner(System.in);
String value;
do {
value = scanner.nextLine();
} while (!moveCoordinate((int)'w'));
scanner.close();
}
public int[] getCoordinate() {
return coordinate.get(0);
public boolean changeCoordinate(String input) {
if (input.length() > 0) {
return moveCoordinate(input.charAt(0));
}
return false;
}
private boolean moveCoordinate(int keys) {
switch (keys) {
case 119: Mouvement.HAUT.editCoordinate(getCoordinate()); break;
case 115: Mouvement.BAS.editCoordinate(getCoordinate()); break;
case 97: Mouvement.GAUCHE.editCoordinate(getCoordinate()); break;
case 100: Mouvement.DROITE.editCoordinate(getCoordinate()); break;
default: return false;
case 0x77: Mouvement.HAUT.editCoordinate(getCoordinate()); break; // w
case 0x73: Mouvement.BAS.editCoordinate(getCoordinate()); break; // s
case 0x61: Mouvement.GAUCHE.editCoordinate(getCoordinate()); break; // a
case 0x64: Mouvement.DROITE.editCoordinate(getCoordinate()); break; // d
default: return false;
}
return true;
}

View File

@@ -1,7 +1,7 @@
package personnages;
public class Robot extends Personnage {
public Robot(int size, int[] coordinate) {
super(size, coordinate);
public Robot(String name, int size, int[] coordinate) {
super(name, size, coordinate);
}
}