Results 1 to 2 of 2
  1. #1
    TrungTran is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default Need help with 2d Array rotation!

    Hi everyone,
    Iím new to the forum, I have posted this in the new to java section not sure it belong there or is it belong over here. So I posted both, Iím not trying to spam the forum.
    Iím trying to write Pentago game for my AI class and i got stuck at this the Matrix rotation part the matrix can be from 3x3 or 6x6 etc , can anyone help me plz :

    public class rotate {

    public static int[][] matrixrotateLeft(int[][] board, int gameBlock)
    {
    int[][] rotLeft = board;

    int rowOffset = ((gameBlock-1)/2)*3 ;
    int colOffset = ((gameBlock-1)%2)*3 ;
    for(int i=0+rowOffset; i<3+rowOffset; i++)
    for(int j=0+colOffset; j<3+colOffset; j++)
    rotLeft[2-j+rowOffset+colOffset][i-rowOffset+colOffset] = board[i][j] ;
    return rotLeft ;
    }


    public static int[][] matrixrotateRight(int[][] board, int gameBlock)
    {

    int[][] rotRight = board; ;
    //------------
    //------------

    int rowOffset = ((gameBlock-1)/2)*3 ;
    //System.out.println("-rowOffset-" + rowOffset) ;
    int colOffset = ((gameBlock-1)%2)*3 ;


    for(int i= rowOffset; i<3+rowOffset; i++){
    for(int j= colOffset; j<3+colOffset; j++){
    rotRight[j+rowOffset-colOffset][2-i+rowOffset+colOffset] = board[i][j] ;
    }}
    return rotRight ;
    }

    public static void main(String[] args)
    {
    /*
    int[][] Matrix = new int[][] {
    { 1, 2, 3, 10, 11 ,12 },
    { 4, 5, 6, 13, 14 ,15 },
    { 7, 8, 9, 16, 17 ,18 },

    { 19, 20, 21, 28, 29 ,30 },
    { 22, 23, 24, 31, 32 ,33 },
    { 25, 26, 27, 34, 35 ,36 },
    };
    */
    int[][] Matrix = new int[][] {
    { 1, 2, 3 },
    { 4, 5, 6 },
    { 7, 8, 9 },
    };

    for (int r=0; r<Matrix.length; r++) {
    for (int c=0; c<Matrix[r].length; c++) {
    System.out.print(" " + Matrix[r][c]); }
    System.out.println(""); }


    System.out.println("-----------------------------------------");

    Matrix = matrixrotateRight(Matrix,1);

    for (int r=0; r<Matrix.length; r++) {
    for (int c=0; c<Matrix[r].length; c++) {
    System.out.print(" " + Matrix[r][c]); }
    System.out.println(""); }


    }

    }


    here is the out put but it;s wrong :
    1 2 3
    4 5 6
    7 8 9
    -----------------------------------------
    7 4 1
    2 5 2
    1 2 1


    it should be this output

    7 4 1
    8 5 2
    9 6 3


    can anyone point out what did i do wrong ..
    thank you ,

  2. #2
    TrungTran is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    here is the 2nd tried I make on my code, it did rotate the block that I want. But then it set the other 3 blocks to null. Can anyone help me with this or point me to the right direction. Thanks



    public class rotate {

    public static String[][] matrixrotateLeft(String[][] board, int gameBlock)
    {
    //_________

    int size = board.length;
    String[][] rotLeft = new String[size][size];


    //____________
    //int[][] rotLeft = board;

    int rowOffset = ((gameBlock-1)/2)*3 ;
    int colOffset = ((gameBlock-1)%2)*3 ;
    for(int i=0+rowOffset; i<3+rowOffset; i++)
    for(int j=0+colOffset; j<3+colOffset; j++)
    rotLeft[2-j+rowOffset+colOffset][i-rowOffset+colOffset] = board[i][j] ;
    return rotLeft ;
    }


    public static int[][] matrixrotateRight(int[][] board, int gameBlock)
    {
    int size = board.length;
    int[][] rotRight = new int[size][size];

    //int[][] rotRight = board; ;
    //------------
    //------------

    int rowOffset = ((gameBlock-1)/2)*3 ;
    //System.out.println("-rowOffset-" + rowOffset) ;
    int colOffset = ((gameBlock-1)%2)*3 ;


    for(int i= rowOffset; i<3+rowOffset; i++){
    for(int j= colOffset; j<3+colOffset; j++){
    rotRight[j+rowOffset-colOffset][2-i+rowOffset+colOffset] = board[i][j] ;
    }}
    return rotRight ;
    }


    public static void main(String[] args)
    {
    String[][] Matrix = new String[][] {
    { "1", "2", "3", "a", "b", "c" },
    { "4", "5", "6", "d", "e", "f" },
    { "7", "8", "9", "j", "k", "l" },

    { "1", "2", "3", "d", "2", "3" },
    { "4", "5", "6", "f", "2", "3" },
    { "7", "8", "9", "g", "2", "3" },
    };


    for (int r=0; r<Matrix.length; r++) {
    for (int c=0; c<Matrix[r].length; c++) {
    System.out.print(" " + Matrix[r][c]); }
    System.out.println(""); }


    System.out.println("-----------------------------------------");

    Matrix = matrixrotateLeft(Matrix,2);

    for (int r=0; r<Matrix.length; r++) {
    for (int c=0; c<Matrix[r].length; c++) {
    System.out.print(" " + Matrix[r][c]); }
    System.out.println(""); }


    }

    }




    1 2 3 a b c
    4 5 6 d e f
    7 8 9 j k l
    1 2 3 d 2 3
    4 5 6 f 2 3
    7 8 9 g 2 3
    -----------------------------------------
    null null null c f l
    null null null b e k
    null null null a d j
    null null null null null null
    null null null null null null
    null null null null null null

Similar Threads

  1. Log rotation in ubuntu
    By sarah11 in forum New To Java
    Replies: 0
    Last Post: 11-04-2008, 08:46 AM
  2. Transform Translation and Rotation
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:58 PM
  3. Transform Rotation demo
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 09:53 PM
  4. Rotation in java 3D
    By ťmilie- in forum Java Applets
    Replies: 0
    Last Post: 02-13-2008, 11:37 AM
  5. affineTransform rotation
    By MichYer in forum AWT / Swing
    Replies: 0
    Last Post: 07-18-2007, 09:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •