CSci 1113: Introduction to C/C++

Programming for Scientists and Engineers

Homework 9


Introduction

Due Date: Friday, April 02, 2021 before 11:55pm.

Submission: Your final submission for each part of the homework will be a single file: hw9A.cpp for part A and hw9B.cpp for part B. Each will be submitted on a different link on gradescope.

Grading: Grading for this homework will be fully automatic through the tests on gradescope. Part A will be tested as normal (your code will be run with various inputs checking for expected outputs) Part B will have a modified testing scheme documented in it’s section. Some test results will not be shown to you in advance, and will only be revealed after the deadline. All tests, however, will only test the behavior that is described in this document - So if you solve the problem described here you should be fine.

Instructions: This is an individual homework assignment. There are two problems, each worth 20 points. Solve the problem below by yourself (unlike the labs, where you work collaboratively), and submit the solution as a C++ source code file.

Some Rules/guidelines:

1. This is an individual assignment Unlike the lab assignments, this is not collaborative. You must design, implement, and test the solution to each problem on your own without substantial assistance from anyone other than the course instructor or TAs.

2. You may not include code, solutions, or portions of solutions from any external source. You are only allowed to use sources that are part of class: examples from the textbook, lectures, or code you and your partner write to solve lab problems. Using code obtained in other ways, or letting others view or use your code is considered academic misconduct. For more information, see the collaboration rules fifile on the class website, or ask the instructor or TAs if you have any questions on what is and is not allowed.

3. Because this homework is submitted and tested electronically, and automatically, the fol-lowing are more important than in a typical class. Failure to follow these instructions can result in a grade of 0.

• Follow the naming conventions mentioned at the end of the problems.

• Files must be submitted before the deadline or they will not be accepted.

• Files must be submitted to gradescope. Emailing the course staff will generally not count as a submission. (although this is an OK way to request assistance if gradescope is refusing to accept your files)

• The input and output formats listed in the problem description, and demonstrated in the examples, are not suggestions. Follow the provided input and output format exactly, and to the letter. (Remember that spaces and end-of-lines count as letters too!)

• Regardless of how or where you develop your solutions, we expect your programs to compile and execute on gradescope. It is not enough for them to run on your computer. Course staff can help you test in more detail if you have errors on gradescope, but no errors on your computer.

• The problem descriptions will usually show at least one test case and the resulting correct output. However, you should test your program on other test cases (that you make up) as well. Making up good test cases is a valuable programming skill, and is part of ensuring your code solution is correct.


Before starting

Both parts of this homework relates to the “HeroineQuestV2.cpp”. This is a rather large piece of code, so you should try to familiarize yourself with it before attempting to solve the problems. You do not need to understand every part of the code to solve the problems, just the general structure of the code. Please do these before attempting to provide a solution:

1. First, run the code to get an idea of how the program works. The descriptions below won’t make sense without this understanding

2. Secondly, read over the code briefly and make sure you feel like you understand the general structure. You might find some parts hard to understand (battles, for instance, are appar-ently a little troublesome) but you should feel like you have a general sense of what happens where and what the major functions do. Please treat this as part of the homework itself. It would be inappropriate to have someone (outside the teaching staff) explain this code to you.

Hints for this part of the problem:

• In general, I recommend starting with the basic (not-main) functions. They are pretty well-named and (possibly excepting the battle code) are pretty easy to understand.

• Then I recommend looking over the classes, ask yourself “what is each class responsible for?”

• Then, once you feel like you under stand the general role of these major parts of the code, loop back to the main function and make sure you understand it’s basic structure.

• Remember, your goal isn’t to understand this code perfectly, just to know enough to make the part A and part B chances.

• The left-hand side of geany (under “Symbols”) lists the functions (and variables), which can allow you to jump around to them in the code quickly. Most other coding programs (“IDEs”) have similar organizational tools to find functions/classes.)

Brief introduction to Heroine quest

When running the program, first you enter a name for the heroine:

Then you are shown to a map. You move using “wasd” (type one then press enter).

Walking into other things on the map (rabbits, town, water, etc.) can lead to interesting encoun-ters.


Problem A: A Pirate’s Life For Me (20 points)

For this problem we are going to add a boat that the heroine can get. Once the heroine has a boat they should be able to step onto water without drowning.

Specifically, your task is to modify the program so that:

1. When in town, the user can type “c” to buy a canoe. (Any other input/command will leave the town. Make sure it take two enters to leave town (as our grader assumes this)) You can assume the user will enter a single character, then press enter. Then one more enter to leave the “town screen”.

2. After buying a canoe, the heroine can travel on water types without dying. If they try to go off the edge of the screen, they should just stay where they are.


Hints

• When modifying the code for the town screen, use getline to get the “c” or “x” input. The tests will fail if you simply cin >> someChar.

• You will need to match the town screen prompts and formatting exactly. This shouldn’t require changing the picture, but it might require changing the rest of the text.


