This repository has been archived by the owner on Jan 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Palani Johnson
committed
Dec 8, 2021
1 parent
c34a5c1
commit 542c8a0
Showing
5 changed files
with
167 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
initial | ||
|
||
game | ||
game.ppm | ||
game.ppm | ||
omp_game |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* File: game.c | ||
* | ||
* This file contains some of my utility functions | ||
* that I use in some of my other files | ||
* | ||
*/ | ||
#include "game_of_life.h" | ||
|
||
|
||
// UTILITY | ||
|
||
// Gets the file extension | ||
char *get_filename_ext(char *filename) { | ||
char *dot = strrchr(filename, '.'); | ||
if(!dot || dot == filename) { | ||
fprintf(stderr, "Invalid file type. File must be .rle or .cells\n"); | ||
exit(EXIT_FAILURE); | ||
}; | ||
return dot + 1; | ||
} | ||
|
||
// Writes the current buffer of a life struct to ppm format | ||
void ppm_write(struct GameOfLife *life, FILE *f, char *vid_buff) { | ||
fprintf(f, "P6\n%d %d 255\n", life->width, life->height); | ||
fwrite(vid_buff, sizeof(char), life->width * life->height * 3, f); | ||
fflush(f); | ||
} | ||
|
||
char * alloc_video_buff(struct GameOfLife *life) { | ||
return malloc(life->width * life->height * sizeof(char) * 3); | ||
} | ||
|
||
// | ||
|
||
// LIFE FUNCTIONS | ||
|
||
// Fills a life struct with useful data | ||
void init_life(struct GameOfLife *life, char *init_type, int width, int height, int op1, int op2) { | ||
life->width = width; | ||
life->height = height; | ||
life->buff_size = (life->width + 2) * (life->height + 2); | ||
|
||
life->next_buff = malloc(life->buff_size * sizeof(bool)); | ||
life->buff = strcmp(init_type, "random") || strcmp(init_type, "rand") | ||
? alloc_random_buff(life, op1, op2) | ||
: alloc_buff_from_file(life, init_type, op1, op2); | ||
} | ||
|
||
// Allocates random data to a life struct buffer | ||
bool *alloc_random_buff(struct GameOfLife *life, int seed, int fill) { | ||
use_game_pos(life); | ||
bool *buff; | ||
|
||
if (seed == -1) { | ||
buff = calloc(life->buff_size, sizeof(bool)); | ||
for (int i = 0; i < 3; i++) | ||
buff[game_pos(i, 1)] = 1; | ||
} else { | ||
srand(seed); | ||
buff = malloc(life->buff_size * sizeof(bool)); | ||
for (int j = 0; j < life->height; j++) | ||
for (int i = 0; i < life->width; i++) | ||
buff[game_pos(i, j)] = (rand() / (double)RAND_MAX) | ||
< (double)fill / 100; | ||
} | ||
|
||
return buff; | ||
} | ||
|
||
// TODO: Make this function | ||
bool *alloc_buff_from_file(struct GameOfLife *life, char *file, int seed, int fill) { | ||
bool *buff = calloc(life->buff_size, sizeof(bool)); | ||
return buff; | ||
} | ||
|
||
// Make the next buffer the current buffer in a life struct | ||
void iterate_buff(struct GameOfLife *life) { | ||
bool *temp_buff = life->buff; | ||
life->buff = life->next_buff; | ||
life->next_buff = temp_buff; | ||
} | ||
|
||
// Free the buffers in a life struct | ||
void free_buffs(struct GameOfLife *life) { | ||
free(life->buff); | ||
free(life->next_buff); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#ifndef GAME_OF_LIFE | ||
#define GAME_OF_LIFE | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
#include <stdbool.h> | ||
#include <string.h> | ||
|
||
struct GameOfLife { | ||
int width; | ||
int height; | ||
int buff_size; | ||
bool *buff; | ||
bool *next_buff; | ||
}; | ||
|
||
#define use_game_pos(l) int _w2 = l->width+2; int _w3 = l->width+3 | ||
#define game_pos(i, j) ((j)*_w2 + _w3 + (i)) | ||
|
||
void init_life(struct GameOfLife *life, char *init_type, int width, int height, int op1, int op2); | ||
bool *alloc_random_buff(struct GameOfLife *life, int seed, int fill); | ||
bool *alloc_buff_from_file(struct GameOfLife *life, char *file, int seed, int fill); | ||
void ppm_write(struct GameOfLife *life, FILE *f, char *vid_buff); | ||
void iterate_buff(struct GameOfLife *life); | ||
void free_buffs(struct GameOfLife *life); | ||
char * alloc_video_buff(struct GameOfLife *life); | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.