-1

I need to check if a 2d array is full and if it is a latin square. I have two methods to check for both conditions but when I put the check in the do while loop it doesn't check. I want the game to stop if the board is full then proceed to check if it is a latin square. I set it up to where it checks for an empty element in the array. This is the code for the fullboard check.

public static boolean fullBoard(char [][] square){
    for(int i = 0; i < square.length; i++){
        for(int j = 0; j < square.length; j++){
            if(square[i][j] == 0) { 
                return false;
            }
        }
    }
    return true;
}

This is the code for the do while:

do {
        promptUser(square);
        printBoard(square);

        if(fullBoard(square)) { 
            isLatinSquare(square);
        }
    }while(isLatinSquare(square));
        System.out.println("you win");
        printBoard(square);

}
chisomkun
  • 1
  • 1

2 Answers2

0

Ok I am not sure to understand everything but I will try to help.

When I look at your do while, I can see that the

if(fullBoard(square)) { 
            isLatinSquare(square);

is useless. The method isLatinSquare returns a bool. You don't even use it's returned value.

If you want the game to end when until the game is full and the scare is latin:

do {
        promptUser(square);
        printBoard(square);


    }
while(isLatinSquare(square) && fullBoard(square));

System.out.println("you win");
printBoard(square);

}

if you want to stop the game temporarly when it is full:

do {
        promptUser(square);
        printBoard(square);

        if(fullBoard(square)) { 
            Thread.sleep(2000); //2 sec pause
        }
    }
while(isLatinSquare(square));

System.out.println("you win");
printBoard(square);
  • Ok so I think the issue isn't with the do while loop but with the fullboard check. I ran this do while loop but the check isn't working. After the first user input it is printing you win. When it should check when there are no more empty elements. – chisomkun Sep 02 '18 at 15:54
  • ok. Your full board check loops seem perfect to me. If the square object has a null character in it it will returns false. but the way it is used is the do while, even if you remove that code, it won't make a difference, it is useless. Can you please upvote my commentary and answer? I need 10 reputations point so I can ask an answer with pictures :) – Gabriel Beauchemin-Dauphinais Sep 02 '18 at 16:57
  • Do you know how I can make this check work in the do while loop? – chisomkun Sep 02 '18 at 21:36
  • If the goal of fullBoard is to check if there is a null character in it, I don't understand why you say it doesn't work, it will work. Because your case is specific, it's hard to help better than this, I suggest learning to debug so you can see the problem yourself! – Gabriel Beauchemin-Dauphinais Sep 02 '18 at 22:06
  • I know that it works but it only works on an array that is initialized . When I try to use it to check once the user has filled the array it doesn't work. It trying to print a win message after just one index has been filled. I'm trying to figure out if there is another way to check if the board is full when the user is filling the array. – chisomkun Sep 02 '18 at 22:15
  • It's probably not working because the user filled it with at least one null character ('0'). What exactly is it suppose to check? if the array is empty? if that whats your looking for do , check that out: https://stackoverflow.com/questions/2369967/how-can-i-check-whether-an-array-is-null-empty – Gabriel Beauchemin-Dauphinais Sep 03 '18 at 01:28
0

can you please try it this way

do {
    promptUser(square);
    printBoard(square);

}while(!fullBoard(square));

if(isLatinSquare(square)) {
    System.out.println("you win");
}
else {
    System.out.println("you lose");
}
printBoard(square);