Example of town (buying a canoe, user input underlined):

Jimmy Jones walks into town, do you want to buy a (c)anoe or e(x)it town?

c

You are now the proud owner of your very own floatation device!

You exit town while whistling a jolly tune.

(press enter to continue...)

<enter>


Example of town(exiting, user input underlined):

Jimmy Jones walks into town, do you want to buy a (c)anoe or e(x)it town?

x

You exit town while whistling a jolly tune.

(press enter to continue...)

<enter>


Example of water (before buying canoe, user input underlined)

HP: 10

Which direction do you want to move (’w’=up, ’a’=left, ’s’=down, ’d’=right)? a

The heroic Jane Schrodiner walks in to the ocean wearing heavy armor and joins the fishes...

Game over...


Example of water (after buying canoe, user input underlined)

HP: 10

Which direction do you want to move (’w’=up, ’a’=left, ’s’=down, ’d’=right)? a


HP: 10

Which direction do you want to move (’w’=up, ’a’=left, ’s’=down, ’d’=right)?


Submission

When you are done, name the source code file hw9A.cpp. Then log into gradescope and upload your file for the “Homework 9A” submission. If you name your file incorrectly it will be unable to compile and run your code, so you will fail all test cases. You may submit cpp files as many times as you want until the deadline to try and fix the code if you fail a test case. Following rigorous naming conventions and using test cases are something computer programmers often must do in “real life” programming, and so submitting your program with the correct name and functionality is part of doing this assignment correctly.


Problem B: A little R&R (20 points)

DO NOT INCLUDE PART A in your part B solution. Instead start part B over from the original file. This time, we will be adding a “hunger” mechanic, wherein the heroine must eat occasionally, or they will die.

Modify the code to:

1. In addition to HP, keep track of a “hunger” value for the player (shown right below HP). After moving with “wasd”, decrease the hunger value by one. Hunger should start out at 10. (Hunger should only go down when you actually input “wasd”, if the user enters a non-movement letter hunger should not change)

2. Create a garden with a capital G two spaces above the town.

3. When visiting the garden, increase the “hunger” value by 7 (so if you want you can really pack on the pounds and not come back for a while). (Make sure you have to press ¡enter¿ only once to leave the garden, much like how the town was in the original code.)

4. If the hunger value is at zero when the player moves, decrease HP by one. (Even if the player moves onto the garden... they will get back their “hunger”, but still lose 1 HP as shown in the example).


Hints

• Your garden screen cannot take any extra input – only “press enter to continue” otherwise our grader will get out-of-sync (as it only expects to press enter once with no other input to leave the garden screen)

• Only change hunger when the user enters “wasd” – other letters, or other inputs in the game, should not change hunger.

• Hunger should never become negative

• While the order in the code isn’t super important, logically hunger goes down before the heroine moves. So if the heroine is starving and goes to the garden, they should take damage to move before going to the garden.

• If the HP hits 0 the player should die. You might not need to write code for this, but it is worth stating that as an expectation.


Example (user input underlined:)

HP: 8

Hunger: 1

Which direction do you want to move (’w’=up, ’a’=left, ’s’=down, ’d’=right)? w


HP: 8

Hunger: 0

Which direction do you want to move (’w’=up, ’a’=left, ’s’=down, ’d’=right)? w


HP: 7

Hunger: 0

Which direction do you want to move (’w’=up, ’a’=left, ’s’=down, ’d’=right)? d


HP: 6

Hunger: 0

Which direction do you want to move (’w’=up, ’a’=left, ’s’=down, ’d’=right)? s


A grandma with a suspiciously long nose says: ’O great asdf, have a feast of carrots to help vanquish those evil rabbits’. 

Once you are satiated, you venture back out into the wilderness...

(press enter to continue...)

<enter>


HP: 5

Hunger: 7

Which direction do you want to move (’w’=up, ’a’=left, ’s’=down, ’d’=right)? a


HP: 5

Hunger: 6

Which direction do you want to move (’w’=up, ’a’=left, ’s’=down, ’d’=right)? d


A grandma with a suspiciously long nose says: ’O great asdf, have a feast of carrots to help vanquish those evil rabbits’.

Once you are satiated, you venture back out into the wilderness...

(press enter to continue...)

<enter>


HP: 5

Hunger: 12

Which direction do you want to move (’w’=up, ’a’=left, ’s’=down, ’d’=right)? a


HP: 5

Hunger: 11

Which direction do you want to move (’w’=up, ’a’=left, ’s’=down, ’d’=right)? invalid


HP: 5

Hunger: 11

Which direction do you want to move (’w’=up, ’a’=left, ’s’=down, ’d’=right)? q

Game over...


Submission

When your code is written submit your hw9B.cpp file to the gradescope “Homework 9B” submis-sion. If you name your file incorrectly it will be unable to compile and run your code, so you will fail all test cases. You may submit cpp files as many times as you want until the deadline to try and fix the code if you fail a test case.