Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <string.h>
- #include <time.h>
- typedef struct direction
- {
- int x;
- int y;
- }direction_t;
- typedef struct snake
- {
- direction_t *head;
- direction_t *tail;
- }snake_t;
- void array(char **ground, int size, int score);
- void gen_food(char **table, int size, direction_t food, direction_t head);
- int main(int argc, char *argv[])
- {
- int score=0;
- int i,j;
- int size = atoi(argv[1]); //converting argument to integer
- if(size == 0)
- {
- printf("invalid input\n");
- exit(EXIT_FAILURE);
- }
- size += 2; //adding 2 extra lines/colums to array (?) ama balw a sto terminal
- char **ground;
- ground = (char**)malloc(size * sizeof(char*));
- direction_t food, head;
- gen_food(ground, size, food, head);
- array(ground,size,score);
- return 0;
- }
- void array(char **ground, int size, int score,direction_t food.x, direction_t food.x)
- {
- int i,j;
- for( i=0; i<size; i++)
- {
- *(ground + i) = (char*)malloc(size * sizeof(char));
- }
- for(i=0; i<size; ++i)
- {
- for ( j=0; j < size; ++j)
- {
- if(i == 0 || i == size-1)
- {
- ground[i][j] = '-';
- }
- else if(j==0 || j == size-1)
- {
- ground[i][j] = '|';
- }
- else if (x.food == j && y.food == i ) {
- ground[i][j] = "X";
- }
- else
- {
- ground[i][j] = ' ';
- }
- }
- }
- for (i=0;i<size; ++i)
- {
- for(j=0;j<size; ++j)
- {
- printf("%c",ground[i][j]);
- }
- printf("\n");
- }
- }
- void gen_food(char **table, int size, direction_t food, direction_t head)
- {
- srand ( time(NULL) );
- rand();
- food.x = rand()%(size+2);
- food.y = rand()%(size+2);
- head.x = rand()%(size+2);
- head.y = rand()%(size+2);
- if(food.x == head.x && food.y == head.y)
- {
- printf("please try again");
- system("exit");
- }
- else
- {
- if(food.x > 0 && food.y > 0)
- //{
- if(food.x < size && food.y < size)
- {
- table[food.x][food.y] = 'X';
- // printf("%c", table[food.x][food.y]);
- // printf("%d, %d\n", food.x, food.y);
- }
- else
- {
- system("exit");
- }
- // else return 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement