Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstdlib>
  4. #include <time.h>
  5.  
  6. const int N=10;
  7.  
  8. void fillWithDots(char letters[][N], int n);
  9. void printMultiArray(char letters[][N],int n);
  10. void createRoute(char letters[][N], int n);
  11. int myRand(int max, int min);
  12.  
  13. int main(){
  14. char a = 'A';
  15. char b = a++;
  16. printf("%c",b);
  17. srand(time(NULL));
  18. const unsigned N = 10;
  19. char letters[N][N];
  20. fillWithDots(letters,N);
  21. printMultiArray(letters,N);
  22.  
  23. createRoute(letters,N);
  24. printMultiArray(letters,N);
  25. }
  26.  
  27. void fillWithDots(char letters[][N], int n){
  28. for (int i = 0; i<n;i++){
  29. for (int j=0; j<N;j++){
  30. letters[i][j]='.';
  31. }
  32. }
  33. }
  34. void createRoute(char letters[][N], int n){ // 0 - NORTH, 1 - EAST, 2 SOUTH, 3 WEST
  35. int xPos=0;
  36. int yPos=0;
  37.  
  38. char a = 'A';
  39. letters[xPos][yPos]=a;
  40.  
  41. while (true){
  42. int direction = myRand(3,0);
  43.  
  44. if (direction == 0){ // NORTH
  45. if (yPos-1>=0 && letters[xPos][yPos-1]=='.') yPos-1;
  46. else continue;
  47. }
  48. else if (direction == 1){ //EAST
  49. if (xPos+1<=N-1 && letters[xPos+1][yPos-1]=='.') xPos+1;
  50. else continue;
  51. }
  52. else if (direction == 2){ //SOUTH
  53. if (yPos+1<=N-1 && letters[xPos][yPos+1]=='.') yPos+1;
  54. else continue;
  55. }
  56. else if (direction == 3){ // WEST
  57. if (xPos-1>=0 && letters[xPos-1][yPos]=='.') xPos-1;
  58. }
  59. letters[xPos][yPos]=++a;
  60. if (a=='Z') break;
  61. }
  62. }
  63.  
  64.  
  65.  
  66.  
  67.  
  68. int myRand(int max, int min){
  69. return rand()%(max-min + 1) + min;
  70. }
  71. void printMultiArray(char letters[][N],int n){
  72. for (int i = 0; i<n;i++){
  73. for (int j=0; j<N;j++){
  74. printf("%c",letters[i][j]);
  75. }
  76. printf("\n");
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement