Add: conway game (basic) but flickers?
This commit is contained in:
Binary file not shown.
@@ -35,7 +35,7 @@ void kmain(unsigned int multiboot_info_address)
|
|||||||
clear();
|
clear();
|
||||||
colorputs(ascii_title, 10);
|
colorputs(ascii_title, 10);
|
||||||
colorputs(" by @xamidev - star the repo for a cookie!\n\n", 14);
|
colorputs(" by @xamidev - star the repo for a cookie!\n\n", 14);
|
||||||
|
|
||||||
timer_install();
|
timer_install();
|
||||||
serial_printf(2, "%d\tinitialized timer handler", global_ticks);
|
serial_printf(2, "%d\tinitialized timer handler", global_ticks);
|
||||||
keyboard_install();
|
keyboard_install();
|
||||||
|
|||||||
@@ -65,7 +65,8 @@ void shell_install()
|
|||||||
register_command("uptime", program_uptime);
|
register_command("uptime", program_uptime);
|
||||||
register_command("echo", program_echo);
|
register_command("echo", program_echo);
|
||||||
register_command("sysinfo", program_sysinfo);
|
register_command("sysinfo", program_sysinfo);
|
||||||
|
register_command("conway", program_conway);
|
||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
char input_buffer[BUFFER_SIZE];
|
char input_buffer[BUFFER_SIZE];
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "crypto.h"
|
#include "crypto.h"
|
||||||
|
#include "../libc/stdint.h"
|
||||||
|
|
||||||
int lcg(int seed)
|
int lcg(int seed)
|
||||||
{
|
{
|
||||||
@@ -22,3 +23,21 @@ int randint(int seed)
|
|||||||
int x = lcg(seed);
|
int x = lcg(seed);
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static uint32_t next = 1;
|
||||||
|
|
||||||
|
uint32_t rand()
|
||||||
|
{
|
||||||
|
next = next * 1103515245 + 12345;
|
||||||
|
return (next/65536) % 32768;
|
||||||
|
}
|
||||||
|
|
||||||
|
float rand_float()
|
||||||
|
{
|
||||||
|
return rand() / 32767.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
void srand(uint32_t seed)
|
||||||
|
{
|
||||||
|
next = seed;
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,7 +3,12 @@
|
|||||||
|
|
||||||
#define RAND_MAX 1024
|
#define RAND_MAX 1024
|
||||||
|
|
||||||
|
#include "../libc/stdint.h"
|
||||||
|
|
||||||
int lcg(int seed);
|
int lcg(int seed);
|
||||||
int randint(int seed);
|
int randint(int seed);
|
||||||
|
uint32_t rand();
|
||||||
|
float rand_float();
|
||||||
|
void srand(uint32_t seed);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
132
src/programs/conway.c
Normal file
132
src/programs/conway.c
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
#include "conway.h"
|
||||||
|
#include "../libc/stdio.h"
|
||||||
|
#include "../kernel/system.h"
|
||||||
|
#include "../libc/crypto.h"
|
||||||
|
#include "../libc/stdint.h"
|
||||||
|
#include "../drivers/serial.h"
|
||||||
|
#include "../libc/string.h"
|
||||||
|
|
||||||
|
void print_grid(const unsigned char grid[X][Y])
|
||||||
|
{
|
||||||
|
for (int i=0; i<X; i++)
|
||||||
|
{
|
||||||
|
for (int j=0; j<Y; j++)
|
||||||
|
{
|
||||||
|
//(grid[i][j] == LIVE) ? putc(42) : putc(32);
|
||||||
|
if (grid[i][j] == LIVE) {
|
||||||
|
serial_printf(3, "alive");
|
||||||
|
colorputc(32, 120);
|
||||||
|
} else {
|
||||||
|
putc(32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int count_live_neighbors(const unsigned char grid[X][Y], int i, int j)
|
||||||
|
{
|
||||||
|
int live_neighbors = 0;
|
||||||
|
|
||||||
|
for (int x=-1; x<=1; x++)
|
||||||
|
{
|
||||||
|
for (int y=-1; y<=1; y++)
|
||||||
|
{
|
||||||
|
if (x==0 && y==0) continue;
|
||||||
|
|
||||||
|
int ni = i+x;
|
||||||
|
int nj = j+y;
|
||||||
|
|
||||||
|
if (ni >= 0 && ni < X && nj >= 0)
|
||||||
|
{
|
||||||
|
if (grid[ni][nj] == LIVE) live_neighbors++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return live_neighbors;
|
||||||
|
}
|
||||||
|
|
||||||
|
void grid_new_generation(unsigned char grid[X][Y], unsigned char temp[X][Y])
|
||||||
|
{
|
||||||
|
for (int i=0; i<X; i++)
|
||||||
|
{
|
||||||
|
for (int j=0; j<Y; j++)
|
||||||
|
{
|
||||||
|
int cell = grid[i][j];
|
||||||
|
int live_neighbors = count_live_neighbors(grid, i, j);
|
||||||
|
|
||||||
|
if (cell == LIVE)
|
||||||
|
{
|
||||||
|
switch(live_neighbors)
|
||||||
|
{
|
||||||
|
case 2:
|
||||||
|
case 3:
|
||||||
|
temp[i][j] = LIVE;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
temp[i][j] = DEAD;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (cell == DEAD && live_neighbors == 3)
|
||||||
|
{
|
||||||
|
temp[i][j] = LIVE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i=0; i<X; i++)
|
||||||
|
{
|
||||||
|
for (int j=0; j<Y; j++)
|
||||||
|
{
|
||||||
|
grid[i][j] = temp[i][j];
|
||||||
|
temp[i][j] = DEAD;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void soup(unsigned char grid[X][Y])
|
||||||
|
{
|
||||||
|
srand(global_ticks);
|
||||||
|
for (int i=0; i<X; i++)
|
||||||
|
{
|
||||||
|
for (int j=0; j<Y; j++)
|
||||||
|
{
|
||||||
|
grid[i][j] = rand_float() > SOUP_PROB ? LIVE : DEAD;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void program_conway(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
unsigned char grid[X][Y] = {0};
|
||||||
|
unsigned char temp[X][Y] = {0};
|
||||||
|
|
||||||
|
if (argc == 1)
|
||||||
|
{
|
||||||
|
soup(grid);
|
||||||
|
} else if (argc == 2 && strcmp(argv[1], "-g") == 0) {
|
||||||
|
grid[1][2] = LIVE;
|
||||||
|
grid[2][3] = LIVE;
|
||||||
|
grid[3][1] = LIVE;
|
||||||
|
grid[3][2] = LIVE;
|
||||||
|
grid[3][3] = LIVE;
|
||||||
|
} else if (argc == 2 && strcmp(argv[1], "-l") == 0) {
|
||||||
|
grid[10][3] = LIVE; grid[10][4] = LIVE; grid[10][5] = LIVE; grid[10][6] = LIVE;
|
||||||
|
grid[11][2] = LIVE; grid[11][6] = LIVE;
|
||||||
|
grid[12][6] = LIVE;
|
||||||
|
grid[13][2] = LIVE; grid[13][5] = LIVE;
|
||||||
|
}
|
||||||
|
|
||||||
|
print_grid(grid);
|
||||||
|
puts("generation 0");
|
||||||
|
for (int i=1; i<GENERATIONS; i++)
|
||||||
|
{
|
||||||
|
grid_new_generation(grid, temp);
|
||||||
|
delay(DELAY);
|
||||||
|
clear();
|
||||||
|
print_grid(grid);
|
||||||
|
printf("generation %d", i);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
18
src/programs/conway.h
Normal file
18
src/programs/conway.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef CONWAY_H
|
||||||
|
#define CONWAY_H
|
||||||
|
|
||||||
|
#define X 25
|
||||||
|
#define Y 80
|
||||||
|
|
||||||
|
#define GENERATIONS 100
|
||||||
|
#define DEAD 0
|
||||||
|
#define LIVE 1
|
||||||
|
#define SOUP_PROB 0.7
|
||||||
|
#define DELAY 10
|
||||||
|
|
||||||
|
void print_grid(const unsigned char grid[X][Y]);
|
||||||
|
int count_live_neighbors(const unsigned char grid[X][Y], int i, int j);
|
||||||
|
void grid_new_generation(unsigned char grid[X][Y], unsigned char temp[X][Y]);
|
||||||
|
void soup(unsigned char grid[X][Y]);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -45,7 +45,7 @@ void program_uptime()
|
|||||||
|
|
||||||
void program_help()
|
void program_help()
|
||||||
{
|
{
|
||||||
printf("help\tpanic\twords\tprimes\trainbow\tclear\nmath\tbf\t uptime echo\t sysinfo\n");
|
printf("help\tpanic\twords\tprimes\trainbow\tclear\nmath\tbf\t uptime echo\t sysinfo\tconway\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Panic
|
// Panic
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ void program_sysinfo();
|
|||||||
|
|
||||||
void get_cpuid();
|
void get_cpuid();
|
||||||
void get_meminfo(unsigned int multiboot_info_address);
|
void get_meminfo(unsigned int multiboot_info_address);
|
||||||
|
void program_conway();
|
||||||
|
|
||||||
// Misc
|
// Misc
|
||||||
void program_rainbow();
|
void program_rainbow();
|
||||||
|
|||||||
Reference in New Issue
Block a user