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 <time.h>
- #define MAXX 100
- #define MAXY 100
- #define UP 'w'
- #define DOWN 's'
- #define RIGHT 'd'
- #define LEFT 'a'
- typedef struct snake
- {
- int headx;
- int heady;
- int tailx;
- int taily;
- char direction;
- char pre_direction;
- int snake_size;
- }snake_t;
- typedef struct food
- {
- int x;
- int y;
- }food_t;
- typedef struct position
- {
- int positionx[MAXX][MAXY];
- int positiony[MAXX][MAXY];
- }position_t;
- void array(char **ground, int arraysize ,snake_t *s, food_t *f);
- void init_food(int x, int y, food_t *f);
- void generate_food(char **ground, int arraysize, food_t *f);
- void init_snake(snake_t *s);
- void move_head(snake_t *s,position_t *p , char keyinput);
- //void move_tail(char **ground,snake_t *s,position_t *p);
- void control_snake(char **ground,int arraysize,snake_t *s, position_t *p,food_t *f);
- void gameover(char **ground,snake_t *s,int arraysize); //4
- int main(int argc, char *argv[])
- {
- int arraysize = atoi(argv[1]);
- //converting argument to integer
- if (argv[1] == NULL||argc!=2)
- {
- printf("invalid input\n");
- system("exit");
- }
- if (arraysize < 15 || arraysize > 100)
- {
- printf("try another dimension");
- system("exit");
- }
- else
- {
- int i, j;
- arraysize += 2; //adding 2 extra lines/colums to array (?) ama balw a sto terminal
- char **ground = (char **)malloc(arraysize * sizeof(char *));
- for (i = 0; i < arraysize; i++)
- {
- *(ground + i)= (char *)malloc(arraysize * sizeof(char));
- }
- snake_t s;
- init_snake(&s); //1
- food_t f;
- int x,y;
- init_food(x,y,&f);
- array(ground, arraysize ,&s ,&f);
- generate_food(ground, arraysize, &f);
- while(1)
- {
- position_t p;
- char keyinput;
- move_head(&s,&p,keyinput);
- control_snake(ground,arraysize,&s,&p,&f);//3
- //while(1) dilwste to int alive = 1 kai otan xanei apla allazete to se 0 gia na spaei to loop
- }
- /* while(! keyinput)
- {
- if(gameover()) break;
- sleep(1);
- }
- if(gameover())
- {
- printf("gameover!");
- printf("your score is", s->snake_size);
- }
- */
- }
- }
- void array(char **ground, int arraysize ,snake_t *s, food_t *f)
- {
- int x,y;
- init_food(x,y,f);
- generate_food(ground, arraysize, f);
- int i, j;
- for (i = 0; i < arraysize; ++i)
- {
- for (j = 0; j < arraysize; ++j)
- {
- if (i == 0 || i == arraysize - 1)
- {
- ground[i][j] = '-';
- }
- else if (j == 0 || j == arraysize - 1)
- {
- ground[i][j] = '|';
- }
- else if (f->x == j && f->y == i)
- {
- ground[i][j] = 'X';
- }
- else
- {
- ground[i][j] = ' ';
- }
- }
- }
- for (i = 0; i < arraysize; ++i)
- {
- for (j = 0; j < arraysize; ++j)
- {
- printf("%c", ground[i][j]);
- }
- printf("\n");
- }
- }
- void init_food(int x, int y, food_t *f)
- {
- f->x = x;
- f->y = y;
- }
- void generate_food(char **ground,int arraysize, food_t *f)
- {
- int x,y;
- x = rand()%(arraysize-1);
- y = rand()%(arraysize-1);
- for(x=0; x<arraysize-1; x++)
- {
- for(y=0; y<arraysize-1; y++)
- {
- while(ground[x][y] != ' ')
- {
- x = rand()%(arraysize-1);
- y = rand()%(arraysize-1);
- sleep(1);
- }
- f->x = x;
- f->y = y;
- ground[f->x][f->y] = 'X';
- }
- }
- }
- void init_snake(snake_t *s) //1
- {
- s->tailx =5;
- s->taily = 5;
- s->headx = s->tailx + s->snake_size;
- s->heady = 5;
- s->direction = 's';
- s->pre_direction = 'a';
- }
- char move()
- {
- int flags;
- char key[2];
- flags = fcntl(STDIN_FILENO, F_GETFL);
- fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
- // To get the input from the terminal without pressing the enter button
- system("/bin/stty raw");
- read(STDIN_FILENO, key, 2);
- // Reset the "system ("/bin/stty raw")" to display correctly the output
- system("/bin/stty cooked");
- return key[0];
- }
- void control_snake(char **ground, int arraysize,snake_t *s, position_t *p,food_t *f) //2
- {
- char keyinput = move();
- move_head(s,p,keyinput);
- if(s->headx == f -> x && s -> heady == f->y)
- {
- s -> snake_size++;
- generate_food(ground, arraysize, f);
- }
- else
- {
- //move_tail(ground,s,p);
- }
- }
- void move_head(snake_t *s,position_t *p , char keyinput)
- {
- char inserted_key = move();
- if(s->direction == LEFT || s->direction == RIGHT || s->direction == UP || s->direction == DOWN)
- {
- if(inserted_key == LEFT) //elegxos ma pathsw allo koumpi
- {
- s->headx--;
- }
- else if(inserted_key == RIGHT)
- {
- s->headx++;
- }
- else if(inserted_key == UP)
- {
- s->heady++;
- }
- else if(inserted_key == DOWN)
- {
- s->heady--;
- }
- }
- else
- {
- //ground[[p->x][s->size][p->y][s->size]] == '@';
- }
- }
- void gameover(char **ground,snake_t *s,int arraysize)
- {
- int i,j;
- for(i = 0; i <= arraysize; i++ )
- {
- for(j=0; j<= arraysize; j++)
- {
- if(s -> headx == '-' || s->headx == '|' || s->headx == '*' ||s->heady == '-' || s->heady == '|' || s->heady == '*')
- {
- printf("game over");
- system("exit");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement