Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 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); //1o sxolio
  45. void game_over(); //4
  46.  
  47. int main(int argc, char *argv[])
  48. {
  49.  
  50.  
  51. int arraysize = atoi(argv[1]);
  52. //converting argument to integer
  53. if (argv[1] == NULL||argc!=2)
  54. {
  55. printf("invalid input\n");
  56. system("exit");
  57. }
  58.  
  59. if (arraysize < 15 || arraysize > 100)
  60. {
  61. printf("try another dimension");
  62. system("exit");
  63. }
  64.  
  65. else
  66. {
  67. while( !(gameover()) )
  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. food_t f;
  78. array(ground, arraysize ,&s ,&f);
  79.  
  80. int x,y;
  81. init_food(x,y,&f);
  82. generate_food(ground, arraysize, &f);
  83. //init_snake(s); //1
  84. //control_snake();//3
  85. }
  86. while(! keyinput)
  87. {
  88. if(gameover()) break;
  89. sleep(1);
  90. }
  91. if(gameover())
  92. {
  93. printf("gameover!");
  94. printf("your score is", s->snake_size); }
  95.  
  96.  
  97. }
  98.  
  99. }
  100.  
  101.  
  102.  
  103. void array(char **ground, int arraysize ,snake_t *s, food_t *f)
  104. {
  105. int x,y;
  106. init_food(x,y,&f);
  107. generate_food(ground, arraysize, &f);
  108.  
  109. int i, j;
  110.  
  111. for (i = 0; i < arraysize; ++i)
  112. {
  113.  
  114. for (j = 0; j < arraysize; ++j)
  115. {
  116. if (i == 0 || i == arraysize - 1)
  117. {
  118. ground[i][j] = '-';
  119. }
  120. else if (j == 0 || j == arraysize - 1)
  121. {
  122. ground[i][j] = '|';
  123. }
  124. else if (f->x == j && f->y == i)
  125. {
  126. ground[i][j] = 'X';
  127. }
  128. else
  129. {
  130. ground[i][j] = ' ';
  131. }
  132. }
  133. }
  134. for (i = 0; i < arraysize; ++i)
  135. {
  136. for (j = 0; j < arraysize; ++j)
  137. {
  138. printf("%c", ground[i][j]);
  139. }
  140. printf("\n");
  141. }
  142. }
  143.  
  144.  
  145. void init_food(int x, int y, food_t *f)
  146. {
  147. f->x = x;
  148. f->y = y;
  149. }
  150.  
  151. void generate_food(char **ground,int arraysize, food_t *f)
  152. {
  153. int x,y;
  154.  
  155. x = rand()%(arraysize-1);
  156. y = rand()%(arraysize-1);
  157.  
  158. for(x=0; x<arraysize-1; x++)
  159. {
  160. for(y=0; y<arraysize-1; y++)
  161. {
  162. while(ground[x][y] != ' ')
  163. {
  164. x = rand()%(arraysize-1);
  165. y = rand()%(arraysize-1);
  166.  
  167. sleep(1);
  168. }
  169. f->x = x;
  170. f->y = y;
  171. ground[f->x][f->y] = 'X';
  172.  
  173. }
  174. }
  175.  
  176.  
  177.  
  178. }
  179.  
  180. /*void init_snake(snake_t s) //1
  181. {
  182. s.tailx =5;
  183. s.taily = 5;
  184. s.headx = s.tailx + s.snake_size;
  185. s.heady = 5;
  186. s.direction = 's';
  187. s.pre_direction = 'a';
  188.  
  189. }
  190.  
  191. char move()
  192. {
  193. int flags;
  194. char key[2];
  195.  
  196. flags = fcntl(STDIN_FILENO, F_GETFL);
  197. fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
  198.  
  199. // To get the input from the terminal without pressing the enter button
  200. system("/bin/stty raw");
  201. read(STDIN_FILENO, key, 2);
  202. // Reset the "system ("/bin/stty raw")" to display correctly the output
  203. system("/bin/stty cooked");
  204. return key[0];
  205. }
  206.  
  207. 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
  208. {
  209. char keyinput = move();
  210. move_head(p,keyinput);
  211. if(s->head->x == f->f.x && s->head.y == f.y)
  212. {
  213. s->snake_size++;
  214. generate_food(**ground, arraysize, f);
  215.  
  216. }
  217. else
  218. {
  219. move_tail(ground,s,p);
  220. }
  221.  
  222. void move_head(position_t p , char keyinput)
  223. {
  224. if(direction == LEFT || direction == RIGHT || direction == UP || direction == DOWN)
  225. {
  226. if(keyinput == LEFT) //elegxos ma pathsw allo koumpi
  227. {
  228.  
  229. headx--;
  230.  
  231. }
  232. else if(keyinput == RIGHT)
  233. {
  234. headx++;
  235.  
  236. }
  237. else if(keyinput == UP)
  238. {
  239. heady++;
  240. }
  241. else if(keyinput == DOWN)
  242. {
  243. heady--;
  244. }
  245. }
  246. else
  247. {
  248. ground[[p->x][s->size][p->y][s->size]] == '@';
  249. }
  250. }
  251. void move_tail(char **ground,snake_t s,position_t p)
  252. {
  253. int ;
  254. ground[[p->x][s->size][p->y][s->size]] == ' ';
  255. for(i=0, i<s->size, i++)
  256. {
  257. p->x[i] = p->[x+1];
  258. p->y[i] = p->[y+1];
  259. }
  260. }
  261. }
  262. */
  263.  
  264.  
  265. void gameover()
  266. {
  267. int i,j;
  268. int arraysize;
  269. snake_t s;
  270.  
  271. for(i =0; i <= arraysize; i++ )
  272. {
  273. for(j=0; j<=arraysize; j++)
  274. {
  275. if(s.headx == '-' || s.headx == '|' || s.headx == '*' ||s.heady == '-' || s.heady == '|' || s.heady == '*')
  276. {
  277. printf("game over");
  278. system("exit");
  279.  
  280.  
  281. }
  282.  
  283. }
  284. }
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement