Results 1 to 3 of 3

Thread: danbwater

  1. #1
    danbwater is offline Member
    Join Date
    Oct 2013
    Location
    Arkansas
    Posts
    10
    Rep Power
    0

    Default BoardGameTester, connect four, TicTacToe, Mastermind errors

    I am new to this, and a little slow. I am building a BoardGameTester, connect four, TicTacToe, Mastermind. Please help me. Thanks for your time.

    This is the error I receive.

    Java Code:
    run:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    	at games.board.Board.<init>(Board.java:21)
    	at BoardGameTester.main(BoardGameTester.java:22)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)

    Here is the main.
    Java Code:
    import games.board.Board;  
    import games.board.Cell;
    import games.board.Mark;
      
      
      
    public class BoardGameTester {  
      
        /** 
         * @param args 
         */  
        private static Board tttgameboard, mmgameboard, cfgameboard ;  
        public static void main(String[] args) {  
            // TODO Auto-generated method stub  
            // 3x3 board for TicTacToe  
            @SuppressWarnings("LocalVariableHidesMemberVariable")
            Board tttgameboard;  
            tttgameboard = new Board(3,3);
           
                // 6x7 board for Connect Four  
               Board cfgameboard;        
            cfgameboard = new Board (6,7);
           
               // 5x8 board for MasterMind; blue mark  
               Board mmgameboard = new Board (5,8);
                 
               System.out.println (" Tic Tac Toe game board ");  
                tttgameboard.setCell(Mark.NOUGHT,1,1);  
                System.out.println(tttgameboard.toString());  
           
                System.out.println(" Connect Four game board ");  
                cfgameboard.setCell(Mark.YELLOW,0,0);  
                System.out.println(cfgameboard.toString());  
           
                System.out.println (" Mastermind game board ");  
                mmgameboard.setCell(Mark.BLUE,0,0);  
                System.out.println(mmgameboard.toString());  
                  
            class Board {  
                  private Cell[][] cells; 
                  
                public Board(int rows, int columns) {  
                    cells = new Cell[rows][columns];  
                    int r = 0;  
                    int c = 0;  
                    for (int r1 = 0; r1 < cells[c].length; r1++) {       // taking out [0]; replacing [0] w/ [c]  
                        for (int c1 = r; c1 < cells[r1].length; c1++) {  //taking out [1]; replacing [1] w/ [r]  
                           cells [r1][c1] = new Cell (r1,c1);  
           
                        }  
                    }  
                 }  
      
           
                
              }     
        }  
        public static Board getTttgameboard() {  
            return tttgameboard;  
        }  
        public static void setTttgameboard(Board tttgameboard) {  
            BoardGameTester.tttgameboard = tttgameboard;  
        }  
        public static Board getMmgameboard() {  
            return mmgameboard;  
        }  
        public static void setMmgameboard(Board mmgameboard) {  
            BoardGameTester.mmgameboard = mmgameboard;  
        }  
        public static Board getCfgameboard() {  
            return cfgameboard;  
        }  
        public static void setCfgameboard(Board cfgameboard) {  
            BoardGameTester.cfgameboard = cfgameboard;  
        } 
    }
    here is the Board.

    Java Code:
    package games.board;
    
    import java.awt.GridLayout;
    import java.awt.event.ActionListener;
    import javax.swing.JPanel;
    
    
    
    @SuppressWarnings("unused")
    public class Board extends JPanel {
    /**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
            @SuppressWarnings("FieldMayBeFinal")
            private Cell[][] cells;
    public Board(int rows, int columns) {
    cells = new Cell[rows][columns];
    for( int r = 0; r < cells[0].length; r++ ) {
    for (int c = 0; c < cells[1].length; c++) {
    cells[r][c] = new Cell(r,c);
    }
    }
    }
    public Board(int rows, int columns, ActionListener ah) {
        cells = new Cell[rows][columns];
        this.setLayout(new GridLayout());
        for( int r = 0; r < cells.length; r++ ) {
        for (int c = 0; c < cells[r].length; c++) {
        cells[r][c] = new Cell(r,c);
        this.add(cells[r][c]);
        cells[r][c].addActionListener(ah);
        }
        }
        }
    private void add(Cell cell) {
    	// TODO Auto-generated method stub
    	
    }
    public void setCell(Mark mark, int row, int column) throws
    IllegalArgumentException {
    if (cells[row][column].getContent() == Mark.EMPTY)
    cells[row][column].setContent(mark);
    else throw new IllegalArgumentException("Player already there!");
    }
    public Cell getCell(int row, int column) {
    return cells[row][column];
    }
            @Override
            public String toString() {
    StringBuilder str = new StringBuilder();
                for (Cell[] cell : cells) {
                    str.append("|");
        for (Cell cell1 : cell) {
            switch (cell1.getContent()) {
                case NOUGHT:
                    str.append("O");
                    break;
            case CROSS:
                str.append("X");
                break;
            case YELLOW:
                str.append("Y");
                break;
            case RED:
                str.append("R");
                break;
            case BLUE:
                str.append("B");
                break;
            case GREEN:
                str.append("G");
                break;
            case MAGENTA:
                str.append("M");
                break;
            case ORANGE:
                str.append("M");
                break;
            default: //Empty
                str.append("");
            }
            str.append("|");
        }
                    str.append("\n");
                }
        return str.toString();
    
    }
    
    }
    Last edited by danbwater; 10-03-2013 at 03:37 AM.

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: danbwater

    use the built in de bugger to find out where your index value requested is greater thaan the number of stored indexes.

    Your error occurs when you try to access a piece of memory that doesnt exist, thus out of the bound of the array

    example;

    Java Code:
    int[] ints = new int[3];
    
    public void  getOutOfBoundEx() {
    
    ints[2] = 16;
    ints[3] = 17; //THIS WILL CAUSE THE ERROR as element [3] doesnt not exist
    
    }
    ints[3] doesnt not exist because array indexing starts at 0, so int[3] actually looks like:

    int[0]
    int[1]
    int[2]


    it still has 3 elements but counting starts at zero,usually this error occurs when you return the length of the array and try to set a value to the last location in the array. to avoid this use ints.length -1.

    example;

    Java Code:
    ints[ints.length] = 20; //error will be thrown because ints.length = 3, so expression is now ints[3] = 20; but ints[3] does not exist
    
    ints[ints.length -1] = 20;// will work fine, as ints.length -1 = 2, so ints[2] is now 20. ints[2] is also the last element in the array.

  3. #3
    danbwater is offline Member
    Join Date
    Oct 2013
    Location
    Arkansas
    Posts
    10
    Rep Power
    0

    Default Re: danbwater

    Thank you for your time all the help. It is working correctly now.
    Thanks again :)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •