Skip to content

Instantly share code, notes, and snippets.

@akhlopiachyi
Created August 10, 2017 12:00
Show Gist options
  • Save akhlopiachyi/6089b3036ee6b3187f72bbe3682974a1 to your computer and use it in GitHub Desktop.
Save akhlopiachyi/6089b3036ee6b3187f72bbe3682974a1 to your computer and use it in GitHub Desktop.
diff --git a/ex10/src/filler.c b/ex10/src/filler.c
index ca436e0..aa6e39e 100644
--- a/ex10/src/filler.c
+++ b/ex10/src/filler.c
@@ -8,8 +8,8 @@ int main()
filler_t new_filler;
create_filler(&new_filler);
- start_game(&filler);
- destroy_filler(&new_filler);
+ start_game(&new_filler);
+// destroy_filler(&new_filler);
return 0;
}
diff --git a/ex10/src/game.c b/ex10/src/game.c
index 67c9d1b..f64ae89 100644
--- a/ex10/src/game.c
+++ b/ex10/src/game.c
@@ -4,7 +4,53 @@
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);
+ }
+
+ }
}
diff --git a/ex10/src/game_logic.c b/ex10/src/game_logic.c
index e09f2f1..d5d4550 100644
--- a/ex10/src/game_logic.c
+++ b/ex10/src/game_logic.c
@@ -4,5 +4,20 @@
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;
}
diff --git a/ex10/src/input.c b/ex10/src/input.c
index 17af4a2..2775158 100644
--- a/ex10/src/input.c
+++ b/ex10/src/input.c
@@ -1,6 +1,6 @@
#include <stdlib.h>
#include <stdio.h>
-#include "../include/my_string.h"
+#include "../include/my_string.h" //поразка "my_string.h"
#include "../include/filler.h"
#include <errno.h>
#include <string.h>
@@ -9,8 +9,63 @@
int if_read_is_finished(stream_t *buff)
{
-
- return 0;
+ 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)
@@ -18,8 +73,8 @@ req_t *read_request(filler_t *filler)
req_t *request;
read_input(filler);
- if(if_read_is_finished(filler->curent_stream) == 0)
- request = parse_all(filler->curent_stream->str);
+ if(if_read_is_finished(filler->current_stream) == 0)
+ request = parse_all(filler->current_stream->str);
return request;
}
@@ -28,25 +83,41 @@ 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 || errno == EWOULDBLOCK)
- break;
+ if(errno == EAGAIN)
+ {
+ logger = fopen("filler.log", "a");
+ fprintf(logger, "EAGAIN#\n");
+ fclose(logger);
+ }
else
{
- dprintf(2, "Error reading");
+ fatal("Error reading");
break;
}
}
if(filler->current_stream == NULL)
- filler->current_stream = string_create(buffer);
+ 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);
}
}
diff --git a/ex10/src/parse.c b/ex10/src/parse.c
index 81c4dd1..1f11c88 100644
--- a/ex10/src/parse.c
+++ b/ex10/src/parse.c
@@ -3,98 +3,97 @@
#include <string.h>
#include "filler.h"
-req_t *parse_all(char *all)
+req_t *parse_all(char *all)
{
- int i = 0;
- map_t map;
- map.h = 0;
- map.w = 0;
- elem_t elem;
- req_t *request = (req_t*) malloc(sizeof(req_t));
- pos_s pos1, pos2;
- int flag = 0;
- int slash_n = 0;
-
- while(*(all++) != '\0')
+ 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
{
- if(*all == '\n')
- {
- slash_n++;
- continue;
- }
+ *(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));
+ }
- if(i == 0)
+ for(int i = 0; i < map.h; i++)
+ {
+ for(int j = 0; j < map.w + 1; j++)
{
- request->symbol = *(all);
+ 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');
- if(slash_n == 1)
- {
- pos1 = parse_size(all);
- map.h = pos1.x;
- map.w = pos1.y;
- slash_n++;
- }
+ position = parse_size(pos1);
+ elem.h = position.x;
+ elem.w = position.y;
- if(slash_n == 2)
- {
- for(int i = 0; i < map.h; i++)
- {
- for(int j = 0; j < map.w; j++)
- {
- map.array[i][j] = *(all + i*map.h + j);
-
- if(*(all + i*map.h + j+1) == '\n')
- {
- j++;
- }
- }
- }
-
- }
+ request->elem.array = (char **) malloc(request->elem.h * sizeof(char));
- all = all+map.h*(map.w+1);
- slash_n+=map.h;
- if(slash_n == map.h + 2)
- {
- pos2 = parsw_size(all);
- elem.h = pos2.x;
- elem.w = pos2.y;
- slash_n++;
- }
+ for(int i = 0; i < elem.h; i++)
+ {
+ request->elem.array[i] = (char *)malloc((request->elem.w + 1) * sizeof(char));
+ }
- if(slash_n == map.h + 3)
+ for(int i = 0; i < elem.h; i++)
+ {
+ for(int j = 0; j < elem.w +1; j++)
{
- for(int i = 0; i<elem.h; i++)
- {
- for(int j = 0; j<elem.w; j++)
- {
- elem.array[i][j] = *(all + i* elem.h + j);
- if(*(all + i*elem.h + j + 1) == '\n')
- {
- j++;
- }
- }
- }
+ request->elem.array[i][j] = *(all + m);
+ m++;
}
-
- slash_n += elem.h + 1;
- all = all + elem.h*(elem.w + 1);
}
-
+
request->map = map;
request->elem = elem;
+
+ fprintf(logger, "end of parsing");
+ fclose(logger);
return request;
}
-pos_t parse_size(char *answer)
+pos_t parse_size(char *answer)
{
- pos_t cur;
- int flag = 0;
- char *c1 = NULL;
- char *c2 = NULL;
- int t1 = 0;
- int t2 = 0;
+ 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));
diff --git a/ex10/src/print.c b/ex10/src/print.c
index c4c68a8..0cd4f77 100644
--- a/ex10/src/print.c
+++ b/ex10/src/print.c
@@ -4,5 +4,9 @@
void print_pos(pos_t p)
{
- dprintf(1, "%d %d", p->x, p->y);
+ dprintf(1, "%d %d", p.x, p.y);
+
+ FILE *logger = fopen("filler.log" , "a");
+ fprintf(logger, "Print pos\n");
+ fclose(logger);
}
diff --git a/ex10/src/tools.c b/ex10/src/tools.c
index b62bdaf..cfee1db 100644
--- a/ex10/src/tools.c
+++ b/ex10/src/tools.c
@@ -1,34 +1,78 @@
#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