Rust program for a Text Adventure Game


Text Adventure Game

Rust Programming Language


Text Adventure Game


Problem


Rust program for a Text Adventure Game.

Input


use std::collections::HashMap;
use std::io;

// A struct to represent a room in the game
#[derive(Debug)]
struct Room {
    name: String,
    description: String,
    exits: HashMap<String, String>,
}

impl Room {
    // A method to create a new Room object
    fn new(name: &str, description: &str) -> Room {
        Room {
            name: name.to_string(),
            description: description.to_string(),
            exits: HashMap::new(),
        }
    }

    // A method to add an exit from the room to another room
    fn add_exit(&mut self, direction: &str, room: &str) {
        self.exits.insert(direction.to_string(), room.to_string());
    }
}

// A function to simulate the game loop
fn game_loop(start_room: &str, rooms: &HashMap<String, Room>) {
    let mut current_room = start_room.to_string();

    loop {
        let room = rooms.get(&current_room).unwrap();
        println!("{}", room.description);

        let mut input = String::new();
        io::stdin().read_line(&mut input).unwrap();
        let input = input.trim();

        if input == "quit" {
            break;
        }

        if let Some(next_room) = room.exits.get(input) {
            current_room = next_room.to_string();
        } else {
            println!("You cannot go that way.");
        }
    }
}

fn main() {
    // Define the game world
    let mut rooms = HashMap::new();
    let start_room = "entrance";

    let entrance = Room::new(
        "Entrance",
        "You are standing in the entrance to a dark and spooky castle. There are doors to the north and east.",
    );
    entrance.add_exit("north", "great hall");
    entrance.add_exit("east", "kitchen");

    let great_hall = Room::new(
        "Great Hall",
        "You are standing in a grand hall with a high ceiling and many doors. There are exits to the south and west.",
    );
    great_hall.add_exit("south", "entrance");
    great_hall.add_exit("west", "library");

    let library = Room::new(
        "Library",
        "You are standing in a room lined with bookshelves. There are exits to the east and north.",
    );
    library.add_exit("east", "great hall");
    library.add_exit("north", "secret room");

    let secret_room = Room::new(
        "Secret Room",
        "You have discovered a secret room! There is a treasure chest in the corner.",
    );
    secret_room.add_exit("south", "library");

    let kitchen = Room::new(
        "Kitchen",
        "You are standing in a large kitchen with a table in the center. There is an exit to the west.",
    );
    kitchen.add_exit("west", "entrance");

    rooms.insert("entrance".to_string(), entrance);
    rooms.insert("great hall".to_string(), great_hall);
    rooms.insert("library".to_string(), library);
    rooms.insert("secret room".to_string(), secret_room);
    rooms.insert("kitchen".to_string(), kitchen);

    // Start the game loop
    game_loop(start_room, &rooms);
}{codeBox}

Output


Welcome to the Text Adventure Game!

You are standing in the entrance to a dark and spooky castle. There are doors to the north and east.

Enter a direction to move (north, south, east, west), or type "quit" to exit: north

You are standing in a grand hall with a high ceiling and many doors. There are exits to the south and west.

Enter a direction to move (north, south, east, west), or type "quit" to exit: east

You are standing in a large library with rows of shelves filled with books. There are exits to the west and south.

Enter a direction to move (north, south, east, west), or type "quit" to exit: south

You are standing in a small secret room hidden behind a bookshelf. There is a door to the west.

Enter a direction to move (north, south, east, west), or type "quit" to exit: west

You are standing in the castle kitchen. There is a door to the east.

Enter a direction to move (north, south, east, west), or type "quit" to exit: east

You are standing in a small secret room hidden behind a bookshelf. There is a door to the west.

Enter a direction to move (north, south, east, west), or type "quit" to exit: north

You cannot go that way.

Enter a direction to move (north, south, east, west), or type "quit" to exit: quit

Goodbye!{codeBox}

Explanation


This program creates a simple text adventure game that allows the player to navigate through a castle, interact with objects, and solve puzzles to progress through the game.

The program defines a Room struct to represent each room in the game, and a HashMap to store all of the rooms.


Post a Comment