Search code examples
javaarrayscharacterinstancetic-tac-toe

Java - How to make instance of a instance pick up variables in the main instance?


This is my first time here on Stackoverflow, this question might have been asked before, but I had little trouble finding those threads. I'm currently working on a Tic Tac Toe Android Game, as my first project to make an app.

The case is I'm working on my PlayState-class where the main-part of the game is to take place. PlayState is to be a instance that appears when you've selected it in the Menu. This is how I've done it in PlayState:

As I have PlayState as an instance, not wanting to have any static methods. I also made an EndMechanism-class, that is created as an instance within PlayState. That class checks for Wins/Lose/Draw. The way I want to do this is to make each Tile (3x3) get a Character (X or O) tagged by a variable in a loop.

boardTileMark[row][col] = 'x';

As I've managed to do that by using the Character class:

boardTiles = new BoardTile[3][3]; // To make the Tiles 3 by 3
boardTileMark = new Character[boardTiles.length][boardTiles[0].length];

I've used the size of the real BoardTile to make the size numbers of marks. So everything so far works now.

And then it's the EndMechanism-class as an instance:

endMech = new EndMechanism();

Not to go into to much details everything works so far when I press a square, it gets a X or O (graphic) boardTile and also a char 'x' or 'o' is given parallell on boardTileMark.

This is the problem now:

As I want to check if it's a Win, draw or lose on EndMechanism, I'm realy not sure how to pick up those marks into EndMechanism, without copy over the values and then make the same code in EndMechanism.

This is how I set boardTileMark's to x or o.

public void markBoardTiles(){
        for(int row = 0;row < boardTiles.length;row++){
            for(int col = 0;col < boardTiles[0].length;col++){
                if(boardTiles[row][col].selected() && boardTiles[row][col].cross()){
                    boardTileMark[row][col] = 'x';
                }
                else if(boardTiles[row][col].selected() && boardTiles[row][col].circle()){
                    boardTileMark[row][col] = 'o';
                }
            }
        }
    }

If there is something that is confusing please tell, and I'll describe it better. Thank you for your time :)

PS: I'm trying to make this game as solid as possible, so I can use it for another project. So keeping the game itself and the check for wins in same class is not what I intend to do.


Solution

  • I think you've made too broad description so the question is a little bit unclear and contains much information which are not important to your problem and therefore decrease a chance for getting an answer.

    As I understand correctly your whole description could be shortened to: "I have a class PlayState and this class contains two dimensional array of marks and an instance of a class EndMechanism. The instance of a class EndMechanism needs to access two dimensional array of marks from PlayState1 class."

    If it's right then your real question is how to access variables of outer class. And such problem is popular in Java and already have an answer in other questions, for example here:

    access variables of outer class in Java

    So I am flagging this question as a duplicate, but still I will provide an answer.

    But in your situation I would not create a new class (EndMechanism) only for checking end conditions, but I would put function which checks end conditions in a PlayState class. Checking end conditions is similar functionality as what markBoardTiles function does so it should be in the same class. If it would be in the same class you would not have to deal with described problem. Generally if some class, let's say X uses only variables from class Y and class X have the same kind of functionality as Y then functions from X should be in class Y.

    But if you want to stay with your current approach you should call PlayState.this.markBoardTiles inside your EndMechanism class and you will have access to markBoardTiles variable.