fix bug
This commit is contained in:
104
src/game_core.c
104
src/game_core.c
@@ -7,75 +7,75 @@ const int directions[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1},
|
|||||||
const uint32_t colors[2] = {BLACK, WHITE};
|
const uint32_t colors[2] = {BLACK, WHITE};
|
||||||
|
|
||||||
Board init_board(size_t width, size_t height, int random) {
|
Board init_board(size_t width, size_t height, int random) {
|
||||||
Board board = {NULL, width, height};
|
Board board = {NULL, width, height};
|
||||||
board.pixels = malloc(height * sizeof(uint32_t *));
|
board.pixels = malloc(height * sizeof(uint32_t*));
|
||||||
for (size_t y = 0; y < height; y++) {
|
for (size_t y = 0; y < height; y++) {
|
||||||
|
board.pixels[y] = malloc(width * sizeof(uint32_t));
|
||||||
board.pixels[y] = malloc(width * sizeof(uint32_t));
|
for (size_t x = 0; x < width; x++) {
|
||||||
for (size_t x = 0; x < width; x++) {
|
if (random)
|
||||||
if (random)
|
board.pixels[y][x] = colors[rand() % 2];
|
||||||
board.pixels[y][x] = colors[rand() % 2];
|
else
|
||||||
else
|
board.pixels[y][x] = BLACK;
|
||||||
board.pixels[y][x] = BLACK;
|
}
|
||||||
}
|
}
|
||||||
}
|
return board;
|
||||||
return board;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_board(Board board) {
|
void free_board(Board board) {
|
||||||
for (size_t y = 0; y < board.height; y++)
|
for (size_t y = 0; y < board.height; y++)
|
||||||
free(board.pixels[y]);
|
free(board.pixels[y]);
|
||||||
free(board.pixels);
|
free(board.pixels);
|
||||||
}
|
}
|
||||||
|
|
||||||
Board copy_board(Board board) {
|
Board copy_board(Board board) {
|
||||||
Board copy = init_board(board.width, board.height, 0);
|
Board copy = init_board(board.width, board.height, 0);
|
||||||
for (size_t x = 0; x < board.width; x++) {
|
for (size_t x = 0; x < board.width; x++) {
|
||||||
for (size_t y = 0; y < board.height; y++) {
|
for (size_t y = 0; y < board.height; y++) {
|
||||||
copy.pixels[y][x] = board.pixels[y][x];
|
copy.pixels[y][x] = board.pixels[y][x];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
return copy;
|
||||||
return copy;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void display_board(Board board) {
|
void display_board(Board board) {
|
||||||
for (size_t y = 0; y < board.height; y++) {
|
for (size_t y = 0; y < board.height; y++) {
|
||||||
for (size_t x = 0; x < board.width; x++) {
|
for (size_t x = 0; x < board.width; x++) {
|
||||||
printf("%c", board.pixels[y][x] == BLACK ? ' ' : 'X');
|
printf("%c", board.pixels[y][x] == BLACK ? ' ' : 'X');
|
||||||
|
}
|
||||||
|
puts("");
|
||||||
}
|
}
|
||||||
puts("");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int count_adjacent(Board board, size_t x, size_t y) {
|
int count_adjacent(Board board, size_t x, size_t y) {
|
||||||
size_t i;
|
size_t i;
|
||||||
int w, h;
|
int w, h;
|
||||||
uint8_t count = 0;
|
uint8_t count = 0;
|
||||||
for (i = 0; i < 8; i++) {
|
for (i = 0; i < 8; i++) {
|
||||||
w = x + directions[i][1];
|
w = (int)x + directions[i][0];
|
||||||
h = y + directions[i][0];
|
h = (int)y + directions[i][1];
|
||||||
if (!((w < 0 || w >= (int)board.width) ||
|
if (!((w < 0 || w >= (int)board.width) ||
|
||||||
(h < 0 || h >= (int)board.height)))
|
(h < 0 || h >= (int)board.height)))
|
||||||
if (board.pixels[h][w] == WHITE)
|
if (board.pixels[h][w] == WHITE)
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
Board process_game(Board board) {
|
Board process_game(Board board) {
|
||||||
Board new_board = copy_board(board);
|
Board new_board = copy_board(board);
|
||||||
size_t x, y;
|
size_t x, y;
|
||||||
uint8_t count = 0;
|
uint8_t count = 0;
|
||||||
for (y = 0; y < board.height; y++) {
|
for (y = 0; y < board.height; y++) {
|
||||||
for (x = 0; x < board.width; x++) {
|
for (x = 0; x < board.width; x++) {
|
||||||
count = count_adjacent(board, x, y);
|
count = count_adjacent(board, x, y);
|
||||||
if (board.pixels[y][x] == BLACK && count == 3) {
|
if (board.pixels[y][x] == BLACK && count == 3) {
|
||||||
new_board.pixels[y][x] = WHITE;
|
new_board.pixels[y][x] = WHITE;
|
||||||
} else if (board.pixels[y][x] == WHITE && count != 2 && count != 3) {
|
} else if (board.pixels[y][x] == WHITE && count != 2 &&
|
||||||
new_board.pixels[y][x] = BLACK;
|
count != 3) {
|
||||||
}
|
new_board.pixels[y][x] = BLACK;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
free_board(board);
|
||||||
free_board(board);
|
return new_board;
|
||||||
return new_board;
|
|
||||||
}
|
}
|
||||||
|
|||||||
167
src/main.c
167
src/main.c
@@ -1,92 +1,101 @@
|
|||||||
#include "game_core.h"
|
|
||||||
#include "utils.h"
|
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include "game_core.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
#define SEED (5)
|
#define SEED (5)
|
||||||
#define WIDTH (800)
|
#define WIDTH (800)
|
||||||
#define HEIGHT (600)
|
#define HEIGHT (600)
|
||||||
#define SIM_WIDTH (10)
|
#define SIM_WIDTH (800)
|
||||||
#define SIM_HEIGHT (10)
|
#define SIM_HEIGHT (600)
|
||||||
|
|
||||||
t_flags process_args(int argc, char **argv) {
|
t_flags process_args(int argc, char** argv) {
|
||||||
t_flags output_flags = {0};
|
t_flags output_flags = {0};
|
||||||
if (argc <= 1)
|
if (argc <= 1)
|
||||||
return output_flags;
|
return output_flags;
|
||||||
if (argv[1][0] != '-')
|
if (argv[1][0] != '-')
|
||||||
return output_flags;
|
return output_flags;
|
||||||
|
|
||||||
char *flags = argv[1];
|
char* flags = argv[1];
|
||||||
int i = 1;
|
int i = 1;
|
||||||
char c = flags[i];
|
char c = flags[i];
|
||||||
while (c) {
|
while (c) {
|
||||||
if (c == 'g')
|
if (c == 'g')
|
||||||
output_flags.graphical = 1;
|
output_flags.graphical = 1;
|
||||||
if (c == 'm')
|
if (c == 'm')
|
||||||
output_flags.multithread = 1;
|
output_flags.multithread = 1;
|
||||||
if (c == 'o')
|
if (c == 'o')
|
||||||
output_flags.opti = 1;
|
output_flags.opti = 1;
|
||||||
c = flags[++i];
|
c = flags[++i];
|
||||||
}
|
|
||||||
return output_flags;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
|
||||||
t_flags flags = process_args(argc, argv);
|
|
||||||
srand(time(NULL));
|
|
||||||
|
|
||||||
if (flags.graphical) {
|
|
||||||
Board b = init_board(WIDTH, HEIGHT, 1);
|
|
||||||
|
|
||||||
SDL_Init(SDL_INIT_VIDEO);
|
|
||||||
|
|
||||||
SDL_Window *win = SDL_CreateWindow("pixels", 0, 0, WIDTH, HEIGHT, 0);
|
|
||||||
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
|
|
||||||
|
|
||||||
SDL_Texture *tex =
|
|
||||||
SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888,
|
|
||||||
SDL_TEXTUREACCESS_STREAMING, SIM_WIDTH, SIM_HEIGHT);
|
|
||||||
|
|
||||||
uint32_t pixels[HEIGHT][WIDTH];
|
|
||||||
int running = 1;
|
|
||||||
SDL_Event e;
|
|
||||||
while (running) {
|
|
||||||
while (SDL_PollEvent(&e))
|
|
||||||
if (e.type == SDL_QUIT)
|
|
||||||
running = 0;
|
|
||||||
|
|
||||||
// Envoie la matrice à la texture
|
|
||||||
uint32_t flat[HEIGHT * WIDTH];
|
|
||||||
for (size_t y = 0; y < HEIGHT; y++)
|
|
||||||
for (size_t x = 0; x < WIDTH; x++)
|
|
||||||
flat[y * WIDTH + x] = b.pixels[y][x];
|
|
||||||
|
|
||||||
SDL_Rect dst = {0, 0, WIDTH, HEIGHT};
|
|
||||||
SDL_UpdateTexture(tex, NULL, flat, SIM_WIDTH * sizeof(uint32_t));
|
|
||||||
SDL_RenderClear(ren);
|
|
||||||
SDL_RenderCopy(ren, tex, NULL, &dst);
|
|
||||||
SDL_RenderPresent(ren);
|
|
||||||
b = process_game(b);
|
|
||||||
SDL_Delay(500);
|
|
||||||
}
|
}
|
||||||
SDL_DestroyTexture(tex);
|
return output_flags;
|
||||||
SDL_DestroyRenderer(ren);
|
}
|
||||||
SDL_DestroyWindow(win);
|
|
||||||
SDL_Quit();
|
int main(int argc, char* argv[]) {
|
||||||
free_board(b);
|
t_flags flags = process_args(argc, argv);
|
||||||
|
srand(time(NULL));
|
||||||
} else {
|
|
||||||
|
if (flags.graphical) {
|
||||||
Board b = init_board(SIM_WIDTH, SIM_HEIGHT, 1);
|
Board b = init_board(SIM_WIDTH, SIM_HEIGHT, 1);
|
||||||
display_board(b);
|
|
||||||
puts("----------");
|
SDL_Init(SDL_INIT_VIDEO);
|
||||||
b = process_game(b);
|
|
||||||
display_board(b);
|
SDL_Window* win = SDL_CreateWindow("pixels", 0, 0, WIDTH, HEIGHT, 0);
|
||||||
free_board(b);
|
SDL_Renderer* ren =
|
||||||
}
|
SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
SDL_Texture* tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888,
|
||||||
|
SDL_TEXTUREACCESS_STREAMING,
|
||||||
|
SIM_WIDTH, SIM_HEIGHT);
|
||||||
|
|
||||||
|
int running = 1;
|
||||||
|
SDL_Event e;
|
||||||
|
while (running) {
|
||||||
|
while (SDL_PollEvent(&e))
|
||||||
|
if (e.type == SDL_QUIT)
|
||||||
|
running = 0;
|
||||||
|
|
||||||
|
uint32_t flat[SIM_HEIGHT * SIM_WIDTH];
|
||||||
|
for (size_t y = 0; y < SIM_HEIGHT; y++)
|
||||||
|
for (size_t x = 0; x < SIM_WIDTH; x++)
|
||||||
|
flat[y * SIM_WIDTH + x] = b.pixels[y][x];
|
||||||
|
|
||||||
|
SDL_Rect dst = {0, 0, WIDTH, HEIGHT};
|
||||||
|
SDL_UpdateTexture(tex, NULL, flat, SIM_WIDTH * sizeof(uint32_t));
|
||||||
|
SDL_RenderClear(ren);
|
||||||
|
SDL_RenderCopy(ren, tex, NULL, &dst);
|
||||||
|
SDL_RenderPresent(ren);
|
||||||
|
b = process_game(b);
|
||||||
|
SDL_Delay(50);
|
||||||
|
}
|
||||||
|
SDL_DestroyTexture(tex);
|
||||||
|
SDL_DestroyRenderer(ren);
|
||||||
|
SDL_DestroyWindow(win);
|
||||||
|
SDL_Quit();
|
||||||
|
free_board(b);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
Board b = init_board(6, 6, 0);
|
||||||
|
b.pixels[0][1] = WHITE;
|
||||||
|
b.pixels[1][2] = WHITE;
|
||||||
|
b.pixels[2][0] = WHITE;
|
||||||
|
b.pixels[2][1] = WHITE;
|
||||||
|
b.pixels[2][2] = WHITE;
|
||||||
|
display_board(b);
|
||||||
|
puts("----------");
|
||||||
|
b = process_game(b);
|
||||||
|
display_board(b);
|
||||||
|
puts("----------");
|
||||||
|
b = process_game(b);
|
||||||
|
display_board(b);
|
||||||
|
puts("----------");
|
||||||
|
b = process_game(b);
|
||||||
|
display_board(b);
|
||||||
|
free_board(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user