Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1631 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  Programmation d'un jeu : BUG !

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Programmation d'un jeu : BUG !

n°2190227
togam
Posté le 15-05-2013 à 11:21:45  profilanswer
 

Bonjour !
Je suis en terminale S dans une spécialité Information et Sciences du Numérique (ISN) et je dois préparer un projet pour passer un oral poru le bac.
On a seulement commencé à apprendre le Java cette année, autant dire que c'est grave la galère, sachant que ce jeu doit être réalisée en Java :$ (mon jeu consiste juste à déplacer un personnage sur une grille et rencontre des monstres pour les attaquer)
J'ai pas mal taffer dessus, c'est loin d'être parfait, mais je rencontre un bug lorsque j'associe les déplacements aux combats.  
Les déplacement Haut et Bas fonctionnent très très bien avec les attaques, mais lorsque j'essaye les deplacements Gauches et Droites, un message d'erreur s'affiche :  
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
 at Grille.deplacerD(Grille.java:243)
(ou at Grille.deplacerG)
 at Main.main(Main.java:48)"
J'ai fais quelques recherches pour résoudre ça mais rien n'y fais, je n'arrive pas à débloqué ! Pourtant, avant ces déplacements marchaient très bien..  
Merci d'avance pour vos réponses !
Je vous envoie mon programme Grille (si vous avez besoin des parties Main, Personnage et monstre, je vous les enverrais aussi)
 
 
import java.util.*;
 
 
 
public class Grille {
 
 
 // Attributs  
 
 private int nbLig;
 private int nbCol;
 private char [][] grille;
 
 // Constructeurs
 
 public Grille(int n, int p){
  nbLig = n;
  nbCol = p;
  grille = new char[nbLig][nbCol];
   
  /* i++ : augmenter la valeur de un à chaque fois (i+1)*/
  for(int i=0; i<nbLig; i++){
   for(int j=0; j<nbCol; j++){
     
    grille[i][j] = '-';
     
   }
  }
 
 }
 
 
 // Acesseurs
 
 public char getCase(int l, int c){
  return grille[l-1][c-1];
  }
 
 //Methodes
 
 public void afficher(){
  System.out.println();
  for(int i=0; i<nbLig; i++){
   for(int j=0; j<nbCol; j++){
     
    System.out.print(" | " + grille[i][j]);
   }
   
   System.out.println(" | " );    
 }
 
System.out.println();
 
}
 
 // Placer
 
 public void placer(int l, int c, char t){   /* placer(ligne, colonne, type) */
 
  l = l-1;    
  c = c-1;
 
  if (l<0 || c<0 || l>nbLig || c>nbCol){
   System.out.print("Erreur de déplacement !" );
   return;
  }
   grille[l][c] = t;
 
 }
 
  Personnage p1 = new Personnage();
  Monstre m1 = new Monstre();
 
  public int Combat(){
 
   System.out.println("Début du combat..." );
   
  if(p1.getvie() > 0 && m1.getvie() > 0){
     
   System.out.println("\nVoulez-vous attaquez ? \n1: Oui \n2: Non" );
   
   Scanner sc2 = new Scanner(System.in);
   int nb = sc2.nextInt();
   switch (nb){
   
   case 1 : System.out.println("\nVous lancez une attaque !" );
   m1.setvie(m1.getvie() - p1.attaquer());
   System.out.println();
   System.out.println("Caractéristiques de l'adversaire : " + m1.tostring());
   System.out.println();
   System.out.println("Vous êtes attaqué !" );
   System.out.println();
   p1.settvie(p1.getvie() - m1.attaquer()) ;
   System.out.println("Vos caractéristiques : " + p1.tostring());
   afficher();
   break;
   case 2 : System.out.println("Fuite.. Pas de dégâts infligés." );
   afficher();
   }}
   
  else{ System.out.println("\nFin du Combat... " );
   
   
   if(p1.getvie() <= 0 && m1.getvie() > 0){
   System.out.println("\nFin du Combat... " );
   System.out.print("Vous avez perdu. Fin du jeu." ); }
       
   
   if(p1.getvie() >0 && m1.getvie() < 0){
    char tmp;
   System.out.println("\nFin du Combat... " );
   System.out.print("Vous avez gagné !" );
   
   for(int i=0; i<nbLig; i++){
    for(int j=0; j<nbCol; j++){
     
    if(grille[i][j] == 'J'){    
     if(i+1 <= nbLig && grille[i+1][j] == 'M'){
      tmp = grille[i][j];  
      grille[i][j] = '-';  
      grille[i+1][j] = tmp;  
      return 1;
     }}
    if(grille[i][j] == 'J'){  
     if(i-1 >= 0 && grille[i-1][j] == 'M'){  
      tmp = grille[i][j];  
      grille[i][j] = '-';  
      grille[i-1][j] = tmp;  
      return 1;
     }}
    if(grille[i][j] == 'J'){  
     if(j+1 <= nbCol && grille[i][j+1] == 'M'){    
      tmp = grille[i][j];    
      grille[i][j] = '-';    
      grille[i][j+1] = tmp;  
      return 1;  
      }}
     
    if(grille[i][j] == 'J'){    
     if(j-1 >= 0 && grille[i][j-1] == 'M'){  
      tmp = grille[i][j];  
      grille[i][j] = '-';    
      grille[i][j-1] = tmp;  
      return 1;  
      }}
     
    }}
  }  
  }    
     
 return 0;   }  
 
 
 
