Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
His questions look to be only ones that ask for clarification of just what your unclear problems might be and nothing more, and they don't appear "silly" to me, not in the least. The only thing silly I see in this entire thread is your last post above which adds nothing to the discussion other than suggesting its owner might have an over-developed sense of entitlement. My experience here has been that if you ask a well constructed, clear and specific question, you usually get a well constructed, clear and specific answer. Else we must ask for clarification, and keep asking until either the question becomes well defined or remains unanswerable.
My objective has been to help you understand what the code was doing and where to look to find its problems.
There are several basic concepts about what extending classes means that you don't understand.
Don't expect me to tell you what coding changes to make to the code. I think that would be a waste of time because you need to understand what the code is doing and not just copy some code that could fix the problem.
Right now it looks like the code has most of what you want and that there is a method that needs to be fixed to do the "rotation". I suggest that you copy that method into a small simple testing program and work on getting it to work. Pass it an array and print out the array after it changes it so you can visually verify that the method works like you want. when it is working, then it can be copied back to the big program.
If you don't understand my answer, don't ignore it, ask a question.
Well, it does seem that it's the matrixMultiplication method that's not working for some reason. If you wanted to help, you would have pointed out what was the problem.
This worked fine, but I guess the rotation matrix doesn't work for some reason doesn't even modify the matrix for some reason.HTML Code:synchronized void rotateClockwise() { //contents = multiplyMatrix(contents); contents = matrix(); updateLocation(); } public static int [][] matrix() { int [][] a = {{1,1,1,1}, {1,1,1,1}, {1,1,1,1}, {1,1,1,1}, }; return a; }
I thought I have said several times that I don't know what you want that code to do? Explain what the algorithm is that the code is supposed to implement.pointed out what was the problem.
Can you show an array before its passed to the method and what the array should be after the method work on it?
Please explain what "doesn't work" means?the rotation matrix doesn't work
Show the input and the output for the method and post what the output should be.
If you don't understand my answer, don't ignore it, ask a question.
I told you already. I want to make the shapes rotate.
Can you just tell me how to detect collision?
Thst sound like you have an image and want to change its orientation when it is drawn. But this code is not drawing images so I'm confused.I want to make the shapes rotate.
I thought you wanted code to change the contents of a two dimensional array.
What objects are colliding? How are the boundaries of the objects defined? The Shape class has some methods like contains and intersects that are useful in detecting collisions between two Shape objects.how to detect collision
If you don't understand my answer, don't ignore it, ask a question.
public class rotation { public static int [][] multiplyMatrix(int [][] m1) { int [][] m2 = {{0,0,0,1}, {0,0,1,0}, {0,1,0,0}, {1,0,0,0}, }; int[][] result = new int[4][4]; // multiply for (int i=0; i<4; i++) for (int j=0; j<4; j++) for (int k=0; k<4; k++) if (m1[i][k] * m2[k][j] > 0) { result[i][j] = 1; } else { result[i][j] = 0; } return result; } public static void printArray(int [][] array) { for(int row = 0; row < array.length; row++) { for(int col = 0; col < array[row].length; col++) { if (array[row][col] > 0) { System.out.printf("1"); } else System.out.printf("0"); } System.out.printf("\n"); } } public static int [][] transpose(int [][] m1) { int m = 4; int n = 4; int c = 0; int d = 0; int[][] transpose = new int [n][m]; for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < n ; d++ ) { transpose[d][c] = m1[c][d]; } } return transpose; } public static void main(String[] args) { int [][] m1 = {{0,0,1,1}, {0,0,1,1}, {0,0,0,0}, {0,0,0,0}, }; int [][] transpose = multiplyMatrix(m1); transpose = transpose(transpose); printArray(transpose); } }
I did what you asked and got this
input:
{0,0,1,1},
{0,0,1,1},
{0,0,0,0},
{0,0,0,0},
output
1 1 0 0
0 0 0 0
0 0 0 0
0 0 0 0
I calculated the transpose for input and then multiplied it with m2 to get the rotation.
the algorithm that I was given seems to be flawed.
--- Update ---
Tetris block should collide. The boundaries are defined by 1 and 0. I don't know if the methods in the shape class can be applied here.
Implementing Tetris: Collision Detection | Gamedevtuts+
Basically, the site explains us how to do it, but I don't know if it can be applied to my program.
I have no idea what the correct algorithm is. You will need to find it.algorithm that I was given seems to be flawed.
Are you sure the algorithm is flawed or could it be your code is not following the algorithm?
Looks like the same kind of problem about "collisions". You need the correct algorithm to be able to detect a collision between Tetris blocks.Tetris block should collide. The boundaries are defined by 1 and 0.
What is a "Tetris block"? You have not used that term before.
If you don't understand my answer, don't ignore it, ask a question.
Wait, are you saying I shouldn't change contents, but I should change shape? You told me we can't modify shape.
--- Update ---
Can't you look at my code and tell me what is wrong? The tetris block is shape.
--- Update ---
Strangely the transpose method gives me this:
1000
1000
0000
0000
and not this:
0000
0000
0011
0011
--- Update ---
Ah never mind, the transpose method works, but not the multiplyMatrix method...
it returns
0000
0000
0000
0000
--- Update ---
Ok it works now
0000
0000
1100
1100
0000
0000
0011
0011
--- Update ---
1110
0010
0000
0000
0111
0100
0000
0000
contents is a two dim int array in the Grid class. Its contents can be changed.are you saying I shouldn't change contents, but I should change shape? You told me we can't modify shape.
I don't know where there is class variable named shape.
Your question does not make sense because there is no shape variable to change.
If you don't understand my answer, don't ignore it, ask a question.
Define what "objects" are colliding? What data values make for a collision?how to implement collision detection
With a physical shape like rectangles, a collision is when their sides touch.
The position of their sides is determined by their x,y location and their width and height.
I do NOT see where there is a shape variable that can be changed.changing contents instead of shape
If you don't understand my answer, don't ignore it, ask a question.
You always need to know what the code is supposed to do before you can implement it. I can't help you find the algorithm.no idea how to implement it
I don't know if or when the values in the contents array can be changed. It depends on what those values are used for. Look at the design for the program.Are you saying that it's ok to change contents?
If you don't understand my answer, don't ignore it, ask a question.
I agree. Your questions about some algorithms for your game are not questions that anyone not coding your game can answer. Not many people would be interested in reading the doc for the game and figuring out what its rules are so they can write code for you.
Good luck on your project.
If you don't understand my answer, don't ignore it, ask a question.
Usually, when someone responds to me. He usually points out what might be the problem instead of just asking questions that lead to nowhere and actually read the code I have given him.
3 pages and not a single helpful answer. I never experienced this before.
And how can you say the program is not complicated if you don't even understand it? If you understood it, you would easily be able to know how to go about doing certain things with the existing code. I didn't even ask for the algorithm.
You would never explain what the problems were in terms of the code.
Where is the shape?
What does rotate the shape mean?
What is a tetris block?
What does a collision mean?
I could make up stuff.
A tetris block is a 2 dim array (the contents array defines a block)
A collision is when more than 3 elements in one array are the same as the corresponding 3 elements in another array.
Rotate an array would be when the rows and columns of a 2 dim array are swapped.
And so on.
If you don't understand my answer, don't ignore it, ask a question.
I'd like to mention that I didn't program the entire thing
I am just trying to complete this lab project I found on google:
CSE131 Lab 9: Tetris Game using Object-Oriented Design
So you can't expect me to know more than you. I don't know where the shape is. Rotating the shape means rotate the 2d array representing the shape of a tetris block. I don't think I need to explain that. It should have been obvious. The other questions didn't need answers if you actually looked into the code. If you don't look at the code, I don't think it's possible to help, because I didn't program the entire thing.
I have an understanding of how java programming works.
I know nothing about Tetris and/or thousands of other java applications that exist.
I do expect you to know more about the app (Tetris) than I do.
If you don't understand my answer, don't ignore it, ask a question.
Are you saying you never played Tetris? That's not even possible.
--- Update ---
So basically, I can't have a tetris block go through another tetris block. It must detect the collision and pile up. I thought about a way to implement this, but I need three different methods
I am asked to do the following:
Write a method in the Board class that takes a row and column index and a two-dimensional array as parameters, and checks to see that the non-zero entries of the given array correspond to empty positions on the grid, assuming that the given array's upper left were positioned at the given row and column. You'll use this to determine if a piece can move into a given position.
So I have a Piece object and two variable currentX and currentY. I am assuming those will be passed into the method described above. currentX and currentY represent the top corner of the shapes. Now, how do I implement collision detection? I think we need to create an array of the same size as shape array that represent what's immediately below the shape and then do two for loops to see if there is a collision if the shape moves below.
I am trying to find a simpler way though.
Is the gird a large 2 dim array containing 0s and non-0s?
The array to test with is smaller than the grid array and also contains 0s and non-0s.
the array to test is to be aligned at the given row, column in the bigger array.
test that each overlain element in the big array is 0 when the the overlaying smaller array's element is non-0
Sounds like a nested loop using row and column indexes adjusted for each array.
ThesSmaller array's 0,0 is the row,col value for the alignment point on the larger array.
If the smaller array's value is not 0, then the larger array's value must be 0.
If you don't understand my answer, don't ignore it, ask a question.
wholegrain (February 17th, 2013)
Ok, thanks. Board must be the array containing the previous pieces from what I could parse. But I have to write a method to save the previous pieces. I wrote this method, but I don't know if it would work.
public boolean legalDown(int currentX, double currentY, int[][] board) { int x = currentX; int y = currentY-1; for (int row = 0; row < 4; row++) { for(int col = 0; col < 4; col++) { if (shape[row][col] == 0 && board[x+col][y]row] == 1) { return true; } else { return false; } } } }
Ok, I came up with this, but I don't know if I can access shape directly like this. If I don't pass shape as a parameter to the method. What would happen? Do I need to call it like this: currentPiece.legalDown? I am not sure. Also, I don't know if it would work. Also, there is no method to save the 1 and 0 of the previous pieces right now, right? I have to write it in Board, right?
--- Update ---
Ok, I am trying to test the method, but it doesn't work it always returns true. I am trying to figure out why.HTML Code:public class collisiontest { public static boolean legalDown(int currentX, int currentY, int[][] board, int[][] shape) { int x = currentX; int y = currentY-1; for (int row = 0; row < 4; row++) { for(int col = 0; col < 4; col++) { if (shape[row][col] == 0 && (board[x+col][y+row] == 0 || board[x+col][y+row] == 1)) { return true; } else { if (shape[row][col] == 1 && board[x+col][y+row] == 0) { return true; } else { return false; } } } } return true; } public static void main(String[] args) { int [][] m1 = {{1,1,0,0}, {1,1,0,0}, {0,0,0,0}, {0,0,0,0}, }; int[][] board = {{0,0,0,0}, {1,1,1,1}, {1,1,1,1}, {1,1,1,1}, {1,1,1,1}, {1,1,1,1}, {1,1,1,1}, }; int[][] board2 = {{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {1,1,0,0}, }; int currentX = 0; int currentY = 4; System.out.println(legalDown(currentX, currentY, board, m1)); } }
The posted method returns true or false on the first elements it looks at. Shouldn't it look at all the elements before making the decision for one of the values to be returned? The other value could be returned at the first failure.
I don't know the definition of a piecea method to save the previous pieces.
or what a previous piece is
or what saving a piece means.
If legalDown() is a method in the Piece class (or the class it extends)do I need to call it like this: currentPiece.legalDown?
and shape is a variable in the Piece class (or the class it extends)
then that is how you could call the legalDown() method.
Can you explain what the method: legalDown() is supposed to do?
What are its args?
When does it return true or false?
If you don't understand my answer, don't ignore it, ask a question.