Tic Tac Toe problem help!
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!
Re: Tic Tac Toe problem help!
Yikes, that sure is a lot of unformatted code- make sure you use the highlight tags to make code easier to read.
Think about the flow of the program. When does the player take a turn? When does the computer make a turn? When should you check for wins? Sketch out a rough flow of the program and then fill in the pieces from there.
Re: Tic Tac Toe problem help!
Ok! So I tried inserting the following if statement:
if (t.getPlayer()==1)
{
String playerMove = JOptionPane.showInputDialog("Choose a square player: " + t.getPlayer());
int choice = Integer.parseInt(playerMove);
}
else
{
Random rand = new Random();
int choice = 1 + rand.nextInt(9);
} |
But i get the following error: "choice cannot be resolved to a variable"
How would I make it possible for choice to have different values depending on the player turn?
Re: Tic Tac Toe problem help!
Oh and by the way, sorry that my code is not very neat and not highlighted, but I'm a beginner to java and the forum!
Re: Tic Tac Toe problem help!
I found my mistake! I had to declare "choice" before the if statement. The next part of my assignment is to make this client-server but I'm guessing this isn't the right part of the forum for that! :cool:
Re: Tic Tac Toe problem help!
Quote:
Originally Posted by
vess28
Oh and by the way, sorry that my code is not very neat and not highlighted, but I'm a beginner to java and the forum!
Hi and welcome.
See the announcements page for helpful forum tips.