Advertisement
AlexandraCatana

Untitled

Jan 24th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <list>
  4. #include <string>
  5. using namespace std;
  6.  
  7. class Point
  8. {
  9. private:
  10. int x, y;
  11. public:
  12. Point() {}
  13. Point(int x, int y)
  14. {
  15. this->x = x;
  16. this->y = y;
  17. }
  18. int getX() { return x; }
  19. int getY() { return y; }
  20. };
  21.  
  22. class Ship
  23. {
  24. public:
  25. string name;
  26. int size;
  27. int nrOfFunctionalParts;
  28. Point startingPoint;
  29. string direction; //hr - horizontal right, hl - horizontal left, vu- vertical up, vd vertical down
  30. public:
  31. Ship(string name, int size, Point startingPoint, string direction)
  32. {
  33. this->name = name;
  34. this->size = size;
  35. this->startingPoint = startingPoint;
  36. this->direction = direction;
  37. this->nrOfFunctionalParts = size;
  38. }
  39. //virtual void shooting(Point p) = 0;
  40. };
  41.  
  42. class Carrier : public Ship
  43. {
  44. public:
  45. Carrier(string name, int size, Point startingPoint, string direction) :Ship("carrier", 5, startingPoint, direction) {}
  46. };
  47.  
  48. class Battleship : public Ship
  49. {
  50. public:
  51. Battleship(Point startingPoint, string direction) :Ship("battleship", 4, startingPoint, direction) {}
  52. };
  53.  
  54. class Cruiser : public Ship
  55. {
  56. public:
  57. Cruiser(Point startingPoint, string direction) :Ship("cruiser", 3, startingPoint, direction) {}
  58. };
  59.  
  60. class Submarine : public Ship
  61. {
  62. public:
  63. Submarine(Point startingPoint, string direction) :Ship("submarine", 5, startingPoint, direction) {}
  64. };
  65.  
  66. class Destroyer : public Ship
  67. {
  68. public:
  69. Destroyer(Point startingPoint, string direction) :Ship("destroyer", 2, startingPoint, direction) {}
  70. };
  71.  
  72. int grid1[10][10]; // tabla pe care primul jucator isi noteaza loviturile
  73. int grid2[10][10]; // tabla pe care jucatorul 2 isi aseaza navele
  74.  
  75. bool shooting(Point p, Ship s)
  76. {
  77. if ( (s.direction == "vu" && s.startingPoint.getX() == p.getX() && p.getY() - s.startingPoint.getY() <= s.size )
  78. || (s.direction == "vd" && s.startingPoint.getX() == p.getX() && s.startingPoint.getY() - p.getY() <= s.size)
  79. || (s.direction == "hl" && s.startingPoint.getY() == p.getY() && s.startingPoint.getX() - p.getX() <= s.size)
  80. || (s.direction == "hd" && s.startingPoint.getY() == p.getY() && s.startingPoint.getX() - p.getX() <= s.size) )
  81. {
  82. grid1[p.getX()][p.getY()] = 1; // primul jucator nimereste o barca
  83. grid2[p.getX()][p.getY()] = -1; // al doilea jucator pierde o parte din barca
  84. --s.nrOfFunctionalParts;
  85. return true;
  86. }
  87. else
  88. {
  89. grid1[p.getX()][p.getY()] = -1; // primul jucatorul marcheaza pozitia ca fiin una goala
  90. return false;
  91. }
  92. }
  93.  
  94. int main()
  95. {
  96. list<Ship*> listOfShips;
  97. listOfShips.push_back(new Battleship(Point(1, 2), "vd"));
  98. listOfShips.push_back(new Cruiser(Point(5, 5), "hl"));
  99. listOfShips.push_back(new Destroyer(Point(8, 3), "hr"));
  100. listOfShips.push_back(new Submarine(Point(8, 7), "vu"));
  101.  
  102. int dx[] = { 0, -1, 1 };
  103. int dy[] = { 0, -1, 1 };
  104.  
  105. Point start(4, 4);
  106. while (listOfShips.empty() == false)
  107. {
  108. for (int i = 0; i <= 2; ++i)
  109. {
  110. for (int j = 0; j <= 2; ++j)
  111. {
  112. Point aux(start.getX() + dx[i], start.getY() + dy[j]);
  113. if (grid1[aux.getX()][aux.getY()] == 0)
  114. {
  115. for (auto it = listOfShips.begin(); it != listOfShips.end(); ++it)
  116. {
  117. bool a = shooting(start, **it);
  118. if ((*it)->nrOfFunctionalParts == 0)
  119. listOfShips.erase(it);
  120. if (a == true) break;
  121. }
  122. start = aux;
  123. }
  124. }
  125. }
  126. }
  127.  
  128. system("pause");
  129. return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement