Hello guys! This is what my code looks like at the moment:
public class TicClass
{
private int playerTurn;
private char playerMarker;
public TicClass()
{
playerTurn = 1;
playerMarker = ' ';
}
public char playerMarker() //Player assignments.
{
if(playerTurn == 1)
{
playerMarker = 'X';
return 'X';
}
else
{
playerMarker = 'O';
return 'O';
}
}
public int getPlayer()
{
return playerTurn;
}
public void swapPlayers()
{
if(playerTurn == 1)
{
playerTurn = 2;
}
else
{
playerTurn = 1;
}
}
}
and:
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);
}
}
}
}
}
What I would like to do is instead of having two players, make the program select a random move for player two!
I am guessing that I will need to use the java random generator, which would look something like:
Random rand = new Random();
int num = 1 + rand.nextInt(9);
The problem is that I have no idea if this is the right code, where to put it, and how to implement the rest of it for my program to work!
Any help or suggestions are appreciated!