Reprise de Channel, toujours pas fini

This commit is contained in:
Cpt-Adok
2024-05-21 15:41:41 +02:00
parent c3baac913d
commit c34925f6e4
5 changed files with 198 additions and 36 deletions

View File

@@ -4,7 +4,7 @@ public class Players extends Personnage {
public Players(String name, int[] coordinate) {
super(name, coordinate);
}
public Integer changeCoordinate(String input) {
if (input.length() > 0) {
return (int)input.charAt(0);

View File

@@ -1,8 +1,6 @@
package Characters;
import java.util.ArrayList;
//import java.util.Random;
import javax.swing.JOptionPane;
import Environnement.*;
@@ -10,18 +8,32 @@ import Objects.Effects;
public class Robot extends Personnage {
public Robot(String name, int[] coordinate) {
Map m;
Mouvements move;
public Robot(String name, int[] coordinate,Map m) {
super(name, coordinate);
this.m=m;
move=this.compare(this.getCoordinate().get(0), this.choix().get(0));
jouer(m);
}
public Mouvements jouer(Map m){
Mouvements move=this.compare(this.getCoordinate().get(0), this.choix(m).get(0));
return move;
public String jouer(Map m){
if (this.move==Mouvements.HAUT){
return "U";
}else if(this.move==Mouvements.BAS){
return "D";
}else if(this.move==Mouvements.GAUCHE){
return "L";
}else if(this.move==Mouvements.DROITE){
return "R";
}
return "Problème";
}
public boolean estPossible(int x,int y,Map m){
JOptionPane.showInputDialog((m.getGrid().length+" "+ m.getGrid()[0].length).toString());
Object [][] grille=new Object[][] {m.getGrid()};
public boolean estPossible(int x,int y){
JOptionPane.showInputDialog((this.m.getGrid().length+" "+ this.m.getGrid()[0].length).toString());
Object [][] grille=new Object[][] {this.m.getGrid()};
if (grille[x][y]==Effects.IMPASSABLE){
return false;
}
@@ -33,33 +45,33 @@ public class Robot extends Personnage {
return t;
}
public ArrayList<int []> coupsPossibles(int [] co,Map m) {
public ArrayList<int []> coupsPossibles(int [] co) {
ArrayList<int []> coupsValables=new ArrayList<int []> ();
if (this.estPossible(co[0]+1,co[1], m)){
if (this.estPossible(co[0]+1,co[1])){
coupsValables.add(creerTab(co[0]+1, co[1]));
}else if (this.estPossible(co[0],co[1]+1, m)){
}else if (this.estPossible(co[0],co[1]+1)){
coupsValables.add(creerTab(co[0], co[1]+1));
}else if (this.estPossible(co[0]-1,co[1], m)){
}else if (this.estPossible(co[0]-1,co[1])){
coupsValables.add(creerTab(co[0]-1, co[1]));
}else if (this.estPossible(co[0],co[1]-1, m)){
}else if (this.estPossible(co[0],co[1]-1)){
coupsValables.add(creerTab(co[0], co[1]-1));
}
return coupsValables;
}
public ArrayList <int []> casesAutour(Map m){
ArrayList <int []> t =this.coupsPossibles(this.getCoordinate().get(0),m);
public ArrayList <int []> casesAutour(){
ArrayList <int []> t =this.coupsPossibles(this.getCoordinate().get(0));
ArrayList <int []> t2 = new ArrayList<> ();
for (int i=0;i<t.size();i++){
t.get(i)[0]+=1;
this.fusion(t2,this.coupsPossibles(t.get(i), m));
this.fusion(t2,this.coupsPossibles(t.get(i)));
t.get(i)[0]-=2;
this.fusion(t2,this.coupsPossibles(t.get(i), m));
this.fusion(t2,this.coupsPossibles(t.get(i)));
t.get(i)[0]+=1;
t.get(i)[1]+=1;
this.fusion(t2,this.coupsPossibles(t.get(i), m));
this.fusion(t2,this.coupsPossibles(t.get(i)));
t.get(i)[1]-=2;
this.fusion(t2,this.coupsPossibles(t.get(i), m));
this.fusion(t2,this.coupsPossibles(t.get(i)));
}
this.killDouble(t2);
return t2;
@@ -72,11 +84,11 @@ public class Robot extends Personnage {
return t;
}
public ArrayList <int []> choix(Map m){
ArrayList <int[]> cases=casesAutour(m);
public ArrayList <int []> choix(){
ArrayList <int[]> cases=casesAutour();
ArrayList <ArrayList <int []>> w=new ArrayList<>();
for (int i=0;i<cases.size();i++){
w.add(this.coupsPossibles(casesAutour(m).get(i),m));
w.add(this.coupsPossibles(casesAutour().get(i)));
}
ArrayList<int []> max=w.get(0);
for (ArrayList <int []> e :w){

View File

@@ -1,23 +1,63 @@
package Connexion;
import Connexion.Reseau;
import display.*;
import java.util.ArrayList;
public class Channel extends Reseau{
import javax.swing.JOptionPane;
import Environnement.*;
import Characters.*;
import display.TerminalChannel;
public class Channel{
Reseau reseau;
ArrayList<Personnage> personnages;
int numJoueur;
TerminalChannel term;
Map m;
public Channel(String channel, Terminal t, int numJoueur){
super(channel);
this.numJoueur=numJoueur;
public Channel(Reseau r, ArrayList<Personnage> p, int j,TerminalChannel term,Map m){
this.reseau=r;
this.personnages=p;
this.numJoueur=j;
this.term=term;
this.m=m;
}
public void partie(Terminal t){
boolean partie=true;
while(partie){
if (t.getRound()%2!=this.numJoueur){
this.getNewArrayListContent();
public String typePersonnage(int i){
if (i>=0 && i<personnages.size()){
Personnage p=personnages.get(i);
if (p instanceof Characters.Players){
return "J";
}
return "R";
}
return null;
}
public void jeu(){
String j1=this.typePersonnage(0);
Players player=(Players) this.personnages.get(0);
while (term.run()){
if (term.round%2!=0){
term.playerRound(player);
}else{
reseau.sendContent(this.jouer(j1));
}
}
}
public String jouer(String j){
if (j=="J"){
Players player=(Players) this.personnages.get(0);
return this.getInput(reseau);
}
Robot bot=(Robot) this.personnages.get(0);
return bot.jouer(m);
}
public String getInput(Reseau r){
String c=" ";
return c;
}
}

View File

@@ -87,7 +87,7 @@ public class Terminal {
return false;
}
private void run() {
public void run() {
TerminalDisplay.clearTerminal();
if (edges) map.addEdges();
boolean isNotGameOver = true;

View File

@@ -0,0 +1,110 @@
package display;
import java.util.Scanner;
import Characters.*;
import Connexion.Reseau;
import Environnement.*;
public class TerminalChannel {
private static Map map;
private static Personnage[] personnages;
public int round = 0;
public Reseau reseau;
public static boolean edges = false;
public TerminalChannel(Map m, Personnage[] personnage, Reseau r) {
personnages = personnage;
map = m;
this.reseau=r;
run();
if (edges) map.addEdges();
}
private static void placePersonnages(Personnage[] personnages) {
for(Personnage personnage : personnages) {
map.placePersonnages(personnage);
}
}
public int getInput(Reseau r){
char c=r.getLastedContent().charAt(0);
int i=c;
return i;
}
/**
* <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
*/
public boolean playerRound(Players player) {
TerminalDisplay.printMap(map, personnages);
// TerminalDisplay.printMapType(map);
int[] latestCoordinate = player.keepLatestCoordinate();
int input=getInput(null);
player.moveCoordinate(input);
if(map.isGameOver(input, player)) {TerminalDisplay.clearTerminal(); System.out.println("GameOver"); return false;}
if(player.isIncreaseSize()) player.increaseSnake(latestCoordinate);
TerminalDisplay.clearTerminal();
map.clearMap(edges);
player.incrementRound();
return true;
}
private static boolean robotRound(Robot robot) {
return false;
}
private 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;
}
public boolean run() {
TerminalDisplay.clearTerminal();
if (edges) map.addEdges();
boolean isNotGameOver = true;
int i = 0;
// place les personnages dans la grille.
placePersonnages(personnages);
while(isNotGameOver) {
for (i = 0; i<personnages.length; i++) {
Personnage personnage = personnages[i];
int[] coordinate = personnage.getPrimaryCoordinate();
System.out.println("Round : " + this.round + " | N : " + Personnage.n);
System.out.println(" Joueur " + (i+1) + " : " + personnage.getName() +
" (" + coordinate[0]+", "+ coordinate[1] +") | size : " + personnage.getSize());
isNotGameOver = instancePersonnage(personnage);
if(isNotGameOver) placePersonnages(personnages);
else break;
}
this.round++;
}
System.out.println("Le joueur " + (i+1) + " à perdu !");
return false;
}
}