0

So, what I am trying to do is a three in a row game, and so far I have managed to make it work, but I am struggling a bit when it comes to getting a winner, since I need to check that all the elements of either a row, a column or a diagonal are the same.

So far I have managed to get it to kinda work by using a boolean, a counter and a for loop. Here is an example of how my code looks

//Code to check the rows horizontally
public void checkH(){
int cont1 = 0;
Boolean winner1 = false;
for(int i=0;i<size;i++){
    if(a[0][i]==1 || a[1][i]==1 || a[2][i]==1){
        cont1++;
        if(cont1==3){
            winner1 = true;
        }

So, as y'all can see what I am doing in that code is telling the program that if the array in either one of the rows is equal to one and if that same case happens when it goes through all the positions in the row, then the counter is going to add plus one, and once the counter hits 3, the boolean will be true and there will be a winner, but here is the catch: if, for example, the 2D array looks like this:

int a[][] = {{1,0,0},
         {1,1,0},
         {0,0,0}};

then the counter is still hitting three, even though they are not aligned. I know I havent specified that kind of condition in the program, but that's what I am struggling with. What I would like to do is to be able to make that condition with loops, so that I dont have to fill the whole thing with if statements.

Any leads you guys could give me on this would be highly appreciated. Thanks in advance!

1 Answers1

1

If you are finding it difficult to search for a solution/tutorial on the web, notice that the three in a row game is also called tic-tac-toe. So, if you search for "tic tac toe algorithm" you will find several examples on how to solve it, as it is a somewhat usual interview question. Here is a reference for the reader’s convenience.

Now, for the desire to use for loops instead of chained ifs (or an if with multiple logical comparisons), it is a question about row-wise, column-wise and diagonal-wise traversal of a matrix. Here is a reference for the row and column traversals, and here is another reference for the diagonal traversal.

Specific to your question, below is a pseudo-code showing the check for column and row using for and the cell values to have a small number of if statements. But please notice this is just an example, as there are many interesting ways to solve a tic-tac-toe game that you may want to take a look, from traversing trees to using a string and regex.

public bool checkRow() { 

  int i, j; 
  int count = 0;

  // accessing element row wise 
  for (i = 0; i < MAX; i++) { 
    for (j = 0; j < MAX; j++) { 
        // Considering we were checking for 1
        // And array can have 0 or 1
        // You can add the cell value and avoid an if
        count += arr[i][j];
       // if you go with arr[j][i] you will be traversing the columns and not the rows.
    } 

    // if all cells in the row are 1, we have a winner
    if(count == MAX)
        return true;
  } 

  return false
} 
diogoslima
  • 169
  • 7