I'm making a tic tac toe game and I have randomily assign player 1 one to either "X" or "O" and same with player 2. I have no clue how to start the code
It should make player one either X or O and make player 2 whatever is left over from player 1
I'm making a tic tac toe game and I have randomily assign player 1 one to either "X" or "O" and same with player 2. I have no clue how to start the code
It should make player one either X or O and make player 2 whatever is left over from player 1
You can generate a random number between for example 0 and 1 by using a random object or the Math.Random() function.
Based on the random number you can select via if and else, which letter "X"/"O" the first player will get. The second player can then be assigned what is left.
Have look on Math.Random: Math.random() explanation
public class RandomTicTacToeSymbols {
public static void main(String args[]) {
String[] symbols = {"X" , "O"};
int randomIndex = (int) Math.round( Math.random());
String playerOneSymbol = symbols[randomIndex];
String playerTwoSymbol = symbols[(randomIndex+1) % 2];
System.out.println(String.format("Player 1 -> %s - Player 2 -> %s", playerOneSymbol, playerTwoSymbol) );
}
}