  public int deplacerH(){   /* deplacerH = deplacer vers le haut le personnage */
   
   char tmp;  
   
    /*i++ * augmenter la valeur de un à chaque fois (i+1)*/
   for(int i=0; i<nbLig; i++){
    for(int j=0; j<nbCol; j++){
     
     
    if(grille[i][j] == 'J'){   //On cherche le joueur
     if(i-1 >= 0 && grille[i-1][j] == '-'){     //On teste la position
      tmp = grille[i][j];   //On copie le perso
      grille[i][j] = '-';   //On vide la case  
      grille[i-1][j] = tmp;  //Et on deplace le perso
      return 1;
     }}
     
      //S'il y a un monstre
     while(i-1 >= 0 && grille[i-1][j] == 'M'){
      Combat();
 
       
     }
 
      }
       
     }
  return 0; }
 
     
  public int deplacerB(){  
   
   char tmp;   //variable temporaire//
   
   /* i++ : augmenter la valeur de un à chaque fois (i+1)*/
   for(int i=0; i<nbLig; i++){
    for(int j=0; j<nbCol; j++){
     
    if(grille[i][j] == 'J'){   //On cherche le joueur
     if(i+1 <= nbLig && grille[i+1][j] == '-'){     //On teste la position
      tmp = grille[i][j];   //On copie le perso
      grille[i][j] = '-';   //On vide la case  
      grille[i+1][j] = tmp;  //Et on deplace le perso
      return 1;
     }}
     //S'il y a un monstre
     while(i+1 <= nbLig && grille[i+1][j] == 'M'){
      Combat();
     }
 
      }
       
     }
  return 0; }
   
   
   public int deplacerD(){  
     
    char tmp;   //variable temporaire//
     
    /* i++ : augmenter la valeur de un à chaque fois (i+1)*/
    for(int i=0; i<nbLig; i++){
     for(int j=0; j<nbCol; j++){
       
     if(grille[i][j] == 'J'){   //On cherche le joueur
      if(j+1 <= nbCol && grille[i][j+1] == '-'){     //On teste la position
       tmp = grille[i][j];   //On copie le perso
       grille[i][j] = '-';   //On vide la case  
       grille[i][j+1] = tmp;  //Et on deplace le perso
       return 1; }}
      //S'il y a un monstre
     while(j+1 <= nbCol && grille[i][j+1] == 'M'){
       Combat(); }
       
      }
 
       }
       
       
   return 0; }
       
 
     
    public int deplacerG(){   /* deplacerH = deplacer vers le haut le personnage */
     
     char tmp;   //variable temporaire//
     
     /* i++ : augmenter la valeur de un à chaque fois (i+1)*/
     for(int i=0; i<nbLig; i++){
      for(int j=0; j<nbCol; j++){
       
      if(grille[i][j] == 'J'){   //On cherche le joueur
       if(j-1 >= 0 && grille[i][j-1] == '-'){     //On teste la position
        tmp = grille[i][j];   //On copie le perso
        grille[i][j] = '-';   //On vide la case  
        grille[i][j-1] = tmp;  //Et on deplace le perso
        return 1; }}
       //S'il y a un monstre
       while(i-1 >= 0 && grille[i][j-1] == 'M'){
        Combat();
       }
 
        }
         
       }
    return 0; }
       
   
    }

