In this problem set, you will use while loops and nested if-then-else statement to make a program that acts as a Tamagotchi! In case Tamagotchi sounds like gibberish, Tamagotchis were one of the biggest toy fads of the 1990s. They were housed in a small round device. Just like a real pet, you have to feed and play with your tamagotchi or it will die.
The grader of this problem set expects your output to exactly match our model Tamagotchi implementation. You should try playing through our model example and compare your outputs with ours as you add additional functionality.
As with in lecture, begin by opening up VSCode, ensuring your project is still open, and then running the following two commands in the VSCode Integrated Terminal:
In the same directory used in lecture:
2.0 Honor Code Header
All problem sets begin with an Honor Code pledge. Notice this header is contained within block comment tags discussed in Lecture 1. You can copy paste the pledge below and fill in your name and ONYEN.
/* * * Author: * * ONYEN: * * UNC Honor Pledge: I certify that no unauthorized assistance has been received * or given in the completion of this work. I certify that I understand and * could now rewrite on my own, without assistance from course staff, * the problem set code I am submitting. */
2.1 Imports
The next step after ensuring the honor code header is to import the functions we'll use from the introcs library. The first line of code to add to your app is the following:
import { print, promptString } from "introcs";
2.2 The main Function
Your program's execution will begin in a special function named `main`. Inside this function, you will put all of your code. It will run once main is called (main();).
export let main = async () => { // TODO: Put all of your code here }; main();
Now, we can start making a Tamagotchi pet!
3.1: Create status variables and Tamagotchi name
We need to create 3 variables that represent the state of your Tamagotchi and one that stores the number of days your Tamagotchi has been alive.
3.1 a) Since you can play with our Tamagotchi so long as it is alive, you need to create a variable that represents whether or not the Tamagotchi is alive. Name this variable isAlive and initialize its value to true.
3.1 b) You will also be tracking the hunger and happiness levels (from 0 to 100) of the Tamagotchi. Declare two variables named hungerLevel and happinessLevel and initialize them to 50.
3.1 c) So you can keep track of how long you Tamagotchi has been alive, declare a variable called daysAlive and initialize its value to 1. Each loop of the while loop will be considered a new day for the Tamagotchi.
3.1 d) Now that your Tamagotchi is alive, give it a name by prompting for a string and storing it in a variable called name.
3.2: Loop and change the Tamagotchi's state
3.2 a) The looping condition
In your Tamagotchi App, the while loop runs as long as your Tamagotchi is alive. The looping condition, then, is to loop while the isAlive variable's is true. Now you can begin writing the repeating block of the loop.
3.2 b) Once inside the loop, print a string that announces the current value of the daysAlive variable (to let us know how many days the Tamagotchi has been alive).
3.2 c) Prompt the user as to whether they want to feed or play with their Tamagotchi
The prompt you create should be the following string, but with Gotchi replaced by the name of your Tamagotchi:
"Do you want to play with or feed Gotchi? Type 'play', 'feed', or 'neither'."
Store the result of this prompt in a variable called feedOrPlay.
3.2 d) Vary hunger and happiness levels based off feedOrPlay
For this part, you will need to write a series of if-then-else statements to satisfy the following conditions:
1. If the user fed their Tamagotchi (i.e. the string they entered equals "feed"), the Tamagotchi will be less hungry but also less happy because he/she was not played with. Under this condition, print the name of your Tamagotchi concatenated with the string " has been fed!", decrease the hungerLevel variable by 10, and decrease the happinessLevel variable by 10.
2. If the user played with their Tamagotchi (i.e. the string they entered equals "play"), print the name of your Tamagotchi concatenated with the string " is happy!", increase the happinessLevel variable by 10 (but only if it's less than 100), and increase the hungerLevel variable by 15.
3. Finally, if the user did not feed or play with your Tamagotchi (i.e. the string they entered equals "neither" or something else), print the name of your Tamagotchi concatenated with the string " was neither fed nor played with.", decrease happiness by 10, and increase hunger by 15.
3.3: Check if Tamagotchi is still alive
In this while loop, instead of incrementing a counter at the end of the repeat block, you will check to see if the isAlive condition should be changed to false. Your Tamagotchi dies under any of the following conditions:
1. If hungerLevel is less than or equal to 0, you have overfed him/her.
2. if hungerLevel is greater than 100, you have starved him/her.
3. If happinessLevel is less than or equal to 0, your Tamagotchi has died of sadness.
When any of these conditions are met, change the value of your isAlive variable to false and print a string that contains the name of your Tamagotchi and what has caused this (i.e. "Gotchi's hunger has reached 0%", "Gotchi's happiness has reached 0%", or "Gotchi's hunger has exceeded 100%". Be sure to replace Gotchi with the name of your Tamagotchi, though).
3.4: Report on the status of a living Tamagotchi
Since there is no screen on our Tamagotchi App, we need to tell the user what the health and happiness status of his/her Tamagotchi is.
If your Tamagotchi is still alive after the checking the conditions in 3.3 (i.e. isAlive is true), print three statements. The first should say that your Tamagotchi is still living; the second should report the hunger level of your Tamagotchi; and the third should report the happiness level of your Tamagotchi. Be sure to include your Tamagotchi's name in each print statement, the word "hunger" (for #2), and the word "happiness" (for #3).
Since your Tamagotchi is still alive, it has lived another full day! Increase the value of daysAlive by 1.
3.5: Report the death of your Tamagotchi
Since the while loop will terminate if your Tamagotchi has died and isAlive is false, write a print statement after the loop that declares he/she has died and how long she has lived. The statement should look like the string below, with "Gotchi" being your Tamagotchi's name and "X" being replaced with the number of days your Tamagotchi lived.
Gotchi has died after X days. RIP :(
Congratulations, now that you have completed the obituary of your Tamagotchi you are all finished!
Part 3.6: Publish your app.
With your program running in your web browser, find the green "Publish App" button. If you are not still logged into introcs.com, you will be prompted to log in, and then your app will be bundled and published to the web!
Part 3.7: Submit for grading
Before submitting for grading, you should compare your program's output with our demo Tamagotchi's output. The grader is setup to test that your output strings match the reference implementation's.
To submit for grading first publish your project as per Part 3.6. Then, go to My101 in the top-right right corner of this page and select "Submit" next to the assignment. From here you should see a button that allows you to submit for grading. Your work will be graded through an automated process. If you receive points off, please select the "Report" link to see which tests did not pass. You can resubmit as many times as you'd like without penalty up until the deadline. We want you to keep working toward full credit on problem sets!