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 ,