💿
CS61B Fall 2022
  • CS61B Fall 2022
  • Prerequisites to running the assignments.
  • Lab 01: Setting up your computer
  • Lab 02: JUnit Tests and Debugging
  • Project 0: 2048
  • Lab 03: Timing Tests and Randomized Comparison Tests
  • Lab 04: A Debugging Mystery
  • Project 1: Deques
  • HW 2: Percolation
  • Lab 07: BSTMap
  • Project 2a: NGordNet (NGrams)
  • Lab 08: HashMap
  • Project 2b: NGordNet (WordNet)
  • Lab 11: BYOW Introduction
  • Project 3 BYOW
    • Phase 1: World Generation
Powered by GitBook
On this page
  • Part II: Use the Tile Rendering Engine
  • Implementation

Lab 11: BYOW Introduction

Lab assignments continue with Lab 11 (after Lab 8), Lab 9 and Lab 10 were days dedicated to working on Project 2.

PreviousProject 2b: NGordNet (WordNet)NextProject 3 BYOW

Last updated 1 year ago

to running the code.

Instructions:

Solution:

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.

Source: UC Berkeley CS61B FA 22 ()

Implementation

/**
 * 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);
    }
}
Prerequisites
https://fa22.datastructur.es/materials/lab/lab11/
https://github.com/tomthestrom/cs61b/tree/master/lab11/PlusWorld
https://fa22.datastructur.es/materials/lab/lab11/img/exampleSideLength3.png