0
String [] board = new String [9];
String [] sequence = new String [8];

sequence[0] = board[0]+board[1]+board[2];
sequence[1] = board[0]+board[3]+board[6];
sequence[2] = board[0]+board[4]+board[8];
sequence[3] = board[1]+board[4]+board[7];
sequence[4] = board[2]+board[5]+board[8];
sequence[5] = board[2]+board[4]+board[6];
sequence[6] = board[3]+board[4]+board[5];
sequence[7] = board[6]+board[7]+board[8];

Say I was to make sequence[0]="XXO";

How could I take sequence[0] and change the the board points so that:

board[0]="X";
board[1]="X";
board[2]="O";

I am trying to run this for loop and I have to initialize the board array before the sequences part as I am printing it out to the screen and can't have the values be null.

for(int i = 0; i < 8; i++
{
    if(sequence[i].equals("X"+"X"+" "))
    {
        sequence[i] = "XXO";
    }
}
4
  • now it is more complicated, what did you mean by choice+choice i really don't get you Commented Jun 3, 2017 at 18:48
  • I am making a TicTacToe game where the person can choose what they want to be and the computer chooses the opposite of that, but I edited to make more sense to you now. I also do not want to hard code all of the ways that one could win so I thought this would be much easier, but I need to make it so that the sequence changes the board slots. Commented Jun 3, 2017 at 18:50
  • why you don't use List instead maybe you need something like this List<String> board = new ArrayList<>(); String[] sequence = new String[8]; for (int i = 0; i < 8; i++) { if (sequence[i].equals("X" + "X" + " ")) { sequence[i] = "XXO"; board.addAll(Arrays.asList(sequence)); } } ? Commented Jun 3, 2017 at 18:54
  • I need to make it so the Board[2] becomes an O. Commented Jun 3, 2017 at 19:24

3 Answers 3

1

You can use String::split which return an array of String for example :

String[] board;//you could also to not initialize the array here 
               //because you will do that in split
String[] sequence = new String[8];
sequence[0] = "XXO";
board = sequence[0].split("");//split and initialize the board array

//print the values
System.out.println(board[0]);
System.out.println(board[1]);
System.out.println(board[2]);

Output

X
X
O
Sign up to request clarification or add additional context in comments.

9 Comments

note that java 7(to which you have linked to) and below, would result in an empty String at the beginning, when using string.split(""). For this reason, you should probably link to the Java 8 Docs. For Java 8, and above, your way is perfect. Take an upvote
thank you @ChandlerBing i appreciate it, but i really don't get you what is the difference between split in java 7 and java 8 sorry what did you mean by would result in an empty String at the beginning, when using string.split(""). For this reason, you should probably link to the Java 8 Docs. For Java 8, and above ?
waahhh this is really a new information for me, thank you @ChandlerBing you already teach me something new, so i should to change the link to java 8 right?
Haha, I feel honored!
|
0

You generally shouldn't store the same data in different ways (the Don't Repeat Yourself principle), sometimes because it leads to a mess like this.

One correct way to do what you want is the following:

board[0] = String.valueOf(sequence[0].charAt(0));
board[1] = String.valueOf(sequence[0].charAt(1));
board[2] = String.valueOf(sequence[0].charAt(2));

Comments

0

As you want only one character as an element in your board array you can use character array instead of String array and then can use

char board[] = new char[9];
board = sequence[0].toCharArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.