I can get the Tic Tac Toe game board to print, but not the other game boards.
I get ArrayIndexOutOfBoundsException: 6
which is the second array's row.
If I edit the Board(), I can get the other boards to print but the columns end up being the same number as the rows.
My question is where do the columns mirror the rows? And/Or What other method do I need to run to get the 3 boards to print?
package boardgametester; import games.board.*; /** * * @author Gorgo */ public class BoardGameTester { public static void main(String[] args) { // 3x3 board for Tic Tac Toe Board tttgameboard = new Board (3,3); // 6x7 board for Connect Four Board 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()); } }
Board Class
package games.board; public class Board { private Cell[][] cells; public Board(int rows, int columns) { cells = new Cell[rows][columns]; for (int r = 0; r < cells[0].length; r++) { //I can get the other game boards to print if I take out: [0] for (int c = 0; c < cells[1].length; c++) { // and [1] cells [r][c] = new Cell (r,c); } } } 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]; } public String toString() { StringBuilder str = new StringBuilder(); for (int r = 0; r < cells.length; r++) { str.append("|"); for (int c = 0; c < cells.length; c++) { switch (cells[r][c].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("N"); break; default: //Empty str.append(" "); } str.append("|"); } str.append("\n"); } return str.toString(); } }
And the Cell class
public class Cell { private Mark content; private int row, column; public Cell(int row, int column) { this.row = row; this.column = column; content = Mark.EMPTY; } public Mark getContent() { return content;} public void setContent (Mark content) { this.content = content; } public int getRow() { return row; } public int getColumn() { return column; } }