Skip to content

Instantly share code, notes, and snippets.

@akhlopiachyi
Created August 10, 2017 12:12
Show Gist options
  • Save akhlopiachyi/1d0f38b07a07c911192ffd9254c2b421 to your computer and use it in GitHub Desktop.
Save akhlopiachyi/1d0f38b07a07c911192ffd9254c2b421 to your computer and use it in GitHub Desktop.
filler.c
#include "../include/filler.h"
#include <stdio.h>
#include <stdlib.h>
int main()
{
filler_t new_filler;
create_filler(&new_filler);
start_game(&new_filler);
// destroy_filler(&new_filler);
return 0;
}
game.c
#include <stdio.h>
#include <stdlib.h>
#include "../include/filler.h"
void start_game(filler_t *filler)
{
req_t *req;
fd_set rfds;
fd_set wfds;
pos_t p;
int flag = 1;
FILE *logger = fopen("filler.log", "w");
fprintf(logger, "start game\n");
fclose(logger);
create_req(req);
set_nonblocking(0);
while(42)
{
FD_ZERO(&rfds);
FD_ZERO(&wfds);
switch(filler->status)
{
case 1:
FD_SET(1, &wfds);
break;
case 0:
FD_SET(0, &rfds);
break;
}
int ret = select(2, &rfds, &wfds, NULL, NULL);
if(FD_ISSET(0, &rfds))
{
req = read_request(filler);
if(req != NULL)
{
p = play(req, filler);
flag++;
filler->status = 1;
}
}
if(FD_ISSET(1, &wfds))
{
print_pos(p);
filler->status = 0;
string_destroy(filler->current_stream);
filler->current_stream = NULL;
destroy_req(req);
}
}
}
game_logic.c
#include <stdio.h>
#include <stdlib.h>
#include "../include/filler.h"
pos_t play(req_t *core, filler_t *filler)
{
pos_t res;
int h = core->map.h;
int w = core->map.w;
for(int i = core->map.h; i>=0; i--)
{
for(int j = 0; j < w; j++)
{
res.x = j;
res.y = i;
if(!check_free_space(&core->map, &core->elem, res) && !check_connection(&core->map, &core->elem, res, core->symbol))
return res;
}
}
return res;
}
input.c
#include <stdlib.h>
#include <stdio.h>
#include "../include/my_string.h" //поразка "my_string.h"
#include "../include/filler.h"
#include <errno.h>
#include <string.h>
#define BUFF 64
int if_read_is_finished(stream_t *buff)
{
int pos = 0;
int first_size = 0;
int elem_pos = 0;
char *arr_elem_size = (char *) malloc(BUFF * 2 * sizeof(char));
pos_t el_size;
int i;
int new_pos;
while(pos <= buff->size)
{
if(buff->str[pos] > '9' || buff->str[pos] < '0')
pos++;
else
if (first_size == 0)
{
while(pos <= buff->size && buff->str[pos] != '\n')
pos++;
pos++;
first_size = 1;
}
else
{
elem_pos = pos;
while(pos <= buff->size && buff->str[pos] != '\n')
pos++;
if(buff->str[pos] != '\n')
return -1;
else
{
i = 0;
new_pos = elem_pos;
do {
*(arr_elem_size + i) = *(buff->str + new_pos);
new_pos++;
i++;
} while(*(buff->str + new_pos - 1) != '\n');
*(arr_elem_size+i) = '\0';
el_size = parse_size(arr_elem_size);
FILE *logger = fopen("filler.log", "a");
fprintf(logger, "finished: pos = %d, elem = %d %d, buf->size = %d\n", pos, el_size.x, el_size.y, buff->size);
fclose(logger);
if(buff->size < pos + el_size.x*(el_size.y + 1))
return -1;
else
return 0;
}
}
}
return -1;
}
req_t *read_request(filler_t *filler)
{
req_t *request;
read_input(filler);
if(if_read_is_finished(filler->current_stream) == 0)
request = parse_all(filler->current_stream->str);
return request;
}
void read_input(filler_t *filler)
{
char string[BUFF];
int r;
FILE *logger = fopen("filler.log", "a");
fprintf(logger, "input reading\n");
fclose(logger);
while(42)
{
memset(string, 0, BUFF);
r = read(0, string, BUFF - 1);
logger = fopen("filler.log", "a");
fprintf(logger, "buffer: %s\n", string);
fclose(logger);
if(r < 0)
{
if(errno == EAGAIN)
{
logger = fopen("filler.log", "a");
fprintf(logger, "EAGAIN#\n");
fclose(logger);
}
else
{
fatal("Error reading");
break;
}
}
if(filler->current_stream == NULL)
filler->current_stream = string_create(string);
else
string_append(filler->current_stream, string);
logger = fopen("filler.log", "a");
fprintf(logger, "buff : %s\n", filler->current_stream->str);
fclose(logger);
}
}
my_string.c
#include "../include/my_string.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUF 64
stream_t *string_init()
{
stream_t *new = (stream_t *) malloc(sizeof(stream_t));
new->size = 0;
new->limit = 0;
new->str = '\0';
return new;
}
stream_t *string_create(char *str)
{
stream_t *string = string_init();
int size = strlen(string->str);
int lim = size / BUF;
lim++;
string->str = (char*) malloc(lim * BUF * sizeof(char));
strcpy(string->str, str);
string->limit = lim * BUF;
return string;
}
void string_append(stream_t *ptr, char *str)
{
int size;
if(ptr == NULL)
{
ptr = string_create(str);
}
else
{
size = strlen(str);
if(ptr->size + size >= ptr->limit)
{
ptr->str = (char *)realloc(ptr->str, ptr->limit + BUF);
ptr->limit = ptr->limit + BUF;
*(ptr->str + ptr->size + size) = 0;
}
for(int i = 0; i<size; i++)
{
*(ptr->str + ptr->size + i) = *(str + i);
}
ptr->size = ptr->size + size;
*(ptr->str + ptr->size + size) = 0;
}
}
void string_destroy(stream_t *str)
{
free(str->str);
free(str);
}
parse.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "filler.h"
req_t *parse_all(char *all)
{
int i = 0;
map_t map;
elem_t elem;
req_t *request = (req_t*) malloc(sizeof(req_t));
pos_t position;
int flag = 0;
int n = 0;
int m = 0;
char *pos1 = (char *)malloc(64*sizeof(char));
memset(pos1, '\0', 64);
FILE *logger=fopen("filler.log", "a");
fprintf(logger, "parse");
request->symbol = *all;
m += 2;
do
{
*(pos1 + n) = *(all + m);
m++;
n++;
}while(*(all + m - 1) != '\0');
position = parse_size(pos1);
map.h = position.x;
map.w = position.y;
request->map.array = (char **) malloc(request->map.h * sizeof(char*));
for(int i = 0; i < map.h; i++)
{
request->map.array[i] = (char *) malloc((request->map.w+1) * sizeof(char));
}
for(int i = 0; i < map.h; i++)
{
for(int j = 0; j < map.w + 1; j++)
{
request->map.array[i][j] = *(all + m);
m++;
}
}
memset(pos1, '\0', 64);
n = 0;
do
{
*(pos1 + n) = *(all + m);
m++;
n++;
}while(*(all + m - 1) != '\0');
position = parse_size(pos1);
elem.h = position.x;
elem.w = position.y;
request->elem.array = (char **) malloc(request->elem.h * sizeof(char));
for(int i = 0; i < elem.h; i++)
{
request->elem.array[i] = (char *)malloc((request->elem.w + 1) * sizeof(char));
}
for(int i = 0; i < elem.h; i++)
{
for(int j = 0; j < elem.w +1; j++)
{
request->elem.array[i][j] = *(all + m);
m++;
}
}
request->map = map;
request->elem = elem;
fprintf(logger, "end of parsing");
fclose(logger);
return request;
}
pos_t parse_size(char *answer)
{
pos_t cur;
int flag = 0;
char *c1 = NULL;
char *c2 = NULL;
int t1 = 0;
int t2 = 0;
c1 = (char*) malloc(strlen(answer) * sizeof(char));
c2 = (char*) malloc(strlen(answer) * sizeof(char));
memset(c1, '\0', strlen(answer));
memset(c2, '\0', strlen(answer));
for(int i = 0; *(answer++) != '\n'; i++)
{
if(*(answer) >= '0' && *(answer) <= '9')
{
if(flag == 0)
{
*(c1 + t1) = *(answer);
t1++;
}
if(flag != 0)
{
*(c2 + t2) = *(answer);
t2++;
}
}
if(*(answer) == ' ' && *c1 != '\0')
{
flag = 1;
}
}
cur.x = atoi(c1);
cur.y = atoi(c2);
free(c1);
free(c2);
return cur;
}
print.c
#include <stdio.h>
#include <stdlib.h>
#include "../include/filler.h"
void print_pos(pos_t p)
{
dprintf(1, "%d %d", p.x, p.y);
FILE *logger = fopen("filler.log" , "a");
fprintf(logger, "Print pos\n");
fclose(logger);
}
tools.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "../include/filler.h"
int set_nonblocking(int fd)
{
int flags = fcntl(fd, F_GETFL, 0);
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}
void fatal(char *msg)
{
dprintf(2, msg);
exit(1);
}
void create_filler(filler_t *filler)
{
filler->current_stream = NULL;
filler->status = 0;
}
void destroy_filler(filler_t *filler)
{
}
void create_req(req_t *req)
{
req = (req_t*) malloc(sizeof(req_t));
}
void destroy_req(req_t *req)
{
free(req);
req = NULL;
}
int check_free_space(map_t *map, elem_t *new_elem, pos_t p)
{
for(int i = 0; i < new_elem->h; i++)
for(int j = 0; j < new_elem->w; j++)
if(new_elem->array[i][j] == '*')
{
if(i + p.y < map->h && j + p.x < map->w && i + p.y >= 0 && j + p.x >= 0)
{
if(map->array[i + p.y][j + p.x] != '.')
return -1;
}
else
return -1;
}
return 0;
}
int check_connection(map_t *map, elem_t *new_elem, pos_t p, char symbol)
{
int i_max = map->w - p.y;
int j_max = map->h - p.x;
for(int i = 0; i < new_elem->h; i++)
for(int j = 0; j < new_elem->w; j++)
if(new_elem->array[i][j] != '.')
{
if(i + p.y + 1 < map->h && map->array[i + p.y + 1][j + p.x] == symbol)
return 0;
if (i + p.y - 1 >= 0 && map->array[i + p.y - 1][j + p.x] == symbol)
return 0;
if (j + p.x + 1 < map->w && map->array[i + p.y][j + p.x + 1] == symbol)
return 0;
if (j + p.x - 1 >= 0 && map->array[i + p.y][j + p.x - 1] == symbol)
return 0;
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment