Lab 12: BYOW Intro

In this lab, you will code with the tile engine used in Project 5, use randomness to generate worlds, and learn how to collaborate on coding projects with git!

Before you start: We strongly recommend that you watch Lecture 29 (Software Engineering I: BYOW) starting at 29:43. That part of the lecture introduces the tile drawing engine that you’ll be using in this lab.

If you are a CS 61B student, and you have not yet signed up for a Lab 12 checkoff slot on Beacon, please do so now. You will need to attend this checkoff in order to earn full points for Project 5. If you have any questions, please refer to the main Ed post.

FAQ

Each assignment will have an FAQ linked at the top. You can also access it by adding “/faq” to the end of the URL. The FAQ for Lab 12 is located here.

Goals and Outcomes

By the end of this lab, you will…

  • Have drawn shapes onto a 2D world of tiles.
  • Have used a seeded Random object to generate the same “random” world every time.
  • Have split work with your partners using git commits, pushes, and pulls.

Setup

Follow the steps below to set up your group repository

  1. Go to your email associated with beacon and accept the invite to your group repository
  2. Clone your repository with SSH inside your 61b folder. It should look like
    git clone git@github.com:Berkeley-CS61B-Student/fa26-proj5-g***.git
    
  3. cd into the fa26-proj5-g*** repo and add the group skeleton
    git remote add skeleton git@github.com:Berkeley-CS61B-Student/proj5-skeleton-fa26.git
    
  4. Pull from the skeleton
    git pull skeleton main --allow-unrelated-histories
    

    If you see “fatal: refusing to merge unrelated histories,” try running git pull skeleton main --allow-unrelated-histories --no-rebase instead.

  5. Open Intellij in the lab12 folder now in the repo and add libraries as usual!

This starter code is in the lab12 folder. NOTE as with previous labs you do not need to submit this assignment. Instead, you will get credit by completing a checkoff with your TA during lab.

This lab requires partners! Each partner works on their own laptop and makes their own commits.

  • Partner A implements Plus.java.
  • Partner B implements Square.java.
  • Partner C implements ShapeWorld.java. If you don’t have a Partner C, Partners A and B work on ShapeWorld.java together.

Do not edit your partner’s file! Our tests check your git history to make sure each partner committed their own work.

Part 1: Drawing Shapes

Partner A and Partner B.

The Tile Renderer

To render tiles, we use TETile objects, and a 2D array TETile[][] to represent our world! These TETile objects contain information used to display the tile, like a character or a custom image.

To populate TETile objects we have a Tileset, a set of tiles that we have predefined for you! To set a tile, assign a TETile to a spot in the array, like world[x][y] = Tileset.FLOWER. You can build your own tiles from scratch using the TETile constructor (see TETile.java), or you can choose from the premade tiles in Tileset.java.

Similar to Percolation, our world is a 2D array, where each spot holds one tile. world[x][y] is the tile in column x and row y, with (0, 0) at the bottom left corner.

There are two kinds of shapes in this lab: a Plus and a Square. Both implement the Shape interface in Shape.java, so they have the same methods. Each shape has the following variables:

  • TILE: the tile used to draw the shape. Plus uses Tileset.FLOWER and Square uses Tileset.WALL. For more options, look at src/tileengine/Tileset.java.
  • x and y: the coordinates of the bottom left corner of the shape.
  • arm (for a plus) or side (for a square): the size of the shape.
    • For a plus, arm is how many tiles one arm reaches out from the center. This means its center is at (x + arm, y + arm).
    • For a square, side is how many tiles long each side is. The square is only an outline, so the tiles inside it are left unchanged.

For example, new Plus(3, 3, 2) is centered at (5, 5), and new Square(2, 3, 4) covers x = 2 to 5 and y = 3 to 6:

    new Plus(3, 3, 2)          new Square(2, 3, 4)

          ❀                        # # # #
          ❀                        #     #
      ❀ ❀ ❀ ❀ ❀                    #     #
          ❀                        # # # #
          ❀

Task 1: Fill in width() and height().

  • Partner A: in Plus.java, both should be 2 * arm + 1.
  • Partner B: in Square.java, both should be side.

Now, given an x, y, and size, we know exactly where the shape should be on our 2D grid, as well as how big it is.

Task 2: Implement draw(TETile[][] world), which sets every tile of your shape to TILE. No need to worry about going out of bounds; that’s our next step!

  • Partner A: draw the plus in Plus.java.
  • Partner B: draw the square outline in Square.java.

Run PlusTest or SquareTest to check your work.

Committing Your Shape

Once your shape is done, git add only your own file, then commit and push. For example, Partner A would run:

git add src/shapes/Plus.java
git commit -m "Implement Plus"
git push

Then, whoever is writing ShapeWorld.java should run git pull so that they have both shapes before starting Part 2.

Part 2: Building a World of Shapes

Partner C, or Partners A and B together.

If you are not actively coding/typing, you should still be there to help your partner verbally!

Staying in Bounds

Awesome! Now we can create Plus and Square objects and draw them onto our world. But can we draw any shape at any position? What if we have a plus at x = 38 and y = 28 with arm = 2 in a world that is 40 tiles wide and 30 tiles tall? Try visualizing what this would look like. Its right and top arms go off the edge of the world! We would get an ArrayIndexOutOfBoundsException when we try to draw them. Before we draw a shape, we must make sure it stays inside our world.

Take a look at ShapeWorld.java. This file builds the world that gets shown in your visualizer. WIDTH is how wide the world is, and HEIGHT is how tall it is. Using these, along with a shape’s position and size, we can decide whether the shape would fall off of our world. If it does, we don’t want to draw it.

Task 3: In ShapeWorld.java, implement fitsInWorld(Shape s).

  • Trio: Partner C does this task.
  • Duo: Partners A and B do this task together.

Random Shapes

Next, we want to create a randomly generated world that has pluses and squares scattered throughout. Computers can’t actually make truly random numbers. Instead, a Random object uses a pseudorandom number generator, which produces a sequence of numbers that looks random.

In randomShape, x is already set to a random number from 0 to WIDTH, and y to a random number from 0 to HEIGHT. It is your job to use random.nextBoolean(), which returns a random boolean, to decide which shape to make. Then, give it a random size the same way x and y were picked: an arm from 1 to 3 for a plus, or a side from 3 to 6 for a square. Make sure to use the random object we pass in, rather than making a new one.

Task 4: In ShapeWorld.java, implement randomShape(Random random).

  • Trio: Partner C does this task.
  • Duo: Partners A and B do this task together.

Placing Shapes

Now, placeShapes should attempt to place ATTEMPTS random shapes onto our world. Return the kept shapes as a List.

The seed passed into placeShapes is used to create a Random object. A Random object made with the same seed produces the same sequence of numbers every time, so the same seed will always give the same world. Make exactly one Random object in placeShapes.

Task 5: In ShapeWorld.java, implement placeShapes(long seed).

  • Trio: Partner C does this task.
  • Duo: Partners A and B do this task together.

Rendering the World

Now, with render(), we can draw any list of shapes passed in. render() is written for you: it builds a WIDTH by HEIGHT grid, fills it with empty tiles (Tileset.NOTHING), draws every shape on top of the empty tiles, and returns the finished world.

Run ShapeWorldTest to check your work, then run ShapeWorldDemo to see your world! You can pass a different seed as a program argument to see a different world. Run GitHistoryTest to check that your git history follows the partner rules.

Submission

Show your Lab TA the passed tests for credit!


Table of contents