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); //1o sxolio
- void game_over(); //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
- {
- while( !(gameover()) )
- { 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;
- food_t f;
- array(ground, arraysize ,&s ,&f);
- int x,y;
- init_food(x,y,&f);
- generate_food(ground, arraysize, &f);
- //init_snake(s); //1
- //control_snake();//3
- }
- 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, snake_t s, int headx, int heady, int tailx, int taily, char direction, char pre_direction, int snake_size, char keyinput) //2
- {
- char keyinput = move();
- move_head(p,keyinput);
- if(s->head->x == f->f.x && s->head.y == f.y)
- {
- s->snake_size++;
- generate_food(**ground, arraysize, f);
- }
- else
- {
- move_tail(ground,s,p);
- }
- void move_head(position_t p , char keyinput)
- {
- if(direction == LEFT || direction == RIGHT || direction == UP || direction == DOWN)
- {
- if(keyinput == LEFT) //elegxos ma pathsw allo koumpi
- {
- headx--;
- }
- else if(keyinput == RIGHT)
- {
- headx++;
- }
- else if(keyinput == UP)
- {
- heady++;
- }
- else if(keyinput == DOWN)
- {
- heady--;
- }
- }
- else
- {
- ground[[p->x][s->size][p->y][s->size]] == '@';
- }
- }
- void move_tail(char **ground,snake_t s,position_t p)
- {
- int ;
- ground[[p->x][s->size][p->y][s->size]] == ' ';
- for(i=0, i<s->size, i++)
- {
- p->x[i] = p->[x+1];
- p->y[i] = p->[y+1];
- }
- }
- }
- */
- void gameover()
- {
- int i,j;
- int arraysize;
- snake_t s;
- 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