<p>i got my program to work but theres one problem with tictactoe. it wont check the diagonal winner. if anyone could help me out.ill point out the part of the code that needs to be changed</p>
<p>import java.util.Scanner;
import static java.lang.System.*;</p>
<p>public class TicTacToe
{
private char[][] mat;
int once =0;
int luck = 0;
int[] arrays = new int[3];</p>
<pre><code>public TicTacToe()
{
mat = new char[3][3];
}
public TicTacToe(String game)
{
if(once == 0){
mat = new char[3][3];
once++;
}
for(int row =0;row < 3; row++)
{
for(int col = 0; col < 3;col++)
{
if(luck >= game.length() + 1)
{
luck =0;
}
mat[row][col] = game.charAt(luck);
luck++;
}
}
}
public String getWinner()
{
int steps = 0;
for(int x = 0; x < mat.length;x++)
{
if(mat[x][0]==mat[x][1] && mat[x][0] == mat[x][2])
{
if(mat[x][0] == 'X')
{
steps = 1;
}
else
{
steps = 2;
}
}
</code></pre>
<p>}
for(int i = 0; i < mat.length; i++)
{
if(mat[0]==mat[1] && mat[0]* == mat[2])
{
if(mat[0] == 'X')
{
steps = 3;
}
else
{
steps = 4;
}
}
}
//THIS PART===> for(int k = 0; k < mat.length; k++)
{
if(mat[k][k]==mat[1][k] && mat[k][k] == mat[2][k])
{
if(mat[k][k] == 'X') //TO HERE
{
steps = 5;
}
else
{
steps = 6;
}
}
}</p>
<pre><code> //0 = none
//1 = horizontal x
//2 = horizontal y
//3 = vertical x
//4 = vertical y
if(steps == 1)
{
return "X wins horizontally!";
}
else if(steps == 2)
{
return "O wins horizontally!";
}
else if(steps == 3)
{
return "X wins vertically!";
}
else if(steps == 4)
{
return "O wins vertically!";
}
else if(steps == 5)
{
return "X wins diagonally!";
}
else if(steps == 6)
{
return "O wins diagonally!";
}
return "cat's game - no winner!";
</code></pre>
<p>}</p>
<pre><code>public String toString()
{
String output="";
for(int row =0;row < mat.length; row++)
{
for(int col = 0; col < mat[row].length;col++)
{
output = output + " " + mat[row][col];
}
output = output + "
";
}
return output;
}
</code></pre>
<p>}</p>