Turning 1D array to 2D array in TicTacToe
Matrices ... 1D or 2D format

In computing it is common to have matrix-like problems. Graphics, mathematics, and Tic-Tac-Toe.

It is often easier to have the matrix data stored as a 1D array, or 'flattened'.

Whether you have it as a 1D or a 2D array does not really matter for the functional aspect, but it can often have a really big impact on the readability.

A matrix of M x N dimension would be stored, for example, as :

int[][] matrix = new int[M][N];


To 'visit' all members of that matrix you need to:

for (int r = 0; r &lt; M; r++) {
    for (int c = 0; c &lt; N; c++) {
        // do someting with matrix[r][c];
    }
}


As a flattened matrix, it would be:

int[] matrix = new int[M * N];


To visit the members, it is:

for (int i = 0; i &lt; matrix.length; i++) {
    // do something with matrix[i];
}


When you have complicated indexing, the flattene system can be more efficient.

Additionally, the flattened matrix has interesting memory properties that often leads to better performance on High-Performing Computational clusters.

Converting from one format to the other is relatively simple.

final int width = 3;
matrix1D[row * width + col] = matrix2D[row][col];
matrix2D[i / width][i % width] = matrix1D[i];


Review


In your case, your matrix is small, and you should chose whichever format makes the code most readable.

If you choose a 2D implementation, you should make sure your winning combinations array is expressed as a 2D format....

int[][][] winningcombinations = {
  { {0, 0}, {0, 1}, {0, 2} }, // Top
  { {1, 0}, {1, 1}, {1, 2} }, // Middle
  { {2, 0}, {2, 1}, {2, 2} }, // Bottom
  { {0, 0}, {1, 0}, {2, 0} }, // left
  .......
}

Magic Numbers

Java arrays have a length property. Use it. You have the code:


for(int i=0; i&lt;=7; i++){ .... }



This should be:

for (int i = 0; i &lt;= winCombinations.length; i++) { .... }

Code Style

In Java recommended code style puts spaces around operators and reserved words.


for(int i=0; i&lt;=7; i++){ .... }



should be (ignoring the 7)

for (int i = 0; i &lt;= 7; i++) { .... }