Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <time.h>
  6.  
  7. #define MAXX 100
  8. #define MAXY 100
  9.  
  10. #define UP 'w'
  11. #define DOWN 's'
  12. #define RIGHT 'd'
  13. #define LEFT 'a'
  14.  
  15.  
  16. typedef struct snake
  17. {
  18. int headx;
  19. int heady;
  20. int tailx;
  21. int taily;
  22. char direction;
  23. char pre_direction;
  24. int snake_size;
  25.  
  26. }snake_t;
  27.  
  28. typedef struct food
  29. {
  30. int x;
  31. int y;
  32.  
  33. }food_t;
  34.  
  35. typedef struct position
  36. {
  37. int positionx[MAXX][MAXY];
  38. int positiony[MAXX][MAXY];
  39. }position_t;
  40.  
  41. void array(char **ground, int arraysize ,snake_t *s, food_t *f);
  42. void init_food(int x, int y, food_t *f);
  43. void generate_food(char **ground, int arraysize, food_t *f);
  44. void init_snake(snake_t *s);
  45. void move_head(snake_t *s,position_t *p , char keyinput);
  46. //void move_tail(char **ground,snake_t *s,position_t *p);
  47. void control_snake(char **ground,int arraysize,snake_t *s, position_t *p,food_t *f);
  48. void gameover(char **ground,snake_t *s,int arraysize); //4
  49.  
  50. int main(int argc, char *argv[])
  51. {
  52. int arraysize = atoi(argv[1]);
  53. //converting argument to integer
  54. if (argv[1] == NULL||argc!=2)
  55. {
  56. printf("invalid input\n");
  57. system("exit");
  58. }
  59.  
  60. if (arraysize < 15 || arraysize > 100)
  61. {
  62. printf("try another dimension");
  63. system("exit");
  64. }
  65.  
  66. else
  67. {
  68. int i, j;
  69. arraysize += 2; //adding 2 extra lines/colums to array (?) ama balw a sto terminal
  70. char **ground = (char **)malloc(arraysize * sizeof(char *));
  71. for (i = 0; i < arraysize; i++)
  72. {
  73. *(ground + i)= (char *)malloc(arraysize * sizeof(char));
  74. }
  75.  
  76. snake_t s;
  77. init_snake(&s); //1
  78. food_t f;
  79. int x,y;
  80. init_food(x,y,&f);
  81. array(ground, arraysize ,&s ,&f);
  82.  
  83. generate_food(ground, arraysize, &f);
  84. while(1)
  85. {
  86. position_t p;
  87. char keyinput;
  88. move_head(&s,&p,keyinput);
  89. control_snake(ground,arraysize,&s,&p,&f);//3
  90. //while(1) dilwste to int alive = 1 kai otan xanei apla allazete to se 0 gia na spaei to loop
  91. }
  92. /* while(! keyinput)
  93. {
  94. if(gameover()) break;
  95. sleep(1);
  96. }
  97. if(gameover())
  98. {
  99. printf("gameover!");
  100. printf("your score is", s->snake_size);
  101. }
  102. */
  103.  
  104.  
  105. }
  106.  
  107. }
  108.  
  109.  
  110.  
  111. void array(char **ground, int arraysize ,snake_t *s, food_t *f)
  112. {
  113. int x,y;
  114. init_food(x,y,f);
  115. generate_food(ground, arraysize, f);
  116.  
  117. int i, j;
  118.  
  119. for (i = 0; i < arraysize; ++i)
  120. {
  121.  
  122. for (j = 0; j < arraysize; ++j)
  123. {
  124. if (i == 0 || i == arraysize - 1)
  125. {
  126. ground[i][j] = '-';
  127. }
  128. else if (j == 0 || j == arraysize - 1)
  129. {
  130. ground[i][j] = '|';
  131. }
  132. else if (f->x == j && f->y == i)
  133. {
  134. ground[i][j] = 'X';
  135. }
  136. else
  137. {
  138. ground[i][j] = ' ';
  139. }
  140. }
  141. }
  142. for (i = 0; i < arraysize; ++i)
  143. {
  144. for (j = 0; j < arraysize; ++j)
  145. {
  146. printf("%c", ground[i][j]);
  147. }
  148. printf("\n");
  149. }
  150. }
  151.  
  152.  
  153. void init_food(int x, int y, food_t *f)
  154. {
  155. f->x = x;
  156. f->y = y;
  157. }
  158.  
  159. void generate_food(char **ground,int arraysize, food_t *f)
  160. {
  161. int x,y;
  162.  
  163. x = rand()%(arraysize-1);
  164. y = rand()%(arraysize-1);
  165.  
  166. for(x=0; x<arraysize-1; x++)
  167. {
  168. for(y=0; y<arraysize-1; y++)
  169. {
  170. while(ground[x][y] != ' ')
  171. {
  172. x = rand()%(arraysize-1);
  173. y = rand()%(arraysize-1);
  174.  
  175. sleep(1);
  176. }
  177. f->x = x;
  178. f->y = y;
  179. ground[f->x][f->y] = 'X';
  180.  
  181. }
  182. }
  183.  
  184.  
  185.  
  186. }
  187.  
  188. void init_snake(snake_t *s) //1
  189. {
  190. s->tailx =5;
  191. s->taily = 5;
  192. s->headx = s->tailx + s->snake_size;
  193. s->heady = 5;
  194. s->direction = 's';
  195. s->pre_direction = 'a';
  196.  
  197. }
  198.  
  199. char move()
  200. {
  201. int flags;
  202. char key[2];
  203.  
  204. flags = fcntl(STDIN_FILENO, F_GETFL);
  205. fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
  206.  
  207. // To get the input from the terminal without pressing the enter button
  208. system("/bin/stty raw");
  209. read(STDIN_FILENO, key, 2);
  210. // Reset the "system ("/bin/stty raw")" to display correctly the output
  211. system("/bin/stty cooked");
  212. return key[0];
  213. }
  214.  
  215. void control_snake(char **ground, int arraysize,snake_t *s, position_t *p,food_t *f) //2
  216. {
  217. char keyinput = move();
  218. move_head(s,p,keyinput);
  219. if(s->headx == f -> x && s -> heady == f->y)
  220. {
  221. s -> snake_size++;
  222. generate_food(ground, arraysize, f);
  223.  
  224. }
  225. else
  226. {
  227. //move_tail(ground,s,p);
  228. }
  229. }
  230.  
  231. void move_head(snake_t *s,position_t *p , char keyinput)
  232. {
  233. char inserted_key = move();
  234. if(s->direction == LEFT || s->direction == RIGHT || s->direction == UP || s->direction == DOWN)
  235. {
  236. if(inserted_key == LEFT) //elegxos ma pathsw allo koumpi
  237. {
  238.  
  239. s->headx--;
  240.  
  241. }
  242. else if(inserted_key == RIGHT)
  243. {
  244. s->headx++;
  245.  
  246. }
  247. else if(inserted_key == UP)
  248. {
  249. s->heady++;
  250. }
  251. else if(inserted_key == DOWN)
  252. {
  253. s->heady--;
  254. }
  255. }
  256. else
  257. {
  258. //ground[[p->x][s->size][p->y][s->size]] == '@';
  259. }
  260. }
  261.  
  262.  
  263.  
  264.  
  265. void gameover(char **ground,snake_t *s,int arraysize)
  266. {
  267. int i,j;
  268.  
  269.  
  270. for(i = 0; i <= arraysize; i++ )
  271. {
  272. for(j=0; j<= arraysize; j++)
  273. {
  274. if(s -> headx == '-' || s->headx == '|' || s->headx == '*' ||s->heady == '-' || s->heady == '|' || s->heady == '*')
  275. {
  276. printf("game over");
  277. system("exit");
  278.  
  279.  
  280. }
  281.  
  282. }
  283. }
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement