#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    8
    Rep Power
    0

    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.

    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.


    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.



    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();
     
    }
     
    }
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,696
    Rep Power
    1959
    Take a look at how you created the loops in Board constructor with the ActionListener.
    Then look at the Board constructor without the ActionListener; you have to fix the loops in this constructor.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    8
    Rep Power
    0
    at line 21 in the board class I changed it to read

    Code:
    System.out.println(r+""+c);
    now I am getting the error of

    Code:
        run:  
        00  
        01  
        02  
        10  
        11  
        12  
        20  
        Exception in thread "main" java.lang.NullPointerException  
        21  
        22  
        00  
        01  
        02  
        03  
        04  
        05  
        06  
        10  
        11  
        12  
            at games.board.Board.setCell(Board.java:42)  
        13  
        14  
        15  
        16  
        20  
        21  
        22  
        23  
        24  
        25  
        26  
        30  
        31  
        32  
        33  
            at BoardGameTester.main(BoardGameTester.java:30)  
        34  
        35  
        36  
        40  
        41  
        42  
        43  
        44  
        45  
        46  
        50  
        51  
        52  
        53  
        54  
        55  
        56  
        60  
        61  
        62  
        63  
        64  
        65  
        66  
        00  
        01  
        02  
        03  
        04  
        05  
        06  
        07  
        10  
        11  
        12  
        13  
        14  
        15  
        16  
        17  
        20  
        21  
        22  
        23  
        24  
        25  
        26  
        27  
        30  
        31  
        32  
        33  
        34  
        35  
        36  
        37  
        40  
        41  
        42  
        43  
        44  
        45  
        46  
        47  
        50  
        51  
        52  
        53  
        54  
        55  
        56  
        57  
        60  
        61  
        62  
        63  
        64  
        65  
        66  
        67  
        70  
        71  
        72  
        73  
        74  
        75  
        76  
        77  
         Tic Tac Toe game board   
        Java Result: 1  
        BUILD SUCCESSFUL (total time: 1 second)
    I am at a total loss here
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,696
    Rep Power
    1959
    You have this in one constructor:
    Code:
    for( int r = 0; r < cells[0].length; r++ ) {
    for (int c = 0; c < cells[1].length; c++) {
    think it should be like the loops you have in the other constructor:
    Code:
        for( int r = 0; r < cells.length; r++ ) {
        for (int c = 0; c < cells[r].length; c++) {
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    8
    Rep Power
    0
    Thank you for your time all the help. It is working correctly now.
    Thanks again

IMN logo majestic logo threadwatch logo seochat tools logo