0% found this document useful (0 votes)
35 views

Assignment4 COMP1010 Fall2023

This document provides instructions for Assignment 4 of an introductory computer science course. The assignment involves writing a text-based adventure game where the player controls an adventurer to collect gold and escape a room by falling down a hole, avoiding a grid bug. The game initialization involves randomizing the room size and starting positions of entities. Printing the room involves drawing tiles for walls, floors, and game entities. Getting player input consists of validating direction and number of steps. The game logic involves moving the adventurer and bug, checking for collisions, and updating the game state.

Uploaded by

Anjola Adeyemi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Assignment4 COMP1010 Fall2023

This document provides instructions for Assignment 4 of an introductory computer science course. The assignment involves writing a text-based adventure game where the player controls an adventurer to collect gold and escape a room by falling down a hole, avoiding a grid bug. The game initialization involves randomizing the room size and starting positions of entities. Printing the room involves drawing tiles for walls, floors, and game entities. Getting player input consists of validating direction and number of steps. The game logic involves moving the adventurer and bug, checking for collisions, and updating the game state.

Uploaded by

Anjola Adeyemi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ASSIGNMENT 4

DEPARTMENT AND COURSE NUMBER: COMP 1010


COURSE TITLE: Introductory Computer Science 1
TERM: Fall 2023
Assignment 4
DUE DATE: FRIDAY NOVEMBER 24TH, 2023 AT 11:59PM

Material Covered: Weeks 7-8


• Loops
• Nested Loops

Assignment Guidelines:
• All students in this course must read and meet the expectations described in
the Expectations for Individual Work in Computer Science (follow the link and read
all information provided).
• These assignments are your chance to learn the material for the exams. Code your
assignments independently. We use software to compare all submitted assignments to each
other, and pursue academic dishonestly vigorously. You must complete the Honesty
Declaration before you will be able to submit your assignment.
• Assignments must be completed using course material. Do not use advanced material we
have not yet covered in the course, even if you have prior programming experience.
Assignment 4 should be completed using concepts from Weeks 1-8.
• You must adhere to the general assignment instructions AND the programming
standards document published on the course website on UM Learn.
• Submit one .java file for this assignment. Name the files using your name, the assignment
number, and the question number, exactly as in this example:
LastnameFirstnameA4.java. Use your name exactly as shown in UM Learn (without
hyphens, if applicable).
• You may submit the assignment multiple times, but only the most recent version will be
marked.

Background
This assignment will be submitted as a single file. Evaluation is separated into multiple parts that
build on one another. You may (and should) break your program down into methods as you see
fit. While those methods do not need to align to the parts as specified below, we strongly suggest
that you use them as guidelines. However you break your program down, make sure that code
related to each part is clearly discernable to your marker! Comment and group your code as
appropriate to ensure readability.

For this assignment, you'll be writing a simple game based on the legendary open-sourced text-
based adventure game, NetHack! In this game, a player attempts to navigate an adventurer
through simple rectangular "dungeon" room to fetch some gold and escape down a hole in the
floor, all while avoiding a grid bug that's wandering about. Score in this game is determined by
the number of steps the character took, and the number of inputs the player took to get the
character to move. Sample output from a completed solution can be found in the assignment
folder. Your overall output does not have to look exactly like output from the sample solution!

1
ASSIGNMENT 1
DEPARTMENT AND COURSE NUMBER: COMP 1010

Part 1: Game Initialization [4 points]


Before getting into the game loop and logic, there are some initialization steps that we need to do
to set up the state of the game.

A. Room Randomization
As mentioned above, our game will take place in a single room. You should start by randomizing
the width and height of this room. You may choose bounds for how big or small the room can be,
within reason. Later on, you may even want to play around with these values to adjust how easy
or hard the game is!

B. Entity Randomization
In our game of NetHack there will also be 4 entities you need to track and manage:
• The player's adventurer
• The grid bug enemy
• A pile of gold
• A hole in the floor
First, choose a reasonable origin for your coordinate system so you can locate entities within the
room. (Eg. the lower-right corner is (0, 0) and the top left is (width-1, height-1).) Ensure that you
leave comments that describe your choice or that your code is self-descriptive!
With that decided, you can now randomize starting location for each of these entities within your
room. To ensure that the game isn't too easy, or doesn't end before it begins, make sure the
following are true before initialization is completed:
1. The grid bug and the adventurer are not placed in the same location initially
2. The adventurer and the gold are not placed in the same location initially
3. The gold and the hole are not placed in the same location initially
You may use a loop that is theoretically infinite to randomize initial coordinates, knowing that in
practice the random number generator will not repeatedly generate the same values forever.
Part 2: Printing the Room [5 points]
In NetHack, given that it's played entirely in the console; the room is displayed as a two-
dimensional space using various characters to represent the environment and the entities within
it. Each place in which a character is drawn is referred to as a tile. The next step is to print out
your room using the following characters for the tiles:

* The adventurer as an at symbol ('@')


