So, a cube is different than before in the sense that we now have a cube that has both a length and a color. Here, I've modified the cube constructor to have a length and a color, and because we've modified the cubes construction of a color, we're going to store that color in a new private member variable. Let's go ahead and see this cube come together. You already know how to represent a color. So, in last week's assignment, you built the UIUC HSLAPixel. Let's go ahead and build a program that creates a number of different classes to represent all of these entities. That's the relationship we have between the game, the stacks, and the cubes. So, inside the game, we have stacks and in each stack we have cubes. Here, the entire container might just be called the game. All three of these stacks are part of something even larger because we need some container to store the stacks. So, what we have is we have a series of individual stacks. I see this as this maybe stack at index zero, a stack at index one and a stack at index two. The second thing I see, as I see three individual stacks. So, we want to modify the cube class, and we'll do that in just a minute. So, I'm gonna go ahead and use a color to represent the cube in addition to its length. But I see these cubes don't just have a length, they also have a color. So, we're going to use our UIUC cube class to represent a cube. So, we already have a class to represent our cubes. The first thing I see is we have these cubes. So, what I see here is I see three distinct classes or three distinct things that we want to represent. This process continues, but we want to talk about how we can represent this problem programmatically with code. Notice that here I've stacked up all three of the cubes except for the largest cube in the middle, and now I can go ahead and move the largest cube from where it began to where we eventually want to indent. We can continue this process of juggling these cubes all around with the goal being that we want to slowly move the largest cubes to the right. Here, I went ahead and move the yellow cube on top of the purple cube, which leaves a space to move the orange cube down on top of the empty stack. Instead, our options are really to move the yellow cube on top of the purple cube or the yellow cube on top of the orange cube. We can't move the orange cube on top of anything because the orange cube is too large. After removing the purple cube, then we have only two options now. Something that's against the rules of this game. The idea here being that we can't put it on top of this yellow cube because that would put a larger cube on top of a smaller cube. Next thing we might want to do is to go ahead and take this purple cube and move it all the way to a destination. Made one step in the Tower of Hanoi problem and we've gotten maybe slightly close to removing this entire stack from the source here to the destination. For example, we might start moving the smallest cube on top and moving it over one stack. The goal of this problem is to transfer all of the cubes from the leftmost stack to the rightmost stack in such a way that no larger cube can sit on top of a smaller cube. Let's explore a classical problem in computer science called the Towers of Hanoi problem.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |