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";
    }
}
  • now it is more complicated, what did you mean by `choice+choice` i really don't get you – Youcef LAIDANI Jun 03 '17 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. – Nathan Matteson Jun 03 '17 at 18:50
  • why you don't use List instead maybe you need something like this `List 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)); } }` ? – Youcef LAIDANI Jun 03 '17 at 18:54
  • I need to make it so the Board[2] becomes an O. – Nathan Matteson Jun 03 '17 at 19:24

3 Answers3

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
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • 1
    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 – dumbPotato21 Jun 03 '17 at 17:36
  • 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** ? – Youcef LAIDANI Jun 03 '17 at 17:42
  • I was referring to this. [Why in Java 8 split sometimes removes empty strings at start of result array?](https://stackoverflow.com/questions/22718744/why-in-java-8-split-sometimes-removes-empty-strings-at-start-of-result-array/) – dumbPotato21 Jun 03 '17 at 17:44
  • 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? – Youcef LAIDANI Jun 03 '17 at 17:50
  • 1
    Haha, I feel honored! – dumbPotato21 Jun 03 '17 at 17:53
  • Sorry, but would this work for the other sequences such as sequence[7]? – Nathan Matteson Jun 03 '17 at 18:23
  • what did you mean @NathanMatteson ? – Youcef LAIDANI Jun 03 '17 at 18:34
  • I'm running a for loop that runs through all the sequences and checks them to see if there is an xx" " and if there is put an x in the final space and there are 8 other sequences, so I was wondering if this could apply to all of the other board slots. – Nathan Matteson Jun 03 '17 at 18:38
  • ok can you share your code please so we can help you @NathanMatteson – Youcef LAIDANI Jun 03 '17 at 18:40
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));
Chai T. Rex
  • 2,972
  • 1
  • 15
  • 33
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();
Nadeem
  • 121
  • 2
  • 13