How can I better test whether the player won in my tic-tac-toe? I created a tic-tac-toe using java and I would like to know if there are any practices that I can improve and make the code cleaner and more performative
import java.util.Scanner;
public class Main {
    public static boolean test(String[] values){
        //Check diagonals
        if((values[0].equals(&quot;X&quot;) &amp;&amp; values[4].equals(&quot;X&quot;) &amp;&amp; values[8].equals(&quot;X&quot;)) || (values[0].equals(&quot;0&quot;) &amp;&amp; values[4].equals(&quot;0&quot;) &amp;&amp; values[8].equals(&quot;0&quot;))){
            System.out.print(values[0] + &quot; win&quot;);
            return true;
        }
        if((values[2].equals(&quot;X&quot;) &amp;&amp; values[4].equals(&quot;X&quot;) &amp;&amp; values[6].equals(&quot;X&quot;)) || (values[2].equals(&quot;0&quot;) &amp;&amp; values[4].equals(&quot;0&quot;) &amp;&amp; values[6].equals(&quot;0&quot;))){
            System.out.print(values[2] + &quot; win&quot;);
            return true;
        }
        //Check verticals
        for(int x = 0;x &lt; 9;x+=3){
            if((values[0+x].equals(&quot;X&quot;) &amp;&amp; values[1+x].equals(&quot;X&quot;) &amp;&amp; values[2+x].equals(&quot;X&quot;)) || (values[0+x].equals(&quot;0&quot;) &amp;&amp; values[1+x].equals(&quot;0&quot;) &amp;&amp; values[2+x].equals(&quot;0&quot;))){
                System.out.print(values[0+x] + &quot; win&quot;);
                return true;
            }
        }
        //Checks horizontal
        for(int x = 0;x &lt; 3;x++){
            if((values[0+x].equals(&quot;X&quot;) &amp;&amp; values[3+x].equals(&quot;X&quot;) &amp;&amp; values[6+x].equals(&quot;X&quot;)) || (values[0+x].equals(&quot;0&quot;) &amp;&amp; values[3+x].equals(&quot;0&quot;) &amp;&amp; values[6+x].equals(&quot;0&quot;))){
                System.out.print(values[0+x] + &quot; win&quot;);
                return true;
            }
        }
        return false;
    }
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        String[] game = {&quot; &quot;, &quot; &quot;, &quot; &quot;, &quot; &quot;, &quot; &quot;, &quot; &quot;, &quot; &quot;, &quot; &quot;, &quot; &quot;};
        String[] players = {&quot;X&quot;, &quot;0&quot;};
        for(int x = 0;x &lt; 9;x++){
            System.out.print(players[x%2] + &quot;: &quot;);
            int currentPlay = read.nextInt();
            while(currentPlay &lt; 1 || currentPlay &gt; 9 || game[currentPlay -1] != &quot; &quot;){
                System.out.print(&quot;Invalid, play again: &quot;);
                currentPlay = read.nextInt();
            }
            if(x % 2 == 0){
                game[currentPlay -1]  = &quot;X&quot;;
            }
            else{
                game[currentPlay -1] = &quot;0&quot;;
            }
            System.out.printf(&quot;%s | %s | %s\n%s | %s | %s\n%s | %s | %s\n&quot;, game[0], game[1], game[2], game[3], game[4], game[5], game[6], game[7], game[8]);
            if(test(game)){
                break;
            }
            if(!test(game) &amp;&amp; x == 8){
                System.out.print(&quot;Tie&quot;);
            }
        }
    }
}
I hate this sort of hard-coded testing - there is usually a better approach to represent our data.
A few thoughts come to mind.

You don't stop players playing into an already occupied cell.
Why not use a 2-d array (actually an array of arrays)? Then you can more naturally scan rows, columns, and even diagonals.
If you score X as 1 and O as -1, then a winning row, column or diagonal adds up to 3 (X wins) or -3 (O wins).
If you initialise the cells to an appropriate value, I think you can also detect a hung game early - for example if you initialise to 4, then any row, column or diagonal with a sum &lt; 4 but not 3 or -3 is unwinnable. If they're all unwinnable, there's no point making further plays. (I need to think this through a bit more!)
I thought it through more carefully. If X is 1, O is -1 and an empty cell is 4 (for example), then a line summing to 3 is a win for X; summing to -3 is a win for O; 4 is an unwinnable line as it must have an X, an O and a blank cell; other values are undecided lines. If there are 8 unwinnable lines, then the match is a draw.]