Im working my way through some book exercises (the art and science of java, eric s.roberts) and I am at my wits end on this one (its Q11 C4 if you have the book)
Basically you have to use for loops to display a pyramid of bricks like this
the bricks have names constants BRICK_WIDTH, BRICK_HEIGHT and BRICKS_IN_BASE
I have tried nested for loops but am going mad, I could only get a square grid of bricks or just the side so I went back to basics and did it like this (the program uses custom acm package, but that doesnt matter in principle here)
as you can see this approach (if it was logically continued (there is only 3 layers of bricks here)) does produce the correct output but the code has to be written with the BRICKS_IN_BASE number of for loops, so changing the constant means changing the code. I KNOW its supposed to be nested loops but for the life on me cant work it out. Can anyone assist me for sanities sake?
Edit by mw: Added Code Tags to preserve indentation. [ July 18, 2007: Message edited by: marc weber ]
What would it take to use exactly the same code in all three of those loops? The first one starts i at bricks-0, the second at bricks-1, the third at bricks-2, so we'd need a new variable that has value 0 the first time, 1 the second, 2 the third.
Then we have x at i*width, i*width+.5, i*width+1. We need another variable that goes up from zero by halves. Adding float 0.5 repeatedly is likely to get accumulating error, so lets go up by 1 and divide by 2. And now that's the same as j isn't it?
Do you see where this is going? Fix up the y position and we can put this inside a loop that increments j ...
If it gets too deeply nested, make a new method.
Does that make sense? I haven' tried this, but I'm suspicious of the x position calculation. See how it looks and adjust if necessary.
Finally, just for myself, I'd rename "j" to "row" and "i" to "brickInRow" or something more meaningful. Simple i and j for ints is so common that many people will choke on that suggestion, so take it or leave it. [ July 18, 2007: Message edited by: Stan James ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Originally posted by Matthew Pearson: ...i am unsure how to make it symmetrical down the middle to get the pyramid, any suggestions?
The question is how much space to insert to the left of the first brick ("visible" column), which depends on how many bricks are in the row, which depends on which row you're in. In other words, you can calculate how much space to begin each row with based on "SIZE" and "row."
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
I have just done, it didnt get your last post, im glad i didnt cause i worked it out myself, the pyramid structure works for all sizes of width, height nad number of bricks in base. Thank you very much for your time and patience, without which I would have given up!! I may now continue the program so you dont have to recompile to change constants, have a GUI. once again thank you
I made a variable x_offset which moves the draw position for each row of bricks, the variable gets less for each row. And the positions of each subsequent brick is calculated by the heigh and width of the brick mutliplied by the row and column respectively. Initially I had arbitary values but hen realized I could use some maths. Here it is
the drawRect is (x position, y postion, width of rect, height of rect) I see by looking at this I could change the x_offset to SIZE*width/2 in the orignal intialization. May start the GUI now...