can you help me to change these IF and ELSE statements with WHILE or FOR loops on these classes and can we chenge this method
Integer.parseInt(playerMove)
with anotherone ?
import javax.swing.JOptionPane;
public class Tic
{
public static void main(String[] args)
{
TicClass t = new TicClass();
char one = '1'; //Numbers for matrix.
char two = '2';
char three = '3';
char four = '4';
char five = '5';
char six = '6';
char seven = '7';
char eight = '8';
char nine = '9';
int gameOver = 0;
System.out.println("[" + one + "] [" + two + "] [" + three + "]");
System.out.println("[" + four + "] [" + five + "] [" + six + "]");
System.out.println("[" + seven + "] [" + eight + "] [" + nine + "]");
System.out.println("");
if(gameOver != 1)
{
for (int i = 0; i < 9; i++) // Game Loop
{
String playerMove = JOptionPane.showInputDialog("Choose a square player: " + t.getPlayer());
int choice = Integer.parseInt(playerMove);
if(choice == 1 && one != 'X'&& one != 'O') // Checks for valid moves
{
one = t.playerMarker();
t.swapPlayers();
}
else if (choice == 2 && two != 'X'&& two != 'O')
{
two = t.playerMarker();
t.swapPlayers();
}
else if (choice == 3 && three != 'X'&& three != 'O')
{
three = t.playerMarker();
t.swapPlayers();
}
else if (choice == 4 && four != 'X'&& four != 'O')
{
four = t.playerMarker();
t.swapPlayers();
}
else if (choice == 5 && five != 'X'&& five != 'O')
{
five = t.playerMarker();
t.swapPlayers();
}
else if (choice == 6 && six != 'X'&& six != 'O')
{
six = t.playerMarker();
t.swapPlayers();
}
else if (choice == 7 && seven != 'X'&& seven != 'O')
{
seven = t.playerMarker();
t.swapPlayers();
}
else if (choice == 8 && eight != 'X'&& eight != 'O')
{
eight = t.playerMarker();
t.swapPlayers();
}
else if (choice == 9 && nine != 'X'&& nine != 'O')
{
nine = t.playerMarker();
t.swapPlayers();
}
else
{
System.out.println("Invalid Choice");
i--;
}
System.out.println("[" + one + "] [" + two + "] [" + three + "]"); // Prints char. board
System.out.println("[" + four + "] [" + five + "] [" + six + "]");
System.out.println("[" + seven + "] [" + eight + "] [" + nine + "]");
System.out.println("");
// Checks for winners
if((one == 'O' && four == 'O' && seven == 'O') || (one == 'X' && four == 'X' && seven == 'X')) //Vertical rows
{
gameOver = 1;
System.out.println("Game Over!");
System.exit(0);
}
else if((two == 'O' && five == 'O' && eight == 'O') || (two == 'X' && five == 'X' && eight == 'X'))
{
gameOver = 1;
System.out.println("Game Over!");
System.exit(0);
}
else if((three == 'O' && six == 'O' && nine == 'O') || (three == 'X' && six == 'X' && nine == 'X'))
{
gameOver = 1;
System.out.println("Game Over!");
System.exit(0);
}
else if((one == 'X' && two == 'X' && three == 'X') || (one == 'O' && two == 'O' && three == 'O')) //Horizontal rows
{
gameOver = 1;
System.out.println("Game Over!");
System.exit(0);
}
else if((four == 'X' && five == 'X' && six == 'X') || (four == 'O' && five == 'O' && six == 'O'))
{
gameOver = 1;
System.out.println("Game Over!");
System.exit(0);
}
else if((seven == 'X' && eight == 'X' && nine == 'X') || (seven == 'O' && eight == 'O' && nine == 'O'))
{
gameOver = 1;
System.out.println("Game Over!");
System.exit(0);
} //Diagonal rows
else if((one == 'X' && five == 'X' && nine == 'X') || (one == 'O' && five == 'O' && nine == 'O'))
{
gameOver = 1;
System.out.println("Game Over!");
System.exit(0);
}
else if((seven == 'X' && five == 'X' && three == 'X') || (seven == 'O' && five == 'O' && three == 'O'))
{
gameOver = 1;
System.out.println("Game Over!");
System.exit(0);
}
}
}
}
}