Search code examples
javatic-tac-toe

Java ArrayList for Tic Tac Toe game


Hello I'm starting out with Java and I need a little help. I'm currently working on a tic tac toe game project and I'm stuck. At the beginning of the game I want all 9 spots on the game board to have a ' ' so that the board shows as empty, but when players select their next move I want them to use the following format: A1, A2, A3, B1, etc. How do I link my arrayList with these strings?

Example A1 would be = 0, A2 = 1.... C3=8

    public static void printGameBoard() {
    System.out.println("    A    B    C");
    System.out.println("   ______________");
    System.out.println("  |    |    |    |");
    System.out.println("1 |" + board.get(0) + " | " + board.get(1) + " | " + board.get(2) + " | ");
    System.out.println("  |----|----|----|");
    System.out.println("2 |" + board.get(3) + " | " + board.get(4) + " | " + board.get(5) + " | ");
    System.out.println("  |----|----|----|");
    System.out.println("3 |" + board.get(6) + " | " + board.get(7) + " | " + board.get(8) + " | ");
    System.out.println("  |____|____|____|\n");

public static int gameplay() {
    boardSize = 9;
    board = new ArrayList();

    for(int i = 0; i < boardSize; i++) {
        board.add(Character.valueOf(' '));
    }       

    return playerTurn();
}

Solution

  • So, you're ArrayList is a linear container, contain 0-n elements. Your grid is a two dimensional container, allowing elements to be referenced via yxx reference. You need a means to be able to convert between these.

    You know that the grid is a 3x3 matrix (3 rows by 3 columns), so a little bit of simple maths will come in handy.

    A references the first col, so your index would be (y - 1) + (0 * 3) (remember, Java is 0 indexed) B references the second col, so your index would be (y - 1) + (1 * 3) And C references the third col, so your index would be (y - 1) + (2 * 3)

    Now, what you need is away to convert the letters to a number, astonishing, this is actually very simply...

    String cell = "A1";
    int col = cell.charAt(0) - 'A';
    

    will return 0.

    A slightly safer solution would be to remove the case entirely from your operation, something like...

    int col = cell.toLowerCase().charAt(0) - 'a';
    

    Now, you'd need to do some validation on the input to make sure the values are within range.

    But what about the row??

    String cell = "A1";
    int col = cell.toLowerCase().charAt(0) - 'a';
    int row = Integer.parseInt(cell.substring(1)) - 1;
    
    System.out.println(row + "x" + col);
    

    Simple :)

    So, you could then put it together something like...

    if (row >= 0 && row < 3 && col >= 0 && col < 3) {
        int index = (row * 3) + col;
        board.set(index, "x");
    }
    

    or something along those lines

    And because I got my maths all turned around backwards....

    String[] cols = new String[]{"A", "B", "C"};
    for (int rowIndex = 1; rowIndex < 4; rowIndex++) {
        for (String colValue : cols) {
            String cell = colValue + rowIndex;
            int col = cell.toLowerCase().charAt(0) - 'a';
            int row = Integer.parseInt(cell.substring(1)) - 1;
            int index = (row * 3) + col;
            System.out.println("Cell = " + cell + "; index = " + index);
        }
    }
    

    Outputs

    Cell = A1; index = 0
    Cell = B1; index = 1
    Cell = C1; index = 2
    Cell = A2; index = 3
    Cell = B2; index = 4
    Cell = C2; index = 5
    Cell = A3; index = 6
    Cell = B3; index = 7
    Cell = C3; index = 8