Skip to content

Instantly share code, notes, and snippets.

@sgeos
Last active July 15, 2023 12:54
Show Gist options
  • Save sgeos/27d759d86fef41375bb273cbad296f10 to your computer and use it in GitHub Desktop.
Save sgeos/27d759d86fef41375bb273cbad296f10 to your computer and use it in GitHub Desktop.
// ChatGPT Prompt
// Please write a Rust function to generate rooms in a Rogue style dungeon.
// Thoughts:
// As far as basic structure goes, this works. It needs a lot of work to be useful.
use rand::Rng;
struct Dungeon {
width: usize,
height: usize,
tiles: Vec<Vec<char>>,
}
struct Room {
x: usize,
y: usize,
width: usize,
height: usize,
}
fn generate_dungeon(width: usize, height: usize, num_rooms: usize) -> Dungeon {
let mut rng = rand::thread_rng();
let mut tiles = vec![vec!['"'; width]; height]; // Edit: Changed '.' to '"'
let mut rooms = Vec::new();
for _ in 0..num_rooms {
let room_width = rng.gen_range(4..10);
let room_height = rng.gen_range(4..10);
let x = rng.gen_range(1..width - room_width - 1);
let y = rng.gen_range(1..height - room_height - 1);
create_room(&mut tiles, x, y, room_width, room_height);
rooms.push(Room { x, y, width: room_width, height: room_height });
}
Dungeon {
width,
height,
tiles,
}
}
// Edit: Changed create_room() to fill_rectangle() and added a tile parameter to the end
fn fill_rectangle(tiles: &mut Vec<Vec<char>>, x: usize, y: usize, width: usize, height: usize, tile: char) {
for i in (x..x + width).into_iter() {
for j in (y..y + height).into_iter() {
tiles[j][i] = tile;
}
}
}
// Edit: moved body to above function and called fill rectangle twice to create walls and a floor
fn create_room(tiles: &mut Vec<Vec<char>>, x: usize, y: usize, width: usize, height: usize) {
fill_rectangle(tiles, x, y, width, height, '#');
fill_rectangle(tiles, x+1, y+1, width-2, height-2, '.');
}
fn main() {
let dungeon = generate_dungeon(40, 20, 5);
for row in dungeon.tiles {
let line: String = row.into_iter().collect();
println!("{}", line);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment