Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-22-2008, 11:14 PM
Member
 
Join Date: Nov 2008
Posts: 4
Rep Power: 0
TrungTran is on a distinguished road
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 ,
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-23-2008, 06:35 AM
Member
 
Join Date: Nov 2008
Posts: 4
Rep Power: 0
TrungTran is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Log rotation in ubuntu sarah11 New To Java 0 11-04-2008 08:46 AM
Transform Translation and Rotation Java Tip java.awt 0 06-22-2008 11:58 PM
Transform Rotation demo Java Tip java.awt 0 06-21-2008 09:53 PM
Rotation in java 3D émilie- Java Applets 0 02-13-2008 11:37 AM
affineTransform rotation MichYer AWT / Swing 0 07-18-2007 09:55 AM


All times are GMT +2. The time now is 12:01 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org