Skip to content

Commit

Permalink
psuedo-maze generation is done
Browse files Browse the repository at this point in the history
  • Loading branch information
malloc-nbytes committed Jan 27, 2024
1 parent bd2e5dc commit 28ebe87
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 75 deletions.
19 changes: 19 additions & 0 deletions src/graphics.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <cassert>
#include <cstdlib>
#include <stdint.h>
#include <vector>
Expand Down Expand Up @@ -37,4 +38,22 @@ void graphics_create_ppm(Image &img, const char *filepath)
}

std::fclose(fp);
}

Image graphics_scale_ppm(Image &img, size_t scale)
{
assert(scale != 0);
Image scaled_img = Image {img.height*scale, img.width*scale};

for (size_t i = 0; i < img.height; i++) {
for (size_t j = 0; j < img.width; j++) {
Pixel &color = img(i, j);
for (size_t k = 0; k < scale; k++) {
for (size_t l = 0; l < scale; l++) {
scaled_img(i*scale+k, j*scale+l) = color;
}
}
}
}
return scaled_img;
}
9 changes: 4 additions & 5 deletions src/include/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@ struct Image {
size_t width;
size_t height;
std::vector<Pixel> pixels;

inline Image(size_t width, size_t height)
: width(width), height(height), pixels(width*height) {}


inline Image(size_t w, size_t h) : width(w), height(h), pixels(w*h) {}

inline Pixel &operator()(size_t i, size_t j)
{
return pixels[i*width+j];
}
};

void graphics_create_ppm(Image &img, const char *filepath);

Image graphics_scale_ppm(Image &img, size_t scale);

#endif // GRAPHICS_H
1 change: 1 addition & 0 deletions src/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ typedef std::vector<std::string> strvec_t;
std::string utils_generate_html(std::string title, std::string description, long seed);
void utils_generate_file(std::string output_filepath, std::string output_body);
int utils_rng_roll(int min, int max, long seed);
int utils_roll_seed(void);
strvec_t utils_walkdir(const std::string& path);
void utils_zip_files(std::string out_file_name, strvec_t file_names, std::string password="");
std::string utils_file_to_str(const std::string filepath);
Expand Down
9 changes: 1 addition & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@
#include "./include/utils.h"
#include "./include/puzzle.h"

int roll_seed(void)
{
long seed = (uint)time(nullptr);
srand(seed);
return seed;
}

void create_nested_zipfiles(std::vector<Puzzle> &puzzles)
{
const char *zipdir = "zipfiles/puzzle";
Expand All @@ -39,7 +32,7 @@ void create_nested_zipfiles(std::vector<Puzzle> &puzzles)

int main(void)
{
long seed = roll_seed();
long seed = utils_roll_seed();

std::vector<Puzzle> puzzles = {
puzzle_create1(seed),
Expand Down
37 changes: 32 additions & 5 deletions src/puzzle.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#include <unistd.h>

#include "./include/utils.h"
#include "./include/puzzle.h"
Expand Down Expand Up @@ -32,17 +34,42 @@ Puzzle puzzle_create2(long seed)
return Puzzle{"puzzle2", "331E54F4AA00"};
}

#define MAZE_WALL ({0,0,0})
#define MAZE_PATH ({255,255,255})
#define MAZE_SIZE 16

void force_unique_shortest_path(Image &img)
{
(void)img;
assert(false && "unimplemented");
}

Puzzle puzzle_create3(long seed)
{
std::string description = utils_file_to_str("./puzzle3/.desc.txt");
std::string html_content = utils_generate_html("Maze Puzzle", description, seed);
utils_generate_file("./puzzle3/instructions.html", html_content);
Image img = Image {2,2};
img(0, 0) = Pixel {0, 0, 0};
img(0, 1) = Pixel {0, 0, 0};
img(1, 0) = Pixel {255, 255, 255};
img(1, 1) = Pixel {255, 255, 255};

int s = MAZE_SIZE;
Image img = Image {(size_t)s, (size_t)s};
for(int i = 0; i < s; i++) {
for(int j = 0; j < s; j++) {
img(i, j) = i%2 ? Pixel MAZE_PATH : Pixel MAZE_WALL;
}
}

for(int i = 0; i < s; i++) {
img(i, utils_rng_roll(0,s-1,seed+i)) = Pixel MAZE_PATH;
img(i, utils_rng_roll(0,s-1,seed+i*s)) = Pixel MAZE_PATH;
}

img(0, 0) = Pixel {255, 0, 255};
img(s-1, s-1) = Pixel {255, 255, 0};

img = graphics_scale_ppm(img, 70);

graphics_create_ppm(img, "./puzzle3/maze.ppm");

return Puzzle{"puzzle3", "uru6r3u3ld"};
}

Expand Down
57 changes: 0 additions & 57 deletions src/puzzle1/instructions.html

This file was deleted.

7 changes: 7 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ int utils_rng_roll(int min, int max, long seed)
return rand() % (max - min) + min;
}

int utils_roll_seed(void)
{
long seed = (uint)time(nullptr);
srand(seed);
return seed;
}

// Description:
// Zips a vector of files into a single zip file.
// Parameters:
Expand Down

0 comments on commit 28ebe87

Please sign in to comment.