# Lab 11: BYOW Introduction

[Prerequisites](/cs61b-fall-2022/prerequisites-to-running-the-assignments..md) to running the code.

Instructions: <https://fa22.datastructur.es/materials/lab/lab11/>

Solution: <https://github.com/tomthestrom/cs61b/tree/master/lab11/PlusWorld>

This lab served as an intro to the Tile Rendering engine which we will use to build our own world (BYOW - Build Your Own World) in Project 3.

As such, it's not really an exercise in using DS\&A, more like a tutorial on how to draw various types of tiles.

Part 1 - Meet the tile rendering engine explains how the rendering engine works.

## Part II: Use the Tile Rendering Engine

**Plus World Intro**

Above, we’ve seen how we can draw a world and generate randomness. Your task for the first half of lab is to use the tile generator we’ve seen to make a plus shape, like below.

<figure><img src="https://fa22.datastructur.es/materials/lab/lab11/img/exampleSideLength3.png" alt=""><figcaption><p>Source: UC Berkeley CS61B FA 22 (<a href="https://fa22.datastructur.es/materials/lab/lab11/img/exampleSideLength3.png">https://fa22.datastructur.es/materials/lab/lab11/img/exampleSideLength3.png</a>)</p></figcaption></figure>

### Implementation

```java
/**
 * Draws a world consisting of plus shaped regions.
 */
public class PlusWorld {
    private static final int WIDTH = 50;
    private static final int HEIGHT = 50;

    private static final int MAX_PLUS_SIZE = 6;

    /**
     * Creates a plus size of squares (of flowers) of the given size at the given coordinates
     */
    private static void addPlus(TETile[][] tiles, int size, int startX, int startY) {
        size = Math.min(size, MAX_PLUS_SIZE);

        int centralX = startX;
        int leftX = centralX - size;
        int rightX = centralX + size;

        int topY = startY;
        int middleY = topY - size;
        int bottomY = middleY - size;

        //top square
        addSquare(tiles, size, centralX, topY);
        //middle square
        addSquare(tiles, size, centralX, middleY);
        //middle left square
        addSquare(tiles, size, leftX, middleY);
        //middle right square
        addSquare(tiles, size, rightX, middleY);
        //bottom square
        addSquare(tiles, size, centralX, bottomY);
    }

    /**
     * Creates a square of Flower tiles at the given coordinates
     */
    private static void addSquare(TETile[][] tiles, int size, int startX, int startY) {

        for (int y = startY; y <= startY + size; y++) {
            for (int x = startX; x <= startX + size; x++) {
               tiles[x][y] = Tileset.FLOWER;
            }
        }
    }

    public static void main(String[] args) {
        TERenderer ter = new TERenderer();
        ter.initialize(WIDTH, HEIGHT);

        TETile[][] world = new TETile[WIDTH][HEIGHT];

        for (int x = 0; x < WIDTH; x += 1) {
            for (int y = 0; y < HEIGHT; y += 1) {
                world[x][y] = Tileset.NOTHING;
            }
        }

        int plusSquareSize = 3;

        int startX = world[0].length / 2 - (plusSquareSize / 2);
        int startY = world.length / 2 + plusSquareSize;

        addPlus(world, plusSquareSize, startX, startY);
        ter.renderFrame(world);
    }
}

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tomthestrom.gitbook.io/cs61b-fall-2022/lab-11-byow-introduction.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
