Results 1 to 2 of 2
- 11-22-2008, 10:14 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 4
- Rep Power
- 0
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 ,
- 11-23-2008, 05:35 AM #2
Member
- Join Date
- Nov 2008
- Posts
- 4
- Rep Power
- 0
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
-
Log rotation in ubuntu
By sarah11 in forum New To JavaReplies: 0Last Post: 11-04-2008, 07:46 AM -
Transform Translation and Rotation
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 10:58 PM -
Transform Rotation demo
By Java Tip in forum java.awtReplies: 0Last Post: 06-21-2008, 08:53 PM -
Rotation in java 3D
By émilie- in forum Java AppletsReplies: 0Last Post: 02-13-2008, 10:37 AM -
affineTransform rotation
By MichYer in forum AWT / SwingReplies: 0Last Post: 07-18-2007, 08:55 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks