Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hands on OOP submission by M. Rasheed Qais T. #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions MRasheedQaisT_ITB/README.md
Original file line number Diff line number Diff line change
@@ -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
162 changes: 162 additions & 0 deletions MRasheedQaisT_ITB/raisePet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#include <iostream>
#include <string>
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;
}