mood
Publicité
Posté le 15-05-2013 à 11:21:45  profilanswer
 

n°2190246
breizhbugs
Posté le 15-05-2013 à 13:01:34  profilanswer
 

Bonjour,
La réponse est simple: pour un tableau de nbLig elements, les indices valables vont de 0 inclus à nbLig exclus.
Tes boucles for de deplacerH/B/D/G sont donc correctes à priori. Or dans ces boucles il t'arrive de vérifier soit i-1/+1 ou j -1/+1 ce qui causes un accès en dehors des bornes pour i/j=0 ou i/j= nbLig/nbCol.
 
pour deplacer H/B tu as le bon garde fou:
if (i-1 >= 0...)
mais pour deplacer G/D, ton garde fou est incorrect! il devrait être
if (j+1<nbCol...
au lieu de if  
(j+1<=nbCol...
 
dans deplacerG le while(i-1 est incorrect sachant qu'ensuite tu accède a grille[i][j-1] (fait gaffe a l'indentation, ce while n'est pas dans ton if( j-1!) (idem pour deplacerD!)
 
 
Bref revois les bornes de tes conditions....


Message édité par breizhbugs le 15-05-2013 à 13:02:12

---------------
Seul Google le sait...
n°2197238
Profil sup​primé
Posté le 12-07-2013 à 08:27:37  answer
 

Up.
 
A mon tour de vous parler de mon problème.
 
Je suis GROS débutant sous Java. J'ai rapidement lu Java for Dummies ces deux derniers mois et je vous avoue ne pas avoir fait tout les exercices :o
 
Je me suis attelé, comme exercice "perso", à la conversion en Java d'un jeu que j'avais commencé à programmer en QBASIC... Néanmoins, ça compile pas. J'ai pu résoudre/détourner la majeure partie des erreurs, mais celle ci m'échappe.
 
code source
 

Citation :


package mainprogram;
import java.util.Scanner;
import java.util.Random;
import static java.lang.System.out;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
 
 class mainprogram {
 public static void main(String args[])
   throws FileNotFoundException {
 
 //define variables
  //create instances
 Random myRandom = new Random();
 Scanner myScanner = new Scanner(System.in);
 Scanner diskScanner = new Scanner(new File("save.txt" ));
 PrintStream diskWriter = new PrintStream("save.txt" );
 
 char choix;
 char reroll;
 int lineskip;
 int game = 1;
 
  //character stats
 int str = 0;
 int hpmax = 0;
 int hp = 0;
 int intl = 0;
 int mp = 0;
 int mpmax = 0;
 int xp = 0;
 int level = 0;
 int gold = 0;
   
 
 
 //display title screen
 out.println("                  Arena" );
 out.println("" );
 out.println(" .... a java exercise disguised as an RPG" );
 //cls(); // supposed to call clearscreen.jav
 
 
 //loadornew
 
 out.println("press (A) to start a new game" );
 out.println("press (B) to reload the current game" );
 out.println("" );
 out.println("" );
 choix = myScanner.findWithinHorizon(".", 0).charAt(0);
 choix = Character.toUpperCase(choix);
 
 // WARNING : not too sure about quotation marks or checking values here
  switch(choix) {
   case 'A':
 
   //character generation - define stats
   out.println("" );
   out.println("           Character Generation" );
   str = myRandom.nextInt(18) +1;
   hpmax = myRandom.nextInt(50) +1;
   intl = myRandom.nextInt(18) +1;
   mp = intl;
   mpmax = intl;
   xp = 0;
   level = 1;  
   //display build
   do {
   // cls(); // call the dreaded CLS
   out.println();
   out.println();
   out.println();
   out.print("Strength..." );out.println(str);
   out.print("Intelligence..." );out.println(intl);
   out.print("Health Points..." );out.println(hp);
   out.print("Mana points" );out.println(mp);
   out.println("" );
   out.println("" );
   out.print("Do you want to reroll? (y/n)" );  
   reroll = myScanner.findWithinHorizon(".", 0).charAt(0);  
   reroll = Character.toUpperCase(reroll);}
   while (reroll != 'N');
   
 
   case 'B':
   str = diskScanner.nextInt();
   hpmax = diskScanner.nextInt();
   hp = diskScanner.nextInt();
   intl = diskScanner.nextInt();
   mp = diskScanner.nextInt();
   mpmax = diskScanner.nextInt();
   xp = diskScanner.nextInt();
   level = diskScanner.nextInt();
   gold = diskScanner.nextInt();
   break;
   }
   
 
 
 
   //main menu
   while (game == 1) {
   //cls();
   out.println("........ CITY OF SERGI ......." );
   out.println("" );
   out.println("" );  
   out.println("" );
   out.print("GOLD = " );out.println(gold);
   out.println("" );
   out.println("1. ITEM SHOP" );
   out.println("" );
   out.println("2. HEALER" );
   out.println("" );
   out.println("3. THE ARENA" );
   out.println("" );
   out.println("" );
   out.println("Z. Display stats" );
   out.println("" );
   out.println("S. Save and Quit" );
   for (lineskip = 0 ; lineskip == 5 ; lineskip++) {out.println("" );};
   choix = myScanner.findWithinHorizon(".", 0).charAt(0);
   choix = Character.toUpperCase(choix);
   switch (choix) {
    case '1':
     //not implemented yet
    case '2':
     //not implemented yet
 
    case '3':
    case 'Z':
     //cls(); // call the dreaded CLS
     out.println();
     out.println();
     out.println();
     out.print("Strength..." );out.println(str);
     out.print("Intelligence..." );out.println(intl);
     out.print("Health Points..." );out.println(hp);out.print(" /
" );out.print(hpmax);
     out.print("Mana points" );out.println(mp);out.print(" / " );out.print(mpmax);
     out.println("" );
     out.println("" );
     out.println("type enter" );  
     break;
 
    case 'S':
    // The diskWriter prints blanks spaces between each variables so
as to get them more easily when reloading
    diskWriter.print(str);
    diskWriter.print(" " );
    diskWriter.print(hpmax);
    diskWriter.print(" " );
    diskWriter.print(hp);
    diskWriter.print(" " );
    diskWriter.print(intl);
    diskWriter.print(" " );
    diskWriter.print(mp);
    diskWriter.print(" " );
    diskWriter.print(mpmax);
    diskWriter.print(" " );
    diskWriter.print(xp);
    diskWriter.print(" " );
    diskWriter.print(level);
    diskWriter.print(" " );
    diskWriter.print(gold);
    out.println("Saved" );
   
   
   }  
 
 
       
                    }
 
 
   
   
 
   
   }
 
 
 
}
 


 
et erreur dans la console d'Eclipse
 

Citation :


Exception in thread "main" java.util.NoSuchElementException
 at java.util.Scanner.throwFor(Scanner.java:907)
 at java.util.Scanner.next(Scanner.java:1530)
 at java.util.Scanner.nextInt(Scanner.java:2160)
 at java.util.Scanner.nextInt(Scanner.java:2119)
 at mainprogram.mainprogram.main(Mainprogram.java:87)
 


 
WTF? :(
 
Merci à l'avance de toute aide que vous pourriez m'apporter.

n°2198091
Lion_Sn@ke
Posté le 19-07-2013 à 13:32:25  profilanswer
 

Yop,
 
C'est tout bete, t'as oublié un break; à la fin de ton case 'A':
 
(sinon le debug dans eclipse reste ton MEILLEUR AMI)

n°2211735
samantik
Posté le 26-11-2013 à 16:44:20  profilanswer
 

j'ai le même problème


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  Programmation d'un jeu : BUG !

 

Sujets relatifs
Aide programmation C++ (débutant)Programmation sur Labview
Programmation chez flash player26 mars : prochain challenge de programmation en ligne CodinGame
problème programmation VBARe-programmer un logiciel ?
Programmation vbaprogrammation pic
Débutant en programmation orientée objet(jeu d'échecs)Programmation fpga
Plus de sujets relatifs à : Programmation d'un jeu : BUG !


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR