Need Help Organizing Data

Started by
5 comments, last by Zahlman 17 years, 7 months ago
Sorry if this seems like it's missing some information, but I just wrote a really long post, and then my computer ate it. Anyway, my problem is that I keep giving up on my games because I organize them really poorly. Like my latest program, a tetris clone. I set up a 20 x 24 array that represent each block on the game square based on a number. This was going well up untill I got to the rotation and movement. Here's my rotation function:
void Rotate(int piece, x1, x2, x3, x4, y1, y2, y3, y4, int * board)
{
	switch(piece)
	{
		case 1:
			if(y4 > y2)
			{
				if(board[x4+1][y4] == 0 && board[x4+1][y4+1] == 0 && board[x4+1][y4+2] == 0)
				{
					x2 = x4 + 1; y2 = y4; x1 = x4 + 1; y1 = y4 + 1; x3 = x4 + 1; y3 = y3 + 1;
				}
			}
			else if(y2 < y1)
			{
				if(board[x2][y2-1] == 0 && board[x3+1][y3] == 0)
				{
					x1 -= 1 ; y1 -= 1; x3 -= 2; y3 -= 2; x4 +1= ; y4 -= 1;
				}
			}
			else if(y4 < y2)
			{
				if(board[x2+1][y2] == 0 && board[x4][y4+-] == 0)
				{
					x2 += 1; y2 += 1; x1 += 1; y1 -= 1; x3 += 1; y3 -= 2;
				}
			}
			else
			{
				if(board[x2][y2+1] == 0 && board[x4+1][y4] == 0)
				{
					x1 += 1; y1 += 1; x3 += 2; y3 += 2;  y4 -= 1:
				}
			}
			break;

		case 2:
			if(y4 > y3)
			{
				if(board[x2][y2+1] == 0 && board[x2][y2+2] == 0)
				{
					x1 -= 1; y1 += 1; y2 += 2;  x3 -= 2;  x4 -= 1; y4 -= 1;
				}
			} 
			else if(y2 > y1)
			{
				if(board[x1+1][y1] == 0 && board[x1+2][y1] == 0)
				{
					
				}
			}
			break;
		case 3:
			break;
		case 4:
			break;
		case 5:
			break;
		case 6:
			break;
	}
}
This function is hardly a quarter complete, and it's already taken me like 3 hours to write it because it's so hard to visualize this stuff. And I didn't add detecting for if a piece goes above or below the board during rotation. I made a function for that, but I have to add it like 30 times just to check everything. I know there's gotta be an easier way to do this, I just am not good at coming up with ideas. what could I possibly do to improve my ability to organize structures? I know planning before writing code is one way to do it, and using classes and other data structures would help, but I tried using a class in my last attempt to write a game, and it got way out of hand.
Originality is dead.
Advertisement
Rather than reply in regards to the rotation problem in your tetris clone, i'll ask these questions:

1) Do you design on paper/uml program?
2) If no goto 3), else, what do you design?
3) Why not?

If you spent more hours designing, incredible amounts of time is saved later.

I really advise it. Start with:

- brief,
- specs,
- uml,
- class definitions but not implementations
- class unit tests,
- class implementations

This should give you some idea how to go about design simple software.

Hope that helps,

Dave
Quote:Original post by Dave
Rather than reply in regards to the rotation problem in your tetris clone, i'll ask these questions:

1) Do you design on paper/uml program?
2) If no goto 3), else, what do you design?
3) Why not?

If you spent more hours designing, incredible amounts of time is saved later.

I really advise it. Start with:

- brief,
- specs,
- uml,
- class definitions but not implementations
- class unit tests,
- class implementations

This should give you some idea how to go about design simple software.

Hope that helps,

Dave


Well I had a design before...but it seems that I've deleted it. I can't seem to find it at the moment. I didn't have the rotation thing planned out, and I guess that could've been part of my problem. But I think the issue was entirely with the way I chose to set things up. Going block-by block with 4-block pieces probably isn't the most practical way of setting things up. Good planning is a good idea, but having a good design is a lot better.
Originality is dead.
Quote:Original post by BringBackFuturama
Good planning is a good idea, but having a good design is a lot better.


"Good planning" implies good design. If your planning doesn't result in good design, then it wasn't good planning.
Try imagining that each piece is its own tiny playing field. 4x4 should be big enough to fit any rotation of any of the classic tetris pieces.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

As a suggestion, if you don't need to process a case, leave it out, or put in a default: label

switch (piece){ case 1:  // Blah blah blah  break; case 2:  // Blah blah blah  break; // You don't need case 3-6 // as you are just telling the case to break // You can use default: for every other case // This is however optional if you are not going to process anything default:  break;}

Quote:Original post by Adam Hamilton
As a suggestion, if you don't need to process a case, leave it out, or put in a default: label


I assume he just hadn't gotten around to writing those cases yet.

To the OP:

1) You really need to put a type for each parameter. The 'int' declaration doesn't carry over to x1, x2, etc.

2) What do those parameters *represent*? Writing some comments may help.

3) Why do you think the logic needs to be different for each value of "piece"?

4) Are you trying to take the entire board state as input, *find* the piece and rotate it? Don't do that. Instead, have a separate "board" somewhere where you store just the piece. You keep track of where that board is within the overall playing field, and when it comes time to check for overlaps or draw the field, you just draw the existing blocks, then the current piece-board in its location. When the piece has "dropped", add its blocks to the main board. Oh, and don't try to check for overlap *inside* the rotation function. Instead, rotate, check *outside* if the rotated version overlaps, and if it does, rotate back.

This topic is closed to new replies.

Advertisement