Untitled

mail@pastecode.io avatar
unknown
plain_text
21 days ago
7.6 kB
7
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#define MAX_NAME_LENGTH 50

typedef struct CharacterNode {
    int health_point;
    int attack_point;
    char name_char[MAX_NAME_LENGTH];
    int def_point;
    int mana_point;
    int experience;
    bool isAlive;
    struct CharacterNode* next;
} Node;


Node* head = NULL;

void addCharacter(char name[], int level, int hp, int mp, int ap, int dp, int exp, bool status) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    strcpy(newNode->name_char, name);
    newNode->health_point = hp;
    newNode->mana_point = mp;
    newNode->attack_point = ap;
    newNode->def_point = dp;
    newNode->experience = exp;
    newNode->isAlive = status;
    newNode->next = NULL;

    if (head == NULL) {
        head = newNode;
    } else {
        Node* current = head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = newNode;
    }
}

Node* searchCharacter(char name[]) {
    Node* current = head;
    while (current != NULL) {
        if (strcmp(current->name_char, name) == 0) {
            return current;
        }
        current = current->next;
    }
    return NULL;
}

void deleteCharacter(char name[]) {
    Node* current = head;
    Node* prev = NULL;

    if (current != NULL && strcmp(current->name_char, name) == 0) {
        head = current->next;
        free(current);
        return;
    }

    while (current != NULL && strcmp(current->name_char, name) != 0) {
        prev = current;
        current = current->next;
    }

    if (current == NULL) {
        return;
    }

    prev->next = current->next;
    free(current);
}

void sortCharacterbyHP() {
    int swapped, i;
    Node* ptr1;
    Node* lptr = NULL;

    if (head == NULL) {
        return;
    }

    do {
        swapped = 0;
        ptr1 = head;

        while (ptr1->next != lptr) {
            if (ptr1->health_point < ptr1->next->health_point) {
                // Menukar data dalam node
                int temp_hp = ptr1->health_point;
                ptr1->health_point = ptr1->next->health_point;
                ptr1->next->health_point = temp_hp;

                int temp_ap = ptr1->attack_point;
                ptr1->attack_point = ptr1->next->attack_point;
                ptr1->next->attack_point = temp_ap;

                int temp_dp = ptr1->def_point;
                ptr1->def_point = ptr1->next->def_point;
                ptr1->next->def_point = temp_dp;

                int temp_mp = ptr1->mana_point;
                ptr1->mana_point = ptr1->next->mana_point;
                ptr1->next->mana_point = temp_mp;

                int temp_exp = ptr1->experience;
                ptr1->experience = ptr1->next->experience;
                ptr1->next->experience = temp_exp;

                bool temp_status = ptr1->isAlive;
                ptr1->isAlive = ptr1->next->isAlive;
                ptr1->next->isAlive = temp_status;

                char temp_name[MAX_NAME_LENGTH];
                strcpy(temp_name, ptr1->name_char);
                strcpy(ptr1->name_char, ptr1->next->name_char);
                strcpy(ptr1->next->name_char, temp_name);

                swapped = 1;
            }
            ptr1 = ptr1->next;
        }
        lptr = ptr1;
    } while (swapped);
}

void updateCharacterStatus(char name[], bool newStatus) {
    Node* current = searchCharacter(name);
    if (current != NULL) {
        current->isAlive = newStatus;
    }
}

void displayCharacters() {
    Node* current = head;
    while (current != NULL) {
        printf("Nama: %s\n", current->name_char);
        printf("Health Point: %d\n", current->health_point);
        printf("Mana Point: %d\n", current->mana_point);
        printf("Attack Point: %d\n", current->attack_point);
        printf("Defense Point: %d\n", current->def_point);
        printf("Experience: %d\n", current->experience);
        printf("Status: %s\n", current->isAlive ? "Hidup" : "Mati");
        printf("-------------------\n");
        current = current->next;
    }
}






void displayMenu() {
    printf("Menu:\n");
    printf("1. Tambah Karakter\n");
    printf("2. Cari Karakter\n");
    printf("3. Hapus Karakter\n");
    printf("4. Urutkan Karakter berdasarkan Health Point\n");
    printf("5. Perbarui Status Karakter\n");
    printf("6. Tampilkan Semua Karakter\n");
    printf("7. Keluar\n");
    printf("Pilihan: ");
}

int main() {
    int choice;
    char name[MAX_NAME_LENGTH];
    int level, hp, mp, ap, dp, exp;
    bool status;

    do {
        displayMenu();
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("Masukkan nama karakter: ");
                scanf("%s", name);
                printf("Masukkan level: ");
                scanf("%d", &level);
                printf("Masukkan health point: ");
                scanf("%d", &hp);
                printf("Masukkan mana point: ");
                scanf("%d", &mp);
                printf("Masukkan attack point: ");
                scanf("%d", &ap);
                printf("Masukkan defense point: ");
                scanf("%d", &dp);
                printf("Masukkan experience: ");
                scanf("%d", &exp);
                printf("Masukkan status (0 untuk mati, 1 untuk hidup): ");
                scanf("%d", &status);
                addCharacter(name, level, hp, mp, ap, dp, exp, status);
                break;
            case 2:
                printf("Masukkan nama karakter: ");
                scanf("%s", name);
                Node* found = searchCharacter(name);
                if (found != NULL) {
                    printf("Karakter ditemukan:\n");
                    printf("Nama: %s\n", found->name_char);
                    printf("Health Point: %d\n", found->health_point);
                    printf("Mana Point: %d\n", found->mana_point);
                    printf("Attack Point: %d\n", found->attack_point);
                    printf("Defense Point: %d\n", found->def_point);
                    printf("Experience: %d\n", found->experience);
                    printf("Status: %s\n", found->isAlive ? "Hidup" : "Mati");
                } else {
                    printf("Karakter tidak ditemukan.\n");
                }
                break;
            case 3:
                printf("Masukkan nama karakter: ");
                scanf("%s", name);
                deleteCharacter(name);
                break;
            case 4:
                sortCharacterbyHP();
                printf("Karakter telah diurutkan berdasarkan Health Point.\n");
                break;
            case 5:
                printf("Masukkan nama karakter: ");
                scanf("%s", name);
                printf("Masukkan status baru (0 untuk mati, 1 untuk hidup): ");
                scanf("%d", &status);
                updateCharacterStatus(name, status);
                break;
            case 6:
                displayCharacters();
                break;
            case 7:
                printf("Terima kasih telah menggunakan program ini.\n");
                break;
            default:
                printf("Pilihan tidak valid. Silakan coba lagi.\n");
                break;
        }
    } while (choice != 7);

    return 0;
}
Leave a Comment