Lab 8: Functions II
- Due No Due Date
- Points 1
- Submitting a file upload
- File Types cpp
Note: You are encouraged to work on the following with a partner.
Overview
In this lab, you'll get more practice using functions and 2D arrays.
Learning outcomes
By the end of this lab, you should be more comfortable...
- using functions
- passing 2D arrays between functions
- implementing pseudo code
Exercises
zyLab
Complete the following zyLab in the zyBook. While you do it, be sure to implement incrementally—write a line or two of code, then compile/run it. Test frequently by clicking "Submit mode" and then "Submit for grading". You have unlimited submits, so don't worry about using some quota up.
- zyLab 6.25: Flip a coin
Grid World game
Download gridworld.cpp Download gridworld.cpp. This is a mostly implemented game in which a 2D world is represented as a grid of chars. The player, represented as the char 'P', can move up, down, left, or right on the grid, attempting to avoid enemies ('E'), pick up treasure ('T'), and reach the goal ('G'). Movement is shown by printing out the grid after every move. There are four TODOs spread out across three functions, which you should locate and complete:
- printWorld (1 TODO)
- getMove (2 TODOs)
- updateWorld (1 TODO)
Before you start, read through the code to see what global variable and constants there are, what functions there are, and how the functions connect. Note that this does not use the zyBook convention of placing function definitions above main, but rather puts function declarations above main and function definitions below main.
Implement each TODO incrementally: don't wait until you're totally done a TODO to compile and test; go for small wins to build confidence, ensure you're on the right track, and to focus your debugging.
Submitting
For the zyLab, Click "Submit mode" then "Submit for grading" when you are finished so I can see that you have done the zyLab. For gridworld.cpp, upload your .cpp file with what you've accomplished at the end of lab to this assignment.
PA options
Please see the syllabus and course schedule (both on the homepage) for more information about how many programming assignments you are required to pass, due dates, etc. Of note: you do not need to attempt every or even most PAs.
PA 8.1: Grid world with points
Complete and extend grid world so that the player accumulates points when they enter spots that contain treasure ('T'). You should do this by updating the `updateWorld` function so that it takes a new parameter that is the accumulated points in the game and update it according to whether the player's updated coordinates put them in a spot with a 'T'. Update `playGame` to keep track of the points and display them each time the world is printed and again at the end of the game.
You should not add any global variables to the program; pass data using parameters only.
Here are the specs:
// Name:
// Date:
// Partner:
//
// Specifications checklist for PA Option 8.1
// [ ] the header includes your name and anyone you worked with
// [ ] the header includes this specifications checklist
// [ ] the code in indented properly (use the autoformatter if you're not sure)
// [ ] useful comments are present above chunks of code that "hang together" (work toward a higher level goal)
// [ ] the identifiers are well named
// [ ] the program compiles
// [ ] the program runs without crashing or hanging
// [ ] all output and prompts look clean (correct spelling, capitalization, nothing squished)
// [ ] all prompts specify the required format of the input and work as expected
// [ ] all TODOs from Lab 8 are completed successfully:
// [ ] the grid world is displayed after each move in the format described in the `printWorld` JavaDoc
// [ ] the player is only prompted for valid moves
// [ ] invalid move selections cause the player to be reprompted
// [ ] the player's position in `world` is updated according to their move
// [ ] the player's previous position is erased
// [ ] the game is won when the player reaches the goal location
// [ ] the game is lost when the player enters an enemy's position
// [ ] a player earns points when they enter a treasure's position
// [ ] the current number of points is displayed after each move and at the end of the game
// [ ] points are updated in the `updateWorld` function
// [ ] data is passed between `playGame` and `updateWorld` using parameters, not global variables
// [ ] the JavaDoc of the `updateWord` function is updated according to the new function signature
PA 8.2 (advanced): Grid word with random layouts
Complete and extend grid world so that the initial configuration is randomized (enemies, treasure, the player, and the goal are placed at random spots in the grid world). As the player moves throughout the world, the enemies should approach the player (that is, all enemies should all take one step closer to the player's position on each move the player makes). If an enemy enters a treasure spot, the treasure should disappear. You should create functions for initializing the positions of entities and for moving the enemy positions. All necessary data should be passed using parameters—no global variables.
You can pick the number of enemies and treasure (you can even ask the user to enter a difficultly level that you use to decide these), but there should only be one goal and one player.
Here are the specs:
// Name:
// Date:
// Partner:
//
// Specifications checklist for PA Option 8.2 (Advanced)
// [ ] the header includes your name and anyone you worked with
// [ ] the header includes this specifications checklist
// [ ] the code in indented properly (use the autoformatter if you're not sure)
// [ ] useful comments are present above chunks of code that "hang together" (work toward a higher level goal)
// [ ] the identifiers are well named
// [ ] the program compiles
// [ ] the program runs without crashing or hanging
// [ ] all prompts specify the required format of the input and work as expected
// [ ] all output and prompts look clean (correct spelling, capitalization, nothing squished)
// [ ] all TODOs from Lab 8 are completed successfully:
// [ ] the grid world is displayed after each move in the format described in the `printWorld` JavaDoc
// [ ] the player is only prompted for valid moves
// [ ] invalid move selections cause the player to be reprompted
// [ ] the player's position in `world` is updated according to their move
// [ ] the player's previous position is erased
// [ ] the game is won when the player reaches the goal location
// [ ] the game is lost when the player enters an enemy's position
// [ ] enemies, treasure, the goal, and the player are placed randomly on the board
// [ ] no two entities(enemies, treasure, goal, player) should share the same location in the initial layout
// [ ] the code to do this is located in a dedicated function that is called form `playGame`
// [ ] the function has a properly formatted JavaDoc
// [ ] on each move the player makes, the positions of all the enemies steps one closer to the players updated position
// [ ] treasure should disappear if "captured" by an enemy
// [ ] if an enemy "captures" the player, the player loses
// [ ] the code to do this is located in a dedicated function that is called from `playGame`
// [ ] the function has a properly formatted JavaDoc
Submit your PA to First programming assignment, Second programming assignment, Third programming assignment, Fourth programming assignment, or Fifth programming assignment based on whether this is your first, second, etc. PA. If you are working with a partner, both of you must submit individually.
Rubric
Criteria | Ratings | ||
---|---|---|---|
Worked diligently on the lab problems
|
|
||
Submitted all materials according to the instructions
|
|
||
|