Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <string.h>
  6. #include <conio.h>
  7.  
  8. #define N 5
  9.  
  10. typedef struct robotStruct {
  11. int x, y;
  12. int score;
  13. int energy;
  14. } r;
  15.  
  16. typedef struct mapStruct {
  17. int map[N][N];
  18. int tx, ty;
  19. } m;
  20.  
  21. void fillmap(m* map) {
  22. for (int i = 0; i < N; i++) {
  23. for (int j = 0; j < N; j++) {
  24. int tmp = rand()%10;
  25. if (tmp < 5) map->map[i][j] = 0;
  26. else if (tmp < 8) map->map[i][j] = 1;
  27. else map->map[i][j] = 2;
  28. }
  29. }
  30. }
  31.  
  32. void init(r* rbt, m* map) {
  33. srand((unsigned)time(NULL));
  34. rbt->x = rand()%N;
  35. rbt->y = rand()%N;
  36. rbt->score = 0;
  37. rbt->energy = 20;
  38. do {
  39. map->tx = rand()%N;
  40. map->ty = rand()%N;
  41. } while(rbt->x == map->tx && rbt->y == map->ty);
  42. fillmap(map);
  43. }
  44.  
  45. void render(r const rbt, m const map) {
  46. char tersym[3] = {'_', '~', '@'};
  47. for (int i = 0; i < N; ++i) {
  48. for (int j = 0; j < N; ++j) {
  49. int ter = map.map[i][j];
  50. if(i == rbt.y && j == rbt.x) {
  51. putchar('o');
  52. } else if (i == map.ty && j == map.tx) {
  53. putchar('x');
  54. } else {
  55. putchar(tersym[ter]);
  56. }
  57. }
  58. putchar('\n');
  59. }
  60. printf("Energy: %d\n", rbt.energy);
  61. printf("Score : %d\n", rbt.score);
  62. }
  63.  
  64. // 0: left; 1: right; 2: up; 3: down;
  65. int getMove(r const rbt) {
  66. while(1) {
  67. fputs("move > ", stdout);
  68. char c = _getch();
  69. switch(c) {
  70. case 'a':
  71. if (rbt.x != 0) return 0; break;
  72. case 'd':
  73. if (rbt.x != N-1) return 1; break;
  74. case 'w':
  75. if (rbt.y != 0) return 2; break;
  76. case 's':
  77. if (rbt.y != N-1) return 3; break;
  78. }
  79. }
  80. }
  81.  
  82. void YouGotAPoint() {
  83. puts("###################################");
  84. puts("### ###");
  85. puts("### You Got A Point!!! ###");
  86. puts("### ###");
  87. puts("###################################");
  88. system("pause");
  89. system("cls");
  90. }
  91.  
  92. void update(r* rbt, m* map, int move) {
  93. int movedir[4][2] =
  94. { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
  95. rbt->x += movedir[move][0];
  96. rbt->y += movedir[move][1];
  97. system("cls");
  98. if (rbt->x == map->tx && rbt->y == map->ty) {
  99. YouGotAPoint();
  100. (rbt->score)++;
  101. rbt->energy = 20 - rbt->score;
  102. do {
  103. map->tx = rand()%N;
  104. map->ty = rand()%N;
  105. } while(rbt->x == map->tx && rbt->y == map->ty);
  106. } else {
  107. rbt->energy -= map->map[rbt->y][rbt->x] + 1;
  108. }
  109. }
  110.  
  111. void YouDied() {
  112. puts("###################################");
  113. puts("### ###");
  114. puts("### You Died!!! ###");
  115. puts("### ###");
  116. puts("###################################");
  117. }
  118.  
  119. void exitGame(r rbt) {
  120. system("cls");
  121. YouDied();
  122. printf("Score: %d\n", rbt.score);
  123. system("pause");
  124. }
  125.  
  126. int main(void) {
  127. struct robotStruct rbt;
  128. struct mapStruct map;
  129. init(&rbt,&map);
  130. while (rbt.energy > 0) {
  131. render(rbt, map);
  132. update(&rbt, &map, getMove(rbt));
  133. }
  134. exitGame(rbt);
  135. return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement