Search code examples
javaloopsobjectupdatingtic-tac-toe

Object not updating in loop(java)


I have begun creating a small tic tac toe game that every time the lop is gone through again, it should switch between 'X' and 'O' by changing the object location:

    int m=0;
    while(m<=3){
    Test t=new Test();
    User use= t.turn();
    System.out.println(use);

    char[][] board=new char[3][3];


    System.out.println(" 1  2  3");

    for(int y=0;y<board.length;y++){

        System.out.print(y+1);
        for(int x=0;x<board.length;x++){

            board[x][y]=use.getMark();
            System.out.print("|"+board[x][y]+"|");
        }

        System.out.println();
        m++;
        }
    }
}

However every time it seems to be outputting an 'X'. Why is this and how can I fix it?

UPDATE: I don't know if this is needed, but I have the code from my turn method deciding which object to use:

public User turn(){

    User use;
    PlayerX playX=new PlayerX();
    PlayerO playO=new PlayerO();
    Board board=new Board();
    int xturn=playX.getTurn();
    int oturn=playO.getTurn();

    if(xturn<=oturn){
        use=playX;
        System.out.println("X");
        xturn++;
    }else{
        use=playO;
        System.out.println("O");
        oturn++;
    }
    System.out.println(xturn);
    System.out.println(oturn);
    return use;
}


package TicTacToe;

public class User {

private int XCoord;
private int YCoord;
private int Turn=0;
private char Mark;

public int getX(){
    return XCoord;
}
public int getY(){
    return YCoord;

}
public int getTurn(){
    return Turn;

}
public char getMark(){
    return Mark;
}

}

UPDATE 2:

So i have fixed the majority of my problems the only thing I am still wondering(I am sorry if this is a stupid question), is there anyway to reuse an object inside a loop instead of it being recreated every time the loop is run. I ask this because I do not want my turn variable to be refreshed to 0 every time


Solution

  • The values in board are set by calling use.getMark() inside your for loops. Given the use variable doesn't change this is likely to return the same value each iteration (unless it iterates internally which is likely to show a design flaw). You could change board to User[][] and then get the 'mark' using board[x][y].getMark().

    More fundamentally, however, the best way to get your software working is by building unit tests with your code. You should have tests demonstrating that User and Player does what you expect before trying things like printing the board. I would suggest that if you learn to unit test as you develop you'll have far fewer problems and the ones you have will be easier to diagnose and fix. It is a much more sustainable approach than using print statements to diagnose problems.

    The model you are using is quite unusual. It's unclear why your method has a board represented by an array that is inaccessible to any other objects. That seems to be pretty pointless. I would suggest you might want classes Board, Player, Move to properly separate out responsibilities.