Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4.  
  5. #define RIGHE 20
  6. #define COLONNE 20
  7.  
  8. using namespace std;
  9.  
  10. struct blocco{
  11. int x;
  12. int y;
  13. struct blocco * prossimo;
  14. };
  15.  
  16. struct coordinata{
  17. int x;
  18. int y;
  19. };
  20.  
  21.  
  22. void inizializza(int griglia[RIGHE][COLONNE]){
  23. for(int i=0;i<RIGHE;i++)
  24. for(int k=0;k<COLONNE;k++)
  25. griglia[i][k]=0;
  26. return;
  27. }
  28.  
  29. void print(int griglia[RIGHE][COLONNE]){
  30. for(int i=0;i<RIGHE;i++){
  31. for(int k=0;k<COLONNE;k++)
  32. cout << griglia[i][k];
  33. cout << endl;
  34. }
  35. cout << endl;
  36. return;
  37. }
  38.  
  39. bool death(struct blocco* snake){
  40. struct blocco* p = snake;
  41. struct blocco* q = snake;
  42.  
  43. while(p != NULL){
  44. q=p;
  45. p = p-> prossimo;
  46. }
  47. if(q->x == 0 || q->y == 0 || q->x == COLONNE - 1 || q->y == RIGHE - 1)
  48. return true;
  49. p=snake;
  50. q=snake;
  51. while(p!=NULL){
  52. q = p->prossimo;
  53. while(q!=NULL){
  54. if(p->x == q->x && p->y == q->y)
  55. return true;
  56. q=q->prossimo;
  57. }
  58. p=p->prossimo;
  59. }
  60. return false;
  61. }
  62.  
  63. void make_griglia(int griglia[][COLONNE],struct blocco* snake,int mela_x,int mela_y){
  64. griglia[mela_x][mela_y]=2;
  65. struct blocco* p = snake;
  66. while(p != NULL){
  67. if(p->prossimo != NULL)
  68. griglia[p->x][p->y]=1;
  69. else
  70. griglia[p->x][p->y]=3;
  71. p = p-> prossimo;
  72. }
  73. return;
  74. }
  75.  
  76. int main(){
  77. srand(time(NULL));
  78.  
  79. struct blocco* snake = new struct blocco;
  80. snake->x = RIGHE/2;
  81. snake->y = COLONNE/2;
  82. snake->prossimo = NULL;
  83.  
  84. struct coordinata velocita;
  85. velocita.x=1;
  86. velocita.y=0;
  87.  
  88. struct coordinata mela;
  89. mela.x = rand() % (RIGHE-1) +1;
  90. mela.y = rand() % (COLONNE-1) +1;
  91.  
  92. int griglia[RIGHE][COLONNE];
  93. inizializza(griglia);
  94.  
  95. bool game_over;
  96.  
  97. while(death(snake)==false){
  98.  
  99. //MOSSA
  100. char move;
  101. cin >> move;
  102.  
  103. if(move == 's'){
  104. if(velocita.x != -1)
  105. velocita.x = 1;
  106. velocita.y = 0;
  107. }
  108. if(move == 'w'){
  109. if(velocita.x != 1)
  110. velocita.x = -1;
  111. velocita.y = 0;
  112. }
  113. if(move == 'a'){
  114. velocita.x = 0;
  115. if(velocita.y != 1)
  116. velocita.y = -1;
  117. }
  118. if(move == 'd'){
  119. velocita.x = 0;
  120. if(velocita.y != -1)
  121. velocita.y = 1;
  122. }
  123.  
  124. struct blocco* old_head = snake;
  125. while(old_head -> prossimo != NULL)
  126. old_head = old_head -> prossimo;
  127.  
  128. struct blocco* n_head = new struct blocco;
  129. n_head -> x = old_head -> x + velocita.x;
  130. n_head -> y = old_head -> y + velocita.y;
  131. n_head -> prossimo = NULL;
  132.  
  133. old_head -> prossimo = n_head;
  134. if(n_head -> x == mela.x && n_head -> y == mela.y){
  135. mela.x = rand() % (RIGHE-1) +1;
  136. mela.y = rand() % (COLONNE-1) +1;
  137. }
  138. else
  139. snake = snake -> prossimo;
  140. game_over = death(snake);
  141. inizializza(griglia);
  142. make_griglia(griglia,snake,mela.x,mela.y);
  143. print(griglia);
  144.  
  145.  
  146. }
  147. system("pause");
  148. return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement