0

never ending game

( I will add in a method to check for 3 in a row later I just want to get this to end when ever positon in the arrar != ' '(a space character) )

need to make x's and o's game for my class. never ends if I don't set the cancel array back to nothing after every move. won't save entries if I d reset it.

had to leave out the init mehtod for initializing. all it did was loop through the 2d array and set all positions in the 2d array to ' ' (a space character).

    public static char[][] drawBoard(char[][] matrix, char[][] tempMatrix, int rowChoice, int colChoice, char player)
        {

            if(rowChoice == 0 && colChoice == 0 && matrix[0][0] == ' ')
                {
                    matrix[0][0] = player;
                    return matrix;
                }
            else if(matrix[rowChoice][colChoice] == ' ')
                {
                    matrix[rowChoice][colChoice] = player;
                    return matrix;
                }
            else 
                {
                    System.out.println("Cannot enter here. Already full.");
                    tempMatrix[0][0] = '0';
                    return tempMatrix;
                }
        }

    public static void sop(char[][] matrix)
        {
            System.out.println("\n   col \t\t0.\t1.\t2.");
            System.out.println("\nRow 0.\t\t" + matrix[0][0] + "\t" + matrix[0][1] + "\t" + matrix[0][2]);
            System.out.println("\n    1.\t\t" + matrix[1][0] + "\t" + matrix[1][1] + "\t" + matrix[1][2]);
            System.out.println("\n    2.\t\t" + matrix[2][0] + "\t" + matrix[2][1] + "\t" + matrix[2][2]);
        }

    public static void main(String[] args)
        {
            XOGame XO = new XOGame();
            Scanner xoIn = new Scanner(System.in);
            Scanner numIn = new Scanner(System.in);
            final int SIZE = 3;
            final int ROW = 3;
            final int COL = 3;
            final int AREA = ROW * COL;
            int rowChoice;
            int colChoice;
            boolean gameEndStatus;
            char player;
            char xoMatrix[][] = new char[ROW][COL];
            char tempMatrix[][] = new char[ROW][COL]; 

            // initialising matrices
            xoMatrix = XO.init(xoMatrix, ROW, COL, SIZE);
            tempMatrix = XO.init(tempMatrix, ROW, COL, SIZE);

            gameEndStatus = false;
            for(int i = 0; i < AREA && gameEndStatus == false; i++)
                {
                    System.out.print("X or O?\t");
                    player = xoIn.next().charAt(0);
                    System.out.print("Row?\t");
                    rowChoice = numIn.nextInt();
                    System.out.print("Column?\t");
                    colChoice = numIn.nextInt();

                    tempMatrix = drawBoard(xoMatrix, tempMatrix,rowChoice, colChoice, player);

                    if(tempMatrix[0][0] == '0')
                        {
                            //i--;
                        }
                    else
                        {
                            xoMatrix = tempMatrix;
                        }

                    // output 3x3 matrix
                    XO.sop(xoMatrix);

                          // if matrix is full, end game
                      if(i == AREA)
                        {
                          gameEndStatus = true;
                         }
                    //THIS IS THE INITIALISATION AFTER EACH MOVE
                    tempMatrix = XO.init(tempMatrix, ROW, COL, SIZE);
                }// for

            System.out.print("\n\nEnd of game\n\n");
            xoIn.close();
            numIn.close();
        } 

need to make x's and o's game. the idea is that the game keeps running until the gameEndStatus == true and i < area of 3x3. i have found the problem being the fact that i take 1 away from i(same as what it is incremented) so it would run forever but it is not supposed to subtract 1 unless the my control arrays first postion == 0.... tempMatrix[0][0] == 0.

UPDATE: so i found that if it is repeating over and over because im not re-initialising the control array(tempMatrix) back to nothing. There is still a problem. If i re-initialize it back to nothing then it doesnt save all entries to the xoMatrix, it deletes all prior entries and only enters the new matrix

Dip Hasan
  • 225
  • 3
  • 9
  • Could you properly format the code, add {} where applicable and document the code? (e.g. what tempMatrix is?) This would really help us understand the problem. – Pim van Leeuwen Feb 05 '19 at 14:40
  • 1
    The `if(i == AREA)` then block in the loop will never be reached as you use `i < AREA` in the loop's condition, but that shouldn't matter since those two are redundant. – Aaron Feb 05 '19 at 14:44
  • tempMatrix is an empty 2d char array. every postion is initialized to ' ' (a single space). – henstghafbz Feb 05 '19 at 14:45
  • The fact that `drawBoard` validates the input rather than drawing a board and signifies its result through the first character of the first array of its return value is highly confusing and you really ought to drop this. The first branch of its if/elseif/else does the same as its second branch (except only for [0][0]) so you could just remove it. Then have your function only return whether a cell can be updated (boolean) and let your main update it when possible, and update a play counter when it did. Test that counter in the loop's condition (better use a while than a for in this case). – Aaron Feb 05 '19 at 15:01
  • More of a general thought than anything else. You can represent the tic-tac-toe board as a one dimensional array, which simplifies the whole thing significantly. – The Head Rush Feb 05 '19 at 15:27

1 Answers1

0

I found out what your issue is. You don't need to reinitialize tempMatrix since that will delete all values for xoMatrix because of something called "pass-by-reference". Here is more on "pass-by-reference" stuff:

Is Java "pass-by-reference" or "pass-by-value"?

Your issue came from your statement here:

if(i == AREA) { gameEndStatus = true; }

You need to change it so that i is actually >= AREA.

if(i >= AREA) { gameEndStatus = true; }