• June 8th, 2013, 12:06 PM
nausicaa
Hello I'm a student of Java(beginner). and I have an important homework to present next week. This is a program of Power3Game(modified Power4game).
I spent 5days to solve the problem but I could not. So someone help me please.
errors:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Colonne5.Colonne5.sommetColonne(Colonne5.java:115)
at Colonne5.Colonne5.jouerCoup(Colonne5.java:63)
at Colonne5.Colonne5.main(Colonne5.java:132)
(I am using Eclips Juno.)
My code is below.
--------------------------------------------------------------------------------
Code Java:

```package Colonne5;   public class Colonne5 { final static int jouer1 = 0; final static int jouer2 = 1; final static char [] pion = new char [] {'*','0', ' '}; final static int vide = 2; static final int PionEtoile = 1; static final int PionRond = -1; final static int Dwidth = 5; final static int Dheight = 5; final static int PreCOL = 0; final static int DerCOL = Dwidth - 1; final static int Hautligne = Dheight - 1; static int Basligne = 0; // directions final static int H = 0 ; // horizontalement final static int V = 1 ; // verticalement final static int[] HOR = new int[] {1, 0} ; // horizontale final static int[] VER = new int[] {0, 1} ; // verticale final static int[] UP = new int[] {1, 1} ; // diagonale final static int[] DOWN = new int[] {1, -1} ; // diagonale final static int[][] DIRECTION = new int[][] {HOR, VER, UP, DOWN} ; // Attribue private static int[][] damier ; // grid private static String[] name ; // player name private static char player ; // turn of gamer private static int result ; // Variable private static int impactLine ;//last player's dice   public Colonne5 (String[] name){ damier = new int[Dwidth][Dheight];   for (int i = 0 ; i < Dwidth; i++) { for (int j = 0; j < Dheight; j++) { damier[i][j] = vide; } } Colonne5.name = name; player = jouer1; result = vide; } private boolean Libre(int column){ //check the grids are free return damier[column][Hautligne]==vide; } private boolean damierRempli() { int column = PreCOL; while (column <= DerCOL) { if (Libre(column)) return true ; column++ ; } return false ; }   private void jouerCoup() { //start game while (result == vide && damierRempli()) { afficher() ;   Terminal.ecrireString("Enter " + (PreCOL + 1) + " to " +(DerCOL +1) + " ?") ; Terminal.lireInt(); sommetColonne() ; if (coupGagnant(player, impactLine)) result = player ; else demanderCoup() ; } afficher() ; if (result == vide) Terminal.ecrireString(" partie nulle ") ; else System.out.println(name[result] + " gagne") ; }   private boolean coupGagnant (int column, int ligne) { //judge winner int i = 0 ; while (i < DIRECTION.length) { if (coupGagnant(column, ligne, DIRECTION[i])) return true ; i++ ; } return false ; }   private static boolean coupGagnant(int column, int ligne ,int[] dir) { int nextColumn = column + dir[H] ; int nextLine = ligne + dir[V] ; int forward = 0 ; while (damier[nextColumn][nextLine] == player) { nextColumn += dir[H] ; nextLine += dir[V] ; forward++ ; } nextColumn = column - dir[H] ; nextLine = ligne - dir[V] ; int backward = 0 ; while (damier[nextColumn][nextLine] == player) { nextColumn -= dir[H] ; nextLine -= dir[V] ; backward++ ; } return backward + 1 + forward >= 3 ; }   private static int demanderCoup() { //turn of player if(player==jouer1){ player = jouer2; } else player = jouer1; return player; }   private void sommetColonne() { //top line of the piece if(!Libre(impactLine)) throw new RuntimeException(" colonne pleine ") ; impactLine = Hautligne ; // know the colume is free while (impactLine > Basligne && damier[Dheight-1][impactLine-1] == vide) impactLine-- ; damier[Dheight][impactLine] = player ; }   private void afficher() { //show game for (int column = PreCOL ; column <= DerCOL ; column++) Terminal.ecrireString("| " + (column + 1) + " "); Terminal.ecrireStringln("|"); // condition for (int line = Hautligne ; line >= Basligne ; line--) { for (int column = PreCOL ; column <= DerCOL ; column++) Terminal.ecrireString("| " + pion[damier[column][line]] + " ") ; Terminal.ecrireStringln("|"); } }   public static void main(String[] args) { Colonne5 joue = new Colonne5(name) ; joue.jouerCoup(); } }```
• June 8th, 2013, 12:14 PM
jps
Welcome nausicca

Please use code tags when posting code to the forum. See the Announcements page if you need help.

java.lang.ArrayIndexOutOfBoundsException: 5

The array does not have an index number 5. Remember arrays are numbered from 0.
• June 9th, 2013, 02:29 AM
nausicaa