Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <string.h>
  6. #include <time.h>
  7.  
  8.  
  9. typedef struct direction
  10. {
  11. int x;
  12. int y;
  13.  
  14.  
  15. }direction_t;
  16.  
  17. typedef struct snake
  18. {
  19. direction_t *head;
  20. direction_t *tail;
  21.  
  22.  
  23. }snake_t;
  24.  
  25. void array(char **ground, int size, int score);
  26. void gen_food(char **table, int size, direction_t food, direction_t head);
  27.  
  28.  
  29. int main(int argc, char *argv[])
  30.  
  31. {
  32. int score=0;
  33. int i,j;
  34. int size = atoi(argv[1]); //converting argument to integer
  35.  
  36. if(size == 0)
  37. {
  38. printf("invalid input\n");
  39. exit(EXIT_FAILURE);
  40. }
  41. size += 2; //adding 2 extra lines/colums to array (?) ama balw a sto terminal
  42. char **ground;
  43. ground = (char**)malloc(size * sizeof(char*));
  44. direction_t food, head;
  45. gen_food(ground, size, food, head);
  46. array(ground,size,score);
  47. return 0;
  48. }
  49.  
  50.  
  51.  
  52. void array(char **ground, int size, int score,direction_t food.x, direction_t food.x)
  53. {
  54. int i,j;
  55. for( i=0; i<size; i++)
  56. {
  57. *(ground + i) = (char*)malloc(size * sizeof(char));
  58.  
  59. }
  60. for(i=0; i<size; ++i)
  61. {
  62.  
  63. for ( j=0; j < size; ++j)
  64. {
  65. if(i == 0 || i == size-1)
  66. {
  67. ground[i][j] = '-';
  68. }
  69. else if(j==0 || j == size-1)
  70. {
  71. ground[i][j] = '|';
  72. }
  73. else if (x.food == j && y.food == i ) {
  74. ground[i][j] = "X";
  75. }
  76. else
  77. {
  78. ground[i][j] = ' ';
  79. }
  80. }
  81.  
  82. }
  83. for (i=0;i<size; ++i)
  84. {
  85. for(j=0;j<size; ++j)
  86. {
  87. printf("%c",ground[i][j]);
  88. }
  89. printf("\n");
  90. }
  91.  
  92. }
  93.  
  94.  
  95. void gen_food(char **table, int size, direction_t food, direction_t head)
  96. {
  97.  
  98.  
  99. srand ( time(NULL) );
  100. rand();
  101. food.x = rand()%(size+2);
  102.  
  103. food.y = rand()%(size+2);
  104.  
  105. head.x = rand()%(size+2);
  106.  
  107. head.y = rand()%(size+2);
  108. if(food.x == head.x && food.y == head.y)
  109. {
  110. printf("please try again");
  111. system("exit");
  112. }
  113. else
  114. {
  115. if(food.x > 0 && food.y > 0)
  116. //{
  117. if(food.x < size && food.y < size)
  118. {
  119. table[food.x][food.y] = 'X';
  120. // printf("%c", table[food.x][food.y]);
  121. // printf("%d, %d\n", food.x, food.y);
  122.  
  123. }
  124. else
  125. {
  126. system("exit");
  127. }
  128.  
  129. // else return 1;
  130. }
  131.  
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement