From a710fb09e505c3ee837e23ce2aa65d2a75f775bd Mon Sep 17 00:00:00 2001 From: trimonuter Date: Fri, 27 Jan 2023 23:11:56 +0700 Subject: [PATCH] Hands on OOP submission by M. Rasheed Qais T. --- MRasheedQaisT_ITB/README.md | 15 +++ MRasheedQaisT_ITB/raisePet.cpp | 162 +++++++++++++++++++++++++++++++++ 2 files changed, 177 insertions(+) create mode 100644 MRasheedQaisT_ITB/README.md create mode 100644 MRasheedQaisT_ITB/raisePet.cpp diff --git a/MRasheedQaisT_ITB/README.md b/MRasheedQaisT_ITB/README.md new file mode 100644 index 0000000..acdb5cd --- /dev/null +++ b/MRasheedQaisT_ITB/README.md @@ -0,0 +1,15 @@ +Nama / NIM: Muhammad Rasheed Qais Tandjung / 19622230 + +Program yang saya buat adalah sebuah permainan mengurus hewan peliharaan sederhana yang berbasis command line. +Pengguna dapat memilih hewan peliharaannya sebagai anjing atau kucing, lalu hewan peliharaan tersebut memiliki data hunger dan fatigue. +Setiap harinya, data hunger dan fatigue hewan tersebut akan meningkat. Melakukan suatu aktivitas dapat mengurangi meter tersebut. +Apabila meter hunger atau fatigue melewati 15, maka hewan peliharaan akan mati. + +Cara menjalankan program sebagai berikut: + +(a) Pengguna memilih hewan peliharaan dan memberi nama +(b) Akan diberikan opsi untuk memilih opsi a, b, atau c. Pengguna cukup menuliskan pilihannya ke terminal. +(c) Permainan terus berlanjut sampai hewan peliharaan mati. + +Bahasa yang digunakan adalah C++ dan versi compiler yang digunakan adalah: +g++ (MinGW-W64 x86_64-ucrt-posix-seh, built by Brecht Sanders) 12.2.0 \ No newline at end of file diff --git a/MRasheedQaisT_ITB/raisePet.cpp b/MRasheedQaisT_ITB/raisePet.cpp new file mode 100644 index 0000000..eb0c215 --- /dev/null +++ b/MRasheedQaisT_ITB/raisePet.cpp @@ -0,0 +1,162 @@ +#include +#include +using namespace std; + +class Animal { + public: + string name; + bool alive = true; + int age = 0; + int hunger = 0; + int fatigue = 0; + + Animal(string inputName){ + name = inputName; + } + + void eat() { + if (hunger == 0){ + cout << name << " isn't hungry." << "\n"; + } + + else { + cout << name << " is eating." << "\n"; + hunger += -3; + } + + if (hunger < 0){ + hunger = 0; + } + } + + void sleep() { + if (fatigue == 0){ + cout << name << " isn't tired." << "\n"; + } + + else { + cout << name << " is sleeping." << "\n"; + fatigue += -3; + hunger += 4; + } + + if (fatigue < 0){ + fatigue = 0; + } + } + + void makeNoise() { + cout << name << " is making noise." << "\n"; + } +}; + +class Dog : public Animal { + public: + void bark() { + cout << "Bark! Bark!" << "\n"; + cout << name << " is barking." << "\n"; + } +}; + +class Cat : public Animal { + public: + void meow() { + cout << "Meow! Meow!" << "\n"; + cout << name << " is meowing." << "\n"; + } +}; + +int main() { + char choice; + cout << "Please choose your pet: (a) Dog (b) Cat" << "\n"; + cout << "Enter: "; + cin >> choice; + cout << "\n"; + + if (choice == 'a'){ + string dogName; + cout << "Please enter your dog's name: "; + cin >> dogName; + cout << "\n"; + + Animal pet = Animal(dogName); + cout << "Congratulations! You now have your own little " << pet.name << "!"; + cout << "\n"; + + int day = 1; + while (pet.alive){ + cout << "\n"; + cout << "Day " << day <<". What do you want to do today?" << "\n"; + cout << "(a) Feed your dog (b) Sing your dog to sleep (c) Do nothing" << "\n"; + cin >> choice; + cout << "\n"; + + if (choice == 'a'){ + pet.eat(); + pet.fatigue += 2; + } + else if (choice == 'b'){ + pet.sleep(); + } + else if (choice == 'c'){ + cout << "You did nothing the entire day." << "\n"; + pet.fatigue += 2; + pet.hunger += 2; + } + + cout << "Hunger: " << pet.hunger << "\n"; + cout << "Fatigue: " << pet.fatigue << "\n"; + + if ((pet.hunger > 15) || (pet.fatigue > 15)){ + pet.alive = false; + cout << "\n"; + cout << "Game over! " << pet.name << " has died!"; + } + day += 1; + } + } + + else{ + string catName; + cout << "Please enter your cat's name: "; + cin >> catName; + cout << "\n"; + + Animal pet = Animal(catName); + cout << "Congratulations! You now have your own little " << pet.name << "!"; + cout << "\n"; + + int day = 1; + while (pet.alive){ + cout << "\n"; + cout << "Day " << day <<". What do you want to do today?" << "\n"; + cout << "(a) Feed your pet (b) Sing your pet to sleep (c) Do nothing" << "\n"; + cin >> choice; + cout << "\n"; + + if (choice == 'a'){ + pet.eat(); + pet.fatigue += 2; + } + else if (choice == 'b'){ + pet.sleep(); + } + else if (choice == 'c'){ + cout << "You did nothing the entire day." << "\n"; + pet.fatigue += 2; + pet.hunger += 2; + } + + cout << "Hunger: " << pet.hunger << "\n"; + cout << "Fatigue: " << pet.fatigue << "\n"; + + if ((pet.hunger > 15) || (pet.fatigue > 15)){ + pet.alive = false; + cout << "\n"; + cout << "Game over! " << pet.name << " has died!"; + } + day += 1; + } + } + return 0; +}