* The grid bug as a lowercase x ('x')
* The pile of gold as a dollar sign ('$')
* The hole in the floor as a carat ('^')

Additionally, the room itself is represented with walls as octothorps ('#'), and floors as periods
('.'). When drawing the room, you should place walls in a full perimeter outside of the play area.
(That is, beyond the dimensions of the room you created in Part 1.A., not inside the room.)

2
ASSIGNMENT 1
DEPARTMENT AND COURSE NUMBER: COMP 1010

Movable entities (the adventurer and the grid bug) should be drawn over top of/take priority over
non-movable entities (the gold and the hole), with only empty floor tiles being drawn as periods.

You're going to be drawing the room multiple times over the course of the game, so it's probably
a good idea to put it into a method and test it out now...

Example room immediately after initialization:

#########
#.......#
#....^..#
#....@..#
#.......#
#.......#
#.x...$.#
#.......#
#########

Part 3: Getting Player Input [5 points]


As with all good games, we are going to allow the player to provide inputs and execute them
until some final conditions are met and the game ends. Each iteration of movement being
processed and the game state being updated is called a cycle or, as in our case, a step.

Now that you've got the room drawn, it's time to get the player involved. They are going to input
a cardinal direction (no diagonals) and a number of steps they would like the adventurer to step
in that direction. The direction may be a single character (eg. 'u'/'n'), or a word (eg. "up"/"north").
The number of steps must be an integer. Once processed, those input values can be stored in
global variables as needed. You should specify in the prompt whether directions are single
characters or whole words to make life easier for the player (your marker)!

Your program should be able to handle these inputs gracefully! You will need to perform any
and all validation to ensure that your program doesn't crash regardless of what the player inputs.
If a given input is invalid, you should continue to prompt the player until a valid input is
received.

Make sure to keep track of the number of valid inputs given by the player over the course of the
game, as that is one way of keeping score!

Notes:
• There is no need to implement any sort of "quit" or "exit" command for the player to
terminate the program gracefully.
• You make take direction and step number as separate inputs or as one (as in the
example), so long as it does so intuitively and without crashing._

<Example input on the next page…>

3
ASSIGNMENT 1
DEPARTMENT AND COURSE NUMBER: COMP 1010

Example input flow:

What are your next steps? (<dir> <steps>): u4


Input is invalid! Should be in the format '<direction_char>
<num_steps>'.
What are your next steps? (<dir> <steps>): 4u
Input is invalid! Should be in the format '<direction_char>
<num_steps>'.
What are your next steps? (<dir> <steps>): up 3
Input is invalid! Should be in the format '<direction_char>
<num_steps>'.
What are your next steps? (<dir> <steps>): d 6

Part 4: The Game Logic (10 marks)

With all the above in place, you're ready to make a game! Until the game ends you should take
the player's input and run the number of steps they indicated before prompting them. Each step
involves moving the player, moving the grid bug, and updating the game state.

A. Adventurer Movement
Each step should move the adventurer one tile in the direction specified by the player. If the
move would cause the adventurer to bump into a wall, you should complete processing the
current step (including grid bug movement and game state updating) and then prompt the player
for new input.

B. Grid Bug Movement


For each step, the grid bug should also move one tile, this time in a random orthogonal or
diagonal direction. If the grid bug would bump into a wall, it should remain in place for the step.

C. Updating Game State


This is where we track the information about the game's progress. After movement has been
finalized, you will need to check for the following cases, _in order of precedence_:
• If the adventurer and grid bug are on the same tile, the player loses and the game is over
• If the adventurer is on the tile containing the gold pile, consider it "picked up".
o Print a message indicating that this has happened.
o You should not draw the gold pile in the room after this point!
• If the adventurer is on same tile as the hole, and has already picked up the gold, they have
escaped and won the game!
o If they haven’t picked up the gold and are over the hole, the game continues…

D. End of Step
Once all the above has been processed, if the game hasn't ended you should print the current and
remaining step counts as well as the room state to the player, and prompt them to press the Enter
key before continuing on to the next step. (You should use a Scanner to implement this!) If you

4
ASSIGNMENT 1
DEPARTMENT AND COURSE NUMBER: COMP 1010

have executed all the steps the player requested in their last command, you will need to get more
input from them before continuing.

If the game has ended, you should print out a message stating the outcome, the number of input
the player provided, and the number of steps the adventurer took before letting the program end.

Example Output
The output below shows a snippet of the start of a game (user input is in red):

Welcome to the dungeon!

Step 0:
#########
#..$....#
#.......#
#.......#
#....@..#
#..x....#
#########
What are your next steps? (<dir> <steps>): u 2

Step 1:
#########
#..$....#
#.......#
#....@..#
#....^..#
#.x.....#
#########
Queued steps remaining: 1
Press <enter> to continue...

Step 2:
#########
#..$....#
#....@..#
#.......#
#....^..#
#..x....#
#########
What are your next steps? (<dir> <steps>): ...

[Programming Standards are worth 6 points]

You might also like