Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <list>
- #include <string>
- using namespace std;
- class Point
- {
- private:
- int x, y;
- public:
- Point() {}
- Point(int x, int y)
- {
- this->x = x;
- this->y = y;
- }
- int getX() { return x; }
- int getY() { return y; }
- };
- class Ship
- {
- public:
- string name;
- int size;
- int nrOfFunctionalParts;
- Point startingPoint;
- string direction; //hr - horizontal right, hl - horizontal left, vu- vertical up, vd vertical down
- public:
- Ship(string name, int size, Point startingPoint, string direction)
- {
- this->name = name;
- this->size = size;
- this->startingPoint = startingPoint;
- this->direction = direction;
- this->nrOfFunctionalParts = size;
- }
- //virtual void shooting(Point p) = 0;
- };
- class Carrier : public Ship
- {
- public:
- Carrier(string name, int size, Point startingPoint, string direction) :Ship("carrier", 5, startingPoint, direction) {}
- };
- class Battleship : public Ship
- {
- public:
- Battleship(Point startingPoint, string direction) :Ship("battleship", 4, startingPoint, direction) {}
- };
- class Cruiser : public Ship
- {
- public:
- Cruiser(Point startingPoint, string direction) :Ship("cruiser", 3, startingPoint, direction) {}
- };
- class Submarine : public Ship
- {
- public:
- Submarine(Point startingPoint, string direction) :Ship("submarine", 5, startingPoint, direction) {}
- };
- class Destroyer : public Ship
- {
- public:
- Destroyer(Point startingPoint, string direction) :Ship("destroyer", 2, startingPoint, direction) {}
- };
- int grid1[10][10]; // tabla pe care primul jucator isi noteaza loviturile
- int grid2[10][10]; // tabla pe care jucatorul 2 isi aseaza navele
- bool shooting(Point p, Ship s)
- {
- if ( (s.direction == "vu" && s.startingPoint.getX() == p.getX() && p.getY() - s.startingPoint.getY() <= s.size )
- || (s.direction == "vd" && s.startingPoint.getX() == p.getX() && s.startingPoint.getY() - p.getY() <= s.size)
- || (s.direction == "hl" && s.startingPoint.getY() == p.getY() && s.startingPoint.getX() - p.getX() <= s.size)
- || (s.direction == "hd" && s.startingPoint.getY() == p.getY() && s.startingPoint.getX() - p.getX() <= s.size) )
- {
- grid1[p.getX()][p.getY()] = 1; // primul jucator nimereste o barca
- grid2[p.getX()][p.getY()] = -1; // al doilea jucator pierde o parte din barca
- --s.nrOfFunctionalParts;
- return true;
- }
- else
- {
- grid1[p.getX()][p.getY()] = -1; // primul jucatorul marcheaza pozitia ca fiin una goala
- return false;
- }
- }
- int main()
- {
- list<Ship*> listOfShips;
- listOfShips.push_back(new Battleship(Point(1, 2), "vd"));
- listOfShips.push_back(new Cruiser(Point(5, 5), "hl"));
- listOfShips.push_back(new Destroyer(Point(8, 3), "hr"));
- listOfShips.push_back(new Submarine(Point(8, 7), "vu"));
- int dx[] = { 0, -1, 1 };
- int dy[] = { 0, -1, 1 };
- Point start(4, 4);
- while (listOfShips.empty() == false)
- {
- for (int i = 0; i <= 2; ++i)
- {
- for (int j = 0; j <= 2; ++j)
- {
- Point aux(start.getX() + dx[i], start.getY() + dy[j]);
- if (grid1[aux.getX()][aux.getY()] == 0)
- {
- for (auto it = listOfShips.begin(); it != listOfShips.end(); ++it)
- {
- bool a = shooting(start, **it);
- if ((*it)->nrOfFunctionalParts == 0)
- listOfShips.erase(it);
- if (a == true) break;
- }
- start = aux;
- }
- }
- }
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement