Advertisement
helenrut

Untitled

Oct 7th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. char **createBoard(int n, int m);
  7. void deleteBoard(char **p, int n); // Eyðum kvika fylkinu á móti new skipunum
  8. void printBoard(int n, int m, char** p);
  9. void initializeBoard(char **p, int n, int m);
  10. void makeAMove(char **p, int k, string X);
  11.  
  12. int posX;
  13. int posY;
  14.  
  15. int main()
  16. {
  17. int rows, columns, x, y, k;
  18. string X;
  19. cout << "Enter rows and columns and start position x and y: ";
  20. cin >> rows >> columns >> x >> y ;
  21.  
  22. //Heldur utanum hvar hann er staðsettur.
  23. posX = x;
  24. posY = y;
  25.  
  26. char **board = createBoard(rows, columns);
  27. initializeBoard(board, rows, columns);
  28. printBoard(rows, columns, board);
  29. char Notquit;
  30. do
  31. {
  32. cin >> k >> X;
  33.  
  34. makeAMove(board, k, X);
  35. printBoard(rows, columns, board);
  36.  
  37. cout << "Do you wish to continue playing? Y/N:";
  38. cin >> Notquit;
  39. }
  40. while (Notquit == 'Y' || Notquit == 'y');
  41.  
  42. //Merka ! í reitinn sem hann er staddur í.
  43. board[posY][posX] = '!';
  44. //Prenta út borðið
  45. printBoard(rows, columns, board);
  46.  
  47. deleteBoard(board, rows);
  48. return 0;
  49.  
  50. }
  51.  
  52. void makeAMove(char **p, int k, string X)
  53. {
  54. for (int i = 0; i < k; i++)
  55. {
  56.  
  57. switch (X[i])
  58. {
  59. case 'U':
  60. --posY;
  61. break;
  62.  
  63. case 'D':
  64. ++posY;
  65. break;
  66.  
  67. case 'L':
  68. --posX;
  69. break;
  70.  
  71. case 'R':
  72. ++posX;
  73. break;
  74. }
  75. }
  76. p[posY][posX] = 'o';
  77. }
  78.  
  79. void printBoard(int n, int m, char** p)
  80. {
  81. for (int i = 0; i < n; i++)
  82. {
  83. for (int j = 0; j < m; j++)
  84. {
  85. cout << p[i][j] << " ";
  86. }
  87. cout << endl << endl;
  88. }
  89. }
  90.  
  91. char **createBoard(int n, int m)
  92. {
  93. char **p = new char*[n];
  94. for (int i = n - 1; i >= 0; i--)
  95. {
  96. p[i] = new char[m];
  97. }
  98. return p;
  99. }
  100.  
  101. void initializeBoard(char **p, int n, int m)
  102. {
  103. for (int i = 0; i < n; i++)
  104. {
  105. for (int j = 0; j < m; j++)
  106. {
  107. if (posY == i && posX == j)
  108. {
  109. p[i][j] = 'X';
  110. }
  111. else
  112. {
  113. p[i][j] = '.';
  114. }
  115. }
  116. }
  117. }
  118.  
  119. void deleteBoard(char **p, int n) // Eyðum kvika fylkinu á móti new skipunum
  120. {
  121. for (int i = 0; i < n; i++)
  122. {
  123. delete[] p[i];
  124. }
  125. delete[] p;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement