Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Somebody help me please!

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Somebody help me please!

    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.
    --------------------------------------------------------------------------------
    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();	
    }
    }

  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Somebody help me please!

    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.

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Somebody help me please!

    Quote Originally Posted by jps View Post
    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.
    Thank you for your advice and I will use code tag to the forum. I will try to see what's wrong with my code.