2

How would i rotate a string array in java for a tetris game i am making. For example, the string array

[
"JJJJ",
"KKKK",
"UUUU"
]

would become

[
"UKJ",
"UKJ",
"UKJ",
"UKJ"
]

I can do it with a char matrix using this code

public char[][] rotate(char[][] toRotate)
{
    char[][] returnChar = new char[toRotate[0].length][toRotate.length];
    for(int rows = 0; rows<toRotate.length; rows++)
    {
        for(int cols = 0; cols<toRotate[0].length; cols++)
        {
            returnChar[cols][toRotate.length-1-rows]=toRotate[rows][cols];
        }
    }
    return returnChar;
}
2
  • 1
    Why don't you just do it with char arrays, then? Commented Nov 29, 2012 at 22:12
  • 1
    because char arrays are so much harder to create and they are harder to manipulate for the other things i am doing. Commented Nov 29, 2012 at 22:16

3 Answers 3

1

With the Array String is similar to want you have done:

public static String[] rotate(String [] toRotate)
  {

      String [] returnChar = new String[toRotate[0].length()];
      String [] result = new String[toRotate[0].length()];
      Arrays.fill(returnChar, "");

      for(int rows = 0; rows<toRotate.length; rows++)
          for(int cols = 0 ; cols < toRotate[rows].length(); cols++)
              returnChar[cols] = returnChar[cols] + toRotate[rows].charAt(cols);

      for(int i = 0; i < returnChar.length; i++)
          result[i] =  new StringBuffer(returnChar[i]).reverse().toString();

      return result;
  }

I go through all char in each String on array toRotate, concat this char (toRotate[rows].charAt(cols)) to each String returnChar[cols] on the array returnChar

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

Comments

1

Strings are immutable in Java, so you have a few options

  1. Write a wrapper for rotate(char [][]) that turns it back into a string array
  2. Modify the function to create a new array of strings from the input
  3. Create a data structure that holds the data in the most efficient format and then has getters that return it in the format you want it.

3 is essentially what you 'should' be doing. In a Tetris game, you would create a matrix of the size of the game field (possibly padded).

Comments

0

This function does the job of converting the Strings into char[][] so you can use your function.

  public static String[] rotateString(String[] toRotate) {
    char[][] charStrings = new char[toRotate.length][];
    for(int i = 0; i < toRotate.length; i++) {
      charStrings[i] = toRotate[i].toCharArray();
    }

    // This is YOUR rotate function
    char[][] rotatedStrings = rotate(charStrings);
    String[] returnStrings = new String[rotatedStrings.length];
    for(int i = 0; i < rotatedStrings.length; i++) {
      returnStrings[i] = new String(rotatedStrings[i]);
    }

    return returnStrings;
  }

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.