Skip to content

Lesson 3.1

Matt Huesman edited this page Nov 2, 2022 · 1 revision

Introduction

In the last lesson we created a class called Player containing the logic and functionality of our player. In this lesson we'll work on creating a Dungeon class containing the logic and functionality of our dungeon. Thinking through this game, what are the different concepts in our game we can abstract away from each other? Hint: think players, the dungeon as a whole, rooms within that dungeon, and maybe even monsters and items at some point. All these different concepts will be their own classes. We've already done this for the player, but in this lesson we will do the same for the dungeon.

Assignment G

Think about what a dungeon contains. It doesn't necessarily need a player, but it does need rooms. These rooms will contain data like which walls have doors, a room description, and what happens to the player in this room. Try and create a room class without looking at the example code below. Note that rooms will be stored in an array in the dungeon and will refer to each other as an index in that array. For example if room #1 is connected to room #2 it will reference that room as 2.

public class Room {
    private boolean visited = false;

    private final String description;

    public final int northConnectedRoom;
    public final int southConnectedRoom;
    public final int eastConnectedRoom;
    public final int westConnectedRoom;

    private final boolean isDeathRoom;
    private final boolean isWinningRoom;

    public Room( 
        String description,
        int northConnectedRoom,
        int southConnectedRoom,
        int eastConnectedRoom,
        int westConnectedRoom,
        boolean isDeathRoom,
        boolean isWinningRoom
    ) {
        this.description = description;

        this.northConnectedRoom = northConnectedRoom;
        this.southConnectedRoom = southConnectedRoom;
        this.eastConnectedRoom = eastConnectedRoom;
        this.westConnectedRoom = westConnectedRoom;

        this.isDeathRoom = isDeathRoom;
        this.isWinningRoom = isWinningRoom;
    }

    public String getDescription() {
        String modifiedDescription;

        if (visited) {
            modifiedDescription = "you re-enter " + description;
        } else {
            modifiedDescription = "you enter " + description;
        }

        return modifiedDescription;
    }

    public int getConnectingRoom(Direction direction) {
        switch (direction) {
            case NORTH:
                return northConnectedRoom;
        
            case SOUTH:
                return southConnectedRoom;

            case EAST:
                return eastConnectedRoom;

            case WEST:
                return westConnectedRoom;

            default:
                return -1;
        }
    }

    public boolean isDeathRoom() {
        return isDeathRoom;
    }

    public boolean isWinningRoom() {
        return isWinningRoom;
    }
}

Finally create a Dungeon class and create an array of rooms and initialize that array with some cool rooms. As for how we will interact with the Dungeon class create a method called getRoom that takes a room index and returns the room at that index. We'll also need a method called getNextRoom which takes a desiredDirection and currentRoom to return the connecting room. That will be it for our dungeon class!

Assignment H

So far all our code has been written, but none of it is being used. To change that go into our Main class and in our Main class constructor below the welcome message instantiate an instance of Player and our Dungeon. Then create a while loop with the conditions !player.isDead() || !player.hasWon() and within that check if the user has moved and print out the description of their current room if they have. Then process the user's input and store the users next desired room. After that make sure that room is not equal to -1 if it is make sure the user knows they ran into a wall. Within this check if the players current room is a death room or win room and set the player's state accordingly.

Assignment I

Get creative and add some more rooms, work on implementing items or monsters, or add a peek feature as seen in the lesson 3 example code (these are all just examples). Your imagination is the limit and don't forget to ask for help if you need help.

Commit and Push your code!

Clone this wiki locally