google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.: C program that simulates a basic indoor game, "Treasure Hunt".

Sunday, March 30, 2025

C program that simulates a basic indoor game, "Treasure Hunt".

 Here's a simplified C program that simulates a basic indoor game, "Treasure Hunt".


Treasure Hunt Game


#include <stdio.h>

#include <stdlib.h>

#include <time.h>


// Define structure for rooms

typedef struct {

    char name[50];

    char description[100];

    int treasure;

} Room;


// Define structure for players

typedef struct {

    char name[50];

    int score;

} Player;


// Function to create a new room

Room createRoom(char *name, char *description, int treasure) {

    Room room;

    strcpy(room.name, name);

    strcpy(room.description, description);

    room.treasure = treasure;

    return room;

}


// Function to create a new player

Player createPlayer(char *name) {

    Player player;

    strcpy(player.name, name);

    player.score = 0;

    return player;

}


// Function to play the game

void playGame(Room *rooms, int roomCount, Player *player) {

    int currentRoom = 0;

    while (1) {

        printf("\nYou are in the %s.\n", rooms[currentRoom].name);

        printf("%s\n", rooms[currentRoom].description);

        printf("Do you want to:\n1. Move to the next room\n2. Search for treasure\n3. Quit the game\n");

        int choice;

        scanf("%d", &choice);

        switch (choice) {

            case 1:

                if (currentRoom < roomCount - 1) {

                    currentRoom++;

                } else {

                    printf("You are already in the last room!\n");

                }

                break;

            case 2:

                if (rooms[currentRoom].treasure) {

                    printf("You found treasure! Your score increases by 10 points.\n");

                    player->score += 10;

                } else {

                    printf("There is no treasure in this room!\n");

                }

                break;

            case 3:

                printf("Thanks for playing! Your final score is %d.\n", player->score);

                return;

            default:

                printf("Invalid choice! Please choose again.\n");

        }

    }

}


int main() {

    // Create rooms

    Room room1 = createRoom("Room 1", "This is the first room.", 1);

    Room room2 = createRoom("Room 2", "This is the second room.", 0);

    Room room3 = createRoom("Room 3", "This is the third room.", 1);

    Room rooms[] = {room1, room2, room3};

    int roomCount = sizeof(rooms) / sizeof(rooms[0]);


    // Create player

    char playerName[50];

    printf("Enter your name: ");

    scanf("%s", playerName);

    Player player = createPlayer(playerName);


    // Play the game

    playGame(rooms, roomCount, &player);


    return 0;

}



This program demonstrates the following concepts:


1. *Room structure*: A structure to represent rooms with attributes like name, description, and treasure.

2. *Player structure*: A structure to represent players with attributes like name and score.

3. *Gameplay*: A function to simulate the gameplay, where the player navigates through rooms, searches for treasure, and earns points.


Note that this program is a highly simplified example and does not cover real-world complexities like game design, user input handling, and scoring systems.

No comments:

Post a Comment

The rotation of money in business

 The rotation of money in business refers to the flow of funds within a company, encompassing various financial activities and transactions....